Make '$' variables globally unique
This commit is contained in:
parent
a673db77ca
commit
67a7bbc821
14
ir.py
14
ir.py
|
@ -11,6 +11,12 @@ import types_
|
||||||
Expression: TypeAlias = 'MonoFunc | Application | Int | Variable | Builtin | LetBinding | ReplHole | Switch'
|
Expression: TypeAlias = 'MonoFunc | Application | Int | Variable | Builtin | LetBinding | ReplHole | Switch'
|
||||||
Value: TypeAlias = 'MonoFunc | Int | Builtin | ReplHole'
|
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)
|
@dataclass(frozen=True)
|
||||||
class ReplHole:
|
class ReplHole:
|
||||||
typ_bindings: types_.Context
|
typ_bindings: types_.Context
|
||||||
|
@ -120,18 +126,20 @@ class MonoFunc:
|
||||||
case [(var, [])]: # Binds a single variable to the entire input
|
case [(var, [])]: # Binds a single variable to the entire input
|
||||||
return Ok(MonoFunc(var, body))
|
return Ok(MonoFunc(var, body))
|
||||||
|
|
||||||
|
local_variable = mk_dollar()
|
||||||
|
|
||||||
# If those special cases fail, we eliminate the pattern matching to produce a
|
# If those special cases fail, we eliminate the pattern matching to produce a
|
||||||
# single body:
|
# single body:
|
||||||
match_trees = tuple( # Construct a match tree for each possible branch
|
match_trees = tuple( # Construct a match tree for each possible branch
|
||||||
pattern.match_tree(
|
pattern.match_tree(
|
||||||
EMPTY_STRUCT_PATH,
|
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
|
for (pattern, body) in forms
|
||||||
)
|
)
|
||||||
unified_match_tree = merge_all_trees(match_trees) # Unify all the trees
|
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
|
compiled_tree = compile_tree(unified_match_tree, Variable(local_variable)) # Turn each tree into IR
|
||||||
return compiled_tree <= p(MonoFunc, '$')
|
return compiled_tree <= p(MonoFunc, local_variable)
|
||||||
|
|
||||||
def try_apply(self, v: Expression) -> Option[Expression]:
|
def try_apply(self, v: Expression) -> Option[Expression]:
|
||||||
return Some(self.body.subst(v, self.arg))
|
return Some(self.body.subst(v, self.arg))
|
||||||
|
|
Loading…
Reference in New Issue