From: Josh Berdine <josh@berdine.net>
To: SEROT Jocelyn <Jocelyn.SEROT@univ-bpclermont.fr>,
Leo White <lpw25@cam.ac.uk>
Cc: "caml-list" <caml-list@inria.fr>
Subject: Re: [Caml-list] Question on functors (again...)
Date: Tue, 21 Jan 2014 11:30:59 +0000 [thread overview]
Message-ID: <87txcxlggc.fsf@berdine.net> (raw)
In-Reply-To: <20140121120954.Horde.QQKslk0F5ldpqozcxKyq3g1@wmail.univ-bpclermont.fr>
On Tue, Jan 21 2014, SEROT Jocelyn wrote:
> Leo White <lpw25@cam.ac.uk> a écrit :
>>
>> You should add the constraints to the `SET` module type itself:
>>
>> module type SET = sig
>> type t
>> type elt
>> module Elt : sig type t = elt end
>> val choose : t -> elt
>> val singleton : elt -> t
>> end
>
> Unfortunately, this is not possible since the ELT signature is
> actually more complex than just 'sig type t end'.
> To illustrate this, without showing the original code (which, as you
> guessed it, has some extra and maybe unrelated complexity), i've tried
> to reuse your example, by simply changing
>
> module type ELT = sig type t end
>
> by
>
> module type ELT = sig type t val string_of: t -> string end
Shouldn't Elt.t and elt in SET be equal? The following typechecks,
though maybe not what you want:
> module type ELT =
> sig
> type t
> val string_of: t -> string
> end
>
> module type SET = sig
> type t
> type elt
> module Elt : ELT
> val choose: t -> elt
> val singleton: elt -> t
> val string_of: t -> string
> end
module type SET = sig
type t
module Elt : ELT
type elt = Elt.t
val choose: t -> elt
val singleton: elt -> t
val string_of: t -> string
end
> module Make (E : ELT) : SET with module Elt = E and type elt = E.t = struct
> type elt = E.t
> type t = elt list
> module Elt = E
> let choose s = List.hd s
> let singleton e = [e]
> let string_of s = "{" ^ E.string_of (choose s) ^ "}"
> end
>
> module MakePair (E1: ELT) (E2: ELT) : sig
> include ELT with type t = E1.t * E2.t
> val product : E1.t -> E2.t -> t
> end = struct
> type t = E1.t * E2.t
> let product x y = x,y
> let string_of (x,y) = "(" ^ E1.string_of x ^ "," ^ E2.string_of y ^ ")"
> end
>
> module MakeProduct (S1: SET) (S2: SET) : sig
> include SET with type elt = S1.elt * S2.elt
> val product : S1.t -> S2.t -> t
> end = struct
> module P = MakePair(S1.Elt)(S2.Elt)
> module S = Make(P)
> include S
> let product s1 s2 = S.singleton (P.product (S1.choose s1) (S2.choose s2))
> end
module MakeProduct (S1: SET) (S2: SET) : sig
include SET with type Elt.t = S1.elt * S2.elt
val product : S1.t -> S2.t -> t
end = struct
module P = MakePair(S1.Elt)(S2.Elt)
module S = Make(P)
include S
let product s1 s2 = S.singleton (P.product (S1.choose s1) (S2.choose s2))
end
Cheers, Josh
next prev parent reply other threads:[~2014-01-21 11:31 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-20 16:34 Jocelyn Sérot
2014-01-20 20:57 ` Leo White
2014-01-21 11:09 ` SEROT Jocelyn
2014-01-21 11:30 ` Josh Berdine [this message]
2014-01-21 14:30 ` SEROT Jocelyn
2014-01-21 13:32 ` Leo White
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87txcxlggc.fsf@berdine.net \
--to=josh@berdine.net \
--cc=Jocelyn.SEROT@univ-bpclermont.fr \
--cc=caml-list@inria.fr \
--cc=lpw25@cam.ac.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox