Skip to content

Instantly share code, notes, and snippets.

@naohaq
Created July 15, 2015 04:08
Show Gist options
  • Select an option

  • Save naohaq/2e5e01ce9d23341cdb40 to your computer and use it in GitHub Desktop.

Select an option

Save naohaq/2e5e01ce9d23341cdb40 to your computer and use it in GitHub Desktop.
Combinators defined under Applicative.
module Main where
import Control.Applicative
newtype A = A Integer deriving Show
newtype B = B Integer deriving Show
newtype C = C Integer deriving Show
k :: Applicative f => a -> f a
k = pure
s :: Applicative t => t (a -> b) -> t a -> t b
s = (<*>)
b :: Applicative f => (b -> c) -> f b -> f c
b = s (k s) k
c :: Applicative f => f (a -> b) -> a -> f b
c = s (s (k (s (k s) k)) s) (k k)
h :: (Applicative t, Applicative f) => t (b -> c) -> t (f b) -> t (f c)
h = b s (b b)
pbc :: Integer -> B -> C
pbc x (B y) = C $ x + y
pab :: Integer -> A -> B
pab x (A y) = B $ x * y
main :: IO ()
main = print $ h pbc pab 3 (A 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment