Skip to content

Commit 862f81e

Browse files
reals - order, got rid of lossy unification, and aplied solver
1 parent dc8c665 commit 862f81e

File tree

3 files changed

+182
-33
lines changed

3 files changed

+182
-33
lines changed

Cubical/Data/Rationals/Fast/Properties.agda

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,38 @@ maxAbsorbLMin = SetQuotient.elimProp2 (λ _ _ → isSetℚ _ _)
455455
≡⟨ sym (ℤ.·DistR+ (ℕ₊₁→ℤ b) (c ℤ.· ℕ₊₁→ℤ f) (e ℤ.· ℕ₊₁→ℤ d)) ⟩
456456
ℕ₊₁→ℤ b ℤ.· (c ℤ.· ℕ₊₁→ℤ f ℤ.+ e ℤ.· ℕ₊₁→ℤ d) ∎
457457

458+
_impl+_ :
459+
_impl+_ = OnCommonDenomSym.go +Rec
458460

459461
_+_ :
460-
_+_ = OnCommonDenomSym.go +Rec
462+
[ a ] + [ a₁ ] = [ a ] impl+ [ a₁ ]
463+
[ a ] + eq/ a₁ b r i = [ a ] impl+ (eq/ a₁ b r i)
464+
[ a ] + _//_.squash/ x₁ x₂ p q i i₁ =
465+
isSetℚ ([ a ] + x₁) ([ a ] + x₂) (λ i₁ [ a ] + p i₁) (λ i₁ [ a ] + q i₁) i i₁
466+
eq/ a b r i + [ a₁ ] = (eq/ a b r i) impl+ [ a₁ ]
467+
eq/ a b r i + eq/ a₁ b₁ r₁ i₁ =
468+
isSet→isSet' isSetℚ
469+
(λ i₁ [ a ] impl+ eq/ a₁ b₁ r₁ i₁) (λ i₁ [ b ] impl+ eq/ a₁ b₁ r₁ i₁)
470+
(λ i eq/ a b r i impl+ [ a₁ ]) (λ i eq/ a b r i impl+ [ b₁ ]) i i₁
471+
eq/ a b r i + _//_.squash/ x₁ x₂ p q i₁ i₂ =
472+
isGroupoid→isGroupoid' (isSet→isGroupoid isSetℚ)
473+
(λ i₁ i₂ isSetℚ ([ a ] + x₁) ([ a ] + x₂) (λ i₃ [ a ] + p i₃) (λ i₃ [ a ] + q i₃) i₁ i₂)
474+
(λ i₁ i₂ isSetℚ ([ b ] + x₁) ([ b ] + x₂) (λ i₃ [ b ] + p i₃) (λ i₃ [ b ] + q i₃) i₁ i₂)
475+
(λ i i₂ eq/ a b r i + p i₂) (λ i i₂ eq/ a b r i + q i₂)
476+
(λ i i₁ eq/ a b r i + x₁) (λ i i₁ eq/ a b r i + x₂)
477+
i i₁ i₂
478+
_//_.squash/ x x₁ p q i i₁ + z =
479+
isSetℚ (x + z) (x₁ + z) (cong (_+ z) p) (cong (_+ z) q) i i₁
480+
481+
+-impl : x y x + y ≡ x impl+ y
482+
+-impl = SetQuotient.ElimProp2.go w
483+
where
484+
w : SetQuotient.ElimProp2 (λ z z₁ z + z₁ ≡ (z impl+ z₁))
485+
w .SetQuotient.ElimProp2.isPropB _ _ = isSetℚ _ _
486+
w .SetQuotient.ElimProp2.f _ _ = refl
461487

462488
+Comm : x y x + y ≡ y + x
463-
+Comm = OnCommonDenomSym.go-comm +Rec
489+
+Comm x y = +-impl x y ∙∙ OnCommonDenomSym.go-comm +Rec x y ∙∙ sym (+-impl y x)
464490

465491
+IdL : x 0 + x ≡ x
466492
+IdL = SetQuotient.elimProp (λ _ isSetℚ _ _)
@@ -479,11 +505,39 @@ _+_ = OnCommonDenomSym.go +Rec
479505
·Rec .OnCommonDenomSym.g-sym (a , _) (c , _) = ℤ.·Comm a c
480506
·Rec .OnCommonDenomSym.g-eql (a , b) (c , d) (e , _) p = lem₁ a (ℕ₊₁→ℤ d) c (ℕ₊₁→ℤ b) e p
481507

508+
_impl·_ :
509+
_impl·_ = OnCommonDenomSym.go ·Rec
510+
482511
_·_ :
483-
_·_ = OnCommonDenomSym.go ·Rec
512+
[ a ] · [ a₁ ] = [ a ] impl· [ a₁ ]
513+
[ a ] · eq/ a₁ b r i = [ a ] impl· (eq/ a₁ b r i)
514+
[ a ] · _//_.squash/ x₁ x₂ p q i i₁ =
515+
isSetℚ ([ a ] · x₁) ([ a ] · x₂) (λ i₁ [ a ] · p i₁) (λ i₁ [ a ] · q i₁) i i₁
516+
eq/ a b r i · [ a₁ ] = (eq/ a b r i) impl· [ a₁ ]
517+
eq/ a b r i · eq/ a₁ b₁ r₁ i₁ =
518+
isSet→isSet' isSetℚ
519+
(λ i₁ [ a ] impl· eq/ a₁ b₁ r₁ i₁) (λ i₁ [ b ] impl· eq/ a₁ b₁ r₁ i₁)
520+
(λ i eq/ a b r i impl· [ a₁ ]) (λ i eq/ a b r i impl· [ b₁ ]) i i₁
521+
eq/ a b r i · _//_.squash/ x₁ x₂ p q i₁ i₂ =
522+
isGroupoid→isGroupoid' (isSet→isGroupoid isSetℚ)
523+
(λ i₁ i₂ isSetℚ ([ a ] · x₁) ([ a ] · x₂) (λ i₃ [ a ] · p i₃) (λ i₃ [ a ] · q i₃) i₁ i₂)
524+
(λ i₁ i₂ isSetℚ ([ b ] · x₁) ([ b ] · x₂) (λ i₃ [ b ] · p i₃) (λ i₃ [ b ] · q i₃) i₁ i₂)
525+
(λ i i₂ eq/ a b r i · p i₂) (λ i i₂ eq/ a b r i · q i₂)
526+
(λ i i₁ eq/ a b r i · x₁) (λ i i₁ eq/ a b r i · x₂)
527+
i i₁ i₂
528+
_//_.squash/ x x₁ p q i i₁ · z =
529+
isSetℚ (x · z) (x₁ · z) (cong (_· z) p) (cong (_· z) q) i i₁
530+
531+
·-impl : x y x · y ≡ x impl· y
532+
·-impl = SetQuotient.ElimProp2.go w
533+
where
534+
w : SetQuotient.ElimProp2 (λ z z₁ z · z₁ ≡ (z impl· z₁))
535+
w .SetQuotient.ElimProp2.isPropB _ _ = isSetℚ _ _
536+
w .SetQuotient.ElimProp2.f _ _ = refl
484537

485538
·Comm : x y x · y ≡ y · x
486-
·Comm = OnCommonDenomSym.go-comm ·Rec
539+
·Comm x y = ·-impl x y ∙∙ OnCommonDenomSym.go-comm ·Rec x y ∙∙ sym (·-impl y x)
540+
487541

