List Info

Thread: "ocaml_beginners"::[] cross compiling two .mli files




"ocaml_beginners"::[] cross compiling two .mli files
user name
2006-11-30 19:49:29

Hello

have two mli files a.mli and b.mli, where b.mli is using the type "a"
defined in a.mli :
a.mli :
type a = { x : float } ;;

and b.mli :
type b = { an_a : a} ;;

how can I compile b.mli ? and where is this commented in the OCaml
documentation ?

Thanks
François Colonna

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] cross compiling two .mli files
user name
2006-11-30 21:32:49

On Thu, 30 Nov 2006, Francois Colonna wrote:

> Hello
>;
> have two mli files a.mli and b.mli, where b.mli is using the type "a"
> defined in a.mli :
> a.mli :
> type a = { x : float } ;;
>
> and b.mli :
> type b = { an_a : a} ;;
>
> how can I compile b.mli ?

You can't. At least not as writen.
The problem here is that you provide the compiler with no information
regarding where the type a came from, and so you will get an "Unbound type
constructor a" error when you try to compile it. You need to either open
the A module:

(** b.mli **)
open A;;

type b = { an_a : a} ;;

or fully specify the type name:
type b = { an_a : A.a } ;;

Once you do that, then you can do a:
ocamlc -c a.mli
ocamlc -c b.mli

to compile the interfaces (or just ocamlc -c a.mli b.mli -- note the
order is important).

Note that you will need to do this for b.ml as well. And for other
modules that make use of A and B. E.g.

(** c.ml **)
let a = { A.x = 1. };;
let b = { B.an_a = a };;

let _ = Printf.printf "a: %fn" b.B.an_a.A.x;;

> and where is this commented in the OCaml
>; documentation ?

I don't know where it's covered specifically, but chapters 2 and 8
of the manual probably go over it.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy."

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] cross compiling two .mli files
user name
2006-11-30 22:05:28

On Thu, 30 Nov 2006, Francois Colonna wrote:

> Hello
>;
> have two mli files a.mli and b.mli, where b.mli is using the type "a"
> defined in a.mli :
> a.mli :
> type a = { x : float } ;;
>
> and b.mli :
> type b = { an_a : a} ;;
>
> how can I compile b.mli ? and where is this commented in the OCaml
>; documentation ?

You can't do that.
There is actually some experimental support for recursive submodules (all
in the same file), but it doesn't work in every case, and I wouldn't go
there if I were you.

What you can simply do is isolate those types and put them in the same
file "ab.mli":

ab.mli:
type a = { x : float }
and b = { an_a : a}

a.mli:
type a = Ab.a
...

b.mli:
type b = Ab.b
...

Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] cross compiling two .mli files
user name
2006-11-30 22:29:45

On Thu, 30 Nov 2006, Martin Jambon wrote:

> On Thu, 30 Nov 2006, Francois Colonna wrote:
>
> > Hello
>; >
> > have two mli files a.mli and b.mli, where b.mli is using the type "a"
> > defined in a.mli :
> > a.mli :
> > type a = { x : float } ;;
> >
> > and b.mli :
> > type b = { an_a : a} ;;
> >
> > how can I compile b.mli ? and where is this commented in the OCaml
>; > documentation ?
>
> You can't do that.
>; There is actually some experimental support for recursive submodules (all
> in the same file), but it doesn't work in every case, and I wouldn't go
> there if I were you.
>
> What you can simply do is isolate those types and put them in the same
> file "ab.mli":
>
> ab.mli:
> type a = { x : float }
> and b = { an_a : a}
>
> a.mli:
> type a = Ab.a
> ...
>
> b.mli:
> type b = Ab.b
> ...

Oops. I thought the question was specifically about recursive types.

If it's a one way dependency, there is no problem at all.
Use either A.a or "open A" as William suggested.

Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )