caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Partial module include
@ 2012-09-21 16:37 Pierre-Etienne Meunier
  2012-09-21 16:41 ` David House
  0 siblings, 1 reply; 6+ messages in thread
From: Pierre-Etienne Meunier @ 2012-09-21 16:37 UTC (permalink / raw)
  To: caml-list

Hi,

Is it possible in ocaml to redefine a part of an included module, like in the following code ?

module A=struct
	let a=0
	let b=1
end

module B=struct
	include A
	let b=2
end


Thanks
Pierre

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

* Re: [Caml-list] Partial module include
  2012-09-21 16:37 [Caml-list] Partial module include Pierre-Etienne Meunier
@ 2012-09-21 16:41 ` David House
  2012-09-21 16:43   ` Pierre-Etienne Meunier
  0 siblings, 1 reply; 6+ messages in thread
From: David House @ 2012-09-21 16:41 UTC (permalink / raw)
  To: Pierre-Etienne Meunier; +Cc: caml-list

Sure. That exact code will work just fine.

It's a special case of "shadowing", where bindings override any
previous ones, for the duration of their scope. E.g.:

let x = 5 in
let x = 4 in
(* here x is 4 *)

also:

let x = 5 in
let () =
  let x = 4 in
  printf "%d " x
in
printf "%d\n" x

Prints "4 5", because the inner binding only has effect during the
time that it is in scope, and afterwards the original binding is
"uncovered" again.

On Fri, Sep 21, 2012 at 5:37 PM, Pierre-Etienne Meunier
<pierreetienne.meunier@gmail.com> wrote:
> Hi,
>
> Is it possible in ocaml to redefine a part of an included module, like in the following code ?
>
> module A=struct
>         let a=0
>         let b=1
> end
>
> module B=struct
>         include A
>         let b=2
> end
>
>
> Thanks
> Pierre
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

* Re: [Caml-list] Partial module include
  2012-09-21 16:41 ` David House
@ 2012-09-21 16:43   ` Pierre-Etienne Meunier
  2012-09-21 16:47     ` David House
                       ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Pierre-Etienne Meunier @ 2012-09-21 16:43 UTC (permalink / raw)
  To: caml-list

No, sorry, what I wanted to override was a module:

module A=struct
       let a=0
       module B=struct let b=1 end
end

module B=struct
       include A
       module B=struct let b=2 end
end


Em 21/09/2012, às 18:41, David House escreveu:

> Sure. That exact code will work just fine.
> 
> It's a special case of "shadowing", where bindings override any
> previous ones, for the duration of their scope. E.g.:
> 
> let x = 5 in
> let x = 4 in
> (* here x is 4 *)
> 
> also:
> 
> let x = 5 in
> let () =
>  let x = 4 in
>  printf "%d " x
> in
> printf "%d\n" x
> 
> Prints "4 5", because the inner binding only has effect during the
> time that it is in scope, and afterwards the original binding is
> "uncovered" again.
> 
> On Fri, Sep 21, 2012 at 5:37 PM, Pierre-Etienne Meunier
> <pierreetienne.meunier@gmail.com> wrote:
>> Hi,
>> 
>> Is it possible in ocaml to redefine a part of an included module, like in the following code ?
>> 
>> module A=struct
>>        let a=0
>>        let b=1
>> end
>> 
>> module B=struct
>>        include A
>>        let b=2
>> end
>> 
>> 
>> Thanks
>> Pierre
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa-roc.inria.fr/wws/info/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>> 


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

* Re: [Caml-list] Partial module include
  2012-09-21 16:43   ` Pierre-Etienne Meunier
@ 2012-09-21 16:47     ` David House
  2012-09-21 16:55     ` Markus Mottl
  2012-09-21 17:47     ` Jeremy Yallop
  2 siblings, 0 replies; 6+ messages in thread
From: David House @ 2012-09-21 16:47 UTC (permalink / raw)
  To: Pierre-Etienne Meunier; +Cc: caml-list

