当前位置:网站首页>Implementation of simply untyped lambda calculus
Implementation of simply untyped lambda calculus
2022-06-10 22:30:00 【WinterShiver】
To study the A Tutorial Introduction to the Lambda Calculus, by Raul Rojas This article lecture note, Briefly summarize the content of the article .
-- 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
-- Brackets 、 Left Union
-- 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
And right Encoding Data in Lambda Calculus: An Introduction, by Frank (Peng) Fu Learning notes of
-- 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
边栏推荐
猜你喜欢
![[MySQL] summary of common data types](/img/96/010c21f0aa7b443c130c5f55e5277a.png)
[MySQL] summary of common data types

How to do well in the top-level design of informatization in the process of informatization upgrading of traditional enterprises
![Add, delete, query and modify [MySQL] table data (DML)](/img/08/4239bc0486fe8db2e98e54919300b5.png)
Add, delete, query and modify [MySQL] table data (DML)
![Ability to deliver packages within D days [abstract class dichotomy -- Abstract judgment method]](/img/16/026c017e1c54d580fa2d237aa4605c.png)
Ability to deliver packages within D days [abstract class dichotomy -- Abstract judgment method]

很流行的状态管理库 MobX 是怎么回事?

Visio to high quality pdf

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

TcaplusDB君 · 行业新闻汇编(六)

【TcaplusDB知识库】TcaplusDB进程启动介绍

【TcaplusDB知识库】TcaplusDB查看进程所在机器介绍
随机推荐
KDD2022 | 基于对抗性知识蒸馏的深度图神经网络压缩
Array intersection of two arrays II
C程序实例1--个人通讯录管理系统
A WPF developed Print dialog box -printdialogx
数组 加一
Shell基础概念
[tcapulusdb knowledge base] Introduction to tcapulusdb patrol inspection statistics
Array remove duplicates from an array
leetcode:333. 最大 BST 子树
ICML2022 | Sharp-MAML:锐度感知的模型无关元学习
数组 求并集
【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
SQL server queries are case sensitive
A number that appears only once in an array
Sealem finance builds Web3 decentralized financial platform infrastructure
【TcaplusDB知识库】TcaplusDB TcapProxy扩缩容介绍
【TcaplusDB知识库】TcaplusDB推送配置介绍
创新探索层层加码,容智流程挖掘领域模式趋向成熟
笔记(二)