Added Church numeral generation

master
Mark 2022-10-21 19:39:37 -07:00
parent 7a1077e371
commit b5d97cf5c6
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 33 additions and 4 deletions

View File

@ -1,7 +1,9 @@
{ {
"cSpell.words": [ "cSpell.words": [
"autochurch",
"freevar", "freevar",
"pyparsing", "pyparsing",
"runstatus",
"subvar" "subvar"
], ],
"python.analysis.typeCheckingMode": "basic" "python.analysis.typeCheckingMode": "basic"

View File

@ -7,10 +7,8 @@
- Python files: installable, package list, etc - Python files: installable, package list, etc
- $\alpha$-equivalence check - $\alpha$-equivalence check
- Versioning - Versioning
- Automatic church numerals
- Prettyprint functions (combine args, rename bound variables) - Prettyprint functions (combine args, rename bound variables)
- Documentation in README - Documentation in README
- Don't crash on errors
## Todo: ## Todo:
- live syntax check - live syntax check
@ -23,3 +21,4 @@
## Mention in Docs ## Mention in Docs
- lambda functions only work with single-letter arguments - lambda functions only work with single-letter arguments
- church numerals

View File

@ -1,5 +1,6 @@
import pyparsing as pp import pyparsing as pp
import tokens import tokens
import utils
class Parser: class Parser:
""" """
@ -20,6 +21,9 @@ class Parser:
pp_macro = pp.Word(pp.alphas + "_") pp_macro = pp.Word(pp.alphas + "_")
pp_macro.set_parse_action(tokens.macro.from_parse) pp_macro.set_parse_action(tokens.macro.from_parse)
pp_church = pp.Word(pp.nums)
pp_church.set_parse_action(utils.autochurch)
# Function definitions. # Function definitions.
# Right associative. # Right associative.
# #
@ -50,7 +54,7 @@ class Parser:
pp_call <<= pp_expr[2, ...] pp_call <<= pp_expr[2, ...]
pp_call.set_parse_action(tokens.lambda_apply.from_parse) pp_call.set_parse_action(tokens.lambda_apply.from_parse)
pp_expr <<= pp_lambda_fun ^ (lp + pp_expr + rp) ^ pp_macro ^ (lp + pp_call + rp) pp_expr <<= pp_lambda_fun ^ (lp + pp_expr + rp) ^ pp_macro ^ (lp + pp_call + rp) ^ pp_church
pp_all = pp_expr | pp_macro_def pp_all = pp_expr | pp_macro_def
pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_") pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_")

24
utils.py Normal file
View File

@ -0,0 +1,24 @@
import tokens
def autochurch(results):
"""
Makes a church numeral from an integer.
"""
num = int(results[0])
f = tokens.bound_variable()
a = tokens.bound_variable()
chain = a
for i in range(num):
chain = tokens.lambda_apply(f, chain)
return tokens.lambda_func(
f,
tokens.lambda_func(
a,
chain
)
)