【问题标题】:Error Implement an interpreter in Ocaml在 Ocaml 中实现解释器时出错
【发布时间】:2017-02-05 20:56:57
【问题描述】:

我正在尝试在 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")
    ;;

错误就在 (***) 行:“variant type tuple has no constructor Pipe” 我该如何解决?

【问题讨论】:

    标签: ocaml interpreter


    【解决方案1】:

    编译器期望applyPipe 的第一个参数具有tuple 类型。在(***) 行上,applyPipe 应用于类型为exp 的值Pipe(tupla)

    【讨论】:

    • 所以我应该用 applyPipe tupla ... 更改它?
    • 这看起来更合理,是的。
    • 我明白我哪里做错了: Apply 的参数是 expexp ,而 argo 是一个 eval。但现在还没有结束,行 (*) 上出现了一个新错误:“这个表达式的类型为 eval,但预期的表达式为 exp 类型”......我快疯了 ahahah
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多