OCaml类型错误 [英] OCaml error with types

查看:181
本文介绍了OCaml类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从OCaml下面的错误,我不明白为什么。我试图在OCaml中定义一个解释器。我有一些类型和功能来评估这些类型。我粘贴相关的代码。

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

我有一个用于评估exp的函数eval。它正常工作。

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

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

最后,我有一个函数来计算记录字段:

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

问题出在eval_field:OCaml指示我出错:

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.

推荐答案

编译器试图告诉你,你有两种不同的类型命名为 exp ,并且您希望其中的一个在另一个预期的位置。

The compiler is trying to tell you that you have two different types named exp and that you have one of them where the other is expected.

我只看到一个定义在此代码中键入 exp 。我怀疑你的环境不干净。您可以尝试将代码加载到新的OCaml解释器中。或者问题出在你没有显示的代码上。

I only see one definition for the type exp in this code. I suspect your environment isn't clean. You might try loading the code up in a new OCaml interpreter. Or perhaps the problem is with some code you're not showing.

这里是一个显示错误产生的会话:

Here's a session showing how the error is produced:

$ ocaml
        OCaml version 4.02.1

# type abc = A | B | C;;
type abc = A | B | C
# let f (x: abc) = x = A;;
val f : abc -> bool = <fun>
# type abc = A | B | C;;
type abc = A | B | C
# f (C: abc);;
Error: This expression has type abc/1024
       but an expression was expected of type abc/1018

我的猜测是你有一个函数(比如 f ),这个函数是用你的 exp 类型。但是你用这个类型的新定义的值来调用它(如这里所示)。

My guess is that you have a function (like f here) that was defined using an old definition of your exp type. But you're calling it with a value from the new definition of the type (as here).

这篇关于OCaml类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