Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. {-# LANGUAGE
  2. RankNTypes,
  3. TypeApplications,
  4. TypeOperators,
  5. KindSignatures,
  6. TypeFamilies,
  7. TypeInType,
  8. MultiParamTypeClasses,
  9. FunctionalDependencies,
  10. FlexibleInstances,
  11. AllowAmbiguousTypes #-}
  12.  
  13. module Test where
  14.  
  15. import Prelude hiding (Monoid)
  16. import qualified Prelude as P
  17. import Data.Proxy
  18. import Control.Applicative
  19. import Data.Functor.Compose
  20. import Data.Functor.Identity
  21. import Control.Monad
  22.  
  23. class Set p k | p -> k where
  24.  
  25. class Set p k => Category p k | p -> k where
  26. type (→) :: k -> k -> *
  27. infixl 1 →
  28.  
  29. data (a ≅ b) p = Iso { fwd :: a `p` b, bwd :: b `p` a }
  30. infixl 1 ≅
  31.  
  32. class Category p k => Monoidal p k | p -> k where
  33. type (⊗) :: k -> k -> k
  34. type I :: k
  35.  
  36. α :: forall (a :: k) (b :: k) (c :: k). ((a ⊗ b) ⊗ c ≅ a ⊗ (b ⊗ c)) (→)
  37. λ :: forall (a :: k) . (I ⊗ a ≅ a) (→)
  38. ρ :: forall (a :: k) . (a ⊗ I ≅ a) (→)
  39.  
  40. class (Monoidal p c, Monoidal q d) => MonoidalFunctor p q (f :: c -> d) where
  41. ϕ :: (f a ⊗ f b) → f (a ⊗ b)
  42. ϕI :: I → f I
  43.  
  44. class (Monoidal p c, Monoidal q d) => OpMonoidalFunctor p q (f :: c -> d) where
  45. ϕ' :: f (a ⊗ b) → (f a ⊗ f b)
  46. ϕI' :: f I → I
  47.  
  48. class Monoidal p k => Monoid p (m :: k) where
  49. μ :: m ⊗ m → m
  50. η :: I → m
  51.  
  52. data Hask
  53.  
  54. instance Set Hask *
  55.  
  56. instance Category Hask * where
  57. type (→) = (->)
  58.  
  59. instance Monoidal Hask * where
  60. type (⊗) = (,)
  61. type I = ()
  62. α = Iso fwd bwd
  63. where
  64. fwd ((a, b), c) = (a, (b, c))
  65. bwd (a, (b, c)) = ((a, b), c)
  66. ρ = Iso fwd bwd
  67. where
  68. fwd (a, _) = a
  69. bwd a = (a, ())
  70. λ = Iso fwd bwd
  71. where
  72. fwd (_, a) = a
  73. bwd a = ((), a)
  74.  
  75. instance Applicative f => MonoidalFunctor Hask Hask f where
  76. ϕ = uncurry $ liftA2 (,)
  77. ϕI = pure
  78.  
  79. instance P.Monoid m => Monoid Hask m where
  80. μ = uncurry (<>)
  81. η _ = mempty
  82.  
  83. data Hask'
  84.  
  85. newtype f :~> g = Nat { runNat :: forall x. f x -> g x }
  86. newtype (f :&: g) a = Product { runProduct :: (f a, g a) }
  87.  
  88. -- instance Set Hask' (* -> *)
  89. --
  90. -- instance Category Hask' (* -> *) where
  91. -- type (→) = (:~>)
  92.  
  93. -- instance Monoidal Hask' (* -> *) where
  94. -- type (⊗) = (:&:)
  95. -- type I = (Const ())
  96.  
  97. -- instance Alternative f => Monoid Hask' f where
  98. -- μ = Nat $ uncurry (<|>) . runProduct
  99. -- η = Nat $ const empty
  100.  
  101. type f ∘ g = Compose f g
  102.  
  103. data HaskC
  104.  
  105. instance Set HaskC (* -> *)
  106.  
  107. instance Category HaskC (* -> *) where
  108. type (→) = (:~>)
  109.  
  110. instance Monoidal HaskC (* -> *) where
  111. type (⊗) = Compose
  112. type I = Identity
  113.  
  114. α = Iso fwd bwd
  115. where
  116. -- fwd :: ((f ∘ g) ∘ h) :~> (f ∘ (g ∘ h))
  117. fwd = Nat $ _ . getCompose . getCompose
  118. -- bwd :: (f ∘ (g ∘ h)) :~> ((f ∘ g) ∘ h)
  119. bwd = Nat $ _
  120. ρ = Iso fwd bwd
  121. where
  122. -- fwd :: (f ∘ Identity) :~> f
  123. fwd = Nat $ _
  124. -- bwd :: f :~> (f ∘ Identity)
  125. bwd = Nat $ _
  126. λ = Iso fwd bwd
  127. where
  128. -- fwd :: (Identity ∘ f) :~> f
  129. fwd = Nat $ _
  130. -- bwd :: f :~> (Identity ∘ f)
  131. bwd = Nat $ _
  132.  
  133. instance Monad m => Monoid HaskC m where
  134. μ = Nat $ join . getCompose
  135. η = Nat $ return . runIdentity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement