StrongerXi / soc

Compiler for a subset of OCaml
1 stars 0 forks source link

Automatically loaded Pervasives module creates cyclic dependencies #58

Open StrongerXi opened 3 years ago

StrongerXi commented 3 years ago

Say we want to create an infix operator @ in Pervasives module for List.append, so we can conveniently append lists in whatever source file (Pervasives is automatically loaded).

Now we have 2 choices:

let (@) = List.append

or

let rec (@) xs ys =
  match xs with
  | [] -> ys
  | x::xs -> x::(xs @ ys)
;;

Either one creates a cyclic dependency Pervasives -> List -> Pervasives.

The root of this problem is that, to be loaded automatically into every module, Pervasives must not depend on any other module, else there would be a cyclic dependency Pervasives -> X -> Pervasives.

But we do want some values in Pervasives for convenience. Can we do it with acceptable costs?

The way OCaml's compiler does it is by adding built-in types such as list and option. See ocaml/typing/predef.mli for details.

I think it's a decent solution, slightly annoying that we can't define everything in source code, but not too bad in my opinion.

StrongerXi commented 3 years ago

I will work on this when it becomes necessary, probably after I add in multi-file compilation.