Anonymous variables in ocaml type pins

I am new to Ocaml and I got a weird error when I wrote an interpreter to calculate lambda.

let rec valof : exp -> env -> value =
    fun exp env ->
    match exp with
      Var n -> exp2value (lookup env n)
    | Lambda (name , body) -> Clos (name , body , env) (*some thing wrong here*)
    |   _ -> Err
;;

exp, valueand are envdefined as follows:

type exp =
    Num of int
  | Str of string
  | Err
  | Var of string
  | Lambda of string * exp
  | App of exp * exp
;;

type value =
    Num of int
  | Str of string
  | Clos of string * exp * env
  | Err
;;

type env =
    Empty
  | Cons of string * exp * env
;;

When compiling, the compiler complained about the lambda line of the interpreter:

Error: This expression has type env/1490
       but an expression was expected of type env/1457

Any ideas where I squinted? Thank!

+4
source share
1 answer
Error: This expression has type env/1490 
but an expression was expected of type env/1457

This error occurs quite often when you are debugging your code in interactive toplevel (REPL), and this means that you have declared the type twice env. This can happen if you copy the code in the REPL.


;; . OCaml . , ;; , .


, | , . , , | , .


, value exp env, exp, , . , . ( , , , , , ).

, ( (.. ) ( )), and, .

type env =
  | Empty
  | Cons of string * exp * env

and exp =
  | Num of int
  | Str of string
  | Err
  | Var of string
  | Lambda of string * exp
  | App of exp * exp

and value =
  | Num of int
  | Str of string
  | Clos of string * exp * env
  | Err

, value exp Num of int Str of string, OCaml , Int 1 value exp. , OCaml , Int env exp (, , , ). , .

+1

Source: https://habr.com/ru/post/1685044/


All Articles