Type error in OCaml

20 views
Skip to first unread message

Francesco Balzano

unread,
Oct 30, 2015, 1:53:13 PM10/30/15
to ocaml-core

I have the following error from Ocaml and I don't understand why. I'm trying to define an interpreter in oCaml. I have some types and functions to evaluate these types. I paste the relevant code.

I have these types:



type ide = string
type field_name
= ide
type exp
= Eint of int
| Ebool of bool
| Var of ide
| Prod of exp * exp
| Sum of exp * exp
| Diff 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 list * exp
| Funval of exp * exp env
| Appl of exp * exp list
| Dot of ide * field_name
| Field of ide * exp
| Record of ide * exp list;;

type
'a env = Env of (ide * 'a) list;;



I have a function eval used to eval exp. It works correctly.

let rec eval ((e: exp), (r: exp env)) =
match e
with
| Eint(n) -> Eint(n)
| Ebool(b) -> Ebool(b)
| Var(i) -> lookup r i
| Iszero(a) -> iszero(eval(a, r))
| Eq(a, b) -> equ(eval(a, r),eval(b, r))
| Prod(a, b) -> mult(eval(a, r), eval(b, r))
| Sum(a, b) -> plus(eval(a, r), eval(b, r))
| Diff(a, b) -> diff(eval(a, r), eval(b, r))
| Minus(a) -> minus(eval(a, r))
| And(a, b) -> et(eval(a, r), eval(b, r))
| Or(a, b) -> vel(eval(a, r), eval(b, r))
| Not(a) -> non(eval(a, r))
| Ifthenelse(a, b, c) -> let g = eval(a, r) in
if typecheck("bool", g) then
(if g = Ebool(true) then eval(b, r) else eval(c, r))
else failwith ("nonboolean guard")
| Let(i, e1, e2) ->
eval(e2, bind (r, i, eval(e1, r)))
|  Fun(x, a) -> Funval(e, r)
| Appl(e1, e2) -> match eval(e1, r) with
| Funval(Fun(x, a), r1) ->
eval(a, bind_list r1 x e2)
| _ -> failwith("no funct in apply")Inserisci qui il codice...


And finally I have a function to evaluate fields of record:

let eval_field (field:exp) (r: exp env)= match field with
| Field (id, e) -> Field (id, (eval e r))
| _ -> failwith ("Not a Field");;Inserisci qui il codice...


The problem is with eval_field: oCaml signals me ths error:


Characters 22-24: let f1 = Field ("f1", e1);; ^^ Error: This expression has type exp/1542 but an expression was expected of type exp/2350



What could be wrong? Thank you very much for your help.

Richard Mortier

unread,
Oct 30, 2015, 2:31:46 PM10/30/15
to ocaml...@googlegroups.com
On 30 October 2015 at 17:53, Francesco Balzano
<francesco...@gmail.com> wrote:
> I have the following error from Ocaml and I don't understand why. I'm trying
...
> And finally I have a function to evaluate fields of record:
>
> let eval_field (field:exp) (r: exp env)= match field with
> | Field (id, e) -> Field (id, (eval e r))
> | _ -> failwith ("Not a Field");;Inserisci qui il codice...
>
>
> The problem is with eval_field: oCaml signals me ths error:
>
>
> Characters 22-24: let f1 = Field ("f1", e1);; ^^ Error: This expression has
> type exp/1542 but an expression was expected of type exp/2350
>
>
>
> What could be wrong? Thank you very much for your help.

Isn't the signature of `eval` expecting a pair `(e,r)` while you've
given it two arguments, `e` and `r`?

ie., call on second line of `eval_field` should be `eval(e,r)` ?



--
Richard Mortier
mo...@cantab.net

Evgeny Roubinchtein

unread,
Oct 30, 2015, 3:14:23 PM10/30/15
to ocaml...@googlegroups.com
The error message suggests to me that you are working at the
toplevel/utop interactive prompt, and evaluating your program
piecemeal. I am quite certain that the problem you are seeing is an
artifact of the way toplevel works: the internals of the
implementation are "shining through." I would guess that the error
will not occur if you put your code in a file and then use the "#use"
directive to bring the file all at once into the toplevel/utop. I
apologize if this is too vague; I imagine someone else on the list can
give you a better answer/explanation.
> --
> You received this message because you are subscribed to the Google Groups
> "ocaml-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ocaml-core+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages