我刚刚为即将到来的OCaml测试做了一些功课,我遇到了一些麻烦.
考虑由以下抽象语法定义的λ项的语言(其中x是变量):
t ::= x | t t | ?x. t
Run Code Online (Sandbox Code Playgroud)
写一个类型术语来表示λ术语.假设变量表示为字符串.
好的,男孩.
# type t = Var of string | App of (t*t) | Abs of string*t;;
type t = Var of string | App of (t * t) | Abs of (string * t)
Run Code Online (Sandbox Code Playgroud)
术语t的自由变量fv(t)由归纳定义如下:
fv(x) = {x}
fv(t t') = fv(t) ? fv(t')
fv(?x. t) = fv(t) \ {x}
Run Code Online (Sandbox Code Playgroud)
当然可以.
# let rec fv term = match term with
Var x -> [x]
| App (t, t') …Run Code Online (Sandbox Code Playgroud)