ocaml

Why does ocaml keep miss understanding type?


module Value =
struct
  type t = Int of int
end

module M = Map.Make(String)

type expr = 
  | Num of int
  | Add of expr * expr

type t = Value.t M.t (* Value.t is Int of int *)

let rec add_map (st: string list) (e: expr list) (s: t): t =
  match st with
  | [] -> s
  | s1::st -> 
    match e with
    | e1::e -> 
      M.add s1 e1 s; 
      add_map st e s;;

In above function, e is list of user defined type expr, and s is user defined map "t = Int M.t" which store int in key of string. Problem is if I compile this, error says that type of e1 is t = t M.t, and I need expr M.t. Clearly e1 is element of expr list, why does ocaml think it is t?? I know M.add need (M.add string expr (map)


Solution

  • You didn't show the exact error message, but there is a problem with your call to M.add: the map s has type Value.t M.t, but you are giving it a value of type expr, not Value.t.