Skip to content

Instantly share code, notes, and snippets.

@witt3rd
Last active December 4, 2018 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save witt3rd/643501ee8c35eb782a2400b00c9f33f0 to your computer and use it in GitHub Desktop.
Save witt3rd/643501ee8c35eb782a2400b00c9f33f0 to your computer and use it in GitHub Desktop.
Categories in Haskell

Hask(Cat)

{-# LANGUAGE TypeOperators #-}
 
infix 9 
class Category k where
  ident :: a `k` a
  (∘) :: (b `k` c) -> (a `k` b) -> (a `k` c)
  

instance Category (->) where
  ident = \x -> x
  g  f = \x -> g (f x)

infix 3 Δ
class Category k => Cartesian k where
  (Δ) :: (a `k` c) -> (a `k` d) -> (a `k` (c,d)))
  

instance Category (->) where
  ident = \x -> x
  g  f = \x -> g (f x)

sq x = x * x

main = print $ 
  -- ident sq 2
  (sq  sq) 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment