Display name
Display name

Reputation: 257

OCaml compiler throws syntax error during match statement despite proper type matching

The relevant function (not everything shown) is

let rec eval_expr env e = match e with
  | Value(value) -> value
  | ID(var) -> lookup(env, var)
  | Fun(var,expr) -> Closure(env, var, expr)
  | Not(expr) -> let val = eval_expr env expr in begin match val with
      | Bool(a) -> Bool(not a)
      | _ -> raise (TypeError ("Expected type bool")) end
  | Binop(op, expr, expr2) -> begin match op with ...

However, the compiler throws the following error:

       | Not(expr) -> let val = eval_expr env expr in begin match val with
                          ^^^
Error: Syntax error

Based on past experience with OCaml errors inside a match statement, if you're 100% confident that the given line doesn't have an error, the problem is most likely in an earlier line. However, I moved this line to the very start so that the code looks like

let rec eval_expr env e = match e with
  | Not(expr) -> let val = eval_expr env expr in begin match val with
      | Bool(a) -> Bool(not a)
      | _ -> raise (TypeError ("Expected type bool")) end
  | Value(value) -> value
  | ID(var) -> lookup(env, var)
  | Fun(var,expr) -> Closure(env, var, expr)
  | Binop(op, expr, expr2) -> begin match op with ...

and still got the same error. So the error is on this line, what could it be? Here are the types if it helps:

type expr =
  | Value of value
  | ID of var
  | Fun of var * expr
  | Not of expr
  | Binop of op * expr * expr
  | If of expr * expr * expr
  | FunctionCall of expr * expr
  | Let of var * bool * expr * expr

type value =
  | Int of int
  | Bool of bool
  | String of string
  | Closure of environment * var * expr

Upvotes: 0

Views: 78

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

The token val is a keyword in OCaml. You just need to change to a different name. I often use valu.

Upvotes: 2

Related Questions