Ocaml unbound type constructor with module

3.5k views Asked by At

I'm working on this and i'm pretty sure while i was writing it the error din't show up. Now it does and i don't know why and how to solve it, i've searched for long but nothing. it gives Unbound type constructor env on the last line thanks everyone for feedback

module type ENV =
sig
type 't env
val emptyenv : 't -> 't env
val bind : 't env * string * 't -> 't env
val bindlist : 't env * (string list) * ('t list)-> 't env
val applyenv : 't env * string -> 't
exception WrongBindlist
end

module Funenv:ENV =
struct
type 't env = string -> 't
exception WrongBindlist
let emptyenv(x) = function (y: string) -> x
(* x: valore default *)
let applyenv(x,y) = x y
let bind(r, l, e) =
function lu -> if lu = l then e else applyenv(r,lu)
let rec bindlist(r, il, el) = match (il,el) with
| ([],[]) -> r
| i::il1, e::el1 -> bindlist (bind(r, i, e), il1, el1)
| _ -> raise WrongBindlist
end

module Listenv:ENV =
struct
type 't env = (string * 't) list
exception WrongBindlist
let emptyenv(x) = [("", x)]
let rec applyenv(x,y) = match x with
| [(_, e)] -> e
| (i1, e1) :: x1 -> if y = i1 then e1
else applyenv(x1, y)
| [] -> failwith("wrong env")
let bind(r, l, e) = (l, e) :: r
let rec bindlist(r, il, el) = match (il, el) with
| ([],[]) -> r
| i::il1, e::el1 -> bindlist (bind(r, i, e), il1, el1)
| _ -> raise WrongBindlist
end

type ide = string


type exp = Eint of int
| Ebool of bool

type eval = Int of int
| Bool of bool
| Unbound
| Funval of efun
and efun = exp * eval env
1

There are 1 answers

2
Théo Winterhalter On BEST ANSWER

Who do you expect env to be on this last line? env is not some type constructor that you have in scope, but a type constructor that belongs to an ENV module.

You could make the definition of exp and efun in one of theses modules or use Listenv.env for instance.

With the following, it compiles:

type eval = Int of int
| Bool of bool
| Unbound
| Funval of efun
and efun = exp * eval Listenv.env

Another solution, and that might have been what you were doing, is to put this definition directly in the signature:

module type ENV =
  sig
    type 't env
    val emptyenv : 't -> 't env
    val bind : 't env * string * 't -> 't env
    val bindlist : 't env * string list * 't list -> 't env
    val applyenv : 't env * string -> 't

    type ide  = string
    type exp  = Eint of int | Ebool of bool
    type eval = Int of int | Bool of bool | Unbound | Funval of efun
    and  efun = exp * eval env

    exception WrongBindlist
  end

But then, you have to provide them again in Listenv and Funenv.