Version française
Home     About     Download     Resources     Contact us    
Browse thread
Instruction selection in the OCaml compiler: Modules or classes?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Lukasz Stafiniak <lukstafi@g...>
Subject: [Caml-list] Instruction selection in the OCaml compiler: Modulesor classes?
[I'm sorry, I forgot to send to the list!]

On 2/24/07, David Baelde <david.baelde@gmail.com> wrote:
> On 2/24/07, skaller <skaller@users.sourceforge.net> wrote:
> > It seems like a module functor allows both anonymous
> > signatures (structural) and also anonymous argument
> > modules (structural), yet you cannot have
> > anonymous functor applications: you have to bind the application to
> > a module name. If we really had structural typing that name would
> > simply be an alias. Why can't we eliminate that name? ***
>
> One worthy remark here might be that side-effects can occur at functor
> instantiation. Then, forcing the user to think about when the
> instantiation occurs is useful...
>
Yet OCaml already has trouble with some nasty patterns of effects at
functor instantiation.

Taken from one of the slides at http://www.cs.cmu.edu/~rwh/courses/modules/

This is good (Skaller: does it solve your problem?):

module type DICT = functor (Key : Set.OrderedType) -> sig
 type 'a dict
 val domain : 'a dict -> Set.Make(Key).t
end;;

module type PRIO_QUEUE = functor (Elt : Set.OrderedType) -> sig
 type queue
 val contents : queue -> Set.Make(Elt).t
end;;

module QueuedDict (Data : Set.OrderedType)
 (MyDict : DICT) (MyPrioQueue : PRIO_QUEUE) =
struct
 module Dict = MyDict (Data)
 module PQ = MyPrioQueue (Data)
 module DataSet = Set.Make (Data)
 let queued_domain dict pq =
   DataSet.equal (Dict.domain dict) (PQ.contents pq)
end;;

This is bad:

let moonFull = let v = ref false in fun () -> v := not !v; !v
;;

module type S = sig
 type t
 val x : t
 val f : t -> t
 val g : t -> t -> bool
end;;

module Weird (Top : sig end) = (struct
 type t = int
 let x = if moonFull () then 1 else 2
 let f x = x + 2
 let g x y = (3*x + 2*y) / (x - y + 1) = 7
end : S);;

module Gen (X : functor (Top : sig end) -> S) = X (struct end);;

module W1 = Gen (Weird);; (* moon full now *)
module W2 = Gen (Weird);; (* moon no longer full now *)
W1.g W1.x W2.x;;