Compare commits

...

3 Commits

Author SHA1 Message Date
ecbb8661ce
Name cleanup 2022-10-31 08:27:56 -07:00
73b803a1b0
Minor cleanup 2022-10-31 08:27:28 -07:00
c7464076ff
Improved full-reduction logic 2022-10-31 08:20:27 -07:00
4 changed files with 49 additions and 43 deletions

View File

@ -1,6 +1,7 @@
{
"cSpell.words": [
"autochurch",
"Endnodes",
"freevar",
"mdel",
"onefile",

View File

@ -75,15 +75,15 @@ Lamb treats each λ expression as a binary tree. Variable binding and reduction
## Todo (pre-release):
- Make command output accessible in prompt
- Prettier colors
- Prevent macro-chaining recursion
- step-by-step reduction
- Full-reduce option (expand all macros)
- PyPi package
- Cleanup warnings
- Preprocess method: bind, macros to free, etc
- History queue
- `W` isn't fully expanded. Why??
- Truncate long expressions in warnings
## Todo:

View File

@ -36,7 +36,6 @@ class ReductionError(Exception):
def __init__(self, msg: str):
self.msg = msg
class TreeWalker:
"""
An iterator that walks the "outline" of a tree
@ -244,6 +243,7 @@ class ExpandableEndNode(EndNode):
class FreeVar(EndNode):
def __init__(self, name: str, *, runner = None):
super().__init__()
self.name = name
self.runner = runner # type: ignore
@ -347,7 +347,6 @@ class History(ExpandableEndNode):
def copy(self):
return History(runner = self.runner)
bound_counter = 0
class Bound(EndNode):
def __init__(self, name: str, *, forced_id = None, runner = None):
@ -589,7 +588,6 @@ def call_func(fn: Func, arg: Node):
n.parent.set_side(n.parent_side, clone(arg)) # type: ignore
return fn.left
# Do a single reduction step
def reduce(node: Node) -> tuple[ReductionType, Node]:
if not isinstance(node, Node):
@ -616,12 +614,19 @@ def reduce(node: Node) -> tuple[ReductionType, Node]:
return ReductionType.NOTHING, out
def expand(node: Node, *, force_all = False) -> tuple[int, Node]:
"""
Expands expandable nodes in the given tree.
If force_all is false, this only expands
ExpandableEndnodes that have "always_expand" set to True.
If force_all is True, this expands ALL
ExpandableEndnodes.
"""
# Expand all expandable end nodes.
def finalize_macros(node: Node, *, force = False) -> tuple[int, Node]:
if not isinstance(node, Node):
raise TypeError(f"I can't reduce a {type(node)}")
raise TypeError(f"I don't know what to do with a {type(node)}")
out = clone(node)
ptr = out
@ -631,7 +636,7 @@ def finalize_macros(node: Node, *, force = False) -> tuple[int, Node]:
while True:
if (
isinstance(ptr, ExpandableEndNode) and
(force or ptr.always_expand)
(force_all or ptr.always_expand)
):
if ptr.parent is None:
ptr = ptr.expand()[1]
@ -648,6 +653,7 @@ def finalize_macros(node: Node, *, force = False) -> tuple[int, Node]:
macro_expansions += 1
# Tree walk logic
if isinstance(ptr, EndNode):
from_side, ptr = ptr.go_up()
elif isinstance(ptr, Func):
@ -665,4 +671,4 @@ def finalize_macros(node: Node, *, force = False) -> tuple[int, Node]:
if ptr is node.parent:
break
return macro_expansions, out # type: ignore
return macro_expansions, out

View File

@ -110,9 +110,18 @@ class Runner:
def reduce(self, node: lamb.node.Node, *, status = {}) -> None:
# Show warnings
warning_text = []
# Reduction Counter.
# We also count macro (and church) expansions,
# and subtract those from the final count.
k = 0
macro_expansions = 0
stop_reason = StopReason.MAX_EXCEEDED
start_time = time.time()
out_text = []
if status["has_history"] and len(self.history) != 0:
warning_text += [
("class:code", "$"),
@ -121,32 +130,31 @@ class Runner:
("class:warn", "\n")
]
only_macro = isinstance(node, lamb.node.ExpandableEndNode)
if only_macro:
warning_text += [
("class:warn", "All macros will be expanded"),
("class:warn", "\n")
]
m, node = lamb.node.expand(node, force_all = only_macro)
macro_expansions += m
for i in status["free_variables"]:
warning_text += [
("class:warn", "Macro "),
("class:warn", "Name "),
("class:code", i),
("class:warn", " will become a free variable.\n"),
("class:warn", " is a free variable\n"),
]
printf(FormattedText(warning_text), style = lamb.utils.style)
# Reduction Counter.
# We also count macro (and church) expansions,
# and subtract those from the final count.
i = 0
macro_expansions = 0
stop_reason = StopReason.MAX_EXCEEDED
start_time = time.time()
full_reduce = isinstance(node, lamb.node.ExpandableEndNode)
out_text = []
while (self.reduction_limit is None) or (i < self.reduction_limit):
while (self.reduction_limit is None) or (k < self.reduction_limit):
# Show reduction count
if (i >= self.iter_update) and (i % self.iter_update == 0):
print(f" Reducing... {i:,}", end = "\r")
if (k >= self.iter_update) and (k % self.iter_update == 0):
print(f" Reducing... {k:,}", end = "\r")
try:
red_type, node = lamb.node.reduce(node)
@ -161,17 +169,13 @@ class Runner:
break
# Count reductions
i += 1
k += 1
if red_type == lamb.node.ReductionType.FUNCTION_APPLY:
macro_expansions += 1
# Expand all remaining macros
m, node = lamb.node.finalize_macros(node, force = full_reduce)
macro_expansions += m
if i >= self.iter_update:
# Clear reduction counter
print(" " * round(14 + math.log10(i)), end = "\r")
if k >= self.iter_update:
# Clear reduction counter if it was printed
print(" " * round(14 + math.log10(k)), end = "\r")
out_text += [
("class:result_header", f"Runtime: "),
@ -184,22 +188,17 @@ class Runner:
("class:text", f"{macro_expansions:,}"),
("class:result_header", f"\nReductions: "),
("class:text", f"{i:,}\t"),
("class:text", f"{k:,}\t"),
("class:muted", f"(Limit: {self.reduction_limit:,})")
]
if full_reduce:
out_text += [
("class:warn", "\nAll macros have been expanded")
]
if (stop_reason == StopReason.BETA_NORMAL or stop_reason == StopReason.LOOP_DETECTED):
out_text += [
("class:result_header", "\n\n => "),
("class:text", str(node)), # type: ignore
]
self.history.append(lamb.node.finalize_macros(node, force = True)[1])
self.history.append(lamb.node.expand(node, force_all = True)[1])
printf(