summaryrefslogtreecommitdiff
path: root/src/Soat/FirstOrder/Algebra/Coproduct.idr
blob: a8e4c0e6c9a6e7b90abb78c3f99c99707b120752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module Soat.FirstOrder.Algebra.Coproduct

import Data.Setoid.Indexed
import Data.Setoid.Either

import Soat.FirstOrder.Algebra
import Soat.FirstOrder.Signature
import Soat.FirstOrder.Term

import Syntax.PreorderReasoning.Setoid

%default total

public export
CoproductSet : (0 sig : Signature) -> (0 x, y : sig.T -> Type) -> sig.T -> Type
CoproductSet sig x y = Term sig (\i => Either (x i) (y i))

public export
data (~=~) : (0 x, y : RawSetoidAlgebra sig) -> (t : sig.T)
  -> Rel (CoproductSet sig x.raw.U y.raw.U t)
  where
  DoneL    : {0 x, y : RawSetoidAlgebra sig}
    -> {t : sig.T} -> {i, j : x.raw.U t} -> x.relation t i j
    -> (~=~) x y t (Done $ Left i) (Done $ Left j)
  DoneR    : {0 x, y : RawSetoidAlgebra sig}
    -> {t : sig.T} -> {i, j : y.raw.U t} -> y.relation t i j
    -> (~=~) x y t (Done $ Right i) (Done $ Right j)
  Call     : {t : sig.T} -> (op : Op sig t) -> {xs, ys : _} -> Pointwise ((~=~) x y) xs ys
    -> (~=~) x y t (Call op xs) (Call op ys)
  InjectL  : {t : sig.T} -> (op : Op sig t) -> (xs : _)
    -> (~=~) x y t (Done $ Left $ x.raw.sem op xs) (Call op (map (\_ => Done . Left) xs))
  InjectR  : {t : sig.T} -> (op : Op sig t) -> (xs : _)
    -> (~=~) x y t (Done $ Right $ y.raw.sem op xs) (Call op (map (\_ => Done . Right) xs))
  Sym      : {0 x, y : RawSetoidAlgebra sig} -> {t : sig.T} -> {tm, tm' : _}
    -> (~=~) x y t tm tm' -> (~=~) x y t tm' tm
  Trans    : {0 x, y : RawSetoidAlgebra sig} -> {t : sig.T} -> {tm, tm', tm'' : _}
    -> (~=~) x y t tm tm' -> (~=~) x y t tm' tm'' -> (~=~) x y t tm tm''

parameters {0 x, y : RawSetoidAlgebra sig}
  coprodRelRefl : (xRefl : (t : sig.T) -> (a : x.raw.U t) -> x.relation t a a)
    -> (yRefl : (t : sig.T) -> (a : y.raw.U t) -> y.relation t a a)
    -> {t : _} -> (tm : _) -> (~=~) x y t tm tm
  coprodsRelRefl : (xRefl : (t : sig.T) -> (a : x.raw.U t) -> x.relation t a a)
    -> (yRefl : (t : sig.T) -> (a : y.raw.U t) -> y.relation t a a)
    -> {ts : _} -> (tms : _ ^ ts) -> Pointwise ((~=~) x y) tms tms

  coprodRelRefl x y (Done (Left i))  = DoneL $ x _ i
  coprodRelRefl x y (Done (Right i)) = DoneR $ y _ i
  coprodRelRefl x y (Call op ts)     = Call op $ coprodsRelRefl x y ts

  coprodsRelRefl x y []        = []
  coprodsRelRefl x y (t :: ts) = coprodRelRefl x y t :: coprodsRelRefl x y ts

  tmRelImpliesCoprodRel : Term.Rel.(~=~) (\i => x.relation i `Or` y.relation i) t tm tm'
    -> (~=~) x y t tm tm'
  tmRelsImpliesCoprodRels : {0 tms, tms' : _ ^ ts}
    -> Pointwise (Term.Rel.(~=~) (\i => x.relation i `Or` y.relation i)) tms tms'
    -> Pointwise ((~=~) x y) tms tms'

  tmRelImpliesCoprodRel (Done (Left eq))  = DoneL eq
  tmRelImpliesCoprodRel (Done (Right eq)) = DoneR eq
  tmRelImpliesCoprodRel (Call op eqs)     = Call op $ tmRelsImpliesCoprodRels eqs

  tmRelsImpliesCoprodRels []          = []
  tmRelsImpliesCoprodRels (eq :: eqs) = tmRelImpliesCoprodRel eq :: tmRelsImpliesCoprodRels eqs

public export
Coproduct : (x, y : RawAlgebra sig) -> RawAlgebra sig
Coproduct x y = MkRawAlgebra
  { U   = CoproductSet sig x.U y.U
  , sem = Call
  }

public export
CoproductIsAlgebra : IsAlgebra sig x -> IsAlgebra sig y
  -> IsAlgebra sig (Coproduct x y)
CoproductIsAlgebra xIsAlgebra yIsAlgebra = MkIsAlgebra
  { equivalence = MkIndexedEquivalence
    { relation   =
        (~=~)
          (MkRawSetoidAlgebra x xIsAlgebra.equivalence.relation)
          (MkRawSetoidAlgebra y yIsAlgebra.equivalence.relation)
    , reflexive  = \_ =>
      coprodRelRefl
        xIsAlgebra.equivalence.reflexive
        yIsAlgebra.equivalence.reflexive
    , symmetric  = \_, _, _ => Sym
    , transitive = \_, _, _, _ => Trans
    }
  , semCong = Call
  }

public export
CoproductAlgebra : (x, y : Algebra sig) -> Algebra sig
CoproductAlgebra x y = MkAlgebra (Coproduct x.raw y.raw) $ CoproductIsAlgebra x.algebra y.algebra

public export
injectLHomo : x ~> CoproductAlgebra x y
injectLHomo = MkHomomorphism
  { func    = MkIndexedSetoidHomomorphism (\_ => Done . Left) (\_, _, _ => DoneL)
  , semHomo = InjectL
  }

public export
injectRHomo : y ~> CoproductAlgebra x y
injectRHomo = MkHomomorphism
  { func    = MkIndexedSetoidHomomorphism (\_ => Done . Right) (\_, _, _ => DoneR)
  , semHomo = InjectR
  }

public export
coproduct : {z : RawAlgebra sig}
  -> (f : (t : sig.T) -> x t -> z.U t) -> (g : (t : sig.T) -> y t -> z.U t)
  -> (t : sig.T) -> CoproductSet sig x y t -> z.U t
coproduct f g _ = bindTerm (\i => either (f i) (g i))

public export
coproducts : {z : RawAlgebra sig}
  -> (f : (t : sig.T) -> x t -> z.U t) -> (g : (t : sig.T) -> y t -> z.U t)
  -> (ts : List sig.T) -> CoproductSet sig x y ^ ts -> z.U ^ ts
coproducts f g _ = bindTerms (\i => either (f i) (g i))

public export
coproductCong : {x, y, z : Algebra sig} -> (f : x ~> z) -> (g : y ~> z)
  -> {t : sig.T} -> {tm, tm' : _} -> (~=~) x.rawSetoid y.rawSetoid t tm tm'
  -> (z.relation t `on` coproduct {z = z.raw} f.func.H g.func.H t) tm tm'

public export
coproductsCong : {x, y, z : Algebra sig} -> (f : x ~> z) -> (g : y ~> z)
  -> {ts : List sig.T} -> {tms, tms' : _ ^ ts}
  -> Pointwise ((~=~) x.rawSetoid y.rawSetoid) tms tms'
  -> (Pointwise z.relation `on` (coproducts {z = z.raw} f.func.H g.func.H ts)) tms tms'

coproductCong f g (DoneL eq)       = f.func.homomorphic _ _ _ eq
coproductCong f g (DoneR eq)       = g.func.homomorphic _ _ _ eq
coproductCong f g (Call op eqs)    = z.algebra.semCong op $ coproductsCong f g eqs
coproductCong f g (InjectL op ts)  = CalcWith (index z.setoid _) $
  |~ f.func.H _ (x.raw.sem op ts)
  ~~ z.raw.sem op (map f.func.H ts)                                                ...(f.semHomo op ts)
  ~~ z.raw.sem op (map (coproduct f.func.H g.func.H) (map (\_ => Done . Left) ts)) .=.(cong (z.raw.sem op) $ mapComp ts)
  ~~ z.raw.sem op (coproducts f.func.H g.func.H _ (map (\_ => Done . Left) ts))    .=<(cong (z.raw.sem op) $ bindTermsIsMap {a = z.raw} _ _)
coproductCong f g (InjectR op ts)  = CalcWith (index z.setoid _) $
  |~ g.func.H _ (y.raw.sem op ts)
  ~~ z.raw.sem op (map g.func.H ts)                                                 ...(g.semHomo op ts)
  ~~ z.raw.sem op (map (coproduct f.func.H g.func.H) (map (\_ => Done . Right) ts)) .=.(cong (z.raw.sem op) $ mapComp ts)
  ~~ z.raw.sem op (coproducts f.func.H g.func.H _ (map (\_ => Done . Right) ts))    .=<(cong (z.raw.sem op) $ bindTermsIsMap {a = z.raw} _ _)
coproductCong f g (Sym eq)         = z.algebra.equivalence.symmetric _ _ _ $ coproductCong f g eq
coproductCong f g (Trans eq eq')   = z.algebra.equivalence.transitive _ _ _ _
  (coproductCong f g eq)
  (coproductCong f g eq')

coproductsCong f g []          = []
coproductsCong f g (eq :: eqs) = coproductCong f g eq :: coproductsCong f g eqs

public export
coproductHomo : {x, y, z : Algebra sig} -> x ~> z -> y ~> z
  -> CoproductAlgebra x y ~> z
coproductHomo f g = MkHomomorphism
  { func = MkIndexedSetoidHomomorphism
    { H           = coproduct {z = z.raw} f.func.H g.func.H
    , homomorphic = \_, _, _ => coproductCong f g
    }
  , semHomo = \op, tms =>
    reflect (index z.setoid _) $
    cong (z.raw.sem op) $
    bindTermsIsMap {a = z.raw} _ tms
  }

public export
termToCoproduct : (x, y : Algebra sig)
  -> FreeAlgebra (bundle (\t => Either (index x.setoid t) (index y.setoid t))) ~>
     CoproductAlgebra x y
termToCoproduct x y = MkHomomorphism
  { func    = MkIndexedSetoidHomomorphism (\_ => id) (\t, _, _ => tmRelImpliesCoprodRel)
  , semHomo = \op, tms =>
    Call op $
    reflect (index (pwSetoid (CoproductAlgebra x y).setoid) _) $
    sym $
    mapId tms
  }

public export
coproductUnique' : {x, y, z : Algebra sig}
  -> (f : CoproductAlgebra x y ~> z)
  -> (g : CoproductAlgebra x y ~> z)
  -> (congL : {t : sig.T} -> (i : x.raw.U t)
        -> z.relation t (f.func.H t (Done (Left i))) (g.func.H t (Done (Left i))))
  -> (congR : {t : sig.T} -> (i : y.raw.U t)
        -> z.relation t (f.func.H t (Done (Right i))) (g.func.H t (Done (Right i))))
  -> {t : sig.T} -> (tm : _)
  -> z.relation t (f.func.H t tm) (g.func.H t tm)
coproductUnique' f g congL congR tm = bindUnique'
  { v = bundle (\t => Either (index x.setoid t) (index y.setoid t))
  , f = f . termToCoproduct x y
  , g = g . termToCoproduct x y
  , cong = \x => case x of
      (Left x) => congL x
      (Right x) => congR x
  , tm = tm
  }

public export
coproductUnique : {x, y, z : Algebra sig} -> (f : x ~> z) -> (g : y ~> z)
  -> (h : CoproductAlgebra x y ~> z)
  -> (congL : {t : sig.T} -> (i : x.raw.U t)
        -> z.relation t (h.func.H t (Done (Left i))) (f.func.H t i))
  -> (congR : {t : sig.T} -> (i : y.raw.U t)
        -> z.relation t (h.func.H t (Done (Right i))) (g.func.H t i))
  -> {t : sig.T} -> (tm : _)
  -> z.relation t (h.func.H t tm) (coproduct {z = z.raw} f.func.H g.func.H t tm)
coproductUnique f g h = coproductUnique' h (coproductHomo f g)