Skip to content

Instantly share code, notes, and snippets.

@sordina
Created May 17, 2011 08:07
Show Gist options
  • Save sordina/976136 to your computer and use it in GitHub Desktop.
Save sordina/976136 to your computer and use it in GitHub Desktop.
How many arrow laws am I breaking?
{-# Language TypeOperators #-}
import Prelude hiding ((.), id)
import Control.Arrow
import Control.Category
{-
> let math = ("add 2" :-: (+2)) . ("times 3" :-: (*3)) >>> ("invert" :-: (1/))
> math
(invert) . ((add 2) . (times 3))
> runFun math 9
3.4482758620689655e-2
-}
data (:->:) a b = String :-: (a -> b)
runFun :: (a :->: b) -> a -> b
runFun (_ :-: f) a = f a
instance Category (:->:) where
id = "id" :-: id
(s1 :-: f1) . (s2 :-: f2) = ("(" ++ s1 ++ ") . (" ++ s2 ++ ")") :-: (f1 . f2)
instance Show (a:->:b) where
show (s :-: _) = s
instance Arrow (:->:) where
arr f = "Lambda" :-: f
first lf@(s :-: f) = ("first (" ++ show lf ++ ")") :-: (\(b, d) -> (f b, d))
@sordina
Copy link
Author

sordina commented May 17, 2011

I guess I would have to override (&&&) and (***) for the actual arrow behaviour to be meaningful...

This is mainly a Control.Category exercise :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment