free-monad

Optimizing a Free Monad

牧云@^-^@ 提交于 2021-02-19 03:44:10
问题 If I have a value a: Free[Op, A] , is it possible to "flatten" the structure of a so that two Op s that are bound together by the free monad may be collapsed into one? Context: I'd like to perform this as an optimization step before interpretation because a semantic of Op is that its operations are idempotent. So if two appear "in a row", the second can be eliminated at no cost to the semantics of the program. 回答1: As far as I understand there is no way for this kind introspection of Free

Zoom instance over Free Monad

若如初见. 提交于 2020-01-05 04:24:10
问题 I'm attempting to build a free monad (using free) which acts just like a StateT monad, but which allows you to also run monads over a base state AppState . I have a separate constructor LiftAction which holds those types. The idea is that you keep zoom ing Actions down until they reach AppState, which can store different states inside its extension map. Here was my earlier (failed) attempt using mtl: Lift through nested state transformers (mtl) Anyways, since it's basically a wrapper over

Control.MonadPlus.Free without unnecessary distribution

梦想与她 提交于 2020-01-03 11:31:49
问题 I'm trying to use a free monad to build an EDSL for constructing AND/OR decision trees like Prolog, with >>= mapped to AND, and mplus mapped to OR. I want to be able to describe something like A AND (B OR C) AND (D OR E) , but I don't want distributivity to turn this into (A AND B AND D) OR (A AND B AND E) OR (A AND C AND D) OR (A AND C AND E) . Ultimately, I want to transform the AND/OR nodes into reified constraints in a constraint solver, without causing the combinatorial explosion in the

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

亡梦爱人 提交于 2020-01-01 09:13:40
问题 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)) }

Is it possible to implement MonadFix for `Free`?

爱⌒轻易说出口 提交于 2019-12-30 01:41:05
问题 http://hackage.haskell.org/package/free in Control.Monad.Free.Free allows one to get access to the "free monad" for any given Functor . It does not, however, have a MonadFix instance. Is this because such an instance cannot be written, or was it just left out? If such an instance cannot be written, why not? 回答1: Consider the description of what mfix does: The fixed point of a monadic computation. mfix f executes the action f only once, with the eventual output fed back as the input. The word

Free ~> Trampoline : recursive program crashes with OutOfMemoryError

两盒软妹~` 提交于 2019-12-23 09:36:37
问题 Suppose that I'm trying to implement a very simple domain specific language with only one operation: printLine(line) Then I want to write a program that takes an integer n as input, prints something if n is divisible by 10k, and then calls itself with n + 1 , until n reaches some maximum value N . Omitting all syntactic noise caused by for-comprehensions, what I want is: @annotation.tailrec def p(n: Int): Unit = { if (n % 10000 == 0) printLine("line") if (n > N) () else p(n + 1) } Essentially

Structurally enforced Free Alternative, without left distributivity

点点圈 提交于 2019-12-21 03:34:08
问题 There is a nice Free Alternative in the great free package, which lifts a Functor to a left-distributive Alternative. That is, the claim is that: runAlt :: Alternative g => (forall x. f x -> g x) -> Alt f a -> g a is an Alternative homomorphism , with liftAlt . And, indeed, it is one, but only for left-distributive Alternative instances. Of course, in reality, very few Alternative instances are actually left-distributive. Most of the alternative instances that actually matter (parsers, MaybeT

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

大城市里の小女人 提交于 2019-12-13 12:29:05
问题 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 =

Free implementation in scalaz

我是研究僧i 提交于 2019-12-10 13:05:09
问题 The Free implementation in Haskell is: data Free f a = Pure a | Free (f (Free f a)) whereas, the implementation in Scalaz is: sealed abstract class Free[S[_], A] private case class Return[S[_], A](a: A) extends Free[S, A] private case class Suspend[S[_], A](a: S[A]) extends Free[S, A] private case class Gosub[S[_], B, C](a: Free[S, C], f: C => Free[S, B]) extends Free[S, B] why isn't the scalaz implementation similar to Haskell, like: sealed trait Free[F[_],A] case class Return[F[_],A](a: A)

How do I use the Church encoding for Free Monads?

心已入冬 提交于 2019-12-09 09:33:43
问题 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