caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Module inside an object
@ 2014-04-23  9:50 Pierre-Alexandre Voye
  2014-04-24 14:44 ` Goswin von Brederlow
  0 siblings, 1 reply; 2+ messages in thread
From: Pierre-Alexandre Voye @ 2014-04-23  9:50 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1987 bytes --]

Hello,
I would like to define a parameterized module inside an object by the type
of this object.


I have two module interlocked :

(* Parameters*)
module type A = sig
        type agent
        type intern_agent = { i : agent}
        val create : agent -> intern_agent
end

module type E = sig
        type event
end



module  type StateType = sig

  type agent
  type event

  type  state_t = {
    mutable name : string;
    mutable parentstate  :  state_t option;
  }
end


module State (A : A) (E : E) = struct

        type agent = A.agent
        type event = E.event
    type  state_t = {
            mutable name : string;
             mutable parentstate  :  state_t option;
    }


    (*...*)
end



module Agent (S : StateType) =
        struct
          type agent = S.agent
          type event = S.event
          type state_t = S.state_t

      type  agent_t = {
          mutable agent      : agent ;
      }

      let create a1   = {
          agent          = a1;
      }
end


(* An implementation of E*)
type event1 = Event1 | Event2;;
module E = struct type event = event1 end;;




What I would like to do is something like that (which is syntacticly
incorrect but represents what I would like to do) :


class  character = object (self :'self)
 val mutable position        = (0,0)
 val agent                         =
                let A = (module Ag  = struct
                                type agent = 'self
                                type intern_agent = { i : agent}
                                let create a = { i = a }
                              end)
                     in
                let Ag = (module Agent(State(A)(E)) ) in
                Ag.create self

 method getPosition          = position

end;;



How can I write this to be able to define a value which is an Agent
parametrized by itself ?

Thank you,

-- 
Pierre-Alexandre Voye
---------------------
https://twitter.com/#!/ontologiae/
http://linuxfr.org/users/montaigne

[-- Attachment #2: Type: text/html, Size: 2890 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Module inside an object
  2014-04-23  9:50 [Caml-list] Module inside an object Pierre-Alexandre Voye
@ 2014-04-24 14:44 ` Goswin von Brederlow
  0 siblings, 0 replies; 2+ messages in thread
From: Goswin von Brederlow @ 2014-04-24 14:44 UTC (permalink / raw)
  To: caml-list

On Wed, Apr 23, 2014 at 11:50:00AM +0200, Pierre-Alexandre Voye wrote:
> Hello,
> I would like to define a parameterized module inside an object by the type
> of this object.
> 
> 
> I have two module interlocked :
> 
> (* Parameters*)
> module type A = sig
>         type agent
>         type intern_agent = { i : agent}
>         val create : agent -> intern_agent
> end
> 
> module type E = sig
>         type event
> end
> 
> 
> 
> module  type StateType = sig
> 
>   type agent
>   type event
> 
>   type  state_t = {
>     mutable name : string;
>     mutable parentstate  :  state_t option;
>   }
> end
> 
> 
> module State (A : A) (E : E) = struct
> 
>         type agent = A.agent
>         type event = E.event
>     type  state_t = {
>             mutable name : string;
>              mutable parentstate  :  state_t option;
>     }
> 
> 
>     (*...*)
> end
> 
> 
> 
> module Agent (S : StateType) =
>         struct
>           type agent = S.agent
>           type event = S.event
>           type state_t = S.state_t
> 
>       type  agent_t = {
>           mutable agent      : agent ;
>       }
> 
>       let create a1   = {
>           agent          = a1;
>       }
> end
> 
> 
> (* An implementation of E*)
> type event1 = Event1 | Event2;;
> module E = struct type event = event1 end;;
> 
> 
> 
> 
> What I would like to do is something like that (which is syntacticly
> incorrect but represents what I would like to do) :
> 
> 
> class  character = object (self :'self)
>  val mutable position        = (0,0)
>  val agent                         =
>                 let A = (module Ag  = struct
>                                 type agent = 'self
>                                 type intern_agent = { i : agent}
>                                 let create a = { i = a }
>                               end)
>                      in
>                 let Ag = (module Agent(State(A)(E)) ) in

1) I think this creates a new module for every character. I don't
think that is what you want since that would make all characters
incompatible (and you would have to make the character polymorphic or
wrap it in a functor or something). Maybe try:

class  character = 
  let A = ...
  let Ag = ...
  in
  object (self :'self)
  ...

>                 Ag.create self
> 
>  method getPosition          = position
> 
> end;;
> 
> How can I write this to be able to define a value which is an Agent
> parametrized by itself ?
> 
> Thank you,

2) I think you can't use "Ag.create self" because that requires
passing the self as argument to a function before it has finished
being created.

You can make agent a mutable 'a option, create it as None and then use
an initializer to change it to "Ag.create self". You can avoid the
option with a dummy agent (Obj.magic 0 worst case).

3) This looks like a recursive type between a module and an object.
That doesn't work. You can only have recursive modules. So you need to
wrap the object into a seperate module.

Note: recursive modules require a full signature for each module so
you have to write a signature for your object.

MfG
	Goswin


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-04-24 14:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-23  9:50 [Caml-list] Module inside an object Pierre-Alexandre Voye
2014-04-24 14:44 ` Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).