Skip to content

Commit e5757d2

Browse files
committed
Simplify Decidable.Order
This commit makes strictly nonfunctional / style changes to the Order module, like: - using `%default total` instead of explicitly marking every function as total; - grouping arguments of the same type, as in `(a, b, c : t)`; - underscoring unused arguments; - cutting the useless `implementation` keyword; and - proving interface constraints directly in implementation blocks. All of these changes make the code a little nicer to look at.
1 parent 0417c53 commit e5757d2

File tree

1 file changed

+60
-84
lines changed

1 file changed

+60
-84
lines changed

libs/contrib/Decidable/Order.idr

+60-84
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,27 @@ import Data.Fun
77
import Data.Rel
88

99
%access public export
10-
11-
--------------------------------------------------------------------------------
12-
-- Utility Lemmas
13-
--------------------------------------------------------------------------------
10+
%default total
1411

1512
--------------------------------------------------------------------------------
1613
-- Preorders, Posets, total Orders, Equivalencies, Congruencies
1714
--------------------------------------------------------------------------------
1815

1916
interface Preorder t (po : t -> t -> Type) where
20-
total transitive : (a : t) -> (b : t) -> (c : t) -> po a b -> po b c -> po a c
21-
total reflexive : (a : t) -> po a a
17+
transitive : (a, b, c : t) -> po a b -> po b c -> po a c
18+
reflexive : (a : t) -> po a a
2219

2320
interface (Preorder t po) => Poset t (po : t -> t -> Type) where
24-
total antisymmetric : (a : t) -> (b : t) -> po a b -> po b a -> a = b
21+
antisymmetric : (a, b : t) -> po a b -> po b a -> a = b
2522

2623
interface (Poset t to) => Ordered t (to : t -> t -> Type) where
27-
total order : (a : t) -> (b : t) -> Either (to a b) (to b a)
24+
order : (a, b : t) -> Either (to a b) (to b a)
2825

2926
interface (Preorder t eq) => Equivalence t (eq : t -> t -> Type) where
30-
total symmetric : (a : t) -> (b : t) -> eq a b -> eq b a
27+
symmetric : (a, b : t) -> eq a b -> eq b a
3128

3229
interface (Equivalence t eq) => Congruence t (f : t -> t) (eq : t -> t -> Type) where
33-
total congruent : (a : t) ->
34-
(b : t) ->
35-
eq a b ->
36-
eq (f a) (f b)
30+
congruent : (a, b : t) -> eq a b -> eq (f a) (f b)
3731

3832
minimum : (Ordered t to) => t -> t -> t
3933
minimum {to} x y with (order {to} x y)
@@ -49,79 +43,62 @@ maximum {to} x y with (order {to} x y)
4943
-- Syntactic equivalence (=)
5044
--------------------------------------------------------------------------------
5145

52-
implementation Preorder t ((=) {A = t} {B = t}) where
53-
transitive a b c = trans {a = a} {b = b} {c = c}
54-
reflexive a = Refl
46+
Preorder t (=) where
47+
transitive _ _ _ = trans
48+
reflexive _ = Refl
5549

56-
implementation Equivalence t ((=) {A = t} {B = t}) where
57-
symmetric a b prf = sym prf
50+
Equivalence t (=) where
51+
symmetric _ _ = sym
5852

59-
implementation Congruence t f ((=) {A = t} {B = t}) where
60-
congruent a b = cong {a = a} {b = b} {f = f}
53+
Congruence t f (=) where
54+
congruent _ _ = cong
6155

6256
--------------------------------------------------------------------------------
6357
-- Natural numbers
6458
--------------------------------------------------------------------------------
6559

