caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] concept style using first-class module
@ 2011-04-13  4:28 Satoshi Ogasawara
  2011-04-13  5:27 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Satoshi Ogasawara @ 2011-04-13  4:28 UTC (permalink / raw)
  To: caml-list

Hello,

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?

Regards,
  ogasawara

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

* Re: [Caml-list] concept style using first-class module
  2011-04-13  4:28 [Caml-list] concept style using first-class module Satoshi Ogasawara
@ 2011-04-13  5:27 ` Jacques Garrigue
  2011-04-13  6:01   ` Satoshi Ogasawara
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2011-04-13  5:27 UTC (permalink / raw)
  To: Satoshi Ogasawara; +Cc: caml-list

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

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

* Re: [Caml-list] concept style using first-class module
  2011-04-13  5:27 ` Jacques Garrigue
@ 2011-04-13  6:01   ` Satoshi Ogasawara
  0 siblings, 0 replies; 3+ messages in thread
From: Satoshi Ogasawara @ 2011-04-13  6:01 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thank you for the very quick answer.

On 2011/04/13, at 14:27, Jacques Garrigue wrote:
> 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.

I understand it.

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

By chance, I have just noticed a functor becomes first-class yesterday.
I'll try this way.

Thanks,
  ogasawara
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)

iEYEARECAAYFAk2lPFEACgkQzTChtNfYLwZ/NwCfWzK0Pe7gUKvjW8jwGT6HNHCa
ROwAnRUZ4O/4HE4ICZ9Jfn5Mnt/KchlC
=iYVf
-----END PGP SIGNATURE-----


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

end of thread, other threads:[~2011-04-13  6:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-13  4:28 [Caml-list] concept style using first-class module Satoshi Ogasawara
2011-04-13  5:27 ` Jacques Garrigue
2011-04-13  6:01   ` Satoshi Ogasawara

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