def t_cons(environment, node): if not validParameters(environment, node): return TYPE.ERROR from main import infer_type new = node["elements"]["e1"] list = node["elements"]["e2"] list_type = infer_type(environment, list) if isList(list_type): new_type = infer_type(environment, new) empty = TYPE.LIST(TYPE.EMPTY) list_of = innerType(list_type) if new_type == empty and list_type == empty: return empty if new_type != empty and TYPE.EMPTY in list_type: return TYPE.LIST(new_type) elif (new_type == list_of) or (new_type == TYPE.UNDEFINED): return list_type elif new_type != empty and TYPE.UNDEFINED in list_type: return TYPE.LIST(new_type) else: return TYPE.ERROR else: return TYPE.ERROR
def test_tcons_nested_raise_ok(self): node = { "description": "tcons", "elements": { "e1": { "description": "tcons", "elements": { "e1" : { "description": "traise", "elements": {"e1": "raise"} }, "e2" : { "description": "tcons", "elements": { "e1": { "description": "tempty", "elements": {"e1": "empty"} }, "e2": { "description": "tempty", "elements": {"e1": "empty"} } } } } }, "e2": { "description": "tempty", "elements": {"e1" : "empty"} } } } environment = {} assert t_cons(environment, node) == TYPE.LIST(TYPE.LIST(TYPE.UNDEFINED))
def test_tcons_nested2_ok(self): node = { "description": "tcons", "elements": { "e1": { "description": "tcons", "elements": { "e1" : { "description": "tint", "elements": {"e1": "1"} }, "e2" : { "description": "tcons", "elements": { "e1": { "description": "tempty", "elements": {"e1": "empty"} }, "e2": { "description": "tempty", "elements": {"e1": "empty"} } } } } }, "e2": { "description": "tempty", "elements": {"e1" : "empty"} } } } environment = {} assert t_cons(environment, node) == TYPE.LIST(TYPE.LIST(TYPE.INT))
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_tempty_ok(self): node = { "description": "tempty", "elements": { "e1": "empty" } } environment = {} assert t_empty(environment, node) == TYPE.LIST(TYPE.EMPTY)
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_tcons_nested_bools_ok(self): node = { "description": "tcons", "elements": { "e1": { "description": "tcons", "elements": { "e1" : { "description": "tbool", "elements": {"e1": "true"} }, "e2" : { "description": "tempty", "elements": { "e1": "empty"} } } }, "e2": { "description": "tcons", "elements": { "e1" : { "description": "tcons", "elements": { "e1" : { "description": "tbool", "elements": {"e1": "false"} }, "e2" : { "description": "tempty", "elements": { "e1": "empty"} } } }, "e2" : { "description": "tempty", "elements": { "e1": "empty"} } } } } } environment = {} assert t_cons(environment, node) == TYPE.LIST(TYPE.LIST(TYPE.BOOL))
def t_empty(environment, node): if not validParameters(environment, node): return TYPE.ERROR from main import infer_type value = node['elements']['e1'] if value is "empty": return TYPE.LIST(TYPE.EMPTY) else: return TYPE.ERROR
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_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)
def isList(expression): if TYPE.LIST('') in expression: return True else: return False
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" } }, }