haskell - Using fsharp-typeclasses to make a function that works on arbitrary monads -


i'm trying implement composm paper a pattern compositional functions accepts arbitray monad , works on it. in haskell code be:

data expr =     | var string     | abs string expr  composexprm :: (monad m) => (expr -> m expr) -> expr -> m expr composexprm f e =     case e of         abs n e -> f e >>= (\e' -> return $ abs n e')         _       -> return e 

i tried using fsharp-typeclasses code below:

type expr =     | var of string     | abs of string * expr  let rec composexprm f e = match e     | abs (n, e) -> f e >>= (fun e' -> return' <| abs (n, e'))     | var _      -> return' e 

but type inference errors:

> not resolve ambiguity inherent in use of operator 'instance' @ or near program point. consider using type annotations resolve ambiguity.  > type constraint mismatch when applying default type 'obj' type inference variable. no overloads match method 'instance'. available overloads shown below (or in error list window). consider adding further type constraints  > possible overload: 'static member return.instance : _monad:return * maybe<'a> -> ('a -> maybe<'a>)'. type constraint mismatch. type        obj       not compatible type     maybe<'a>       type 'obj' not compatible type 'maybe<'a>'.  > etc..... 

is i'm trying achieve possible fsharp-typeclasses? or have restrict function use specific monad?

keep in mind polymorphic functions using technique should inline, that's "magic" resides.

so, add inline keyword after let rec , compile.

please let me know if function behaves expected. have @ paper later.

in samples find many functions defined work monad. main motivation when started project.


Comments