在OCaml中使用GADTs的简单lambda演算DSL

在OCaml中使用GADTs的简单lambda演算DSL,ocaml,dsl,lambda-calculus,gadt,Ocaml,Dsl,Lambda Calculus,Gadt,如何使用GADT在OCaml中定义一个简单的lambda演算(如DSL)?具体地说,我不知道如何正确定义类型检查器以将非类型化AST转换为类型化AST,也不知道上下文和环境的正确类型 下面是一些使用OCaml中传统方法的简单lambda演算类语言的代码 (* Here's a traditional implementation of a lambda calculus like language *) type typ = | Boolean | Integer | Arrow of typ

如何使用GADT在OCaml中定义一个简单的lambda演算(如DSL)?具体地说,我不知道如何正确定义类型检查器以将非类型化AST转换为类型化AST,也不知道上下文和环境的正确类型

下面是一些使用OCaml中传统方法的简单lambda演算类语言的代码

(* Here's a traditional implementation of a lambda calculus like language *)

type typ =
| Boolean
| Integer
| Arrow of typ*typ

type exp =
| Add of exp*exp
| And of exp*exp
| App of exp*exp
| Lam of string*typ*exp
| Var of string
| Int of int
| Bol of bool

let e1=Add(Int 1,Add(Int 2,Int 3))
let e2=Add(Int 1,Add(Int 2,Bol false)) (* Type error *)
let e3=App(Lam("x",Integer,Add(Var "x",Var "x")),Int 4)

let rec typecheck con e =
    match e with
    | Add(e1,e2) ->
        let t1=typecheck con e1 in
        let t2=typecheck con e2 in
        begin match (t1,t2) with 
        | (Integer,Integer) -> Integer
        | _ -> failwith "Tried to add with something other than Integers"
        end
    | And(e1,e2) ->
        let t1=typecheck con e1 in
        let t2=typecheck con e2 in
        begin match (t1,t2) with 
        | (Boolean,Boolean) -> Boolean 
        | _ -> failwith "Tried to and with something other than Booleans"
        end
    | App(e1,e2) ->
        let t1=typecheck con e1 in
        let t2=typecheck con e2 in
        begin match t1 with 
        | Arrow(t11,t12) ->
            if t11 <> t2 then
                failwith "Mismatch of types on a function application"
            else
                t12
        | _ -> failwith "Tried to apply a non-arrow type" 
        end
    | Lam(x,t,e) ->
        Arrow (t,typecheck ((x,t)::con) e)
    | Var x  ->
        let (y,t) = List.find (fun (y,t)->y=x) con in
        t
    | Int _ -> Integer
    | Bol _ -> Boolean

let t1 = typecheck [] e1
(* let t2 = typecheck [] e2 *)
let t3 = typecheck [] e3

type value = 
| VBoolean of bool
| VInteger of int
| VArrow of ((string*value) list -> value -> value)

let rec eval env e = 
    match e with
    | Add(e1,e2) ->
        let v1=eval env e1 in
        let v2=eval env e2 in
        begin match (v1,v2) with 
        | (VInteger i1,VInteger i2) -> VInteger (i1+i2) 
        | _ -> failwith "Tried to add with something other than Integers"
        end
    | And(e1,e2) ->
        let v1=eval env e1 in
        let v2=eval env e2 in
        begin match (v1,v2) with 
        | (VBoolean b1,VBoolean b2) -> VBoolean (b1 && b2) 
        | _ -> failwith "Tried to and with something other than Booleans"
        end
    | App(e1,e2) ->
        let v1=eval env e1 in
        let v2=eval env e2 in
        begin match v1 with 
        | VArrow a1 -> a1  env v2 
        | _ -> failwith "Tried to apply a non-arrow type" 
        end
    | Lam(x,t,e) ->
        VArrow (fun env' v' -> eval ((x,v')::env') e) 
    | Var x  ->
        let (y,v) = List.find (fun (y,t)->y=x) env in
        v 
    | Int i -> VInteger i 
    | Bol b -> VBoolean b

let v1 = eval [] e1
let v3 = eval [] e3
问题出在这里。首先,我不确定如何在类型texp中为TLam和TVar定义正确的类型。一般来说,我会为类型提供变量名,但我不确定在这种情况下如何做到这一点。其次,我不知道函数typecheck中上下文的正确类型。以前,我使用过某种列表,但现在我确定列表的类型。第三,在省略上下文之后,typecheck函数本身不进行类型检查。它与消息一起失败

File "test03.ml", line 32, characters 8-22:
Error: This expression has type int texp
       but an expression was expected of type a texp
       Type int is not compatible with type a 
这完全有道理。这是一个更大的问题,我不确定什么样的正确类型的打字检查应该是

在任何情况下,您如何着手修复这些功能


编辑1 以下是上下文或环境的可能类型

type _ ctx =
| Empty : unit ctx
| Item :  string * 'a * 'b ctx -> ('a*'b) ctx

编辑2 环境的诀窍是确保将环境的类型嵌入到表达式的类型中。否则,就没有足够的信息来保证事物的类型安全。这是一个完整的翻译。目前,我没有有效的类型检查器来将非类型化表达式转换为类型化表达式

type (_,_) texp =
| TAdd : ('e,int) texp * ('e,int) texp -> ('e,int) texp
| TAnd : ('e,bool) texp * ('e,bool) texp -> ('e,bool) texp
| TApp : ('e,('a -> 'b)) texp * ('e,'a) texp -> ('e,'b) texp
| TLam : (('a*'e),'b) texp -> ('e,('a -> 'b)) texp
| TVar0 : (('a*'e),'a) texp
| TVarS : ('e,'a) texp -> (('b*'e),'a) texp
| TInt : int -> ('e,int) texp
| TBol : bool -> ('e,bool) texp

let te1 = TAdd(TInt 1,TAdd(TInt 2,TInt 3))
(*let te2 = TAdd(TInt 1,TAdd(TInt 2,TBol false))*)
let te3 = TApp(TLam(TAdd(TVar0,TVar0)),TInt 4)
let te4 = TApp(TApp(TLam(TLam(TAdd(TVar0,TVarS(TVar0)))),TInt 4),TInt 5)
let te5 = TLam(TLam(TVarS(TVar0)))

let rec eval : type e t. e -> (e,t) texp -> t = fun env e -> 
    match e with
    | TAdd (e1,e2) ->
        let v1 = eval env e1 in
        let v2 = eval env e2 in
        v1 + v2
    | TAnd (e1,e2) ->
        let v1 = eval env e1 in
        let v2 = eval env e2 in
        v1 && v2
    | TApp (e1,e2) ->
        let v1 = eval env e1 in
        let v2 = eval env e2 in
        v1 v2
    | TLam e ->
        fun x -> eval (x,env) e 
    | TVar0 ->
        let (v,vs)=env in
        v
    | TVarS e ->
        let (v,vs)=env in
        eval vs e 
    | TInt i -> i
    | TBol b -> b
那么,我们有

# eval () te1;;
- : int = 6
# eval () te3;;
- : int = 8
# eval () te5;;
- : '_a -> '_b -> '_a = <fun>
# eval () te4;;
- : int = 9
#eval()te1;;
-:int=6
#eval()te3;;
-:int=8
#eval()te5;;
-:“'uA->'\uB->'\uA=
#eval()te4;;
-:int=9

如果您希望术语表示强制良好的类型性,则需要更改类型环境(和变量)的表示方式:您不能精细地键入从字符串到值的映射(表示映射的类型是同构的)。经典的解决方案是使用(强类型数字)而不是变量名来表示变量。它可能会帮助您首先在非类型世界中执行转换,然后只关心在非类型->GADT过程中键入

下面是rouhgly勾勒的强类型变量的GADT声明:

type (_, _) var =
  | Z : ('a, 'a * 'g) var
  | S : ('a, 'g) var -> ('a, 'b * 'g) var
类型为
('A,'g)var
的值应理解为从类型为
'g
的环境中提取类型为
'A
的值的方法描述。环境由一系列右嵌套元组表示。
Z
案例对应于拾取环境中的第一个变量,而
S
案例忽略最上面的变量,并在环境中看得更深


Shayan Najd(Haskell)实现了这一想法。请随便看看或。

好吧,我终于解决了问题。因为我可能不是唯一一个对此感兴趣的人,所以这里有一套完整的代码,可以同时进行类型检查和评估:

type (_,_) texp =
| TAdd : ('gamma,int) texp * ('gamma,int) texp -> ('gamma,int) texp
| TAnd : ('gamma,bool) texp * ('gamma,bool) texp -> ('gamma,bool) texp
| TApp : ('gamma,('t1 -> 't2)) texp * ('gamma,'t1) texp -> ('gamma,'t2) texp
| TLam : (('gamma*'t1),'t2) texp -> ('gamma,('t1 -> 't2)) texp
| TVar0 : (('gamma*'t),'t) texp
| TVarS : ('gamma,'t1) texp -> (('gamma*'t2),'t1) texp
| TInt : int -> ('gamma,int) texp
| TBol : bool -> ('gamma,bool) texp

type _ typ =
| Integer : int typ
| Boolean : bool typ
| Arrow : 'a typ * 'b typ -> ('a -> 'b) typ

type (_,_) iseq = IsEqual : ('a,'a) iseq
let rec is_equal : type a b. a typ -> b typ -> (a,b) iseq option = fun a b ->
    match a, b with
    | Integer, Integer -> Some IsEqual
    | Boolean, Boolean -> Some IsEqual
    | Arrow(t1,t2), Arrow(u1,u2) ->
        begin match is_equal t1 u1, is_equal t2 u2 with
        | Some IsEqual, Some IsEqual -> Some IsEqual
        | _ -> None
        end
    | _ -> None

type _ isint = IsInt : int isint
let is_integer : type a. a typ -> a isint option = fun a -> 
    match a with
    | Integer -> Some IsInt
    | _ -> None

type _ isbool = IsBool : bool isbool
let is_boolean : type a. a typ -> a isbool option = fun a -> 
    match a with
    | Boolean -> Some IsBool 
    | _ -> None

type _ context =
| CEmpty : unit context 
| CVar : 'a context * 't typ -> ('a*'t) context 

type exp =
| Add of exp*exp
| And of exp*exp
| App of exp*exp
| Lam : 'a typ * exp -> exp
| Var0
| VarS of exp
| Int of int
| Bol of bool

type _ exists_texp =
| Exists : ('gamma,'t) texp * 't typ -> 'gamma exists_texp

let rec typecheck
    : type gamma t. gamma context -> exp -> gamma exists_texp =
fun ctx e ->
    match e with
    | Int i -> Exists ((TInt i) , Integer)
    | Bol b -> Exists ((TBol b) , Boolean)
    | Var0 ->
        begin match ctx with
        | CEmpty -> failwith "Tried to grab a nonexistent variable"
        | CVar(ctx,t) -> Exists (TVar0 , t)
        end
    | VarS e ->
        begin match ctx with
        | CEmpty -> failwith "Tried to grab a nonexistent variable"
        | CVar(ctx,_) ->
            let tet = typecheck ctx e in
            begin match tet with
            | Exists (te,t) -> Exists ((TVarS te) , t)
            end
        end
    | Lam(t1,e) ->
        let tet2 = typecheck (CVar (ctx,t1)) e in
        begin match tet2 with
        | Exists (te,t2) -> Exists ((TLam te) , (Arrow(t1,t2)))
        end
    | App(e1,e2) ->
        let te1t1 = typecheck ctx e1 in
        let te2t2 = typecheck ctx e2 in
        begin match te1t1,te2t2 with
        | Exists (te1,t1),Exists (te2,t2) ->
            begin match t1 with
            | Arrow(t11,t12) ->
                let p = is_equal t11 t2 in
                begin match p with
                | Some IsEqual -> 
                    Exists ((TApp (te1,te2)) , t12)
                | None -> 
                    failwith "Mismatch of types on a function application"
                end
            | _ -> failwith "Tried to apply a non-arrow type" 
            end
        end
    | Add(e1,e2) ->
        let te1t1 = typecheck ctx e1 in
        let te2t2 = typecheck ctx e2 in
        begin match te1t1,te2t2 with
        | Exists (te1,t1),Exists (te2,t2) ->
            let p = is_equal t1 t2 in
            let q = is_integer t1 in
            begin match p,q with
            | Some IsEqual, Some IsInt ->
                Exists ((TAdd (te1,te2)) , t1)
            | _ ->
                failwith "Tried to add with something other than Integers"
            end
        end
    | And(e1,e2) ->
        let te1t1 = typecheck ctx e1 in
        let te2t2 = typecheck ctx e2 in
        begin match te1t1,te2t2 with
        | Exists (te1,t1),Exists (te2,t2) ->
            let p = is_equal t1 t2 in
            let q = is_boolean t1 in
            begin match p,q with
            | Some IsEqual, Some IsBool ->
                Exists ((TAnd (te1,te2)) , t1)
            | _ ->
                failwith "Tried to and with something other than Booleans"
            end
        end

let e1 = Add(Int 1,Add(Int 2,Int 3))
let e2 = Add(Int 1,Add(Int 2,Bol false))
let e3 = App(Lam(Integer,Add(Var0,Var0)),Int 4)
let e4 = App(App(Lam(Integer,Lam(Integer,Add(Var0,VarS(Var0)))),Int 4),Int 5)
let e5 = Lam(Integer,Lam(Integer,VarS(Var0)))
let e6 = App(Lam(Integer,Var0),Int 1)
let e7 = App(Lam(Integer,Lam(Integer,Var0)),Int 1)
let e8 = Lam(Integer,Var0)
let e9 = Lam(Integer,Lam(Integer,Var0))

let tet1 = typecheck CEmpty e1
(*let tet2 = typecheck CEmpty e2*)
let tet3 = typecheck CEmpty e3
let tet4 = typecheck CEmpty e4
let tet5 = typecheck CEmpty e5
let tet6 = typecheck CEmpty e6
let tet7 = typecheck CEmpty e7
let tet8 = typecheck CEmpty e8
let tet9 = typecheck CEmpty e9

let rec eval : type gamma t. gamma -> (gamma,t) texp -> t = fun env e -> 
    match e with
    | TAdd (e1,e2) ->
        let v1 = eval env e1 in
        let v2 = eval env e2 in
        v1 + v2
    | TAnd (e1,e2) ->
        let v1 = eval env e1 in
        let v2 = eval env e2 in
        v1 && v2
    | TApp (e1,e2) ->
        let v1 = eval env e1 in
        let v2 = eval env e2 in
        v1 v2
    | TLam e ->
        fun x -> eval (env,x) e 
    | TVar0 ->
        let (env,x)=env in
        x
    | TVarS e ->
        let (env,x)=env in
        eval env e 
    | TInt i -> i
    | TBol b -> b

type exists_v =
| ExistsV : 't -> exists_v

let typecheck_eval e =
    let tet = typecheck CEmpty e in
    match tet with
    | Exists (te,t) -> ExistsV (eval () te)

let v1 = typecheck_eval e1
let v3 = typecheck_eval e3
let v4 = typecheck_eval e4
let v5 = typecheck_eval e5
let v6 = typecheck_eval e6
let v7 = typecheck_eval e7
let v8 = typecheck_eval e8
let v9 = typecheck_eval e9
以下是我遇到的问题,以及我是如何解决这些问题的

  • 为了正确键入类型化表达式texp,需要将环境类型内置到texp类型中。这意味着,正如gasche正确指出的那样,我们需要某种De Bruijin符号。最简单的就是Var0和VarS。为了使用变量名,我们只需对AST进行预处理
  • 表达式的类型typ需要包括要匹配的变量类型和我们在类型化表达式中使用的类型。换言之,这也需要一个GADT
  • 我们需要三个证明,以便在类型检查器中找出正确的类型。它们是相等的、整数的和布尔的。is_equal的代码实际上在OCaml手册的下面。具体来说,请看eq_类型的定义
  • 对于非类型化AST,类型exp实际上也需要是GADT。lambda抽象需要访问typ,这是一个GADT
  • 类型检查器返回类型化表达式和类型的存在类型。我们需要两者来让程序检查类型。此外,我们需要存在式,因为非类型化表达式可能有类型,也可能没有类型
  • 存在类型exists_texp公开环境/上下文的类型,但不公开类型。为了正确地进行类型检查,我们需要公开此类型
  • 设置好所有内容后,evaluator将严格遵循类型规则
  • 将类型检查器与求值器组合的结果必须是另一个存在类型。先验地,我们不知道结果类型,所以我们必须将其隐藏在存在包中

  • 您应该从这个问题中了解一下,我试图完全实现lambda演算,并将ADT转换为GADT。我主要关注[@gasche][^1]的链接,这是。它有点大,但您可以处理递归(仅适用于尾部递归函数)和原语。享受吧![^1]:
    type (_,_) texp =
    | TAdd : ('gamma,int) texp * ('gamma,int) texp -> ('gamma,int) texp
    | TAnd : ('gamma,bool) texp * ('gamma,bool) texp -> ('gamma,bool) texp
    | TApp : ('gamma,('t1 -> 't2)) texp * ('gamma,'t1) texp -> ('gamma,'t2) texp
    | TLam : (('gamma*'t1),'t2) texp -> ('gamma,('t1 -> 't2)) texp
    | TVar0 : (('gamma*'t),'t) texp
    | TVarS : ('gamma,'t1) texp -> (('gamma*'t2),'t1) texp
    | TInt : int -> ('gamma,int) texp
    | TBol : bool -> ('gamma,bool) texp
    
    type _ typ =
    | Integer : int typ
    | Boolean : bool typ
    | Arrow : 'a typ * 'b typ -> ('a -> 'b) typ
    
    type (_,_) iseq = IsEqual : ('a,'a) iseq
    let rec is_equal : type a b. a typ -> b typ -> (a,b) iseq option = fun a b ->
        match a, b with
        | Integer, Integer -> Some IsEqual
        | Boolean, Boolean -> Some IsEqual
        | Arrow(t1,t2), Arrow(u1,u2) ->
            begin match is_equal t1 u1, is_equal t2 u2 with
            | Some IsEqual, Some IsEqual -> Some IsEqual
            | _ -> None
            end
        | _ -> None
    
    type _ isint = IsInt : int isint
    let is_integer : type a. a typ -> a isint option = fun a -> 
        match a with
        | Integer -> Some IsInt
        | _ -> None
    
    type _ isbool = IsBool : bool isbool
    let is_boolean : type a. a typ -> a isbool option = fun a -> 
        match a with
        | Boolean -> Some IsBool 
        | _ -> None
    
    type _ context =
    | CEmpty : unit context 
    | CVar : 'a context * 't typ -> ('a*'t) context 
    
    type exp =
    | Add of exp*exp
    | And of exp*exp
    | App of exp*exp
    | Lam : 'a typ * exp -> exp
    | Var0
    | VarS of exp
    | Int of int
    | Bol of bool
    
    type _ exists_texp =
    | Exists : ('gamma,'t) texp * 't typ -> 'gamma exists_texp
    
    let rec typecheck
        : type gamma t. gamma context -> exp -> gamma exists_texp =
    fun ctx e ->
        match e with
        | Int i -> Exists ((TInt i) , Integer)
        | Bol b -> Exists ((TBol b) , Boolean)
        | Var0 ->
            begin match ctx with
            | CEmpty -> failwith "Tried to grab a nonexistent variable"
            | CVar(ctx,t) -> Exists (TVar0 , t)
            end
        | VarS e ->
            begin match ctx with
            | CEmpty -> failwith "Tried to grab a nonexistent variable"
            | CVar(ctx,_) ->
                let tet = typecheck ctx e in
                begin match tet with
                | Exists (te,t) -> Exists ((TVarS te) , t)
                end
            end
        | Lam(t1,e) ->
            let tet2 = typecheck (CVar (ctx,t1)) e in
            begin match tet2 with
            | Exists (te,t2) -> Exists ((TLam te) , (Arrow(t1,t2)))
            end
        | App(e1,e2) ->
            let te1t1 = typecheck ctx e1 in
            let te2t2 = typecheck ctx e2 in
            begin match te1t1,te2t2 with
            | Exists (te1,t1),Exists (te2,t2) ->
                begin match t1 with
                | Arrow(t11,t12) ->
                    let p = is_equal t11 t2 in
                    begin match p with
                    | Some IsEqual -> 
                        Exists ((TApp (te1,te2)) , t12)
                    | None -> 
                        failwith "Mismatch of types on a function application"
                    end
                | _ -> failwith "Tried to apply a non-arrow type" 
                end
            end
        | Add(e1,e2) ->
            let te1t1 = typecheck ctx e1 in
            let te2t2 = typecheck ctx e2 in
            begin match te1t1,te2t2 with
            | Exists (te1,t1),Exists (te2,t2) ->
                let p = is_equal t1 t2 in
                let q = is_integer t1 in
                begin match p,q with
                | Some IsEqual, Some IsInt ->
                    Exists ((TAdd (te1,te2)) , t1)
                | _ ->
                    failwith "Tried to add with something other than Integers"
                end
            end
        | And(e1,e2) ->
            let te1t1 = typecheck ctx e1 in
            let te2t2 = typecheck ctx e2 in
            begin match te1t1,te2t2 with
            | Exists (te1,t1),Exists (te2,t2) ->
                let p = is_equal t1 t2 in
                let q = is_boolean t1 in
                begin match p,q with
                | Some IsEqual, Some IsBool ->
                    Exists ((TAnd (te1,te2)) , t1)
                | _ ->
                    failwith "Tried to and with something other than Booleans"
                end
            end
    
    let e1 = Add(Int 1,Add(Int 2,Int 3))
    let e2 = Add(Int 1,Add(Int 2,Bol false))
    let e3 = App(Lam(Integer,Add(Var0,Var0)),Int 4)
    let e4 = App(App(Lam(Integer,Lam(Integer,Add(Var0,VarS(Var0)))),Int 4),Int 5)
    let e5 = Lam(Integer,Lam(Integer,VarS(Var0)))
    let e6 = App(Lam(Integer,Var0),Int 1)
    let e7 = App(Lam(Integer,Lam(Integer,Var0)),Int 1)
    let e8 = Lam(Integer,Var0)
    let e9 = Lam(Integer,Lam(Integer,Var0))
    
    let tet1 = typecheck CEmpty e1
    (*let tet2 = typecheck CEmpty e2*)
    let tet3 = typecheck CEmpty e3
    let tet4 = typecheck CEmpty e4
    let tet5 = typecheck CEmpty e5
    let tet6 = typecheck CEmpty e6
    let tet7 = typecheck CEmpty e7
    let tet8 = typecheck CEmpty e8
    let tet9 = typecheck CEmpty e9
    
    let rec eval : type gamma t. gamma -> (gamma,t) texp -> t = fun env e -> 
        match e with
        | TAdd (e1,e2) ->
            let v1 = eval env e1 in
            let v2 = eval env e2 in
            v1 + v2
        | TAnd (e1,e2) ->
            let v1 = eval env e1 in
            let v2 = eval env e2 in
            v1 && v2
        | TApp (e1,e2) ->
            let v1 = eval env e1 in
            let v2 = eval env e2 in
            v1 v2
        | TLam e ->
            fun x -> eval (env,x) e 
        | TVar0 ->
            let (env,x)=env in
            x
        | TVarS e ->
            let (env,x)=env in
            eval env e 
        | TInt i -> i
        | TBol b -> b
    
    type exists_v =
    | ExistsV : 't -> exists_v
    
    let typecheck_eval e =
        let tet = typecheck CEmpty e in
        match tet with
        | Exists (te,t) -> ExistsV (eval () te)
    
    let v1 = typecheck_eval e1
    let v3 = typecheck_eval e3
    let v4 = typecheck_eval e4
    let v5 = typecheck_eval e5
    let v6 = typecheck_eval e6
    let v7 = typecheck_eval e7
    let v8 = typecheck_eval e8
    let v9 = typecheck_eval e9