summaryrefslogtreecommitdiff
path: root/src/Soat/SecondOrder/Algebra/Lift.idr
blob: 93043c4a4b84ca4299a7c71f81095b1ec3597eb0 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
module Soat.SecondOrder.Algebra.Lift

import Data.List.Elem
import Data.List.Sublist
import Data.Product
import Data.Setoid.Indexed

import Soat.FirstOrder.Algebra
import Soat.FirstOrder.Term
import Soat.SecondOrder.Algebra
import Soat.SecondOrder.Signature.Lift

import Syntax.PreorderReasoning.Setoid

%default total
%ambiguity_depth 4

public export
project : SecondOrder.Algebra.RawAlgebra (lift sig) -> (ctx : List sig.T)
  -> FirstOrder.Algebra.RawAlgebra sig
project a ctx = MkRawAlgebra
  (\t => a.U t ctx)
  (\op => a.sem ctx (MkOp (Op op.op)) . wrap (MkPair []))

public export
projectAlgebra : (0 sig : _) -> Algebra (lift sig) -> (ctx : List sig.T) -> Algebra sig
projectAlgebra sig a ctx = MkAlgebra
  { raw = project a.raw ctx
  , algebra = MkIsAlgebra
    { equivalence = (reindex (flip MkPair ctx) a.setoid).equivalence
    , semCong     = \op => a.algebra.semCong ctx (MkOp (Op op.op)) . wrapIntro
    }
  }

public export
projectHomo : {a, b : SecondOrder.Algebra.Algebra (lift sig)} -> a ~> b
  -> (ctx : _) -> projectAlgebra sig a ctx ~> projectAlgebra sig b ctx
projectHomo f ctx = MkHomomorphism
  { func    = MkIndexedSetoidHomomorphism
    { H           = \t => f.func.H (t, ctx)
    , homomorphic = \t => f.func.homomorphic (t, ctx)
    }
  , semHomo = \op, tms => CalcWith (index b.setoid _) $
    |~ f.func.H (_, ctx) (a.raw.sem ctx (MkOp (Op op.op)) (wrap (MkPair []) tms))
    ~~ b.raw.sem ctx (MkOp (Op op.op)) (map (\ty => f.func.H (snd ty, fst ty ++ ctx)) (wrap (MkPair []) tms))
      ...(f.semHomo ctx (MkOp (Op op.op)) (wrap (MkPair []) tms))
    ~~ b.raw.sem ctx (MkOp (Op op.op)) (wrap (MkPair []) (map (\t => f.func.H (t, ctx)) tms))
      .=.(cong (b.raw.sem ctx (MkOp (Op op.op))) $
          mapWrap (MkPair []) {f = \ty => f.func.H (snd ty, fst ty ++ ctx)} tms)
  }

