On Wed, 2007-05-30 at 22:40 -0700, Erick Tryzelaar wrote:
> How do I write a monad? It should be pretty simple to port a simple 
> haskell monad over, but I'm running into an assert error. I think I'm 
> not passing in the right type function. Here's the monad typeclass:
> 
> ######################################################
> typeclass Monad [M: TYPE->TYPE] {
>   virtual fun bind[a,b]: M a * (a -> M b) -> M b;
>   virtual fun ret[a]: a -> M a;
> }
> ######################################################
> 
> And here's my instance:
> 
> ######################################################
> #import <flx.flxh>
> 
> instance Monad[opt] {
>   fun bind[a,b] (o:opt[a], f:a -> opt[b]): opt[b] =>
>     match o with
>     | None[a] => None[b]
>     | Some ?x => f x
>     endmatch
>   ;

> File "./lpsrc/flx_lookup.ipk", line 1702, characters 6-12: Assertion failed

      (* SHOULD BE CLIENT ERROR not assertion *)
      assert (length ts = length spec_vs);

This basically means "wrong number of type variables". 
I put in a better diagnostic:
///////////////////////////////////////////////////////////
Qualified name lookup finds index 2415
spec_vs=T<2418>
spec_ts=<T2418>
input_ts=
opt is not a type variable
CLIENT ERROR
Wrong number of type variables, expected 1, but got 0
In ./er.flx: line 3, cols 16 to 18
2: 
3: instance Monad[opt] {
                  ***
4:   fun bind[a,b] (o:opt[a], f:a -> opt[b]): opt[b] =>


The problem is opt is a parameterised type, NOT a functor,
that is, a function mapping types to types. You need this:

/////////////////////////////////////////////////////////
#import <flx.flxh>

typedef fun OPT(t:TYPE):TYPE => opt[t];

instance Monad[the OPT] {
  fun bind[a,b] (o:opt[a], f:a -> opt[b]): opt[b] =>
    match o with
    | None[a] => None[b]
    | Some ?x => f x
    endmatch
  ;

  fun ret[a] (x:a): opt[a] => Some x;
}
open Monad[the OPT];
/////////////////////////////////////////////


See the tutorial .. this one is actually a tutorial example :)

The distinction between parameterised types and type functions
is a pain and silly complication ;(

> (and on a side note, this tiny little code block takes 60s to compile 
> with dypgen (!). I didn't realize we had that much of a slowdown!)

Dypgen builds the automaton something like 5 times at run time:
the base language, then as each extension is opened. This costs.
Emmanuel is working on speeding up the automaton build.
It may also be possible to load a built and cached automaton 
from disk later.

The parsing time is also slow for unknown reason. The first
regression test consumes over 1G RAM which kills my box.
The reason isn't known at this stage, the caml gc appears not
be to be collecting memory it should.



-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to