On 2011/09/09, at 20:14, Dmitry Grebeniuk wrote: >> Namely recursive types in ocaml must be regular. >> I.e., they must expand to a finite graph. >> In particular this means that, inside the class lst, all occurences >> of lst must have exactly the parameter 'a. > > Just interesting: where this graph and its finiteness property > are used?
Well, there are many algorithms that must do exhaustive traversal of types to verify some property, and it implies that the type must be finite. Also, while unification on regular types is easy, I don't know of any algorithms for non-regular types. >> As you have already found, you can avoid this problem by >> defining a record which "hides" the use of lst. >> Namely, you must break the cycle with a datatype, >> either record or sum type. >> Unfortunately this also means that this type has to belong >> to some module, and you also lose subtyping. > > I've tried to make the type (lst 'b) belong to some first-class > module returned by some method call (to object with type > lst 'a), but without any success. > And while playing with first-class modules I've noted that > I can't constrain the parametric type of module: > let module M = (lst#mapmodule : Mappable with type t 'a = list a) > -- one more way is blocked (but don't know was it the way > really). This is indeed a restriction of locally abstract types that one cannot use them as row variables. This might be done if there are applications. > But I can reformulate (and maybe make easier) my task: > I can move the code that work with different type parameters > (lst 'a -> lst 'b) to simple functions. For example, "map" for > containers -- to the function like "container_map", which > will work with containers (objects that have some methods), > to make the code work: > container_map func (new lst [1;2;3]); > container_map func (new arr [|1;2;3|]). > I understand that the imaginable "container_map" function > should require some method with some type, that can > give me the value of type lst 'b for lst 'a in case of lists, > and the value of type arr 'b for arr 'a in case of arrays, > but I can't write such type (even with first-class modules). > Is it impossible too? The main problem is that you cannot define a constructor method which return an object with a different parameter. On the other hand, fold can be defined as a polymorphic method, allowing you to define functions building various kinds of containers, starting from an arbitrary one. Jacques Garrigue -- 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