public export
(.renameHomo) : (a : SecondOrder.Algebra.Algebra (lift sig)) -> {ctx, ctx' : _}
  -> (f : ctx `Sublist` ctx')
  -> projectAlgebra sig a ctx ~> projectAlgebra sig a ctx'
(.renameHomo) a f = MkHomomorphism
  { func    = MkIndexedSetoidHomomorphism
    { H           = \t => a.raw.rename t f
    , homomorphic = \t, _, _ => a.algebra.renameCong t f
    }
  , semHomo = \op, tms => CalcWith (index a.setoid _) $
    |~ a.raw.rename _ f (a.raw.sem _ (MkOp (Op op.op)) (wrap (MkPair []) tms))
    ~~ a.raw.sem _ (MkOp (Op op.op)) (map (\ty => a.raw.rename (snd ty) (reflexive {x = fst ty} ++ f)) (wrap (MkPair []) tms))
      ...(a.algebra.semNat f (MkOp (Op op.op)) (wrap (MkPair []) tms))
    ~~ a.raw.sem _ (MkOp (Op op.op)) (wrap (MkPair []) (map (\t => a.raw.rename t f) tms))
      ...(a.algebra.semCong _ (MkOp (Op op.op)) $
          CalcWith (index (pwSetoid (reindex (\ty => (snd ty, fst ty ++ _)) a.setoid)) _) $
          |~ map (\ty => a.raw.rename (snd ty) (reflexive {x = fst ty} ++ f)) (wrap (MkPair []) tms)
          ~~ wrap (MkPair []) (map (\t => a.raw.rename t (reflexive {x = []} ++ f)) tms)
            .=.(mapWrap (MkPair []) tms)
          ~~ wrap (MkPair []) (map (\t => a.raw.rename t f) tms)
            ...(wrapIntro $
                mapIntro'' (\t, tm, tm', eq =>
                  CalcWith (index a.setoid (t, _)) $
                  |~ a.raw.rename t (reflexive {x = []} ++ f) tm
                  ~~ a.raw.rename t f tm
                    .=.(cong (\f => a.raw.rename t f tm) $ uncurryCurry f)
                  ~~ a.raw.rename t f tm'
                    ...(a.algebra.renameCong t f eq)) $
                (pwSetoid (a.setoidAt ctx)).equivalence.reflexive _ tms))
  }

public export
(.substHomo1) : (a : SecondOrder.Algebra.Algebra (lift sig)) -> (ctx : List sig.T)
  -> {ctx' : List sig.T} -> (tms : (\t => a.raw.U t ctx) ^ ctx')
  -> projectAlgebra sig a ctx' ~> projectAlgebra sig a ctx
(.substHomo1) a ctx tms = MkHomomorphism
  { func    = MkIndexedSetoidHomomorphism
    { H           = \t, tm => a.raw.subst t ctx tm tms
    , homomorphic = \t, _, _, eq =>
      a.algebra.substCong t ctx eq $
      (pwSetoid (a.setoidAt _)).equivalence.reflexive _ tms
    }
  , semHomo = \op, tms' => CalcWith (index a.setoid _) $
    |~ a.raw.subst _ ctx (a.raw.sem ctx' (MkOp (Op op.op)) (wrap (MkPair []) tms')) tms
    ~~ a.raw.sem ctx (MkOp (Op op.op)) (map (a.raw.extendSubst ctx tms) (wrap (MkPair []) tms'))
      ...(a.algebra.substCompat ctx (MkOp (Op op.op)) (wrap (MkPair []) tms') tms)
    ~~ a.raw.sem ctx (MkOp (Op op.op)) (wrap (MkPair []) (map (\t, tm => a.raw.subst t ctx tm tms) tms'))
      ...(a.algebra.semCong ctx (MkOp (Op op.op)) $
          CalcWith (index (pwSetoid (reindex (\ty => (snd ty, fst ty ++ ctx)) a.setoid)) _) $
          |~ map (a.raw.extendSubst ctx tms) (wrap (MkPair []) tms')
          ~~ wrap (MkPair []) (map (\t, tm => a.raw.subst t ctx tm (map (\t => a.raw.rename t ([] {ys = []} ++ reflexive)) tms)) tms')
            .=.(mapWrap (MkPair []) tms')
          ~~ wrap (MkPair []) (map (\t, tm => a.raw.subst t ctx tm tms) tms')
            ...(wrapIntro $
                mapIntro' (\t, eq =>
                  a.algebra.substCong t ctx eq $
                  CalcWith (index (pwSetoid (a.setoidAt _)) _) $
                  |~ map (\t => a.raw.rename t ([] {ys = []} ++ reflexive)) tms
                  ~~ map (\t => id) tms
                    ...(mapIntro'' (\t, tm, tm', eq =>
                          CalcWith (index a.setoid (t, ctx)) $
                          |~ a.raw.rename t ([] {ys = []} ++ reflexive) tm
                          ~~ a.raw.rename t reflexive tm
                            .=.(cong (\f => a.raw.rename t f tm) $ uncurryCurry reflexive)
                          ~~ tm
                            ...(a.algebra.renameId t ctx tm)
                          ~~ tm'
                            ...(eq)) $
                        (pwSetoid (a.setoidAt _)).equivalence.reflexive _ _)
                  ~~ tms
                    .=.(mapId tms)) $
                (pwSetoid (a.setoidAt _)).equivalence.reflexive _ _))
  }

-- renameBodyFunc : (f : ctx `Sublist` ctx')
--   -> irrelevantCast (flip Elem ctx) ~>
--      (FreeAlgebra {sig = sig} (irrelevantCast (flip Elem ctx'))).setoid
-- renameBodyFunc f = mate (\_ => Done . curry f)

-- indexFunc : {ctx : List sig.T} -> (tms : Term sig (flip Elem ctx) ^ ts)
--   -> irrelevantCast (flip Elem ts) ~>
--      (FreeAlgebra {sig = sig} (irrelevantCast (flip Elem ctx))).setoid
-- indexFunc tms = mate (\_ => index tms)

freeAlg : List sig.T -> FirstOrder.Algebra.Algebra sig
freeAlg ctx = FreeAlgebra (irrelevantCast (flip Elem ctx))

public export
Initial : (0 sig : _) -> SecondOrder.Algebra.RawAlgebra (lift sig)
Initial sig = MkRawAlgebra
  (\t, ctx => (freeAlg ctx).raw.U t)
  (\t, f => bindTerm {a = Free _} (\_ => Done . curry f))
  (\_, (MkOp (Op op)) => Call (MkOp op) . unwrap (MkPair []))
  Done
  (\_, _, t, ts => bindTerm {a = Free _} (\_ => index ts) t)

public export
InitialIsAlgebra : (0 sig : _) -> SecondOrder.Algebra.IsAlgebra (lift sig) (Initial sig)
InitialIsAlgebra sig = MkIsAlgebra
  { equivalence = MkIndexedEquivalence
    { relation   = \(t, ctx) => (freeAlg ctx).relation t
    , reflexive  = \(t, ctx) => (freeAlg ctx).algebra.equivalence.reflexive t
    , symmetric  = \(t, ctx) => (freeAlg ctx).algebra.equivalence.symmetric t
    , transitive = \(t, ctx) => (freeAlg ctx).algebra.equivalence.transitive t
    }
  , renameCong  = \t, f => bindTermCong
    { a   = freeAlg _
    , env = mate (\_ => Done . curry f)
    }
  , semCong     = \_ , (MkOp (Op op)) => Call (MkOp op) . unwrapIntro
  , substCong   = \_, _, eq, eqs => bindTermCong'
    { a    = freeAlg _
    , cong = \_, Refl => index eqs _
    , eq
    }
  , renameId    = \t, ctx, tm =>
    (freeAlg _).setoid.equivalence.symmetric t _ _ $
    bindUnique (mate (\_ => Done . curry reflexive)) id (\i => Done $ sym $ curryUncurry id i) tm
  , renameComp  = \t, f, g, tm =>
    (freeAlg _).setoid.equivalence.symmetric t _ _ $
    bindUnique
      { a    = freeAlg _
      , env  = mate (\_ => Done . curry (transitive g f))
      , f    = bindHomo (mate (\_ => Done . curry f)) . bindHomo (mate (\_ => Done . curry g))
      , cong = \i => Done $ sym $ curryUncurry (curry f . curry g) i
      , tm
      }
  , semNat      = \f, (MkOp (Op op)), tms =>
    Call (MkOp op) $
    CalcWith (index (pwSetoid (freeAlg _).setoid) _) $
    |~ bindTerms {a = Free _} (\_ => Done . curry f) (unwrap (MkPair []) tms)
    ~~ map (\_ => bindTerm {a = Free _} (\_ => Done . curry f)) (unwrap (MkPair []) tms)
      .=.(bindTermsIsMap {a = Free _} _ _)
    ~~ map (\_ => bindTerm {a = Free _} (\_ => Done . curry (reflexive {x = []} ++ f))) (unwrap (MkPair []) tms)
      ..<(mapIntro' (\t =>
            bindTermCong'
              {rel = \_ => Equal}
              {a = freeAlg _}
              (\_, Refl => Done $ curryUncurry (curry f) _)) $
          (pwSetoid (freeAlg _).setoid).equivalence.reflexive _ (unwrap (MkPair []) tms))
    ~~ unwrap (MkPair []) (map (\ty => bindTerm {a = Free _} (\_ => Done . curry (reflexive {x = fst ty} ++ f))) tms)
      .=.(mapUnwrap (MkPair []) tms)
  , varNat      = \_, _ => Done Refl
  , substNat    = \t, f, tm, tms => bindUnique
    { a = freeAlg _
    , env = mate (\_ => index $ map (\_ => bindTerm {a = Free _} (\_ => Done . curry f)) tms)
    , f   = bindHomo (mate (\_ => Done . curry f)) . bindHomo (mate (\_ => index tms))
    , cong = \i =>
      reflect (index (freeAlg _).setoid _) $
      sym $
      indexMap tms i
    , tm
    }
  , substExnat  = \t, ctx, f, tm, tms => bindUnique
    { a    = freeAlg _
    , env  = mate (\_ => index $ shuffle f tms)
    , f    = bindHomo (mate (\_ => index tms)) . bindHomo (mate (\_ => Done . curry f))
    , cong = \i =>
      reflect (index (freeAlg _).setoid _) $
      sym $
      indexShuffle f i
    , tm
    }
  , substComm   = \t, ctx, tm, tms, tms' => bindUnique
    { a    = freeAlg _
    , env  = mate (\_ => index $ map (\_ => bindTerm {a = Free _} (\_ => index tms')) tms)
    , f    = bindHomo (mate (\_ => index tms')) . bindHomo (mate (\_ => index tms))
    , cong = \i =>
      reflect (index (freeAlg _).setoid _) $
      sym $
      indexMap tms i
    , tm
    }
  , substVarL   = \_, _, _ => (freeAlg _).setoid.equivalence.reflexive _ _
  , substVarR   = \t, ctx, tm =>
    (freeAlg _).setoid.equivalence.symmetric t _ _ $
    bindUnique
      { v    = irrelevantCast (flip Elem ctx)
      , a    = freeAlg ctx
      , env  = mate (\_ => index $ tabulate (Done {sig = sig, v = flip Elem ctx}))
      , f    = id
      , cong = \i =>
        reflect (index (freeAlg ctx).setoid _) $
        sym $
        indexTabulate Done i
      , tm
      }
  , substCompat = \ctx, (MkOp (Op op)), tms, tms' =>
    Call (MkOp op) $
    CalcWith (index (pwSetoid (freeAlg _).setoid) _) $
    |~ bindTerms {a = Free _} (\_ => index tms') (unwrap (MkPair []) tms)
    ~~ map (\_ => bindTerm {a = Free _} (\_ => index tms')) (unwrap (MkPair []) tms)
      .=.(bindTermsIsMap {a = Free _} _ _)
    ~~ map (\t => (Initial sig).extendSubst ctx tms' ([], t)) (unwrap (MkPair []) tms)
      ..<(mapIntro' (\t => bindTermCong'
            {rel = \_ => Equal}
            {a = freeAlg _}
            (\t, Refl => CalcWith (index (freeAlg _).setoid _) $
              |~ index (map (\_ => bindTerm {a = Free _} (\_ => Done . curry ([] {ys = []} ++ reflexive))) tms') _
              ~~ bindTerm {a = Free _} (\_ => Done . curry ([] {ys = []} ++ reflexive)) (index tms' _)
                .=.(indexMap tms' _)
              ~~ index tms' _
                ..<(bindUnique
                      { env  = mate (\_ => Done . curry ([] {ys = []} ++ reflexive))
                      , f    = id
                      , cong = \i => Done $ sym $ trans (curryUncurry _ i) (curryUncurry id i)
                      , tm   = index tms' _
                      }))) $
          (pwSetoid (freeAlg _).setoid).equivalence.reflexive _ (unwrap (MkPair []) tms))
    ~~ unwrap (MkPair []) (map ((Initial sig).extendSubst ctx tms') tms)
      .=.(mapUnwrap (MkPair []) tms)
  }

public export
InitialAlgebra : (0 sig : _) -> SecondOrder.Algebra.Algebra (lift sig)
InitialAlgebra sig = MkAlgebra (Initial sig) (InitialIsAlgebra sig)

public export
freeToInitialHomo : (0 sig : _) -> (ctx : List sig.T)
  -> FreeAlgebra (irrelevantCast (flip Elem ctx)) ~>
     projectAlgebra sig (InitialAlgebra sig) ctx
freeToInitialHomo sig ctx = MkHomomorphism
  { func    = MkIndexedSetoidHomomorphism
    { H           = \_ => id
    , homomorphic = \_, _, _ => id
    }
  , semHomo = \(MkOp op), tms =>
    Call (MkOp op) $
    reflect (index (pwSetoid ((InitialAlgebra sig).setoidAt ctx)) _) $
    sym $
    trans (unwrapWrap (extend (Initial sig).U ctx) _) (mapId tms)
  }

public export
fromInitial : (a : SecondOrder.Algebra.RawAlgebra (lift sig)) -> (t : sig.T) -> (ctx : List sig.T)
  -> (Initial sig).U t ctx -> a.U t ctx
fromInitial a t ctx = bindTerm {a = project a ctx} (\_ => a.var)

public export
fromInitialHomo : (a : Algebra (lift sig)) -> InitialAlgebra sig ~> a
fromInitialHomo a = MkHomomorphism
  { func       = MkIndexedSetoidHomomorphism
    { H           = \(t, ctx) => fromInitial a.raw t ctx
    , homomorphic = \(t, ctx), _, _ => bindTermCong {a = projectAlgebra sig a ctx} (a.varFunc ctx)
    }
  , renameHomo = \t, f => bindUnique'
    {v = irrelevantCast (flip Elem _)}
    {a = projectAlgebra sig a _}
    (bindHomo (a.varFunc _) . bindHomo (mate (\_ => Done . curry f)))
    (a.renameHomo f . bindHomo (a.varFunc _))
    (\i => a.algebra.equivalence.symmetric _ _ _ $ a.algebra.varNat f i)
  , semHomo    = \ctx, (MkOp (Op op)), tms =>
    a.algebra.semCong ctx (MkOp (Op op)) $
    CalcWith (index (pwSetoid (reindex (\ty => (snd ty, fst ty ++ ctx)) a.setoid)) _) $
    |~ wrap (MkPair []) (bindTerms {a = project a.raw ctx} (\_ => a.raw.var) (unwrap (MkPair []) tms))
    ~~ wrap (MkPair []) (map (\t => fromInitial a.raw t ctx) (unwrap (MkPair []) tms))
      .=.(cong (wrap _) $ bindTermsIsMap {a = project a.raw ctx} _ _)
    ~~ wrap (MkPair []) (unwrap (MkPair []) (map (\ty => fromInitial a.raw (snd ty) (fst ty ++ ctx)) tms))
      .=.(cong (wrap _) $ mapUnwrap (MkPair []) tms)
    ~~ map (\ty => fromInitial a.raw (snd ty) (fst ty ++ ctx)) tms
      .=.(wrapUnwrap _)
  , varHomo    = \i => a.algebra.equivalence.reflexive _ $ a.raw.var i
  , substHomo  = \t, ctx, tm, tms => bindUnique'
    {v = irrelevantCast (flip Elem _)}
    {a = projectAlgebra sig a _}
    (bindHomo (a.varFunc _) . bindHomo (mate (\_ => index tms)))
    (a.substHomo1 ctx _ . bindHomo (a.varFunc _))
    (\i => CalcWith (index a.setoid _) $
        |~ bindTerm {a = project a.raw _} (\_ => a.raw.var) (index tms i)
        ~~ index (map (\_ => bindTerm {a = project a.raw _} (\_ => a.raw.var)) tms) i
          .=<(indexMap {f = \_ => bindTerm {a = project a.raw _} (\_ => a.raw.var)} tms i)
        ~~ a.raw.subst _ ctx (a.raw.var i) (map (\_ => bindTerm {a = project a.raw _} (\_ => a.raw.var)) tms)
          ..<(a.algebra.substVarL ctx i _))
    tm
  }

public export
fromInitialUnique : {a : SecondOrder.Algebra.Algebra (lift sig)}
  -> (f : InitialAlgebra sig ~> a)
  -> (t : sig.T) -> (ctx : List sig.T) -> (tm : Term sig (flip Elem ctx) t)
  -> a.relation (t, ctx) (f.func.H (t, ctx) tm) (fromInitial a.raw t ctx tm)
fromInitialUnique {sig = sig} {a = a} f t ctx = bindUnique
  {v = irrelevantCast (flip Elem _)}
  {a = projectAlgebra sig a ctx}
  (a.varFunc ctx)
  (projectHomo f ctx . freeToInitialHomo sig ctx)
  f.varHomo