Ah, no, that cannot be done. Shadowing of modules is mysteriously
disallowed :) (I suspect there is a good reason for this, but I
confess I don't know it.)

If you say what you want to do with a bit more context, there might be
a decent workaround.

On Fri, Sep 21, 2012 at 5:43 PM, Pierre-Etienne Meunier
<pierreetienne.meunier@gmail.com> wrote:
> No, sorry, what I wanted to override was a module:
>
> module A=struct
>        let a=0
>        module B=struct let b=1 end
> end
>
> module B=struct
>        include A
>        module B=struct let b=2 end
> end
>
>
> Em 21/09/2012, às 18:41, David House escreveu:
>
>> Sure. That exact code will work just fine.
>>
>> It's a special case of "shadowing", where bindings override any
>> previous ones, for the duration of their scope. E.g.:
>>
>> let x = 5 in
>> let x = 4 in
>> (* here x is 4 *)
>>
>> also:
>>
>> let x = 5 in
>> let () =
>>  let x = 4 in
>>  printf "%d " x
>> in
>> printf "%d\n" x
>>
>> Prints "4 5", because the inner binding only has effect during the
>> time that it is in scope, and afterwards the original binding is
>> "uncovered" again.
>>
>> On Fri, Sep 21, 2012 at 5:37 PM, Pierre-Etienne Meunier
>> <pierreetienne.meunier@gmail.com> wrote:
>>> Hi,
>>>
>>> Is it possible in ocaml to redefine a part of an included module, like in the following code ?
>>>
>>> module A=struct
>>>        let a=0
>>>        let b=1
>>> end
>>>
>>> module B=struct
>>>        include A
>>>        let b=2
>>> end
>>>
>>>
>>> Thanks
>>> Pierre
>>> --
>>> Caml-list mailing list.  Subscription management and archives:
>>> https://sympa-roc.inria.fr/wws/info/caml-list
>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>>
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

* Re: [Caml-list] Partial module include
  2012-09-21 16:43   ` Pierre-Etienne Meunier
  2012-09-21 16:47     ` David House
@ 2012-09-21 16:55     ` Markus Mottl
  2012-09-21 17:47     ` Jeremy Yallop
  2 siblings, 0 replies; 6+ messages in thread
From: Markus Mottl @ 2012-09-21 16:55 UTC (permalink / raw)
  To: Pierre-Etienne Meunier; +Cc: caml-list

On Fri, Sep 21, 2012 at 12:43 PM, Pierre-Etienne Meunier
<pierreetienne.meunier@gmail.com> wrote:
> No, sorry, what I wanted to override was a module:
>
> module A=struct
>        let a=0
>        module B=struct let b=1 end
> end
>
> module B=struct
>        include A
>        module B=struct let b=2 end
> end

You could try first class modules:

module type B = sig val b : int end

module A = struct
  let a = 0
  let b = (module struct let b = 1 end : B)
end

module B = struct
  include A
  let b = (module struct let b = 2 end : B)
end

-- 
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com

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

* Re: [Caml-list] Partial module include
  2012-09-21 16:43   ` Pierre-Etienne Meunier
  2012-09-21 16:47     ` David House
  2012-09-21 16:55     ` Markus Mottl
@ 2012-09-21 17:47     ` Jeremy Yallop
  2 siblings, 0 replies; 6+ messages in thread
From: Jeremy Yallop @ 2012-09-21 17:47 UTC (permalink / raw)
  To: Pierre-Etienne Meunier; +Cc: caml-list

On 21 September 2012 17:43, Pierre-Etienne Meunier
<pierreetienne.meunier@gmail.com> wrote:
> module A=struct
>        let a=0
>        module B=struct let b=1 end
> end
>
> module B=struct
>        include A
>        module B=struct let b=2 end
> end

You can achieve this using destructive substitution and 'module type
of' in recent versions of OCaml:

    module A =
    struct
      let a = 0
      module B = struct let b = 1 end
    end

    module B =
    struct
      include (A : module type of A with module B := A.B)
      module B = struct let b = 2 end
    end

The 'with module B := B' construct removes the 'B' component from the
type of the 'A' module, allowing you to introduce a replacement:

    # module type ASIG = module type of A;;
    module type ASIG = sig val a : int module B : sig val b : int end end
    # module type ASIG = module type of A with module B := A.B;;
    module type ASIG = sig val a : int end

Jeremy.

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

end of thread, other threads:[~2012-09-21 17:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-21 16:37 [Caml-list] Partial module include Pierre-Etienne Meunier
2012-09-21 16:41 ` David House
2012-09-21 16:43   ` Pierre-Etienne Meunier
2012-09-21 16:47     ` David House
2012-09-21 16:55     ` Markus Mottl
2012-09-21 17:47     ` Jeremy Yallop

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).