Repository navigation
Expand file tree
/
Copy pathNotation.lean
More file actions
614 lines (552 loc) · 30.7 KB
/
Copy pathNotation.lean
File metadata and controls
614 lines (552 loc) · 30.7 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
module
public import ConstructiveCryptography.Notation.Basic
public import RandomSystems.Typed.ParallelAllAttachment
public import RandomSystems.Typed.ParallelAllContext
public import RandomSystems.Typed.SampleLaws
public import ConstructiveCryptography.Instances.Algebra
public import ConstructiveCryptography.CryptographicAlgebra.Game
public import ConstructiveCryptography.Tactics.Categorical
public meta import Mathlib.Util.DelabNonCanonical
/-!
# Notation frontend for interfaces
The notation in `ConstructiveCryptography.Notation.Basic` uses the selected abstract
instances. This frontend also accepts direct typed systems and converters in `Δ`, `∥`,
`≫` and `⊗ₘ`, inferring their interfaces and budgets. It preserves any selected alternative
metric or composition. Direct typed operations print in the same notation when the selected
instances agree.
`cc_normalize` applies the serial, parallel, projection and sampling laws:
* `(α ⊗ₘ β) • (R ∥ S) = α • R ∥ β • S`;
* `TypedConverter.projectLeft A B • (R ∥ S) = R` and
`TypedConverter.projectRight A B • (R ∥ S) = S`;
* `α • sample P R = sample P (fun s => α • R s)` for normalized `P`;
* `(⨂ₘ k, α k) • (∥ k, R k) = ∥ k, α k • R k`.
A retained draw stays shared, including when later sample laws depend on it:
```text
sample s ← P; ∥ k, R s k ≠ ∥ k, (sample s ← P; R s k) in general
```
`TypedConverter.parallelAllContext_smul` isolates one component:
`parallelAllContext R k • S = ∥ j, Function.update R k S j`.
The context keeps the fixed components and their declared domains. Further component
equalities remain explicit premises.
## References
1. U. Maurer and R. Renner. From Indifferentiability to Constructive Cryptography (and Back).
TCC 2016-B. Cryptology ePrint Archive, Paper 2016/903.
-/
@[expose] public section
namespace SystemAlgebra
open CategoryTheory CategoryTheory.MonoidalCategory
/-- Serial composition with the same right-associative binding power as categorical `≫`. -/
scoped syntax:80 (name := ccComposition) (priority := high) term:81 " ≫ " term:80 : term
/-- Tensor of arrows with the same right-associative binding power as Mathlib's `⊗ₘ`. -/
scoped syntax:70 (name := ccTensorHom) (priority := high) term:71 " ⊗ₘ " term:70 : term
open Lean Elab Term in
/-- Rewrite directly nested `≫` nodes, including parenthesized ones, to `CategoryStruct.comp`.
One generic attempt covers that chain without re-entering this handler; other operands keep
their syntax. -/
meta partial def ccCompositionGenericSyntax (stx : TSyntax `term) : TermElabM (TSyntax `term) := do
match stx with
| `($α ≫ $β) =>
let α ← ccCompositionGenericSyntax α
let β ← ccCompositionGenericSyntax β
`(CategoryTheory.CategoryStruct.comp $α $β)
| `(($term)) =>
let term ← ccCompositionGenericSyntax term
`(($term))
| _ => return stx
open Lean Elab Term in
/-- Rewrite directly nested `⊗ₘ` nodes, including parenthesized ones, to
`MonoidalCategoryStruct.tensorHom`, as `ccCompositionGenericSyntax` does for `≫`. -/
meta partial def ccTensorGenericSyntax (stx : TSyntax `term) : TermElabM (TSyntax `term) := do
match stx with
| `($α ⊗ₘ $β) =>
let α ← ccTensorGenericSyntax α
let β ← ccTensorGenericSyntax β
`(CategoryTheory.MonoidalCategoryStruct.tensorHom $α $β)
| `(($term)) =>
let term ← ccTensorGenericSyntax term
`(($term))
| _ => return stx
open Lean Meta Elab Term in
/-- Return `some true` for a `Quiver.Hom` over objects other than `Interface`, `some false`
for `Interface` or a non-morphism type, and `none` while the head or object type is unresolved.
Unfold aliases only up to `Quiver.Hom`, before its field reduces to the converter carrier. -/
meta def ccCompositionHasOtherCategory (type : Expr) : MetaM (Option Bool) := do
let mut type := (← instantiateMVars type).consumeMData
while !type.isAppOf ``Quiver.Hom do
if type.getAppFn.isMVar then return none
let some unfolded ← unfoldDefinition? type | return some false
type := unfolded.consumeMData
let obj ← whnf type.getAppArgs[0]!
if obj.getAppFn.isMVar then return none
return some (!obj.isConstOf ``Interface)
open Lean Meta Elab Term in
/-- Inspect the category selected by an operand alone, restoring all state afterward.
The first pass reads selections with pending endpoint capabilities; the second synthesizes
postponed types. An operand that fails alone selects nothing (`some false`). -/
meta def ccCompositionOperandHasOtherCategory
(stx : TSyntax `term) : TermElabM (Option Bool) := do
let saved ← saveState
let generic ← ccCompositionGenericSyntax stx
try
try
let selected ← withoutErrToSorry do
let term ← elabTerm generic none
ccCompositionHasOtherCategory (← inferType term)
if selected == some true then return selected
catch _ => pure ()
saved.restore (restoreInfo := true)
withoutErrToSorry <| withSynthesize do
let term ← elabTerm generic none
ccCompositionHasOtherCategory (← inferType term)
catch _ =>
return some false
finally
saved.restore (restoreInfo := true)
open Lean Elab Term in
/-- Elaborate a binary operation on arrows `α`, `β`. `generic` is the operation of whatever
category and instances the operands and `expectedType?` select. `typed` is the same operation
with only its category fixed to `Interface` and both operands read as `TypedConverter _ _`.
The invariant: `typed` is tried only after `generic` fails, and only if neither
`expectedType?` nor an operand selects another category. Since `typed` names no instance,
composition and tensor remain those in scope.
* The first generic attempt finishes synthesis, so it fails only when this reading fails.
* Every later attempt starts from the saved state, so a discarded reading leaves no
metavariable assignment or postponed problem behind.
* A selected category keeps the generic reading.
* While the category is unresolved, a generic reading that exposes a category is kept;
otherwise elaboration postpones, because a later argument may still select a category.
* The typed reading leaves endpoint constraints to the enclosing expression.
* If every attempt fails at once, the first generic error is reported. -/
meta def elabTypedCCArrows
-- `generic`, `typed` are the two readings of the operation on operands `α`, `β`.
(generic typed α β : TSyntax `term) (expectedType? : Option Expr) : TermElabM Expr := do
let saved ← saveState
try
withoutErrToSorry <| withSynthesize do
elabTerm generic expectedType?
catch genericError =>
saved.restore (restoreInfo := true)
let genericOnly : TermElabM Expr := do
try
withoutErrToSorry <| elabTerm generic expectedType?
catch _ =>
saved.restore (restoreInfo := true)
throw genericError
let expectedSelection ← match expectedType? with
| some type => ccCompositionHasOtherCategory type
| none => pure (some false)
let leftSelection ← ccCompositionOperandHasOtherCategory α
let rightSelection ← ccCompositionOperandHasOtherCategory β
saved.restore (restoreInfo := true)
if expectedSelection == some true || leftSelection == some true ||
rightSelection == some true then
genericOnly
else
if expectedSelection.isNone || leftSelection.isNone || rightSelection.isNone then
-- Keep the generic category visible while endpoint constraints wait.
try
let term ← withoutErrToSorry <| elabTerm generic expectedType?
if (← ccCompositionHasOtherCategory (← Lean.Meta.inferType term)).isSome then
return term
catch _ => pure ()
saved.restore (restoreInfo := true)
-- A later argument may still select the category before typed hints are considered.
tryPostpone
-- No other category is selected: read both operands as direct converters in Interface.
try
withoutErrToSorry <| elabTerm typed expectedType?
catch _ =>
saved.restore (restoreInfo := true)
throw genericError
open Lean Elab Term in
/-- `α ≫ β` by `elabTypedCCArrows`: composition in the selected category, or composition in
`Interface` of direct converters when no other category is selected. -/
@[term_elab ccComposition]
meta def elabTypedCCComposition : TermElab
-- `α`, `β` are the serial operands; `expectedType?` may determine their outside endpoints.
| `($α ≫ $β), expectedType? => do
elabTypedCCArrows (← ccCompositionGenericSyntax (← `($α ≫ $β)))
(← `(CategoryTheory.CategoryStruct.comp (obj := SystemAlgebra.Interface)
($α : TypedConverter _ _) ($β : TypedConverter _ _))) α β expectedType?
| _, _ => throwUnsupportedSyntax
open Lean Elab Term in
/-- `α ⊗ₘ β` by `elabTypedCCArrows`: the tensor of the selected monoidal structure, or, when no
other category is selected, the tensor in `Interface` of direct converters. -/
@[term_elab ccTensorHom]
meta def elabTypedCCTensorHom : TermElab
-- `α`, `β` are the tensor operands; `expectedType?` may determine their endpoints.
| `($α ⊗ₘ $β), expectedType? => do
elabTypedCCArrows (← ccTensorGenericSyntax (← `($α ⊗ₘ $β)))
(← `(CategoryTheory.MonoidalCategoryStruct.tensorHom (C := SystemAlgebra.Interface)
($α : TypedConverter _ _) ($β : TypedConverter _ _))) α β expectedType?
| _, _ => throwUnsupportedSyntax
open Lean Elab Term in
/-- Elaborate `operation (R : TypedSystem _) (S : TypedSystem _)` for a binary CC operation
(`distance` or `parallel`), the two `_` being independent interfaces. The carrier is then
known before attachments in `R`, `S` are elaborated, so their outside interfaces are inferred.
Elaboration errors are not turned into `sorry`, so a failure is visible here; the saved state
is then restored and the syntax is passed on to the generic handler. The result is the
selected `operation` itself; the hints choose no metric, action or parallel instance. -/
meta def elabTypedCCOperands (operation R S : TSyntax `term)
(expectedType? : Option Expr) : TermElabM Expr := do
let saved ← saveState
try
withoutErrToSorry <| withSynthesize do
elabTerm (← `($operation
($R : TypedSystem _) ($S : TypedSystem _))) expectedType?
catch _ =>
saved.restore
throwUnsupportedSyntax
open Lean Elab Term in
/-- `Δ R S` with typed carrier hints (`elabTypedCCOperands`); the metric is the selected one. -/
@[term_elab ccDistance]
meta def elabTypedCCDistance : TermElab
| `(Δ $R $S), expectedType? => do
elabTypedCCOperands (← `(ConstructiveCryptography.CryptographicAlgebra.distance))
R S expectedType?
| _, _ => throwUnsupportedSyntax
open Lean Elab Term in
/-- `R ∥ S` with typed carrier hints (`elabTypedCCOperands`), one interface per ordered
component; the parallel operation is the selected one. -/
@[term_elab ccParallel]
meta def elabTypedCCParallel : TermElab
| `($R ∥ $S), expectedType? => do
elabTypedCCOperands (← `(ConstructiveCryptography.CryptographicAlgebra.parallel))
R S expectedType?
| _, _ => throwUnsupportedSyntax
open Lean Meta PrettyPrinter.Delaborator in
/-- Include local class-valued declarations when checking whether displayed notation is canonical.
Pretty-printing can run with a local context whose instance table has not been restored. -/
meta def withCCTypedDisplayInstances {α : Type} (action : DelabM α) : DelabM α := do
let declarations := (← getLCtx).foldl (fun acc declaration => declaration :: acc) []
withLocalInstances declarations.reverse action
open Lean Meta PrettyPrinter.Delaborator SubExpr in
/-- Print a direct typed binary operation only when the notation reads the same selected
instances and neither operand selects a different category. -/
meta def delabTypedCCBinary (arity : Nat) (instances : Array Name)
(render : Term → Term → DelabM Term) : Delab :=
whenPPOption Lean.getPPNotation <| whenNotPPOption Lean.getPPExplicit <|
withOverApp arity <| withCCTypedDisplayInstances do
for instanceName in instances do
guard (← isCanonicalInstance (mkConst instanceName))
for index in #[arity - 2, arity - 1] do
let type ← withNaryArg index do inferType (← getExpr)
guard ((← ccCompositionHasOtherCategory type) == some false)
render (← withNaryArg (arity - 2) delab) (← withNaryArg (arity - 1) delab)
open Lean Meta PrettyPrinter.Delaborator SubExpr in
/-- Direct typed serial composition displays as `α ≫ β` under the canonical category. -/
@[delab app.SystemAlgebra.TypedConverter.comp]
meta def delabTypedCCComposition : Delab := withCCTypedDisplayInstances do
let categoryStruct ← mkAppOptM ``CategoryTheory.Category.toCategoryStruct
#[some (mkConst ``Interface), some (mkConst ``Interface.category)]
guard (← isCanonicalInstance categoryStruct)
delabTypedCCBinary 5 #[``Interface.category] fun α β => `($α ≫ $β)
open Lean PrettyPrinter.Delaborator in
/-- Direct typed converter parallel displays as `α ⊗ₘ β` under the selected canonical tensor. -/
@[delab app.SystemAlgebra.TypedConverter.parallel]
meta def delabTypedCCTensor : Delab :=
delabTypedCCBinary 6 #[``Interface.category, ``Interface.monoidalStruct]
fun α β => `($α ⊗ₘ $β)
open Lean PrettyPrinter.Delaborator in
/-- Direct typed system parallel displays as `R ∥ S` when the selected algebra agrees. -/
@[delab app.SystemAlgebra.TypedSystem.parallel]
meta def delabTypedCCSystemParallel : Delab :=
delabTypedCCBinary 4 #[``Interface.category, ``Interface.monoidal,
``Interface.resourceTheory, ``Interface.cryptographicAlgebra] fun R S => `($R ∥ $S)
open Lean Meta PrettyPrinter.Delaborator SubExpr in
/-- The direct identity displays as `𝟙 A` when the selected category structure agrees. -/
@[delab app.SystemAlgebra.TypedConverter.id]
meta def delabTypedCCIdentity : Delab :=
whenPPOption Lean.getPPNotation <| whenNotPPOption Lean.getPPExplicit <|
withOverApp 1 <| withCCTypedDisplayInstances do
let categoryStruct ← mkAppOptM ``CategoryTheory.Category.toCategoryStruct
#[some (mkConst ``Interface), some (mkConst ``Interface.category)]
guard (← isCanonicalInstance categoryStruct)
let A ← withNaryArg 0 delab
`(𝟙 $A)
open Lean PrettyPrinter.Delaborator in
/-- Direct binary interfaces print as `A ⊗ B` when the selected tensor is canonical. -/
@[delab app.SystemAlgebra.Interface.tensor]
meta def delabTypedCCInterfaceTensor : Delab :=
delabTypedCCBinary 2 #[``Interface.category, ``Interface.monoidalStruct]
fun A B => `($A ⊗ $B)
open Lean Meta PrettyPrinter.Delaborator SubExpr in
/-- Direct attachment prints as `α • R` only when that notation selects the same action. -/
@[delab app.SystemAlgebra.TypedConverter.attach]
meta def delabTypedCCAttachment : Delab :=
whenPPOption Lean.getPPNotation <| whenNotPPOption Lean.getPPExplicit <|
withOverApp 4 <| withCCTypedDisplayInstances do
let expression ← getExpr
let arguments := expression.getAppArgs
let converterType ← inferType arguments[2]!
let resourceType ← inferType arguments[3]!
let resultType ← inferType expression
let actionType ← mkAppM ``HSMul #[converterType, resourceType, resultType]
let action ← synthInstance actionType
let applied ← mkAppOptM ``HSMul.hSMul
#[some converterType, some resourceType, some resultType, some action,
some arguments[2]!, some arguments[3]!]
guard (← isDefEq applied expression)
let α ← withNaryArg 2 delab
let R ← withNaryArg 3 delab
`($α • $R)
namespace Interface
attribute [cc_normalization]
comp_smul
identity_attach
associator_hom_attach
parallel_assoc
leftUnitor_hom_attach
parallel_dummy_left
rightUnitor_hom_attach
parallel_dummy_right
whiskerLeft_smul
whiskerRight_smul
whisker_smul_comm
-- Definitional bridges, below the laws' default priority: the categorical action and the
-- `resourceTheory` attachment field left by `cc_construct` are rewritten to `α • R` with
-- `α : TypedConverter A B`, and abstract `parallel`/`dummy` to `TypedSystem.parallel`/`dummy`,
-- where the typed laws below apply.
attribute [cc_normalization 100]
smul_eq_typed_smul attach_eq_typed_smul cc_parallel_eq cc_dummy_eq
-- The typed laws: `(α ≫ β) • R = α • (β • R)`, `id • R = R`, binary and finite-family
-- locality, and `α • sample P R = sample P (fun s => α • R s)`. None moves an initial sample
-- into independent component draws, so a draw around the whole assembly stays shared.
attribute [cc_normalization]
TypedConverter.comp_smul
TypedConverter.identity_attach
TypedConverter.parallel_attach
TypedConverter.parallelAll_attach
TypedConverter.parallelAllContext_smul
TypedConverter.projectLeft_smul_parallel
TypedConverter.projectRight_smul_parallel
TypedSystem.sample_const
-- Hoist samples after structural attachment, so serial composition exposes a component's owner.
attribute [cc_normalization 900] TypedConverter.smul_sample
lemma tensor_smul {A B C D : Interface} (α : A ⟶ B) (β : C ⟶ D)
(R : TypedSystem B) (S : TypedSystem D) :
(α ⊗ₘ β) • (R ∥ S) = (α • R) ∥ (β • S) :=
ConstructiveCryptography.CryptographicAlgebra.attach_parallel
α β R S
open Lean Meta Simp in
/-- `tensor_smul` whatever the spelling of the interfaces: the action of `α ⊗ₘ β` on `R ∥ S`,
with its types written through `⊗` or through `Interface.tensor`, is `α • R ∥ β • S`. The
two spellings agree only up to unfolding the monoidal structure, which `simp` does not do, so
the rule is keyed on the action alone and matches its arguments here. -/
simproc [cc_normalization] tensorSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr MonoidalCategoryStruct.tensorHom _ _ _ A B C D α β := f | return .continue
let_expr TypedSystem.parallel _ _ R S := x | return .continue
let proof := mkAppN (mkConst ``tensor_smul) #[A, B, C, D, α, β, R, S]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `projectLeft_smul_parallel` whatever the spelling of the interfaces: the action of
`TypedConverter.projectLeft A B` on `R ∥ S`, with types written through `⊗` or `Interface.tensor`,
returns `R`. -/
simproc [cc_normalization] projectLeftSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr TypedConverter.projectLeft A B := f | return .continue
let_expr TypedSystem.parallel _ _ R S := x | return .continue
let proof := mkAppN (mkConst ``TypedConverter.projectLeft_smul_parallel) #[A, B, R, S]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `projectRight_smul_parallel` whatever the spelling of the interfaces: the action of
`TypedConverter.projectRight A B` on `R ∥ S`, with types written through `⊗` or `Interface.tensor`,
returns `S`. -/
simproc [cc_normalization] projectRightSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr TypedConverter.projectRight A B := f | return .continue
let_expr TypedSystem.parallel _ _ R S := x | return .continue
let proof := mkAppN (mkConst ``TypedConverter.projectRight_smul_parallel) #[A, B, R, S]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
lemma distance_smul_le {A B : Interface} (α : A ⟶ B) (R S : TypedSystem B) :
Δ (α • R) (α • S) ≤ Δ R S :=
ConstructiveCryptography.CryptographicAlgebra.distance_attach_le
α R S
/-- Opposite typed attachments that exchange two system pairs preserve their distance: for
`forward : A ⟶ B` and `backward : B ⟶ A`, the four equalities `forward • R = R'`,
`forward • S = S'`, `backward • R' = R` and `backward • S' = S` give `Δ R' S' = Δ R S`.
The interface form of `distance_eq_of_mutual_attach`; [1, Definition 2, p. 11]:
“d(αR, αS) ≤ d(R, S) for all α”. -/
lemma distance_eq_of_mutual_smul {A B : Interface}
(forward : A ⟶ B) (backward : B ⟶ A)
(R S : TypedSystem B) (R' S' : TypedSystem A)
(hforwardR : forward • R = R') (hforwardS : forward • S = S')
(hbackwardR : backward • R' = R) (hbackwardS : backward • S' = S) :
Δ R' S' = Δ R S :=
ConstructiveCryptography.CryptographicAlgebra.distance_eq_of_mutual_attach
forward backward R S R' S' hforwardR hforwardS hbackwardR hbackwardS
/-- The hybrid inequality, with both endpoints inferred from the goal. -/
lemma distance_triangle {A : Interface} {R T : TypedSystem A} (S : TypedSystem A) :
Δ R T ≤ Δ R S + Δ S T :=
ConstructiveCryptography.CryptographicAlgebra.distance_triangle
R S T
lemma distance_parallel_left_le {A B : Interface}
(R S : TypedSystem A) (T : TypedSystem B) : Δ (R ∥ T) (S ∥ T) ≤ Δ R S :=
ConstructiveCryptography.CryptographicAlgebra.distance_parallel_left_le
R S T
lemma distance_parallel_right_le {A B : Interface}
(R : TypedSystem A) (S T : TypedSystem B) : Δ (R ∥ S) (R ∥ T) ≤ Δ S T :=
ConstructiveCryptography.CryptographicAlgebra.distance_parallel_right_le
R S T
lemma distance_parallel_le {A B : Interface}
(R S : TypedSystem A) (T U : TypedSystem B) :
Δ (R ∥ T) (S ∥ U) ≤ Δ R S + Δ T U :=
ConstructiveCryptography.CryptographicAlgebra.distance_parallel_le
R S T U
open Lean Meta Simp in
/-- `associator_hom_attach` whatever the spelling of the associator or tensor: `(α_ A B C).hom •
(R ∥ (S ∥ T)) = (R ∥ S) ∥ T` when the interfaces use `⊗` or `Interface.tensor`. -/
simproc [cc_normalization] associatorHomSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr CategoryTheory.Iso.hom _ _ _ _ iso := f | return .continue
let (A, B, C) ← match_expr iso with
| CategoryTheory.MonoidalCategoryStruct.associator _ _ _ A B C => pure (A, B, C)
| Interface.associator A B C => pure (A, B, C)
| _ => return .continue
let_expr TypedSystem.parallel _ _ R ST := x | return .continue
let_expr TypedSystem.parallel _ _ S T := ST | return .continue
let proof := mkAppN (mkConst ``Interface.associator_hom_attach) #[A, B, C, R, S, T]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `parallel_assoc` whatever the spelling of the associator or tensor: `(α_ A B C).inv •
((R ∥ S) ∥ T) = R ∥ (S ∥ T)` when the interfaces use `⊗` or `Interface.tensor`. -/
simproc [cc_normalization] associatorInvSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr CategoryTheory.Iso.inv _ _ _ _ iso := f | return .continue
let (A, B, C) ← match_expr iso with
| CategoryTheory.MonoidalCategoryStruct.associator _ _ _ A B C => pure (A, B, C)
| Interface.associator A B C => pure (A, B, C)
| _ => return .continue
let_expr TypedSystem.parallel _ _ RS T := x | return .continue
let_expr TypedSystem.parallel _ _ R S := RS | return .continue
let proof := mkAppN (mkConst ``Interface.parallel_assoc) #[A, B, C, R, S, T]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `leftUnitor_hom_attach` whatever the spelling of the left unitor or unit tensor:
`(λ_ A).hom • R = TypedSystem.dummy ∥ R`. -/
simproc [cc_normalization] leftUnitorHomSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ β _ _ f R := e | return .continue
let_expr TypedSystem _ := β | return .continue
let_expr CategoryTheory.Iso.hom _ _ _ _ iso := f | return .continue
let A ← match_expr iso with
| CategoryTheory.MonoidalCategoryStruct.leftUnitor _ _ _ A => pure A
| Interface.leftUnitor A => pure A
| _ => return .continue
let proof := mkAppN (mkConst ``Interface.leftUnitor_hom_attach) #[A, R]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `parallel_dummy_left` whatever the spelling of the left unitor or unit tensor:
`(λ_ A).inv • (TypedSystem.dummy ∥ R) = R`. -/
simproc [cc_normalization] leftUnitorInvSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr CategoryTheory.Iso.inv _ _ _ _ iso := f | return .continue
let A ← match_expr iso with
| CategoryTheory.MonoidalCategoryStruct.leftUnitor _ _ _ A => pure A
| Interface.leftUnitor A => pure A
| _ => return .continue
let_expr TypedSystem.parallel _ _ d R := x | return .continue
let_expr TypedSystem.dummy := d | return .continue
let proof := mkAppN (mkConst ``Interface.parallel_dummy_left) #[A, R]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `rightUnitor_hom_attach` whatever the spelling of the right unitor or unit tensor:
`(ρ_ A).hom • R = R ∥ TypedSystem.dummy`. -/
simproc [cc_normalization] rightUnitorHomSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ β _ _ f R := e | return .continue
let_expr TypedSystem _ := β | return .continue
let_expr CategoryTheory.Iso.hom _ _ _ _ iso := f | return .continue
let A ← match_expr iso with
| CategoryTheory.MonoidalCategoryStruct.rightUnitor _ _ _ A => pure A
| Interface.rightUnitor A => pure A
| _ => return .continue
let proof := mkAppN (mkConst ``Interface.rightUnitor_hom_attach) #[A, R]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `parallel_dummy_right` whatever the spelling of the right unitor or unit tensor:
`(ρ_ A).inv • (R ∥ TypedSystem.dummy) = R`. -/
simproc [cc_normalization] rightUnitorInvSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr CategoryTheory.Iso.inv _ _ _ _ iso := f | return .continue
let A ← match_expr iso with
| CategoryTheory.MonoidalCategoryStruct.rightUnitor _ _ _ A => pure A
| Interface.rightUnitor A => pure A
| _ => return .continue
let_expr TypedSystem.parallel _ _ R d := x | return .continue
let_expr TypedSystem.dummy := d | return .continue
let proof := mkAppN (mkConst ``Interface.parallel_dummy_right) #[A, R]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `whiskerLeft_smul` whatever the spelling of the tensor: `(A ◁ β) • (R ∥ S) = R ∥ (β • S)`. -/
simproc [cc_normalization] whiskerLeftSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr CategoryTheory.MonoidalCategoryStruct.whiskerLeft _ _ _ A B' B β := f | return .continue
let_expr TypedSystem.parallel _ _ R S := x | return .continue
let proof := mkAppN (mkConst ``Interface.whiskerLeft_smul) #[A, B, B', β, R, S]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `whiskerRight_smul` whatever the spelling of the tensor: `(α ▷ B) • (R ∥ S) = (α • R) ∥ S`. -/
simproc [cc_normalization] whiskerRightSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue
let_expr CategoryTheory.MonoidalCategoryStruct.whiskerRight _ _ _ A' A α B := f |
return .continue
let_expr TypedSystem.parallel _ _ R S := x | return .continue
let proof := mkAppN (mkConst ``Interface.whiskerRight_smul) #[A, A', B, α, R, S]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
open Lean Meta Simp in
/-- `whisker_smul_comm` whatever the spelling of the tensor:
`(α ▷ B') • ((A ◁ β) • R) = (A' ◁ β) • ((α ▷ B) • R)`. -/
simproc [cc_normalization] whiskerSmulComm (@HSMul.hSMul _ _ _ _ _ _) := fun e => do
let_expr HSMul.hSMul _ β₁ _ _ f inner := e | return .continue
let_expr TypedSystem _ := β₁ | return .continue
let_expr CategoryTheory.MonoidalCategoryStruct.whiskerRight _ _ _ A' A α B' := f |
return .continue
let_expr HSMul.hSMul _ β₂ _ _ g R := inner | return .continue
let_expr TypedSystem _ := β₂ | return .continue
let_expr CategoryTheory.MonoidalCategoryStruct.whiskerLeft _ _ _ _ _ B β := g |
return .continue
let proof := mkAppN (mkConst ``Interface.whisker_smul_comm) #[A, A', B, B', α, β, R]
let_expr Eq _ lhs rhs := ← inferType proof | return .continue
unless ← withDefault (isDefEq e lhs) do return .continue
return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) }
end Interface
-- The compatibility laws, stated with the concrete parallel composition.
-- Reducible-only matching avoids unfolding probabilities on inapplicable rules.
scoped macro_rules
| `(tactic| cc_nonexpand) => `(tactic|
first
| with_reducible cc_exact_rule
"ConstructiveCryptography.CryptographicAlgebra.distance_parallel_left_le" =>
Interface.distance_parallel_left_le _ _ _
| with_reducible cc_exact_rule
"ConstructiveCryptography.CryptographicAlgebra.distance_parallel_right_le" =>
Interface.distance_parallel_right_le _ _ _
| with_reducible cc_exact_rule
"ConstructiveCryptography.CryptographicAlgebra.distance_parallel_le" =>
Interface.distance_parallel_le _ _ _ _
| with_reducible cc_exact_rule
"ConstructiveCryptography.CryptographicAlgebra.distance_attach_le" =>
Interface.distance_smul_le _ _ _)
end SystemAlgebra