haskell - Why can I use pure function to convert to applicative -




i have following type definition:

  data hello b =  hi                   | sali b                   deriving (show, eq)  

and isn't instance of applicative can still use pure convert applicative why?

*exercisestraversable data.monoid control.applicative> :t pure $ hi 34 pure $ hi 34 :: (num a, applicative f) => f (hello b)  *exercisestraversable data.monoid control.applicative> pure $ hi 34 hi 34 

and when try:

*exercisestraversable data.monoid control.applicative> (sali (*2)) <*> (sali 4)  <interactive>:23:1: error:     * non type-variable argument         in constraint: applicative (hello a)       (use flexiblecontexts permit this)     * when checking inferred type         :: forall b a. (num b, applicative (hello a)) => hello b 

that clear, because isn't applicative instance.

since aren't specifying applicative type in particular, ghci defaulting io, i.e. pure $ hi 34 returns value of type num => io (hello b). is, hello b isn't f in a -> f a, it's a.

you can see more explicitly hello b typed wrapped arbitrary applicative specifying various applicative functors value:

prelude> (pure $ hi 34) :: either string (hello int b) right (hi 34) prelude> (pure $ hi 34) :: maybe (hello int b) (hi 34) prelude> (pure $ hi 34) :: [hello int b] [hi 34] 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -