0 | module Control.HigherOrder
8 | (~>) : (Type -> Type) -> (Type -> Type) -> Type
9 | f ~> g = forall a. f a -> g a
14 | (~~>) : (Type -> Type) -> (Type -> Type) -> (Type -> Type)
15 | (f ~~> g) a = f a -> g a
20 | interface HFunctor h where
21 | hmap : (Functor f, Functor g)
30 | data (:+:) : (sig1 : (Type -> Type) -> (Type -> Type))
31 | -> (sig2 : (Type -> Type) -> (Type -> Type))
32 | -> (m : Type -> Type)
35 | Inl : sig1 m a -> (:+:) sig1 sig2 m a
36 | Inr : sig2 m a -> (:+:) sig1 sig2 m a
39 | HFunctor sig1 => HFunctor sig2 => HFunctor (sig1 :+: sig2) where
40 | hmap t (Inl op) = Inl (hmap t op)
41 | hmap t (Inr op) = Inr (hmap t op)
46 | interface Prj (0 sup : (Type -> Type) -> (Type -> Type))
47 | (0 sub : (Type -> Type) -> (Type -> Type)) | sub where
49 | prj : sup m a -> sub m a
54 | interface Inj (0 sub : (Type -> Type) -> (Type -> Type))
55 | (0 sup : (Type -> Type) -> (Type -> Type)) | sub where
57 | inj : sub m a -> sup m a
60 | Prj sub' sub => Inj sub sup => Inj sub' sup where
61 | inj = inj . prj {sup = sub', sub}
67 | [S] Inj sig sig where
73 | [L] Inj sig1 (sig1 :+: sig2) where
79 | [R] Inj sig2 (sig1 :+: sig2) where
85 | [Trans] (inner : Inj siga sigb) => (outer : Inj sigb sigc) => Inj siga sigc where
86 | inj = inj {sub = sigb, sup = sigc}
87 | . inj {sub = siga, sup = sigb}
91 | T : (inner : Inj siga sigb) -> (outer : Inj sigb sigc) -> Inj siga sigc
92 | T inner outer = Trans @{inner} @{outer}
100 | Handler : (Type -> Type)
104 | Handler s m n = s . m ~> n . s
109 | HandlerX : (Type -> Type)
113 | HandlerX s m n = s . m ~~> n . s
116 | interface HFunctor sig => Syntax sig where
118 | emap : forall m. (m a -> m b) -> (sig m a -> sig m b)
121 | weave : (m1 : Monad m) => (m2 : Monad n) => (fs : Functor s)
124 | -> (sig m a -> sig n (s a))
127 | Syntax sig1 => Syntax sig2 => Syntax (sig1 :+: sig2) where
128 | emap f (Inl op) = Inl (emap f op)
129 | emap f (Inr op) = Inr (emap f op)
131 | weave s hdl (Inl op) = Inl (weave s hdl op)
132 | weave s hdl (Inr op) = Inr (weave s hdl op)
136 | data Lift : (sig : (Type -> Type))
137 | -> ((Type -> Type) -> (Type -> Type)) where
138 | MkLift : sig (m a) -> Lift sig m a
141 | unlift : Lift sig m a -> sig (m a)
142 | unlift (MkLift x) = x
146 | LiftE : (sig : Type -> Type) -> (Type -> Type) -> (Type -> Type)
150 | Functor sig => HFunctor (Lift sig) where
151 | hmap t (MkLift op) = MkLift (map t op)
154 | Functor sig => Syntax (Lift sig) where
155 | emap f (MkLift op) = MkLift (map f op)
157 | weave s hdl (MkLift op) = MkLift $
158 | map (\p => hdl (map (const p) s)) op
174 | (~<~) : (f : Functor n)
175 | => (g : Functor ctx1)
176 | => (forall q. HandlerX ctx1 m n q)
177 | -> (forall t. HandlerX ctx2 l m t)
178 | -> (forall p. HandlerX (ctx1 . ctx2) l n p)
179 | hdl1 ~<~ hdl2 = hdl1 . map hdl2