Version française
Home     About     Download     Resources     Contact us    
Browse thread
[oliver: Re: [Caml-list] Strings as arrays or lists...]
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: brogoff@s...
Subject: Re: [Caml-list] extensional polymorphism
On Mon, 3 Mar 2003, james woodyatt wrote:
> On Monday, Mar 3, 2003, at 12:10 US/Pacific, brogoff@speakeasy.net 
> wrote:
> >
> > That's why I said "Agitate for extensional polyorphism!". You really 
> > want a
> > way to overload similar notations.
> 
> If "extensional polymorphism" is what I want in order to be able to 
> define an operator ( >>= ) that performs the monad bind operation on 
> arbitrary monads, then yeah-- I'm all for it.
> 
> I recently discovered the power of monadic programming, but I am in no 
> hurry to switch to Haskell in order to get a nice clean syntax for it.

You can do a fair bit of monadic programming in OCaml now, using the module 
system. If you are interested, I'll show you, but you'll learn more by just 
translating Wadler's early papers into OCaml. If you want to do complicated 
monadics with several different monad types intertwined in the same section of 
code, that may be tricky. 

Also, OCaml is an imperative language, so you have lots of monads built in :-). 
One of the problems I have with Haskell is that while there are a few examples 
where it just suits the problem so well that the solution is magically 
concise and beautiful, I find that there are more places where the emphasis on 
purity transforms trivial programming problems into a PhD level research 
problems. 

> If "extensional polymorphism" will get me a cleaner syntax for monadic 
> programming without forcing me to give up all the things that make 
> Ocaml the best language in the universe for imperative programming, 
> then sign me up right here right now.
>
> How would extensional polymorphism get me what I want?

It may ameliorate the problem I cited above in which you're juggling many monads 
in the same section of code and you don't want to use (module) qualified types 
to distinguish your bindMs and returnMs or whatever you want to call your 
operations. 

-- Brian

PS: Here's a signature, use it to constrain modules, and use functors over these 
modules to express some monadic ops.

module type MONAD =
  sig
    type 'a t 
    val returnM : 'a -> 'a t 
    val bindM : 'a t -> ('a -> 'b t) -> 'b t
    val mapM : ('a -> 'b) -> 'a t -> 'b t
    val joinM : 'a t t -> 'a t 
    (* Ad-hoc show function *)
    val showM : 'a t -> ('a -> string) -> string
  end

and a List monad

module List_monad : MONAD = 
  struct
    type 'a t = 'a list
    let returnM x = [x]
    let bindM m f = List.flatten (List.map f m)  
    let mapM f m = List.map f m
    let joinM mm = List.flatten mm

    let showM m f = 
      List.fold_right 
	(fun x s -> 
	  if s = "" then (f x) ^ s 
	  else (f x) ^ "; " ^ s)
	m ""
  end




-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners