当前位置:网站首页>Simply Untyped Lambda Calculus的实现
Simply Untyped Lambda Calculus的实现
2022-06-10 21:18:00 【WinterShiver】
学习了A Tutorial Introduction to the Lambda Calculus, by Raul Rojas这篇lecture note,简单概括了一下文章内容。
-- A Tutorial Introduction to the Lambda Calculus, by Raul Rojas
-- https://arxiv.org/abs/1503.09060
module SU where
type Name = String
data Expr = Id Name | Lam Name Expr | App Expr Expr deriving (Eq)
instance Show Expr where
show (Id name) = name
show (Lam name expr) = "(\\ " ++ name ++ " -> " ++ show expr ++ ")"
show (App expr1@(Lam _ _) expr2@(Lam _ _)) = "(" ++ show expr1 ++ ") (" ++ show expr2 ++ ")"
show (App expr1@(Lam _ _) expr2@(App _ _)) = "(" ++ show expr1 ++ ") (" ++ show expr2 ++ ")"
show (App expr1@(Lam _ _) expr2) = "(" ++ show expr1 ++ ") " ++ show expr2
show (App expr1 expr2) = show expr1 ++ " " ++ show expr2
-- Grammar
-- 括号、左结合
-- Semantics
free :: Name -> Expr -> Bool
free n (Id name) = True
free n (Lam name expr) = n /= name && free n expr
free n (App expr1 expr2) = free n expr1 || free n expr2
substitute :: Name -> Expr -> Expr -> Expr
substitute n e e'@(Id name) = if n == name then e else e'
substitute n e e'@(Lam name expr) = if n == name then e' else Lam name (substitute n e expr)
substitute n e (App expr1 expr2) = App (substitute n e expr1) (substitute n e expr2)
eval :: Expr -> Expr
eval (Id name) = Id name
eval (Lam name expr) = Lam name (eval expr)
eval (App (Lam name expr) e) = eval $ substitute name e expr -- beta reduction
eval (App expr1 expr2) = App (eval expr1) (eval expr2)
synonym :: Expr -> Expr -> Bool
synonym expr1 expr2 = synonym' (eval expr1) (eval expr2) where
synonym' (Id name1) (Id name2) = name1 == name2
synonym' (Lam name1 expr1) (Lam name2 expr2) = synonym expr1 (substitute name2 (Id name1) expr2)
synonym' (App expr1 expr2) (App expr1' expr2') = synonym expr1 expr1' && synonym expr2 expr2'
changeName :: Name -> Name -> Expr -> Expr -- alpha conversion
changeName n n' (Id name) = Id name
changeName n n' (Lam name expr) = if n == name
then Lam n' (substitute name (Id n') expr)
else Lam name (changeName n n' expr)
changeName n n' (App expr1 expr2) = App (changeName n n' expr1) (changeName n n' expr2)
-- Arithmetic: Church Numeral, nat, +, *
-- Conditionals: T/F, and/or/not, conditional, pair & nat pred, nat ord
-- Recursion: Y combinator; defining recursive function by Y combinator
以及对Encoding Data in Lambda Calculus: An Introduction, by Frank (Peng) Fu的学习笔记
-- Others:
-- Encoding Data in Lambda Calculus: An Introduction, by Frank (Peng) Fu
-- Church Encoding: better presents iteration (iterN)
-- Scott Encoding: better presents `if` (caseN, pattern matching)
-- Parigot: better presents recursion
边栏推荐
- Several Apache related security vulnerability fixes
- C程序实例1--个人通讯录管理系统
- Apache相关的几个安全漏洞修复
- Principle of gravure overprint and factors affecting overprint
- Share some information I accumulated when I was working as a dotnet9 blog site
- Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
- 【问题】解决Websocket字符串长度限制问题单包过大
- 鲸会务智慧景区管理解决方案
- 数组 求并集
- Mysql的回表查询?如何避免?
猜你喜欢

Principle of gravure overprint and factors affecting overprint

小微企业如何低成本搭建微官网

【MySQL】表结构的增删查改操作(DDL)

Sealem Finance打造Web3去中心化金融平台基础设施

【phpstorm】 No data sources are configured to run this SQL and provide advanced c

Abbexa 8-OHdG CLIA kit solution

鲸会务智慧景区管理解决方案

【MySQL】表的约束

JS anchor positioning can extend many functions
To do desktop plug-in, a good helper for office workers
随机推荐
数组 移动0
Array intersection of two arrays II
数组 从指定长度位旋转数组
磁盘序列号,磁盘ID,卷序列号的区别
【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas
Can I make up the exam if I fail the soft exam? Here comes the answer
torch_geometric
Abbexa cdan1 siRNA instruction manual
Notes (IV) - multithreading
Abbexa AML1 DNA binding ELISA Kit instructions
2022-06-09 rk817 PMU battery temperature detection
如何激发文化创新的活力和驱动力
leetcode:333. Maximum BST subtree
ICML2022 | Sharp-MAML:锐度感知的模型无关元学习
【MySQL】表结构的增删查改操作(DDL)
Visio 转为高质量PDF
Record (II)
datagrip 报错 “The specified database user/password combination is rejected...”的解决方法
C program example 1 -- personal address book management system
Business based precipitation component = & gt; manage-table