Make '$' variables globally unique

This commit is contained in:
Emi Simpson 2024-03-17 10:12:02 -04:00
parent a673db77ca
commit 67a7bbc821
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 11 additions and 3 deletions

14
ir.py
View File

@ -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))