monadfix

Mutually recursive evaluator in Haskell

吃可爱长大的小学妹 提交于 2021-02-10 04:55:29
问题 Update: I've added an answer that describes my final solution (hint: the single Expr data type wasn't sufficient). I'm writing an evaluator for a little expression language, but I'm stuck on the LetRec construct. This is the language: type Var = String type Binds = [(Var, Expr)] data Expr = Var Var | Lam Var Expr | App Expr Expr | Con Int | Sub Expr Expr | If Expr Expr Expr | Let Var Expr Expr | LetRec Binds Expr deriving (Show, Eq) And this this the evaluator so far: data Value = ValInt Int

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

MonadFix in strict language

喜你入骨 提交于 2019-12-21 08:17:23
问题 I'm working on camlp4 extension for haskell-like do notation in Ocaml, and trying to figure out how GHC compiles recursive do-bindings (enabled with -XDoRec). I wonder if it possible for monadic fixpoint combinator to exist in strict language (like Ocaml/F#/SML/...)? If yes, how can it look like? Would it be very useful? 回答1: The F# computation expression syntax (related to Haskell do ) supports recursion: let rec ones = seq { yield 1 yield! ones } This is supported because the computation

Why is `mfix` not total in `MaybeT`

Deadly 提交于 2019-12-08 08:03:45
问题 The transformers implementation of MonadFix for MaybeT fails if the function ever evaluates to Nothing . Why is Nothing not propagating over mfix ? mfix' :: MonadFix m => (a -> MaybeT m a) -> MaybeT m a mfix' f = MaybeT $ mfix $ \case Nothing -> return Nothing Just x -> runMaybeT $ f x There must be a good reason that I do not see because ListT does not implement MonadFix at all, and Maybe implements it in the same way as above. 回答1: I think the issue is just that the error message is

Is there an instance of Monad but not of MonadFix?

随声附和 提交于 2019-12-06 18:42:57
问题 The question is mostly in the title. It seems like mfix can be defined for any monadic computation, even though it might diverge: mfix :: (a -> m a) -> m a mfix f = fix (join . liftM f) What is wrong with this construction? Also, why are the Monad and MonadFix typeclasses separate (i.e. what type has an instance of Monad but not of MonadFix )? 回答1: The left shrinking (or tightening) law says that mfix (\x -> a >>= \y -> f x y) = a >>= \y -> mfix (\x -> f x y) In particular this means that

Is there an instance of Monad but not of MonadFix?

こ雲淡風輕ζ 提交于 2019-12-04 23:42:54
The question is mostly in the title. It seems like mfix can be defined for any monadic computation, even though it might diverge: mfix :: (a -> m a) -> m a mfix f = fix (join . liftM f) What is wrong with this construction? Also, why are the Monad and MonadFix typeclasses separate (i.e. what type has an instance of Monad but not of MonadFix )? The left shrinking (or tightening) law says that mfix (\x -> a >>= \y -> f x y) = a >>= \y -> mfix (\x -> f x y) In particular this means that mfix (\x -> a' >> f x) = a' >> mfix f which means that the monadic action inside mfix must be evaluated exactly

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

MonadFix instance for interpreter monad transformer generated by FreeT?

梦想的初衷 提交于 2019-12-03 07:18:20
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 prompt (f resp) instance MonadFix m => MonadFix (FreeT (InteractiveF p r)) m a) mfix = -- ??? I feel like

Using Cont to acquire values from the future and the past

帅比萌擦擦* 提交于 2019-12-03 06:56:06
问题 I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) | Halt However, it's tricky to parse a textual representation of a brainfuck program into this data type. The problem arises with trying to correctly parse square brackets, because there is some knot-tying to do so that the final Instruction inside a loop links to the loop's Control again.

Why can't there be an instance of MonadFix for the continuation monad?

有些话、适合烂在心里 提交于 2019-12-03 05:55:28
问题 How can we prove that the continuation monad has no valid instance of MonadFix? 回答1: Well actually, it's not that there can't be a MonadFix instance, just that the library's type is a bit too constrained. If you define ContT over all possible r s, then not only does MonadFix become possible, but all instances up to Monad require nothing of the underlying functor : newtype ContT m a = ContT { runContT :: forall r. (a -> m r) -> m r } instance Functor (ContT m) where fmap f (ContT k) = ContT (