> >

My example contained a litte mistake:

> > Hi,

> >

> > I've created a small example.

> > Suppose you want to have points that are always positive.

>

> >

>

> Ah, I found time to study this example tonight after all. It did

> answer my question, at least given my current level of understanding.

>

> One key think I did not know about was "include", which is

> important.

>

> Thanks again Chris.

>

> - Phil -

>

You can't move the Points you created with CreateOnlyPositivePoint with

your module MoveOnlyPositivePoint.

You have to write:

include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

PositivePoint.t )

include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

PositivePoint.t )

at the corresponding places...

(This is because the compiler checks the types by name and

"CreateOnlyPositivePoint.t" is another one than

"MoveOnlyPositivePoint.t".)

Here is the complete code:

---------PositivePoint.ml---------------------

type t = int * int

let create ~(x:int) ~(y:int) =

let x_result = if x > 0 then x else 0 in

let y_result = if y > 0 then y else 0 in

x_result,y_result;;

let move ~(point:t) ~(x_offset:int) ~(y_offset:int) =

let x,y = point in

let x_regular = x + x_offset in

let y_regular = y + y_offset in

let x_result = if x_regular > 0 then x_regular else 0 in

let y_result = if y_regular > 0 then y_regular else 0 in

x_result,y_result;;

---------PositivePoint.mli--------------------

type t

val create: x:int -> y:int -> t

val move: point:t -> x_offset:int -> y_offset:int -> t

---------CreateOnlyPositivePoint.ml--------------------

module type CREATE_ONLY_POSITIVE_POINT =

sig

type t

val create: x:int -> y:int -> t

end

include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

PositivePoint.t )

---------MoveOnlyPositivePoint.ml--------------------

module type MOVE_ONLY_POSITIVE_POINT =

sig

type t

val move: point:t -> x_offset:int -> y_offset:int -> t

end

include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

PositivePoint.t )

---------Main.ml--------------------

let _ =

let punkt1 = PositivePoint.create ~x:5 ~y:7 in

let punkt2 = CreateOnlyPositivePoint.create ~x:7 ~y:1987 in

let punkt3 = MoveOnlyPositivePoint.move ~point:punkt2 ~x_offset:77

~y_offset:88 in

let punkt4 = MoveOnlyPositivePoint.move ~point:punkt1 ~x_offset:111

~y_offset:222 in

(*let punkt3 = MoveOnlyPositivePoint.create ~x:13 ~y:14 in*) (* will

fail *)

(*let punkt3 = CreateOnlyPositivePoint.move ~punkt2 ~x:1 ~y:1 in*) (*

will fail *)

punkt1,punkt2;;

Note that this is only one (and surely not the best) solution for the

problem!

As you can see ocaml is very flexible at this point...

Sorry ;-)- --- In ocaml_beginners@yahoogroups.com, chris_www <contact@...> wrote:
>

No worries. I have been studying some other OCaml code for a few days, so I am just now getting back to your example.

> > >

> > > Hi,

> > >

> > > I've created a small example.

> > > Suppose you want to have points that are always positive.

> >

> > >

> >

> > Ah, I found time to study this example tonight after all. It did

> > answer my question, at least given my current level of understanding.

> >

> > One key think I did not know about was "include", which is

> > important.

> >

> > Thanks again Chris.

> >

> > - Phil -

> >

>

> My example contained a litte mistake:

> You can't move the Points you created with CreateOnlyPositivePoint with

> your module MoveOnlyPositivePoint.

>

> You have to write:

>

> include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

> include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

>

> at the corresponding places...

>

> (This is because the compiler checks the types by name and

> "CreateOnlyPositivePoint.t" is another one than

> "MoveOnlyPositivePoint.t".)

>

> Here is the complete code:

> ---------PositivePoint.ml---------------------

> type t = int * int

> let create ~(x:int) ~(y:int) =

> let x_result = if x > 0 then x else 0 in

> let y_result = if y > 0 then y else 0 in

> x_result,y_result;;

> let move ~(point:t) ~(x_offset:int) ~(y_offset:int) =

> let x,y = point in

> let x_regular = x + x_offset in

> let y_regular = y + y_offset in

> let x_result = if x_regular > 0 then x_regular else 0 in

> let y_result = if y_regular > 0 then y_regular else 0 in

> x_result,y_result;;

>

> ---------PositivePoint.mli--------------------

> type t

> val create: x:int -> y:int -> t

> val move: point:t -> x_offset:int -> y_offset:int -> t

>

> ---------CreateOnlyPositivePoint.ml--------------------

> module type CREATE_ONLY_POSITIVE_POINT =

> sig

> type t

> val create: x:int -> y:int -> t

> end

> include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

>

> ---------MoveOnlyPositivePoint.ml--------------------

> module type MOVE_ONLY_POSITIVE_POINT =

> sig

> type t

> val move: point:t -> x_offset:int -> y_offset:int -> t

> end

> include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

> ---------Main.ml--------------------

> let _ =

> let punkt1 = PositivePoint.create ~x:5 ~y:7 in

