fixpoint-combinators

Cases in which we shall not use monadic bind to write mfix down using loop

有些话、适合烂在心里 提交于 2021-02-07 13:22:53
问题 I have been trying to write mfix down using Control.Arrow.loop. I came up with different definitions and would like to see which one is mfix 's actual workalike. So, the solution I reckon to be the right one is the following: mfix' :: MonadFix m => (a -> m a) -> m a mfix' k = let f ~(_, d) = sequenceA (d, k d) in (flip runKleisli () . loop . Kleisli) f As one can see, the loop . Kleisli 's argument works for Applicative instances. I find it to be a good sign as we mostly have our knot-tying

Cases in which we shall not use monadic bind to write mfix down using loop

佐手、 提交于 2021-02-07 13:21:11
问题 I have been trying to write mfix down using Control.Arrow.loop. I came up with different definitions and would like to see which one is mfix 's actual workalike. So, the solution I reckon to be the right one is the following: mfix' :: MonadFix m => (a -> m a) -> m a mfix' k = let f ~(_, d) = sequenceA (d, k d) in (flip runKleisli () . loop . Kleisli) f As one can see, the loop . Kleisli 's argument works for Applicative instances. I find it to be a good sign as we mostly have our knot-tying

fix vs. ArrowLoop

我怕爱的太早我们不能终老 提交于 2020-08-02 07:16:52
问题 Description of loop from Control.Arrow : The loop operator expresses computations in which an output value is fed back as input, although the computation occurs only once. It underlies the rec value recursion construct in arrow notation. Its source code, and its instantiation for (->) : class Arrow a => ArrowLoop a where loop :: a (b,d) (c,d) -> a b c instance ArrowLoop (->) where loop f b = let (c,d) = f (b,d) in c This immediately reminds me of fix , the fixpoint combinator: fix :: (a -> a)

What is the difference between Fix, Mu and Nu in Ed Kmett's recursion scheme package

做~自己de王妃 提交于 2020-06-09 09:25:54
问题 In Ed Kmett's recursion-scheme package, there are three declarations: newtype Fix f = Fix (f (Fix f)) newtype Mu f = Mu (forall a. (f a -> a) -> a) data Nu f where Nu :: (a -> f a) -> a -> Nu f What is the difference between those three data types? 回答1: Mu represents a recursive type as its fold, and Nu represents it as its unfold. In Haskell, these are isomorphic, and are different ways to represent the same type. If you pretend that Haskell doesn't have arbitrary recursion, the distinction

Can one express catamorphism through Data.Function.fix?

吃可爱长大的小学妹 提交于 2020-02-04 05:33:29
问题 I have this lovely fixana function here that performs about 5 times faster than her sister ana . (i have a criterion report to back me on this) ana alg = Fix . fmap (ana alg) . alg fixana alg = fix $ \f -> Fix . fmap f . alg Can I express their cousin cata in the same fashion? cata f = f . fmap (cata f) . unFix It seems to me that I cannot, but I have been humbled by my S.O. fellows quite a few times in the past. 回答1: Actually, this has nothing to do with catamorphisms. Whenever a function is

What is fixed point?

孤街浪徒 提交于 2020-01-04 04:12:24
问题 I'm rewatching some of the earlier lectures on SICP. The notion of a fixed-point is a bit confusing to me. The fixed-point procedure: should I be thinking about it this way, "it's the way to find a fixed-point of a given function." So given f(2) = 2 ? Also why is it stated in this lecture, that a new function y that maps to x / y is a fixed point? 回答1: According to Fixed point (mathematics) on Wikipedia: In mathematics, a fixed point (sometimes shortened to fixpoint, also known as an

haskell - flip fix / fix

落花浮王杯 提交于 2019-12-21 03:59:11
问题 >>>flip fix (0 :: Int) (\a b -> putStrLn "abc") Output: "abc" This is a simplified version of using flip fix . I saw this way of using it in some youtube video which are probably from google tech talk or some other talks. Can somebody give me some pointers(not some memory address, thanks!) that what exactly fix is. I know the general definition from documentation on the official site. And I have scanned through lots of stuff on the internet, just couldn't find an answer that is comprehensive

How to write a Show instance for Mu recursive types

好久不见. 提交于 2019-12-11 17:48:19
问题 I want to write an instance of Show for lists of the following type: newtype Mu f = Mu (forall a. (f a -> a) -> a) data ListF a r = Nil | Cons a r deriving (Show) type List a = Mu (ListF a) Module Data.Functor.Foldable defines it, but it converting it to Fix , something I want to avoid. How can I define this Show instance? 回答1: The slogan, "Follow the types!" , works for us here, fulltime. From your code, with some renaming for easier comprehension, {-# LANGUAGE RankNTypes #-} data ListF a r

How to use Functor instances with Fix types

谁说我不能喝 提交于 2019-12-08 01:57:39
问题 Let's say I want to have a very generic ListF data type: {-# LANGUAGE GADTs, DataKinds #-} data ListF :: * -> * -> * where Nil :: List a b Cons :: a -> b -> List a b Now I can use this data type with Data.Fix to build an f-algebra import qualified Data.Fix as Fx instance Functor (ListF a :: * -> *) where fmap f (Cons x y) = Cons x (f y) fmap _ Nil = Nil sumOfNums = Fx.cata f (Fx.Fix $ Cons 2 (Fx.Fix $ Cons 3 (Fx.Fix $ Cons 5 (Fx.Fix Nil)))) where f (Cons x y) = x + y f Nil = 0 But how I can

Sharing vs. non-sharing fixed-point combinator

删除回忆录丶 提交于 2019-11-30 09:27:51
This is the usual definition of the fixed-point combinator in Haskell: fix :: (a -> a) -> a fix f = let x = f x in x On https://wiki.haskell.org/Prime_numbers , they define a different fixed-point combinator: _Y :: (t -> t) -> t _Y g = g (_Y g) -- multistage, non-sharing, g (g (g (g ...))) -- g (let x = g x in x) -- two g stages, sharing _Y is a non-sharing fixpoint combinator, here arranging for a recursive "telescoping" multistage primes production (a tower of producers). What exactly does this mean? What is "sharing" vs. "non-sharing" in that context? How does _Y differ from fix ? "Sharing"