applicative

ghci special case for Applicative?

瘦欲@ 提交于 2020-04-12 09:57:54
问题 In ghci: λ> :t (pure 1) (pure 1) :: (Applicative f, Num a) => f a λ> show (pure 1) <interactive>:1:1: No instance for (Show (f0 a0)) arising from a use of `show' Possible fix: add an instance declaration for (Show (f0 a0)) In the expression: show (pure 1) In an equation for `it': it = show (pure 1) λ> pure 1 1 Does this mean that ghci execute Applicative and displays the result, just like IO ? Note that pure () and pure (+1) don't print anything. 回答1: You get the same behaviour if you use

Are all fixed size containers strong monoidal functors, and/or vice versa?

北城余情 提交于 2020-03-21 11:38:05
问题 The Applicative typeclass represents lax monoidal functors that preserve the cartesian monoidal structure on the category of typed functions. In other words, given the canonical isomorphisms witnessing that (,) forms a monoidal structure: -- Implementations left to the motivated reader assoc_fwd :: ((a, b), c) -> (a, (b, c)) assoc_bwd :: (a, (b, c)) -> ((a, b), c) lunit_fwd :: ((), a) -> a lunit_bwd :: a -> ((), a) runit_fwd :: (a, ()) -> a runit_bwd :: a -> (a, ()) The typeclass and its laws

Combining parsers in Haskell

守給你的承諾、 提交于 2020-03-20 12:43:37
问题 I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s instance Applicative Parser where pure a = Parser $ \s -> Just (a,s) f <*> a = Parser $ \s -> case parse f s of Just (g,s') -> parse (fmap g a) s' Nothing -> Nothing instance Alternative Parser where empty = Parser $ \s -> Nothing l <|> r = Parser $ \s -> parse l s <|> parse r s ensure :: (a -> Bool) ->

Combining parsers in Haskell

99封情书 提交于 2020-03-20 12:40:11
问题 I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s instance Applicative Parser where pure a = Parser $ \s -> Just (a,s) f <*> a = Parser $ \s -> case parse f s of Just (g,s') -> parse (fmap g a) s' Nothing -> Nothing instance Alternative Parser where empty = Parser $ \s -> Nothing l <|> r = Parser $ \s -> parse l s <|> parse r s ensure :: (a -> Bool) ->

Combining parsers in Haskell

若如初见. 提交于 2020-03-20 12:39:33
问题 I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s instance Applicative Parser where pure a = Parser $ \s -> Just (a,s) f <*> a = Parser $ \s -> case parse f s of Just (g,s') -> parse (fmap g a) s' Nothing -> Nothing instance Alternative Parser where empty = Parser $ \s -> Nothing l <|> r = Parser $ \s -> parse l s <|> parse r s ensure :: (a -> Bool) ->

Different behaviors of Applicative on tuples and lists in Haskell

久未见 提交于 2020-03-01 06:06:52
问题 For example, -- Num a => ([Char], a -> a) <*> ([Char], a) > ("hello ",(*6)) <*> ("world",7) ("hello world",42) -- Num a => [a -> a] <*> [a] > [(*7),(*6)] <*> [6,7] [42,49,36,42] -- Num a => [[Char], a -> a] <*> [[Char], a] > ["hello ",(*6)] <*> ["world",7] <interactive>:17:2: Couldn't match expected type ‘[Char] -> [Char]’ with actual type ‘[Char]’ In the expression: "hello " In the first argument of ‘(<*>)’, namely ‘["hello ", (* 6)]’ In the expression: ["hello ", (* 6)] <*> ["world", 7] For

Different behaviors of Applicative on tuples and lists in Haskell

故事扮演 提交于 2020-03-01 06:05:22
问题 For example, -- Num a => ([Char], a -> a) <*> ([Char], a) > ("hello ",(*6)) <*> ("world",7) ("hello world",42) -- Num a => [a -> a] <*> [a] > [(*7),(*6)] <*> [6,7] [42,49,36,42] -- Num a => [[Char], a -> a] <*> [[Char], a] > ["hello ",(*6)] <*> ["world",7] <interactive>:17:2: Couldn't match expected type ‘[Char] -> [Char]’ with actual type ‘[Char]’ In the expression: "hello " In the first argument of ‘(<*>)’, namely ‘["hello ", (* 6)]’ In the expression: ["hello ", (* 6)] <*> ["world", 7] For

ApplicativeDo not working with sequencing

两盒软妹~` 提交于 2020-01-14 13:23:48
问题 I have this type, basically a Kleisli arrow: {-# language DeriveFunctor #-} data Plan m i o = Plan (i -> m o) deriving Functor instance (Monad m) => Applicative (Plan m i) where pure x = Plan (\_ -> pure x) Plan f <*> Plan x = Plan (\i -> f i <*> x i) Since it has an Applicative instance, I turn on ApplicativeDo and try to build a value using do-notation: {-# language ApplicativeDo #-} myplan :: Plan IO () () myplan = do pure () pure () It doesn't work: No instance for (Monad (Plan IO ()))

How does GHCi print partially-applied values created from “pure”?

ⅰ亾dé卋堺 提交于 2020-01-14 09:32:21
问题 I've been playing around with Applicative instances in order to figure out how they work. However, I honestly don't understand this behavior. If I define my own datatype, then apply pure to it with no other arguments, nothing prints out, but it errors if I try to apply something to the result. ghci> data T = A ghci> pure A ghci> pure A 0 <interactive>:21:1: No instance for (Show T) arising from a use of ‘print’ In a stmt of an interactive GHCi command: print it However, if I make T an

How does GHCi print partially-applied values created from “pure”?

不打扰是莪最后的温柔 提交于 2020-01-14 09:32:08
问题 I've been playing around with Applicative instances in order to figure out how they work. However, I honestly don't understand this behavior. If I define my own datatype, then apply pure to it with no other arguments, nothing prints out, but it errors if I try to apply something to the result. ghci> data T = A ghci> pure A ghci> pure A 0 <interactive>:21:1: No instance for (Show T) arising from a use of ‘print’ In a stmt of an interactive GHCi command: print it However, if I make T an