Interpreter with Ocaml -


i write interpreter ocaml when try :

sem(let("somma",fun("x",sum(den "x",eint(5))),let("pipa",pipe(seq(den "somma",nil)),apply(den "pipa",eint(42)))),(emptyenv unbound));; 

the resault error : "exception: match_failure ("",1,41)

i think error in applypipe don't understand , why there did wrong?

this code :

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*) , 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 , efun = ide * exp * eval env , etuple =     | nil     | seq of eval * etuple ;;      let rec sem ((ex: exp), (r: eval env)) = match ex          .          .          .          | 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             | 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                  | 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")      , applypipe tup argo r = (match tup          | seq(den f, tupla) ->              let appf = apply(den f,argo) in                 applypipe tupla appf r         | nil -> sem(argo,r)         | _ -> failwith("not valid pipe"))     ;; 

the complete code there : http://pastebin.com/vgpanx51 please me thaks

when compile (or evaluate in toplevel) ocaml program, typechecker emit warnings pattern matches irrefutable, i.e., such patterns may raise match_failure exception.

what should do, go through warnings , fix them.

there quite few irrefutable matches in code, e.g., sem function final match apply(_,_) -> failwith("not function") catch apply terms, not catch others, adding _ -> failwith "unimplemented" fix it.

qa

the error in try-code or in interpreter?

it in interpreter, didn't include possible cases in pattern-matching code.

.i extend typechecker

you don't need to. typechecker verifies whether anticipated possible cases, example, let's take simple example:

type abc = | b | c  let string_of_abc abc = match abc   | -> "a"   | b -> "b" 

when try compile (or interpret) above code typechecker tell you:

warning 8: pattern-matching not exhaustive. here example of value not matched: c type abc = | b | c 

so, gives hint, forgot match c constructor, expression string_of_abc c terminate match_failure exception.

you can follow hints , add cases one-by-one. given example, pattern matching in sema function incomplete, , type checker hits following:

warning 8: pattern-matching not exhaustive. here example of value not matched: (pipe _|manytimes (_, _)) 

and indeed, missed case pipe, when interpreter sees

pipe(...) 

it can't find match, according code you're expecting pipe constructor first argument apply, when in example, you're passing second argument let.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -