[MLton] Monadic MLton.Vector.create

Daniel C. Wang danwang@CS.Princeton.EDU
Tue, 28 Mar 2006 21:21:06 -0800


Just a thought, but can you use the value restriction to your advantage 
here? I mean you can't store a polymorphic value into a ref cell. Can 
sub and update be given types too polymorphic to stash away?

|val create: int * 'a * ({key: 'b,
                         sub: 'b * int -> 'a,
                      update: 'b * int * 'a -> unit} -> unit) -> 'a vector |

Unless, I'm missing something there's no way for sub,update, or key to 
escape the scope of  create. I think a similar argument has been made 
for certain monads in Haskell, but my memory is flakey.

Vesa Karvonen wrote:
> Quoting Henry Cejtin <henry.cejtin@sbcglobal.net>:
> [...]
>   
>> The only ugliness to me in the create proposal is the fact  that  update  and
>> sub might be squirreled away, so you have to make them raise Fail if they are
>> called after create returns.  It would be nice if one could make it  so  that
>> they  couldn't  be  in scope or saved any where, but I don't see any way that
>> isn't REALLY convoluted and ugly.
>>     
> [...]
>
> Were you thinking about monads?  Here is a suitable signature for create:
>
> signature CREATE =
>    sig
>       type 'a m
>
>       val create : int -> (int -> 'a m) -> 'a vector
>
>       val >>= : 'a m * ('a -> 'b m) -> 'b m
>       val return : 'a -> 'a m
>       val sub  : int -> 'a m
>       val update : int * 'b -> unit m
>    end
>
> The below functor implements the same fib function as in Stephen's
> earlier post.
>
> functor Fib (C : CREATE) =
>    struct
>       local
>          open C
>          infix >>=
>       in
>       fun fib i =
>           create i (fn i =>
>                        if i <= 1 then
>                           return i
>                        else
>                           sub (i-1) >>= (fn x =>
>                           sub (i-2) >>= (fn y =>
>                           return (x+y))))
>       end
>    end
>
> I wouldn't call it "ugly", but YMMV.
>
> -Vesa Karvonen
>
> _______________________________________________
> MLton mailing list
> MLton@mlton.org
> http://mlton.org/mailman/listinfo/mlton
>