> let punkt2 = CreateOnlyPositivePoint.create ~x:7 ~y:1987 in

> let punkt3 = MoveOnlyPositivePoint.move ~point:punkt2 ~x_offset:77

> ~y_offset:88 in

> let punkt4 = MoveOnlyPositivePoint.move ~point:punkt1 ~x_offset:111

> ~y_offset:222 in

> (*let punkt3 = MoveOnlyPositivePoint.create ~x:13 ~y:14 in*) (* will

> fail *)

> (*let punkt3 = CreateOnlyPositivePoint.move ~punkt2 ~x:1 ~y:1 in*) (*

> will fail *)

> punkt1,punkt2;;

>

>

> Note that this is only one (and surely not the best) solution for the

> problem!

> As you can see ocaml is very flexible at this point...

>

> Sorry ;-)

>

Thanks again.

- P - - --- In ocaml_beginners@yahoogroups.com, chris_www <contact@...> wrote:
>

Greetings. I have had time to study your example some more. It raised a question in my mind.

> > >

> > > Hi,

> > >

> > > I've created a small example.

> > > Suppose you want to have points that are always positive.

> >

> > >

> >

> > Ah, I found time to study this example tonight after all. It did

> > answer my question, at least given my current level of understanding.

> >

> > One key think I did not know about was "include", which is

> > important.

> >

> > Thanks again Chris.

> >

> > - Phil -

> >

>

> My example contained a litte mistake:

> You can't move the Points you created with CreateOnlyPositivePoint with

> your module MoveOnlyPositivePoint.

>

> You have to write:

>

> include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

> include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

>

> at the corresponding places...

>

> (This is because the compiler checks the types by name and

> "CreateOnlyPositivePoint.t" is another one than

> "MoveOnlyPositivePoint.t".)

>

> Here is the complete code:

> ---------PositivePoint.ml---------------------

> type t = int * int

> let create ~(x:int) ~(y:int) =

> let x_result = if x > 0 then x else 0 in

> let y_result = if y > 0 then y else 0 in

> x_result,y_result;;

> let move ~(point:t) ~(x_offset:int) ~(y_offset:int) =

> let x,y = point in

> let x_regular = x + x_offset in

> let y_regular = y + y_offset in

> let x_result = if x_regular > 0 then x_regular else 0 in

> let y_result = if y_regular > 0 then y_regular else 0 in

> x_result,y_result;;

>

> ---------PositivePoint.mli--------------------

> type t

> val create: x:int -> y:int -> t

> val move: point:t -> x_offset:int -> y_offset:int -> t

>

> ---------CreateOnlyPositivePoint.ml--------------------

> module type CREATE_ONLY_POSITIVE_POINT =

> sig

> type t

> val create: x:int -> y:int -> t

> end

> include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

>

> ---------MoveOnlyPositivePoint.ml--------------------

> module type MOVE_ONLY_POSITIVE_POINT =

> sig

> type t

> val move: point:t -> x_offset:int -> y_offset:int -> t

> end

> include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

> PositivePoint.t )

> ---------Main.ml--------------------

> let _ =

> let punkt1 = PositivePoint.create ~x:5 ~y:7 in

> let punkt2 = CreateOnlyPositivePoint.create ~x:7 ~y:1987 in

> let punkt3 = MoveOnlyPositivePoint.move ~point:punkt2 ~x_offset:77

> ~y_offset:88 in

> let punkt4 = MoveOnlyPositivePoint.move ~point:punkt1 ~x_offset:111

> ~y_offset:222 in

> (*let punkt3 = MoveOnlyPositivePoint.create ~x:13 ~y:14 in*) (* will

> fail *)

> (*let punkt3 = CreateOnlyPositivePoint.move ~punkt2 ~x:1 ~y:1 in*) (*

> will fail *)

> punkt1,punkt2;;

>

>

> Note that this is only one (and surely not the best) solution for the

> problem!

> As you can see ocaml is very flexible at this point...

>

> Sorry ;-)

>

In your example, all of the module types are defined and then applied to an existing structure *within* a module definition. Is there some way to define a module type by itself in an external file and apply it in yet other files? In the case of your example, could the module type CREATE_ONLY_POSITIVE_POINT be defined in a file on its own, and then applied to some structure (such as PositivePoint) in a third file? I have tried to get this to work but could not.

I hope that is not too fuzzy a question. My original post was about how structures and signatures are arranged in files, so this is on point, I think.

TIA

- Phil - - On Wed, 20 Apr 2011 00:42:40 -0000, "philippe_sismondi"

<philippe_sismondi@...> wrote:> --- In ocaml_beginners@yahoogroups.com [1], chris_www wrote:

Yes this is possible.

> >

> > > >

> > > > Hi,

> > > >

> > > > I've created a small example.

> > > > Suppose you want to have points that are always positive.

> > >

> > > >

> > >

> > > Ah, I found time to study this example tonight after all. It did

> > > answer my question, at least given my current level of

> understanding.

> > >

> > > One key think I did not know about was "include", which is

> > > important.

> > >

> > > Thanks again Chris.

> > >

> > > - Phil -

> > >

> >

> > My example contained a litte mistake:

> > You can't move the Points you created with CreateOnlyPositivePoint

> with

> > your module MoveOnlyPositivePoint.

> >

> > You have to write:

> >

> > include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

> > PositivePoint.t )

> > include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

>

> > PositivePoint.t )

> >

> > at the corresponding places...

> >

> > (This is because the compiler checks the types by name and

> > "CreateOnlyPositivePoint.t" is another one than

> > "MoveOnlyPositivePoint.t".)

> >

> > Here is the complete code:

> > ---------PositivePoint.ml---------------------

> > type t = int * int

> > let create ~(x:int) ~(y:int) =

> > let x_result = if x > 0 then x else 0 in

> > let y_result = if y > 0 then y else 0 in

> > x_result,y_result;;

> > let move ~(point:t) ~(x_offset:int) ~(y_offset:int) =

> > let x,y = point in

> > let x_regular = x + x_offset in

> > let y_regular = y + y_offset in

> > let x_result = if x_regular > 0 then x_regular else 0 in

> > let y_result = if y_regular > 0 then y_regular else 0 in

> > x_result,y_result;;

> >

> > ---------PositivePoint.mli--------------------

> > type t

> > val create: x:int -> y:int -> t

> > val move: point:t -> x_offset:int -> y_offset:int -> t

> >

> > ---------CreateOnlyPositivePoint.ml--------------------

> > module type CREATE_ONLY_POSITIVE_POINT =

> > sig

> > type t

> > val create: x:int -> y:int -> t

> > end

> > include ( PositivePoint : CREATE_ONLY_POSITIVE_POINT with type t =

>

> > PositivePoint.t )

> >

> > ---------MoveOnlyPositivePoint.ml--------------------

> > module type MOVE_ONLY_POSITIVE_POINT =

> > sig

> > type t

> > val move: point:t -> x_offset:int -> y_offset:int -> t

> > end

> > include ( PositivePoint : MOVE_ONLY_POSITIVE_POINT with type t =

> > PositivePoint.t )

> > ---------Main.ml--------------------

> > let _ =

> > let punkt1 = PositivePoint.create ~x:5 ~y:7 in

> > let punkt2 = CreateOnlyPositivePoint.create ~x:7 ~y:1987 in

> > let punkt3 = MoveOnlyPositivePoint.move ~point:punkt2 ~x_offset:77

>

> > ~y_offset:88 in

> > let punkt4 = MoveOnlyPositivePoint.move ~point:punkt1

> ~x_offset:111

> > ~y_offset:222 in

> > (*let punkt3 = MoveOnlyPositivePoint.create ~x:13 ~y:14 in*) (*

> will

> > fail *)

> > (*let punkt3 = CreateOnlyPositivePoint.move ~punkt2 ~x:1 ~y:1 in*)

> (*

> > will fail *)

> > punkt1,punkt2;;

> >

> >

> > Note that this is only one (and surely not the best) solution for

> the

> > problem!

> > As you can see ocaml is very flexible at this point...

> >

> > Sorry ;-)

> >

> Greetings. I have had time to study your example some more. It

> raised a question in my mind.

>

> In your example, all of the module types are defined and then

> applied to an existing structure *within* a module definition. Is

> there some way to define a module type by itself in an external file

> and apply it in yet other files? In the case of your example, could

> the module type CREATE_ONLY_POSITIVE_POINT be defined in a file on

> its

> own, and then applied to some structure (such as PositivePoint) in a

> third file? I have tried to get this to work but could not.

>

> I hope that is not too fuzzy a question. My original post was about

> how structures and signatures are arranged in files, so this is on

> point, I think.

>

> TIA

>

> - Phil -

Assume you have a file CreateOnlyPositivePoint.mli as follows:

|---------CreateOnlyPositivePoint.mli-------------------

|module type CREATE_ONLY_POSITIVE_POINT =

|sig

| type t

| val create: x:int -> y:int -> t

|end

and

|---------PositivePoint.ml---------------------

|type t = int * int

|let create ~(x:int) ~(y:int) =

| let x_result = if x > 0 then x else 0 in

| let y_result = if y > 0 then y else 0 in

| x_result,y_result;;

|let move ~(point:t) ~(x_offset:int) ~(y_offset:int) =

| let x,y = point in

| let x_regular = x + x_offset in

| let y_regular = y + y_offset in

| let x_result = if x_regular > 0 then x_regular else 0 in

| let y_result = if y_regular > 0 then y_regular else 0 in

| x_result,y_result;;

Then you can use this signature to hide some informations from

PositivePoint from within another module (lets say Client):

|----------Client.ml-------------------------------------

|module MyPoint = ( PositivePoint :

CreateOnlyPositivePoint.CREATE_ONLY_POSITIVE_POINT );;

or

|----------Client2.ml-------------------------------------

|open CreateOnlyPositivePoint;;

|module MyPoint = ( PositivePoint : CREATE_ONLY_POSITIVE_POINT );;

Hope that works...