Skip to content

Instantly share code, notes, and snippets.

@daig
Created October 17, 2017 20:11
Show Gist options
  • Save daig/bfd5fa99dae29239bf53fe50eca2758f to your computer and use it in GitHub Desktop.
Save daig/bfd5fa99dae29239bf53fe50eca2758f to your computer and use it in GitHub Desktop.
module Constrained where
-- | A constained function from @a@ to @b@, utilizing constraint @i@ and providing constraint @j@
-- C subsumes 'a -> b == C () () a b', 'Dict c = C () c a a', and 'c :- k == C c k a a'
newtype C i j a b = C {runC :: i => (j => a) -> b}
-- | Composition. Acts like function composition on the arrows, and
(><) :: C j k a b -> C i j b c -> C i k a c
C f >< C g = C (\x -> g (f x))
-- | 'provide @j' is analogous to 'Dict @j'
provide :: j => C i j a a
provide = C (\a -> a)
-- | lift an unconstrained function
arr :: (a -> b) -> C i i a b
arr f = C f
-- | (convenience function) Lift a constrained function. Requires an extra type parameter
-- so must be called like 'constrained @(i :: * -> Constraint)'
constrained :: (i a => a -> b) -> C (i a) (i a) a b
constrained f = C f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment