From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Satoshi Ogasawara <ogasawara@itpl.co.jp>
Cc: caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] concept style using first-class module
Date: Wed, 13 Apr 2011 14:27:10 +0900 [thread overview]
Message-ID: <EE344C33-CC94-427E-ADFE-5D4B4BF8E011@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <4DA52686.40200@itpl.co.jp>
On 2011/04/13, at 13:28, Satoshi Ogasawara wrote:
> Recently I learned concept style generic programming like Haskell type-class.
> I'd like to emulate that style in OCaml using first-class modules.
>
> e.g.
>
> module type Monad = sig
> type 'a t
> val return : 'a -> 'a t
> val bind : 'a t -> ('a -> 'b t) -> 'b t
> end
>
> module type MonadPlus = sig
> type 'a t
> include Monad with type 'a t := 'a t
> val zero : 'a t
> val plus : 'a t -> 'a t -> 'a t
> end
>
> let foo (module M:MonadPlus) m = M.bind m (fun x -> M.return x)
>
> But above code makes an error at the last line.
>
> Error: This `let module' expression has type 'a M.t -> 'a M.t
> In this type, the locally bound module name M escapes its scope
>
> I have tried to add a (type s) parameter and specialize module
> types but It's not work.
>
> How can I make this code typable? Is it impossible without higher
> order type operator?
Unfortunately, you gave yourself the answer: one would need higher
kinded type variables to do that (they have them in Haskell).
Namely, in order to use M.t in the result, you need to bind it somewhere,
but a first-class module type cannot bind a type, and (type s) only works
with parameterless types.
Note that the extension allowing parameterized types in first-class
module with clauses does not help either, since it doesn't bind the
type constructor either, just allows to pass it around.
So you have to use a real functor here:
module Foo(M:MonadPlus) = struct
let foo m = M.bind m (fun x -> M.return x)
end
You can also wrap this functor in a first-class module, but I don't see
how it would help.
Sorry,
Jacques
next prev parent reply other threads:[~2011-04-13 5:27 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-13 4:28 Satoshi Ogasawara
2011-04-13 5:27 ` Jacques Garrigue [this message]
2011-04-13 6:01 ` Satoshi Ogasawara
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=EE344C33-CC94-427E-ADFE-5D4B4BF8E011@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@yquem.inria.fr \
--cc=ogasawara@itpl.co.jp \
/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