错误在Ocaml中实现解释器

时间:2017-02-05 20:56:58

标签: ocaml interpreter

我正在尝试在Ocaml中编写一个解释器,但我不知道解决了这个程序中的错误:

SYNTAX

type ide = string 
type exp = 
    | Eint of int
    | Ebool of bool
    | Den of ide
    | Sum of exp * exp
    | Diff of exp * exp
    | Prod of exp * exp
    | Eq of exp * exp
    | Minus of exp
    | Iszero of exp
    | Or of exp * exp
    | And of exp * exp
    | Not of exp
    | Ifthenelse of exp * exp * exp
    | Let of ide * exp * exp
    | Fun of ide * exp
    | Apply of exp * exp  
    | Letrec of ide * ide * exp * exp
    | Etup of tuple (*Tupla come espressione*)
    | Pipe of tuple (*Concatenazione di funzioni*)
    | ManyTimes of int * exp (*Esecuzione iterata di una funzione*)
and tuple = 
    | Nil (*Tupla vuota*)
    | Seq of exp * tuple (*Tupla di espressioni*)
;;

SEMANTIC

type eval= 
    | Int of int 
    | Bool of bool 
    | Unbound 
    | RecFunVal of ide * ide * exp * eval env
    | Funval of efun
    | ValTup of etuple
and efun = ide * exp * eval env
and etuple =
    | Nil
    | Seq of eval * etuple
;;

RUN-TIME SUPPORT

        | Fun(i,a) -> Funval(i,a,r)
        | Letrec(f, i, fBody, letBody) ->
            let benv = bind(r, f, (RecFunVal(f, i, fBody, r)))
            in sem(letBody, benv)   
        | Etup(tup) -> (match tup with
            | Seq(ex1, tupla) ->
                let evex1 = sem(ex1, r) in
                let ValTup(etupla) = sem(Etup(tupla), r) in
                    ValTup(Seq(evex1, etupla))
            | Nil -> ValTup(Nil))
        | Apply(Den f, arg1) ->
            (let fclosure= sem(Den f, r) in
               match fclosure with
                 | Funval(arg, fbody, fDecEnv) ->
                     sem(fbody, bind(fDecEnv, arg, sem(arg1, r)))
                 | RecFunVal(f, arg, fbody, fDecEnv) ->
                     let aVal= sem(arg1, r) in
                     let rEnv= bind(fDecEnv, f, fclosure) in
                     let aEnv= bind(rEnv, arg, aVal) in
                       sem(fbody, aEnv)
                 | _ -> failwith("non functional value"))
        | Apply(Pipe tup, arg) -> applyPipe tup arg r
        | Apply(_,_) -> failwith("not function")

    and applyPipe tup argo r = match tup with 
        | Seq(Den f, tupla) -> 
                applyPipe tupla (Apply(Den f,argo)) r
        | Seq(Pipe(tuplaP),tupla) -> 
            let appf = applyPipe tuplaP argo r in 
                applyPipe tupla appf r                   (**)
        | Nil -> sem(argo,r)
        | _ -> failwith("Not a valid Pipe")
    ;;

错误在行(***):“变体类型元组没有构造函数管道” 我该如何解决?

1 个答案:

答案 0 :(得分:2)

编译器期望applyPipe的第一个参数具有类型tuple(***)applyPipe已应用于Pipe(tupla)类型的值exp