free-monad

Is it possible to extend free monad interpreters?

a 夏天 提交于 2019-12-09 07:53:45
问题 Given a free monad DSL such as: data FooF x = Foo String x | Bar Int x deriving (Functor) type Foo = Free FooF And a random interpreter for Foo : printFoo :: Foo -> IO () printFoo (Free (Foo s n)) = print s >> printFoo n printFoo (Free (Bar i n)) = print i >> printFoo n It appears to me that it should be possible to intersperse something into each iteration of printFoo without resorting to doing it manually: printFoo' :: Foo -> IO () printFoo' (Free (Foo s n)) = print s >> print "extra info"

Abstract result types in Free Monads

我只是一个虾纸丫 提交于 2019-12-06 07:39:05
问题 Suppose we want to define a simple DSL for defining UI interactions where we can create objects and then select them: object TestCommand { sealed trait EntityType case object Project extends EntityType case object Site extends EntityType sealed trait TestCommand[A, E] case class Create[A, E](entityType: EntityType, withEntity: E => A) extends TestCommand[A, E] case class Select[A, E](entity: E, next: A) extends TestCommand[A, E] } The problem I have is that I wouldn't want to specify what the

How do I implement Reader using free monads?

☆樱花仙子☆ 提交于 2019-12-05 21:05:41
问题 Ok, so I have figured out how to implement Reader (and ReaderT , not shown) using the operational package: {-# LANGUAGE GADTs, ScopedTypeVariables #-} import Control.Monad.Operational data ReaderI r a where Ask :: ReaderI r r type Reader r a = Program (ReaderI r) a ask :: Reader r r ask = singleton Ask runReader :: forall r a. Reader r a -> r -> a runReader = interpretWithMonad evalI where evalI :: forall b. ReaderI r b -> (r -> b) evalI Ask = id But I can't figure out for my life how to do

Is there an inject equivalent for Haskell in the context of free monads

房东的猫 提交于 2019-12-05 14:08:10
I'm trying to translate this Scala's cats example about composing free monads. The gist of the example seems to be the decomposition of separate concerns into separate data types: data Interact a = Ask (String -> a) | Tell String a deriving (Functor) data DataOp = AddCat String | GetAllCats [String] deriving (Functor) type CatsApp = Sum Interact DataOp Without having these two separate concerns, I would build the "language" for Interact operations as follows: ask :: Free Interact String ask = liftF $ Ask id tell :: String -> Free Interact () tell str = liftF $ Tell str () However, if I want to

Is it possible to do the Free Monad in Clojure?

拈花ヽ惹草 提交于 2019-12-04 21:07:14
问题 There has been some outstanding work with Monads in Clojure by Konrad Hinsen, Jim Duey and Leonardo Borges. My question is - is it possible to do the Free Monad in Clojure? This is an example in Haskell from an article on Scala: data Free f r = Free (f (Free f r)) | Pure r This is the corresponding Scala example sealed abstract class Free[S[+_], +A](implicit S: Functor[S]) { final def map[B](f: A => B): Free[S, B] = flatMap(a => Return(f(a))) final def flatMap[B](f: A => Free[S, B]): Free[S,

MonadFix instance for interpreter monad transformer generated by FreeT?

混江龙づ霸主 提交于 2019-12-04 11:36:56
问题 I have a simplified version of the standard interpreter monad transformer generated by FreeT : data InteractiveF p r a = Interact p (r -> a) type Interactive p r = FreeT (InteractiveF p r) p is the "prompt", and r is the "environment"...one would run this using something like: runInteractive :: Monad m => (p -> m r) -> Interactive p r m a -> m a runInteractive prompt iact = do ran <- runFreeT iact case ran of Pure x -> return x Free (Interact p f) -> do response <- prompt p runInteractive

How to avoid stack overflow when using scalaz's free monad?

拜拜、爱过 提交于 2019-12-04 03:36:28
I had previously thought that part of the goal of the implementation was to avoid this very problem, so maybe I'm doing something obviously dumb? Here is some code: // Stack overflow import scalaz._ sealed trait Command[T] case class Wait(ms: Long) extends Command[Unit] case object Evaluator extends (Command ~> Id.Id) { override def apply[T](cmd: Command[T]) = cmd match { case Wait(t) => Thread.sleep(t) } } object Api { def sleep(ms: Long): Free.FreeC[Command, Unit] = Free.liftFC(Wait(ms)) } val sleep: Free.FreeC[Command, Unit] = Api.sleep(1).flatMap { _ => sleep } Free.runFC(sleep)(Evaluator)

Is it possible to do the Free Monad in Clojure?

一世执手 提交于 2019-12-03 13:28:12
There has been some outstanding work with Monads in Clojure by Konrad Hinsen , Jim Duey and Leonardo Borges . My question is - is it possible to do the Free Monad in Clojure? This is an example in Haskell from an article on Scala: data Free f r = Free (f (Free f r)) | Pure r This is the corresponding Scala example sealed abstract class Free[S[+_], +A](implicit S: Functor[S]) { final def map[B](f: A => B): Free[S, B] = flatMap(a => Return(f(a))) final def flatMap[B](f: A => Free[S, B]): Free[S, B] = this match { case Gosub(a, g) => Gosub(a, (x: Any) => Gosub(g(x), f)) case a => Gosub(a, f) } ..

How do I use the Church encoding for Free Monads?

戏子无情 提交于 2019-12-03 12:22:13
I've been using the Free datatype in Control.Monad.Free from the free package. Now I'm trying to convert it to use F in Control.Monad.Free.Church but can't figure out how to map the functions. For example, a simple pattern matching function using Free would look like this - -- Pattern match Free matchFree :: (a -> r) -> (f (Free f a) -> r) -> Free f a -> r matchFree kp _ (Pure a) = kp a matchFree _ kf (Free f) = kf f I can easily convert it to a function that uses F by converting to/from Free - -- Pattern match F (using toF and fromF) matchF :: Functor f => (a -> r) -> (f (F f a) -> r) -> F f

Is it possible to extend free monad interpreters?

白昼怎懂夜的黑 提交于 2019-12-03 10:49:28
Given a free monad DSL such as: data FooF x = Foo String x | Bar Int x deriving (Functor) type Foo = Free FooF And a random interpreter for Foo : printFoo :: Foo -> IO () printFoo (Free (Foo s n)) = print s >> printFoo n printFoo (Free (Bar i n)) = print i >> printFoo n It appears to me that it should be possible to intersperse something into each iteration of printFoo without resorting to doing it manually: printFoo' :: Foo -> IO () printFoo' (Free (Foo s n)) = print s >> print "extra info" >> printFoo' n printFoo' (Free (Bar i n)) = print i >> print "extra info" >> printFoo' n Is this