Compare commits

..

No commits in common. "39d32a9925b18d45412bbde57e8b5a608951c64a" and "04ce18c89125220b7f2693860f252bd4a48c4c62" have entirely different histories.

5 changed files with 44 additions and 39 deletions

View File

@ -77,7 +77,8 @@ The lines in a file look exactly the same as regular entries in the prompt, but
## Todo (pre-release, in this order): ## Todo (pre-release, in this order):
- Fix unique bound variable printer - Cleanup warnings
- Truncate long expressions in warnings
- Prevent macro-chaining recursion - Prevent macro-chaining recursion
- Full-reduce option (expand all macros) - Full-reduce option (expand all macros)
- step-by-step reduction - step-by-step reduction
@ -88,16 +89,12 @@ The lines in a file look exactly the same as regular entries in the prompt, but
## Todo: ## Todo:
- Cleanup warnings
- Syntax highlight printouts
- Truncate long expressions in warnings
- History queue + indexing - History queue + indexing
- Show history command - Show history command
- Loop detection - Loop detection
- Optimization: reduction can be slow with large trees.
- $\alpha$-equivalence check - $\alpha$-equivalence check
- Command-line options (load a file) - Command-line options (load a file)
- Unchurch command: make church numerals human-readable - Unchurch command: make church numerals human-readable
- Better Syntax highlighting - Better Syntax highlighting
- Complete file names and commands - Syntax highlight printouts
- Tests - Tests

View File

@ -95,7 +95,7 @@ def clone(node: lbn.Node):
break break
return out return out
def prepare(root: lbn.Root, *, ban_macro_name = None) -> list: def prepare(root: lbn.Root, *, ban_macro_name = None) -> dict:
""" """
Prepare an expression for expansion. Prepare an expression for expansion.
This will does the following: This will does the following:
@ -109,19 +109,15 @@ def prepare(root: lbn.Root, *, ban_macro_name = None) -> list:
bound_variables = {} bound_variables = {}
warnings = [] output = {
"has_history": False,
"free_variables": set()
}
it = iter(root) it = iter(root)
for s, n in it: for s, n in it:
if isinstance(n, lbn.History): if isinstance(n, lbn.History):
if len(root.runner.history) == 0: output["has_history"] = True
raise lbn.ReductionError("There isn't any history to reference.")
else:
warnings += [
("class:code", "$"),
("class:warn", " will be expanded to "),
("class:code", f"{n.expand()[1]}\n"),
]
# If this expression is part of a macro, # If this expression is part of a macro,
# make sure we don't reference it inside itself. # make sure we don't reference it inside itself.
@ -139,11 +135,7 @@ def prepare(root: lbn.Root, *, ban_macro_name = None) -> list:
# Turn undefined macros into free variables # Turn undefined macros into free variables
elif n.name not in root.runner.macro_table: elif n.name not in root.runner.macro_table:
warnings += [ output["free_variables"].add(n.name)
("class:warn", "Name "),
("class:code", n.name),
("class:warn", " is a free variable\n"),
]
n.parent.set_side( n.parent.set_side(
n.parent_side, n.parent_side,
n.to_freevar() n.to_freevar()
@ -166,7 +158,7 @@ def prepare(root: lbn.Root, *, ban_macro_name = None) -> list:
elif s == lbn.Direction.LEFT: elif s == lbn.Direction.LEFT:
del bound_variables[n.input.name] del bound_variables[n.input.name]
return warnings return output
# Apply a function. # Apply a function.
# Returns the function's output. # Returns the function's output.

View File

@ -319,10 +319,8 @@ class History(ExpandableEndNode):
return "$" return "$"
def expand(self) -> tuple[lbn.ReductionType, Node]: def expand(self) -> tuple[lbn.ReductionType, Node]:
# We shouldn't ever get here, prepare()
# catches empty history.
if len(self.runner.history) == 0: if len(self.runner.history) == 0:
raise Exception(f"Tried to expand empty history.") raise lbn.ReductionError(f"There isn't any history to reference.")
# .left is VERY important! # .left is VERY important!
# self.runner.history will contain Root nodes, # self.runner.history will contain Root nodes,
# and we don't want those *inside* our tree. # and we don't want those *inside* our tree.

View File

@ -56,23 +56,25 @@ class Runner:
message = self.prompt_message message = self.prompt_message
) )
def parse(self, line) -> tuple[lamb.nodes.Root | MacroDef | Command, list]: def parse(self, line) -> tuple[lamb.nodes.Root | MacroDef | Command, dict]:
e = self.parser.parse_line(line) e = self.parser.parse_line(line)
w = [] o = {}
if isinstance(e, MacroDef): if isinstance(e, MacroDef):
e.expr = lamb.nodes.Root(e.expr) e.expr = lamb.nodes.Root(e.expr)
e.set_runner(self) e.set_runner(self)
w = lamb.nodes.prepare(e.expr, ban_macro_name = e.label) o = lamb.nodes.prepare(e.expr, ban_macro_name = e.label)
elif isinstance(e, lamb.nodes.Node): elif isinstance(e, lamb.nodes.Node):
e = lamb.nodes.Root(e) e = lamb.nodes.Root(e)
e.set_runner(self) e.set_runner(self)
w = lamb.nodes.prepare(e) o = lamb.nodes.prepare(e)
return e, w return e, o
def reduce(self, node: lamb.nodes.Root, *, warnings = []) -> None: def reduce(self, node: lamb.nodes.Root, *, status = {}) -> None:
warning_text = []
# Reduction Counter. # Reduction Counter.
# We also count macro (and church) expansions, # We also count macro (and church) expansions,
@ -84,6 +86,14 @@ class Runner:
start_time = time.time() start_time = time.time()
out_text = [] out_text = []
if status["has_history"] and len(self.history) != 0:
warning_text += [
("class:code", "$"),
("class:warn", " will be expanded to "),
("class:code", str(self.history[-1])),
("class:warn", "\n")
]
only_macro = ( only_macro = (
isinstance(node.left, lamb.nodes.Macro) or isinstance(node.left, lamb.nodes.Macro) or
isinstance(node.left, lamb.nodes.Church) isinstance(node.left, lamb.nodes.Church)
@ -93,8 +103,16 @@ class Runner:
m, node = lamb.nodes.expand(node, force_all = only_macro) m, node = lamb.nodes.expand(node, force_all = only_macro)
macro_expansions += m macro_expansions += m
if len(warnings) != 0:
printf(FormattedText(warnings), style = lamb.utils.style) for i in status["free_variables"]:
warning_text += [
("class:warn", "Name "),
("class:code", i),
("class:warn", " is a free variable\n"),
]
if len(warning_text) != 0:
printf(FormattedText(warning_text), style = lamb.utils.style)
while ( while (
@ -192,7 +210,7 @@ class Runner:
*, *,
silent = False silent = False
) -> None: ) -> None:
e, w = self.parse(line) e, o = self.parse(line)
# If this line is a macro definition, save the macro. # If this line is a macro definition, save the macro.
if isinstance(e, MacroDef): if isinstance(e, MacroDef):
@ -212,7 +230,7 @@ class Runner:
# If this line is a plain expression, reduce it. # If this line is a plain expression, reduce it.
elif isinstance(e, lamb.nodes.Node): elif isinstance(e, lamb.nodes.Node):
self.reduce(e, warnings = w) self.reduce(e, status = o)
# We shouldn't ever get here. # We shouldn't ever get here.
else: else:

View File

@ -67,11 +67,11 @@ XOR = λab.((a (NOT b)) b)
# `Y FAC 6` required 867,920 reductions and took 10 minutes to run. # `Y FAC 6` required 867,920 reductions and took 10 minutes to run.
PAIR = λabi.(i a b) PAIR = λabi.(i a b)
S = λnfa.(f (n f a))
H = λp.((PAIR (p F)) (S (p F))) H = λp.((PAIR (p F)) (S (p F)))
D = λn.(n H (PAIR 0 0) T) S = λnfa.(f (n f a))
D = λn.((n H) ((PAIR 0) 0) T)
Z = λn.(n (λa.F) T) Z = λn.(n (λa.F) T)
NZ = λn.(n (λa.T) F) NZ = λn.(n (λa.T) F)
ADD = λmn.(m S n) ADD = λmn.(m S n)
MULT = λnmf.(n (m f)) MULT = λnmf.(n (m f))
FAC = λyn.(Z n) (1) (MULT n (y (D n))) FAC = λyn.( (Z n)(1)((MULT n) (y (D n))) )