488542
·IdL : x 1 · x ≡ x
489543
·IdL = SetQuotient.elimProp (λ _ isSetℚ _ _) λ (_ , _) eq/ _ _ ℤ!
@@ -633,3 +687,25 @@ x+x≡2x x = cong₂ _+_
633687
(sym (·IdL x))
634688
(sym (·IdL x))
635689
∙ sym (·DistR+ 1 1 x)
690+
691+
692+
eqElim : (lrhs : ℚ × ℚ)
693+
( {k m} fst (lrhs ([ (k , 1+ m) ]))
694+
≡ snd (lrhs ([ (k , 1+ m) ])))
695+
: ℚ) fst (lrhs ε) ≡ snd (lrhs ε)
696+
eqElim lrhs p = SetQuotient.ElimProp.go w
697+
where
698+
699+
w : SetQuotient.ElimProp (λ z fst (lrhs z) ≡ snd (lrhs z))
700+
w .SetQuotient.ElimProp.isPropB _ = isSetℚ _ _
701+
w .SetQuotient.ElimProp.f (n , (1+ n₁)) = p {n} {n₁}
702+
703+
eqElim₂ : (lrhs : ℚ × ℚ)
704+
( {k m k' m'} fst (lrhs [ (k , 1+ m) ] [ (k' , 1+ m') ])
705+
≡ snd (lrhs [ (k , 1+ m) ] [ (k' , 1+ m') ]))
706+
(x y : ℚ) fst (lrhs x y) ≡ (snd (lrhs x y))
707+
eqElim₂ lrhs p = SetQuotient.ElimProp2.go w
708+
where
709+
w : SetQuotient.ElimProp2 (λ z z' fst (lrhs z z') ≡ snd (lrhs z z'))
710+
w .SetQuotient.ElimProp2.isPropB _ _ = isSetℚ _ _
711+
w .SetQuotient.ElimProp2.f (n , (1+ n₁)) (m , (1+ m₁)) = p {n} {n₁} {m} {m₁}

Cubical/HITs/CauchyReals/Order.agda

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ opaque
9191

9292

9393

94-
+ᵣ-impl : x y x +ᵣ y ≡ NonExpanding₂.go sumR x y
94+
+ᵣ-impl : x y x +ᵣ y ≡ NonExpanding₂.go sumR x y
9595
+ᵣ-impl _ _ = refl
9696

9797
rat-+ᵣ-lim : r x y rat r +ᵣ lim x y ≡ lim (λ ε rat r +ᵣ x ε)
@@ -184,9 +184,9 @@ opaque
184184
maxᵣ :
185185
maxᵣ = NonExpanding₂.go maxR
186186

187-
maxᵣ-impl : x y maxᵣ x y ≡ NonExpanding₂.go maxR x y
187+
maxᵣ-impl : x y maxᵣ x y ≡ NonExpanding₂.go maxR x y
188188
maxᵣ-impl _ _ = refl
189-
189+
190190
minᵣ-rat : a b minᵣ (rat a) (rat b) ≡ rat (ℚ.min a b)
191191
minᵣ-rat _ _ = refl
192192

@@ -336,24 +336,21 @@ opaque
336336
x' = x ℚ.+ fst (/3₊ (y-x))
337337
y' = x' ℚ.+ fst (/3₊ (y-x))
338338
in ∣ (x' , y') ,
339-
≤ℚ→≤ᵣ x x' (
340-
subst (ℚ._≤ x') (ℚ.+IdR x)
341-
(ℚ.≤-o+ 0 (fst (/3₊ (y-x))) x
342-
(ℚ.0≤ℚ₊ (/3₊ (y-x)))) )
343-
, subst (ℚ._< y') (ℚ.+IdR x')
344-
(ℚ.<-o+ 0 (fst (/3₊ (y-x))) x'
345-
(ℚ.0<ℚ₊ (/3₊ (y-x))))
346-
, ≤ℚ→≤ᵣ y' y
347-
(subst2 {x = y' ℚ.+ 0} {y = y'} (ℚ._≤_) (ℚ.+IdR y')
348-
({!!}
349-
∙∙
350-
cong {x = ℚ.[ 1 / 3 ] ℚ.+ ℚ.[ 1 / 3 ] ℚ.+ ℚ.[ 1 / 3 ]}
351-
{1} (λ a x ℚ.+ a ℚ.· (y ℚ.- x))
352-
ℚ.decℚ?
353-
∙∙ (cong (x ℚ.+_) (ℚ.·IdL (y ℚ.- x))
354-
∙ ℚ!))
355-
((ℚ.≤-o+ 0 (fst (/3₊ (y-x))) y'
356-
(ℚ.0≤ℚ₊ (/3₊ (y-x)))))) ∣₁
339+
(let p : x y ((x ℚ.+ ((y ℚ.- x) ℚ.· [ 1 / 3 ]))
340+
ℚ.+ ((y ℚ.- x) ℚ.· [ 1 / 3 ])) ℚ.+ ((y ℚ.- x) ℚ.· [ 1 / 3 ]) ≡ y
341+
p = ℚ.eqElim₂ _ (ℚ.eqℚ ℤ!)
342+
in (≤ℚ→≤ᵣ x x' (
343+
subst (ℚ._≤ x') (ℚ.+IdR x)
344+
(ℚ.≤-o+ 0 (fst (/3₊ (y-x))) x
345+
(ℚ.0≤ℚ₊ (/3₊ (y-x)))) )
346+
, subst (ℚ._< y') (ℚ.+IdR x')
347+
(ℚ.<-o+ 0 (fst (/3₊ (y-x))) x'
348+
(ℚ.0<ℚ₊ (/3₊ (y-x))))
349+
, ≤ℚ→≤ᵣ y' y
350+
(subst2 {x = y' ℚ.+ 0} {y = y'} (ℚ._≤_) (ℚ.+IdR y')
351+
(p x y)
352+
((ℚ.≤-o+ 0 (fst (/3₊ (y-x))) y'
353+
(ℚ.0≤ℚ₊ (/3₊ (y-x)))))))) ∣₁
357354

358355
<ᵣ→<ℚ : q r rat q <ᵣ rat r q ℚ.< r
359356
<ᵣ→<ℚ = ElimProp2.go w
@@ -465,7 +462,7 @@ opaque
465462

466463
-ᵣ-impl : x -ᵣ x ≡ fst (fromLipschitz (1 , _) ((rat ∘ ℚ.-_ ) , ℚ-isLip)) x
467464
-ᵣ-impl x = refl
468-
465+
469466
-ᵣ-lip : Lipschitz-ℝ→ℝ 1 -ᵣ_
470467
-ᵣ-lip = snd -ᵣR
471468

@@ -578,7 +575,7 @@ opaque
578575

579576
absᵣ :
580577
absᵣ = fst absᵣL
581-
578+
582579
absᵣ0 : absᵣ 00
583580
absᵣ0 = refl
584581

@@ -675,7 +672,7 @@ opaque
675672
w .Elimℝ-Prop2Sym.rat-ratA r q = (cong -ᵣ_ (+ᵣ-rat _ _) ∙ -ᵣ-rat _) ∙ cong rat (ℚ.-Distr r q) ∙
676673
sym (+ᵣ-rat _ _) ∙
677674
cong₂ _+ᵣ_ (sym (-ᵣ-rat _)) (sym (-ᵣ-rat _))
678-
w .Elimℝ-Prop2Sym.rat-limA r x y x₁ =
675+
w .Elimℝ-Prop2Sym.rat-limA r x y x₁ =
679676
cong (-ᵣ_) (rat-+ᵣ-lim _ _ _)
680677
∙ -ᵣ-lim' _ _
681678
∙ (congLim _ _ _ _ λ q x₁ q ∙ cong (_-ᵣ x q) (-ᵣ-rat r))
@@ -704,9 +701,32 @@ opaque
704701
minusComm-absᵣ : x y absᵣ (x +ᵣ (-ᵣ y)) ≡ absᵣ (y +ᵣ (-ᵣ x))
705702
minusComm-absᵣ x y = -absᵣ _ ∙ cong absᵣ (-[x-y]≡y-x x y)
706703

707-
704+
opaque
705+
unfolding _<ᵣ_
708706
denseℚinℝ : u v u <ᵣ v ∃[ q ∈ ℚ ] ((u <ᵣ rat q) × (rat q <ᵣ v))
709-
denseℚinℝ u v = {!!}
707+
denseℚinℝ u v =
708+
PT.map λ ((p , q) , u≤p , p<q , q≤v)
709+
let
710+
m = (p ℚ.+ q) ℚ.· [ 1 / 2 ]
711+
p<m = subst2 ℚ._<_ (
712+
p ℚ.· [ 1 / 2 ] ℚ.+ p ℚ.· [ 1 / 2 ] ≡⟨ sym $ ℚ.·DistL+ p [ 1 / 2 ] [ 1 / 2 ] ⟩
713+
p ℚ.· [ 4 / 4 ] ≡⟨ cong (p ℚ.·_) (ℚ.[n/n]≡[m/m] 3 0) ⟩
714+
p ℚ.· [ 1 / 1 ] ≡⟨ ℚ.·IdR p ⟩
715+
p ∎)
716+
(sym (ℚ.·DistR+ p q [ 1 / 2 ]))
717+
(ℚ.<-o+ (p ℚ.· [ 1 / 2 ]) (q ℚ.· [ 1 / 2 ]) (p ℚ.· [ 1 / 2 ])
718+
(ℚ.<-·o p q [ 1 / 2 ] (ℚ.0<pos 0 2) p<q))
719+
m<q = subst2 ℚ._<_
720+
(sym (ℚ.·DistR+ p q [ 1 / 2 ]))
721+
(
722+
q ℚ.· [ 1 / 2 ] ℚ.+ q ℚ.· [ 1 / 2 ] ≡⟨ sym $ ℚ.·DistL+ q [ 1 / 2 ] [ 1 / 2 ] ⟩
723+
q ℚ.· [ 4 / 4 ] ≡⟨ cong (q ℚ.·_) (ℚ.[n/n]≡[m/m] 3 0) ⟩
724+
q ℚ.· [ 1 / 1 ] ≡⟨ ℚ.·IdR q ⟩
725+
q ∎)
726+
(ℚ.<-+o (p ℚ.· [ 1 / 2 ]) (q ℚ.· [ 1 / 2 ]) (q ℚ.· [ 1 / 2 ])
727+
((ℚ.<-·o p q [ 1 / 2 ] (ℚ.0<pos 0 2) p<q)))
728+
in
729+
m , ∣ (p , m) , u≤p , p<m , ≤ᵣ-refl _ ∣₁ , ∣ (m , q) , ≤ᵣ-refl _ , m<q , q≤v ∣₁
710730

711731

712732

@@ -717,7 +737,7 @@ opaque
717737
w : Elimℝ-Prop (λ z 0 ≤ᵣ absᵣ z)
718738
w .Elimℝ-Prop.ratA q = isTrans≤≡ᵣ _ _ _ (≤ℚ→≤ᵣ 0 (ℚ.abs' q)
719739
(subst (0 ℚ.≤_) (ℚ.abs'≡abs q) (ℚ.0≤abs q))) (sym (absᵣ-rat' _))
720-
w .Elimℝ-Prop.limA x p x₁ =
740+
w .Elimℝ-Prop.limA x p x₁ =
721741
let
722742

723743
zz : _ ≡ _
@@ -726,12 +746,12 @@ opaque
726746
λ q sym (≤ᵣ→≡ (x₁ q)) ∙ maxᵣ-impl _ _
727747
)
728748

729-
749+
730750
in ≡→≤ᵣ ((cong₂ maxᵣ refl (absᵣ-lim' _ _) ∙ maxᵣ-impl _ _)
731751
∙ (snd (NonExpanding₂.β-rat-lim' {ℚ.max} maxR 0 (λ q (absᵣ (x q)))
732752
λ _ _ absᵣ-nonExpanding _ _ _ (p _ _))
733753
∙ zz) ∙ sym (absᵣ-lim' _ _))
734-
754+
735755

736756
w .Elimℝ-Prop.isPropA _ = isProp≤ᵣ _ _
737757

Cubical/Tactics/CommRingSolver/FastRationalsReflection.agda

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ private
9292
variableList (t ∷ ts)
9393
= varg (con (quote _∷vec_) (t v∷ (variableList ts) ∷ []))
9494

95+
normaliserCallAsTerm : Term Arg Term Term Term
96+
normaliserCallAsTerm R varList lhs =
97+
def
98+
(quote normaliseRing)
99+
(R v∷ lhs v∷ varList ∷ [])
100+
101+
102+
normaliserCallWithVars : Vars Term Term Term
103+
normaliserCallWithVars n vars R lhs =
104+
normaliserCallAsTerm R (variableList vars) lhs
105+
where
106+
variableList : Vars Arg Term
107+
variableList [] = varg (con (quote emptyVec) [])
108+
variableList (t ∷ ts)
109+
= varg (con (quote _∷vec_) (t v∷ (variableList ts) ∷ []))
110+
111+
95112
module pr (R : CommRing ℓ) {n : ℕ} where
96113
open CommRingStr (snd R)
97114

@@ -221,6 +238,16 @@ module CommRingReflection (cring : Term) (names : RingNames) where
221238
ass n = indexOf n vars
222239
in (fst r1 ass , fst r2 ass , vars ))
223240

241+
toAlgebraExpressionLHS : Term TC (Term × Vars)
242+
toAlgebraExpressionLHS lhs = do
243+
(e , vars) buildExpression lhs
244+
245+
returnTC (
246+
let ass : VarAss
247+
ass n = indexOf n vars
248+
in (e ass , vars ))
249+
250+
224251
private
225252
checkIsRing : Term TC Term
226253
checkIsRing ring = checkType ring (def (quote CommRing) (unknown v∷ []))
@@ -244,6 +271,32 @@ private
244271
let solution = solverCallWithVars (length vars) vars commRing lhs' rhs'
245272
unify hole solution
246273

274+
normalise!-macro : Term TC Unit
275+
normalise!-macro hole =
276+
withReduceDefs
277+
(false , ((quote ℚ._+_) ∷ (quote (ℚ.-_)) ∷ (quote ℚ._·_) ∷ []))
278+
do
279+
commRing checkIsRing (def (quote ℚCommRing) [])
280+
goal inferType hole >>= normalise
281+
names findRingNames commRing
282+
283+
-- wait-for-type goal
284+
just (lhs , rhs) get-boundary goal
285+
where
286+
nothing
287+
typeError(strErr "The CommRingSolver failed to parse the goal "
288+
∷ termErr goal ∷ [])
289+
290+
(lhs' , vars) CommRingReflection.toAlgebraExpressionLHS commRing names lhs
291+
292+
let solution = normaliserCallWithVars (length vars) vars commRing lhs'
293+
294+
unify hole solution
295+
247296
macro
248297
ℚ! : Term TC _
249298
ℚ! = solve!-macro
299+
300+
301+
ℚ↓ : Term TC _
302+
ℚ↓ = normalise!-macro

0 commit comments

Comments
 (0)