Lorenzo Maffei
Lorenzo Maffei

Reputation: 3

Error Implement an interpreter in Ocaml

i'm trying to write an interpreter in Ocaml but I do not know resolve the error in this program:

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

The error is on the line (***) : "the variant type tuple has no constructor Pipe" How can I solve it?

Upvotes: 0

Views: 194

Answers (1)

Justin Raymond
Justin Raymond

Reputation: 3558

The compiler expects the first argument of applyPipe to have type tuple. On the line (***) the applyPipe is applied to the value Pipe(tupla) which has type exp.

Upvotes: 2

Related Questions