66-
total LTEIsTransitive : (m : Nat) -> (n : Nat) -> (o : Nat) ->
67-
LTE m n -> LTE n o ->
68-
LTE m o
69-
LTEIsTransitive Z n o LTEZero nlteo = LTEZero
70-
LTEIsTransitive (S m) (S n) (S o) (LTESucc mlten) (LTESucc nlteo) = LTESucc (LTEIsTransitive m n o mlten nlteo)
71-
72-
total LTEIsReflexive : (n : Nat) -> LTE n n
73-
LTEIsReflexive Z = LTEZero
74-
LTEIsReflexive (S n) = LTESucc (LTEIsReflexive n)
75-
76-
implementation Preorder Nat LTE where
77-
transitive = LTEIsTransitive
78-
reflexive = LTEIsReflexive
79-
80-
total LTEIsAntisymmetric : (m : Nat) -> (n : Nat) ->
81-
LTE m n -> LTE n m -> m = n
82-
LTEIsAntisymmetric Z Z LTEZero LTEZero = Refl
83-
LTEIsAntisymmetric (S n) (S m) (LTESucc mLTEn) (LTESucc nLTEm) with (LTEIsAntisymmetric n m mLTEn nLTEm)
84-
LTEIsAntisymmetric (S n) (S n) (LTESucc mLTEn) (LTESucc nLTEm) | Refl = Refl
85-
86-
87-
implementation Poset Nat LTE where
88-
antisymmetric = LTEIsAntisymmetric
89-
90-
total zeroNeverGreater : {n : Nat} -> LTE (S n) Z -> Void
91-
zeroNeverGreater {n} LTEZero impossible
92-
zeroNeverGreater {n} (LTESucc _) impossible
93-
94-
total zeroAlwaysSmaller : {n : Nat} -> LTE Z n
95-
zeroAlwaysSmaller = LTEZero
96-
97-
total ltesuccinjective : {n : Nat} -> {m : Nat} -> (LTE n m -> Void) -> LTE (S n) (S m) -> Void
98-
ltesuccinjective {n} {m} disprf (LTESucc nLTEm) = void (disprf nLTEm)
99-
ltesuccinjective {n} {m} disprf LTEZero impossible
100-
101-
102-
total decideLTE : (n : Nat) -> (m : Nat) -> Dec (LTE n m)
103-
decideLTE Z y = Yes LTEZero
104-
decideLTE (S x) Z = No zeroNeverGreater
60+
Preorder Nat LTE where
61+
transitive Z _ _ _ _ = LTEZero
62+
transitive (S m) (S n) (S o) (LTESucc mlten) (LTESucc nlteo) =
63+
LTESucc (transitive m n o mlten nlteo)
64+
65+
reflexive Z = LTEZero
66+
reflexive (S n) = LTESucc (reflexive n)
67+
68+
Poset Nat LTE where
69+
antisymmetric Z Z LTEZero LTEZero = Refl
70+
antisymmetric (S n) (S m) (LTESucc mLTEn) (LTESucc nLTEm)
71+
with (antisymmetric n m mLTEn nLTEm)
72+
antisymmetric _ _ _ _ | Refl = Refl
73+
74+
decideLTE : (n, m : Nat) -> Dec (LTE n m)
75+
decideLTE Z _ = Yes LTEZero
76+
decideLTE (S _) Z = No zeroNeverGreater where
77+
zeroNeverGreater : LTE (S _) Z -> Void
78+
zeroNeverGreater LTEZero impossible
79+
zeroNeverGreater (LTESucc _) impossible
10580
decideLTE (S x) (S y) with (decEq (S x) (S y))
10681
| Yes eq = rewrite eq in Yes (reflexive (S y))
10782
| No _ with (decideLTE x y)
10883
| Yes nLTEm = Yes (LTESucc nLTEm)
10984
| No nGTm = No (ltesuccinjective nGTm)
85+
where
86+
ltesuccinjective : (LTE n m -> Void) -> LTE (S n) (S m) -> Void
87+
ltesuccinjective disprf (LTESucc nLTEm) = void (disprf nLTEm)
88+
ltesuccinjective _ LTEZero impossible
11089

111-
implementation Decidable [Nat,Nat] LTE where
90+
Decidable [Nat, Nat] LTE where
11291
decide = decideLTE
11392

114-
total
115-
lte : (m : Nat) -> (n : Nat) -> Dec (LTE m n)
116-
lte m n = decide {ts = [Nat,Nat]} {p = LTE} m n
117-
118-
total
119-
shift : (m : Nat) -> (n : Nat) -> LTE m n -> LTE (S m) (S n)
120-
shift m n mLTEn = LTESucc mLTEn
121-
122-
implementation Ordered Nat LTE where
123-
order Z n = Left LTEZero
124-
order m Z = Right LTEZero
93+
lte : (m, n : Nat) -> Dec (LTE m n)
94+
lte m n = decide {ts = [Nat, Nat]} {p = LTE} m n
95+
96+
shift : (m, n : Nat) -> LTE m n -> LTE (S m) (S n)
97+
shift _ _ mLTEn = LTESucc mLTEn
98+
99+
Ordered Nat LTE where
100+
order Z _ = Left LTEZero
101+
order _ Z = Right LTEZero
125102
order (S k) (S j) with (order {to=LTE} k j)
126103
order (S k) (S j) | Left prf = Left (shift k j prf)
127104
order (S k) (S j) | Right prf = Right (shift j k prf)
@@ -132,25 +109,24 @@ implementation Ordered Nat LTE where
132109

133110
using (k : Nat)
134111
data FinLTE : Fin k -> Fin k -> Type where
135-
FromNatPrf : {m : Fin k} -> {n : Fin k} -> LTE (finToNat m) (finToNat n) -> FinLTE m n
112+
FromNatPrf : LTE (finToNat m) (finToNat n) -> FinLTE m n
136113

137-
implementation Preorder (Fin k) FinLTE where
138-
transitive m n o (FromNatPrf p1) (FromNatPrf p2) =
139-
FromNatPrf (LTEIsTransitive (finToNat m) (finToNat n) (finToNat o) p1 p2)
140-
reflexive n = FromNatPrf (LTEIsReflexive (finToNat n))
114+
Preorder (Fin k) FinLTE where
115+
transitive m n o (FromNatPrf p1) (FromNatPrf p2) =
116+
FromNatPrf (transitive (finToNat m) (finToNat n) (finToNat o) p1 p2)
117+
reflexive n = FromNatPrf (reflexive (finToNat n))
141118

142-
implementation Poset (Fin k) FinLTE where
119+
Poset (Fin k) FinLTE where
143120
antisymmetric m n (FromNatPrf p1) (FromNatPrf p2) =
144-
finToNatInjective m n (LTEIsAntisymmetric (finToNat m) (finToNat n) p1 p2)
145-
146-
implementation Decidable [Fin k, Fin k] FinLTE where
121+
finToNatInjective m n (antisymmetric (finToNat m) (finToNat n) p1 p2)
122+
123+
Decidable [Fin k, Fin k] FinLTE where
147124
decide m n with (decideLTE (finToNat m) (finToNat n))
148125
| Yes prf = Yes (FromNatPrf prf)
149-
| No disprf = No (\ (FromNatPrf prf) => disprf prf)
126+
| No disprf = No (\(FromNatPrf prf) => disprf prf)
150127

151-
implementation Ordered (Fin k) FinLTE where
128+
Ordered (Fin k) FinLTE where
152129
order m n =
153-
either (Left . FromNatPrf)
130+
either (Left . FromNatPrf)
154131
(Right . FromNatPrf)
155132
(order (finToNat m) (finToNat n))
156-

0 commit comments

Comments
 (0)