Ocaml翻译

时间:2017-02-06 18:12:31

标签: ocaml interpreter

我用Ocaml写一个翻译但是当我尝试:

sem(Let("Somma",Fun("x",Sum(Den "x",Eint(5))),Let("pipa",Pipe(Seq(Den "Somma",Nil)),Apply(Den "pipa",Eint(42)))),(emptyenv Unbound));;

resault是一个错误:“Exception:Match_failure(”“,1,41)

我认为错误在applyPipe中,但我不明白其中的位置和原因 我哪里错了?

这是我的代码:

type 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*)
;;

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
;;

    let rec sem ((ex: exp), (r: eval env)) = match ex with
         .
         .
         .
         | Let(i, e1, e2) -> sem(e2, bind (r, i, sem(e1, r)))
         | 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(etupl) = sem(Etup(tupla), r) in
                ValTup(Seq(evex1, etupl))
            | 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")
        | _ -> failwith("non implementato")

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

完整的代码在那里:http://pastebin.com/VgpanX51 请帮我解决

1 个答案:

答案 0 :(得分:1)

当您编译(或在顶层评估)OCaml程序时,类型检查器将发出有关无法反驳的所有模式匹配的警告,即可能引发Match_failure异常的此类模式。

你应该做的是通过所有警告并修复它们。

您的代码中存在相当多的无可辩驳的匹配项,例如,sem函数最终匹配Apply(_,_) -> failwith("not function")只会捕获Apply个术语,但不会捕获所有其他术语,添加类似的内容_ -> failwith "unimplemented"会修复它。

QA

  

错误在try-code或我的解释器中?

在解释器中,您没有在模式匹配代码中包含所有可能的情况。

  

。我确实扩展了typechecker

你不需要。类型检查器会验证您是否预见到所有可能的情况,例如,让我们举一个简单的例子:

type abc = A | B | C

let string_of_abc abc = match abc with
  | A -> "A"
  | B -> "B"

当您尝试编译(或解释)上述代码时,类型检查器会告诉您:

Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
C
type abc = A | B | C

因此,它会提示您忘记与C构造函数匹配,因此表达式string_of_abc C将以Match_failure异常终止。

您可以按照提示并逐个添加案例。根据您的示例,sema函数中的模式匹配不完整,类型检查器会向您发送以下内容:

Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(Pipe _|ManyTimes (_, _))

事实上,你错过了Pipe的案例,所以当口译员看到

Pipe(...)

它无法找到匹配项,因为根据您的代码,您只希望将Pipe构造函数作为Apply的第一个参数,在您的示例中,您&# 39;实际上将它作为第二个参数传递给Let