diff --git a/ir.py b/ir.py index b025842..4e13ab6 100644 --- a/ir.py +++ b/ir.py @@ -11,6 +11,12 @@ import types_ Expression: TypeAlias = 'MonoFunc | Application | Int | Variable | Builtin | LetBinding | ReplHole | Switch' Value: TypeAlias = 'MonoFunc | Int | Builtin | ReplHole' +dollar_count: int = 0 +def mk_dollar() -> str: + global dollar_count + dollar_count += 1 + return f'${dollar_count-1}' + @dataclass(frozen=True) class ReplHole: typ_bindings: types_.Context @@ -120,18 +126,20 @@ class MonoFunc: case [(var, [])]: # Binds a single variable to the entire input return Ok(MonoFunc(var, body)) + local_variable = mk_dollar() + # If those special cases fail, we eliminate the pattern matching to produce a # single body: match_trees = tuple( # Construct a match tree for each possible branch pattern.match_tree( EMPTY_STRUCT_PATH, - LeafNode.from_value(bindings_to_lets(pattern.bindings(), Variable('$'), body)) + LeafNode.from_value(bindings_to_lets(pattern.bindings(), Variable(local_variable), body)) ) for (pattern, body) in forms ) unified_match_tree = merge_all_trees(match_trees) # Unify all the trees - compiled_tree = compile_tree(unified_match_tree, Variable('$')) # Turn each tree into IR - return compiled_tree <= p(MonoFunc, '$') + compiled_tree = compile_tree(unified_match_tree, Variable(local_variable)) # Turn each tree into IR + return compiled_tree <= p(MonoFunc, local_variable) def try_apply(self, v: Expression) -> Option[Expression]: return Some(self.body.subst(v, self.arg))