caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Unifying buildt-in types with polymorphic types
@ 2014-05-15 19:24 Peter Frey
  2014-05-15 20:28 ` Jeremy Yallop
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Frey @ 2014-05-15 19:24 UTC (permalink / raw)
  To: caml-list

(* In the code below I have 3 fragments of streams, based on a cons
cell; another on an array and, finally, the third is based on a string.
  I need all streams to have the same signature 'StreamOps'.
  This works fine for a stream where 'a t is a true polymorphic
 value but fails for module StmStr at the bottom of the code   sample;
giving:
       ...
       Values do not match:
         val head : int * 'a cursor -> char
       is not included in
         val head : 'a t -> 'a

I don't know if I run afoul of a form of narrowing; or if this is some
sort of limitation with respect to unboxed types. 

Any help would be greatly appreciated.

Peter Frey
*)


module type StreamOps = sig
  type 'a t 
    val head : 'a t -> 'a
end

module LazyStream : StreamOps = struct 
type 'a t = Cons of 'a * 'a t Lazy.t
  let head (Cons (h, _)) = h
end

module StmLzy = (LazyStream:StreamOps with type 'a t = 'a LazyStream.t)

module MakeEltAry = functor(S:sig include module type of Array end) ->
struct
type 'a cursor = { ofs: int; lim: int; data: 'a array }
 and 'a t = int * 'a cursor 

  let get s i = S.get s.data i 
  let head   (i, s)    = get s i
end

module EltAry = MakeEltAry(Array)
module StmAry = (EltAry:StreamOps with type 'a t = 'a EltAry.t)

module MakeEltStr = functor(S:sig include module type of String end) ->
struct
type 'a cursor = { ofs: int; lim: int; data: string }
 and 'a t = int * 'a cursor 
 
  let get s i = S.get s.data i 
  let head   (i, s)    = get s i
end

module EltStr = MakeEltStr(String)
module StmStr = (EltStr:StreamOps with type 'a t = 'a EltStr.t)






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

end of thread, other threads:[~2014-05-16 21:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-15 19:24 [Caml-list] Unifying buildt-in types with polymorphic types Peter Frey
2014-05-15 20:28 ` Jeremy Yallop
2014-05-16 21:41   ` Peter Frey

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