def test_tapp_chaotic_paramater_functional_ok(self): node2 = { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.FUNC( TYPE.FUNC(TYPE.BOOL, TYPE.INT), TYPE.FUNC(TYPE.FUNC(TYPE.INT, TYPE.INT), (TYPE.FUNC( TYPE.FUNC(TYPE.BOOL, TYPE.BOOL), TYPE.INT)))), "e3": { "description": "tbool", "elements": { "e1": "true" } } } } node3 = { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.FUNC(TYPE.BOOL, TYPE.INT), "e3": { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.FUNC(TYPE.INT, TYPE.INT), "e3": { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.FUNC(TYPE.BOOL, TYPE.BOOL), "e3": { "description": "tint", "elements": { "e1": "5" } } } } } } } } node = { "description": "tapp", "elements": { "e1": node2, "e2": node3, } } environment = {} assert t_app(environment, node) == TYPE.BOOL
def t_letrec(environment, term): from main import infer_type if not parameters_are_valid(environment, term): return TYPE.ERROR if not environment: environment = {} function_name = term['elements']['function_name'] input_type = term['elements']['type1'] output_type = term['elements']['type2'] param_name = term['elements']['param']['elements']['e1'] e1 = term['elements']['e1'] e2 = term['elements']['e2'] environment[function_name] = TYPE.FUNC(input_type, output_type) type_e2 = infer_type(environment, e2) environment[param_name] = input_type type_e1 = infer_type(environment, e1) if type_e2 == TYPE.ERROR or type_e1 == TYPE.ERROR: return TYPE.ERROR if type_e1 == output_type or type_e1 == TYPE.UNDEFINED: return type_e2 return TYPE.ERROR
def test_tapp_output_functional_ok(self): node2 = { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.INT, "e3": { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.BOOL, "e3": { "description": "tbool", "elements": { "e1": "true" } } } } } } node3 = {"description": "tint", "elements": {"e1": 5}} node = { "description": "tapp", "elements": { "e1": node2, "e2": node3, } } environment = {} assert t_app(environment, node) == TYPE.FUNC(TYPE.BOOL, TYPE.BOOL)
def test_tfun_var_ok(self): node = { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.INT, "e3": { "description": "tvar", "elements": { "e1": "x" } } } } environment = {} assert t_fun(environment, node) == "(" + TYPE.INT + ")->" + TYPE.INT assert t_fun(environment, node) == TYPE.FUNC(TYPE.INT, TYPE.INT)
def test_tfun_bool_ok(self): node = { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.INT, "e3": { "description": "tbool", "elements": { "e1": "true" } } } } environment = {} assert t_fun(environment, node) == "(" + TYPE.INT + ")->" + TYPE.BOOL assert t_fun(environment, node) == TYPE.FUNC(TYPE.INT, TYPE.BOOL)
def t_fun(environment, node): if not validParameters(environment, node): return TYPE.ERROR from main import infer_type parameter = node["elements"]["e1"] parameter_type = node["elements"]["e2"] environment[parameter] = parameter_type body_type = infer_type(environment, node["elements"]["e3"]) if body_type != TYPE.ERROR and parameter_type != TYPE.ERROR and parameter_type != TYPE.UNDEFINED: return TYPE.FUNC(parameter_type, body_type) return TYPE.ERROR
def test_tcons_fun_ok(self): node = { "description": "tcons", "elements": { "e1": { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.INT, "e3": { "description": "tbool", "elements": { "e1": "true" } } } }, "e2": { "description": "tcons", "elements": { "e1": { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.INT, "e3": { "description": "tbool", "elements": { "e1": "false" } } } }, "e2": { "description": "tempty", "elements": {"e1": "empty"} } } } } } environment = {} assert t_cons(environment, node) == TYPE.LIST(TYPE.FUNC(TYPE.INT, TYPE.BOOL))
def test_tif_ok_fun(self): node = { "description": "tif", "elements": { "e1": { "description": "tbool", "elements":{ "e1": "false" } }, "e2": { "description": "tfun", "elements":{ "e1": "x", "e2": TYPE.INT, "e3": { "description": "tvar", "elements": { "e1": "x" } } } }, "e3": { "description": "tfun", "elements":{ "e1": "x", "e2": TYPE.INT, "e3": { "description": "tvar", "elements": { "e1": "x" } } } } } } environment = {} assert t_if(environment, node) == TYPE.FUNC(TYPE.INT, TYPE.INT)
import sys sys.path.insert(1, '../') from Definitions.types import TYPE from main import infer_type node_function = { "description": "tfun", "elements": { "e1": "x", "e2": TYPE.FUNC(TYPE.INT, TYPE.INT), "e3": { "description": "tbool", "elements": { "e1": "true" } } } } node_root = { "description": "tapp", "elements": { "e1": node_function, "e2": { "description": "traise", "elements": { "e1": "raise" } }, }