Compare commits

...

4 Commits

Author SHA1 Message Date
39d32a9925
Updated README 2022-11-09 21:54:10 -08:00
89bc4bd2c0
Minor warning cleanup 2022-11-09 21:47:55 -08:00
062a4b4d1b
Updated README 2022-11-09 21:44:58 -08:00
4e3d5fa341
Fixed a macro bug 2022-11-09 21:44:32 -08:00
5 changed files with 39 additions and 44 deletions

View File

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

View File

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

View File

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

View File

@ -56,25 +56,23 @@ class Runner:
message = self.prompt_message
)
def parse(self, line) -> tuple[lamb.nodes.Root | MacroDef | Command, dict]:
def parse(self, line) -> tuple[lamb.nodes.Root | MacroDef | Command, list]:
e = self.parser.parse_line(line)
o = {}
w = []
if isinstance(e, MacroDef):
e.expr = lamb.nodes.Root(e.expr)
e.set_runner(self)
o = lamb.nodes.prepare(e.expr, ban_macro_name = e.label)
w = lamb.nodes.prepare(e.expr, ban_macro_name = e.label)
elif isinstance(e, lamb.nodes.Node):
e = lamb.nodes.Root(e)
e.set_runner(self)
o = lamb.nodes.prepare(e)
w = lamb.nodes.prepare(e)
return e, o
return e, w
def reduce(self, node: lamb.nodes.Root, *, status = {}) -> None:
warning_text = []
def reduce(self, node: lamb.nodes.Root, *, warnings = []) -> None:
# Reduction Counter.
# We also count macro (and church) expansions,
@ -86,14 +84,6 @@ class Runner:
start_time = time.time()
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 = (
isinstance(node.left, lamb.nodes.Macro) or
isinstance(node.left, lamb.nodes.Church)
@ -103,16 +93,8 @@ class Runner:
m, node = lamb.nodes.expand(node, force_all = only_macro)
macro_expansions += m
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)
if len(warnings) != 0:
printf(FormattedText(warnings), style = lamb.utils.style)
while (
@ -210,7 +192,7 @@ class Runner:
*,
silent = False
) -> None:
e, o = self.parse(line)
e, w = self.parse(line)
# If this line is a macro definition, save the macro.
if isinstance(e, MacroDef):
@ -230,7 +212,7 @@ class Runner:
# If this line is a plain expression, reduce it.
elif isinstance(e, lamb.nodes.Node):
self.reduce(e, status = o)
self.reduce(e, warnings = w)
# We shouldn't ever get here.
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.
PAIR = λabi.(i a b)
H = λp.((PAIR (p F)) (S (p F)))
S = λnfa.(f (n f a))
D = λn.((n H) ((PAIR 0) 0) T)
H = λp.((PAIR (p F)) (S (p F)))
D = λn.(n H (PAIR 0 0) T)
Z = λn.(n (λa.F) T)
NZ = λn.(n (λa.T) F)
ADD = λmn.(m S n)
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)))