First class Modules with parametric types (The type constructor F.f would escape its scope) -
First class Modules with parametric types (The type constructor F.f would escape its scope) -
i playing around modules, see in way can used in similar ways haskell type classes. trying play around functor type class:
module type functor = sig type 'a f val fmap : ('a -> 'b) -> ('a f -> 'b f) end module idfunc = struct type 'a f = id of 'a allow fmap f = fun (id a) -> id (f a) allow runid (id a) = end allow outmap (module f : functor) av = f.fmap f av however in case outmap not typed correctly, compiler produces error the type constructor f.f escape scope. know why error caused in case, not sure how work around (because type f parametrized).
i tried utilize locally abstract types:
let outmap (type s) (module f : functor type 'a f = s) f av = f.fmap f av or
let outmap (type a) (type b) (type fa) (type fb) (module f : functor type f = fa type b f = fb) f av = f.fmap f av or
let outmap (type s) (module f : functor type f = s) f av = f.fmap f av which give me various syntax errors or typing errors.
is there way work around this?
in haskell be:
outmap : functor f => (a -> b) -> f -> f b what equivalent in ocaml (if any)?
==== edit ====
i found 1 way similar work:
module type functor = sig type type b type af type bf val fmap : (a -> b) -> (af -> bf) end module type fromto = sig type type b end module idfunc = functor (ft : fromto) -> struct type = ft.a type b = ft.b type 'a f = id of 'a type af = f type bf = b f allow fmap f = fun (id a) -> id (f a) allow runid (id a) = end allow outmap (type a') (type b') (type af') (type bf') (module f : functor type = a' , type b = b' , type af = af' , type bf = bf') f av = f.fmap f av module m = idfunc(struct type = int type b = string end) allow oi = outmap (module m) allow test = oi (fun _ -> "test") (m.id 10) but looks lot of added complexity should much simpler.
i'm afraid cannot straight express trying because illustration of higher-kinded polymorphism (polymorphism on type constructors) not supported in ocaml's core language.
for explanation of why ocaml's core language cannot back upwards higher-kinded polymorphism see section 1.1 in lightweight higher-kinded polymorphism.
since module scheme does back upwards higher-kinded polymophism, usual solution problem create outmap functor rather function.
alternatively, paper linked above describes workaround (implemented in higher library -- available on opam) uses defunctionalisation @ type-level. whether more convenient using functors or not depends on specific utilize cases.
types module ocaml
Comments
Post a Comment