Skip to content

Commit 16fa875

Browse files
authored
Make all STSs in the Agda spec executable (#1270)
As the first task in implementing conformance testing with respect to the formal specification, we shall focus on making all the State Transition Systems (STSs) within the formal spec executable.
2 parents 4488656 + 44131c1 commit 16fa875

File tree

26 files changed

+611
-66
lines changed

26 files changed

+611
-66
lines changed

docs/agda-spec/src/Axiom/Set.agda

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ record Theory {ℓ} : Type (sucˡ ℓ) where
9797
finite : Set A Type ℓ
9898
finite X = ∃[ l ] {a} a ∈ X ⇔ a ∈ˡ l
9999

100+
Show-finite : ⦃ Show A ⦄ Show (Σ (Set A) finite)
101+
Show.show Show-finite (X , (l , _)) = Show-List .show l
102+
100103
weakly-finite : Set A Type ℓ
101104
weakly-finite X = ∃[ l ] {a} a ∈ X a ∈ˡ l
102105

@@ -291,6 +294,11 @@ record Theoryᶠ : Type₁ where
291294
lengthˢ : ⦃ DecEq A ⦄ Set A
292295
lengthˢ X = card (X , DecEq⇒strongly-finite X)
293296

297+
module _ {A : Type} ⦃ _ : Show A ⦄ where
298+
instance
299+
Show-Set : Show (Set A)
300+
Show-Set .show = λ x Show-finite .show (x , (finiteness x))
301+
294302
-- set theories with an infinite set (containing all natural numbers)
295303
record Theoryⁱ : Type₁ where
296304
field theory : Theory
@@ -307,30 +315,32 @@ record Theoryᵈ : Type₁ where
307315
field
308316
∈-sp : ⦃ DecEq A ⦄ spec-∈ A
309317
_∈?_ : ⦃ DecEq A ⦄ Decidable² (_∈_ {A = A})
310-
all? : ⦃ DecEq A ⦄ {P : A Type} (P? : Decidable¹ P) {X : Set A} Dec (All P X)
311-
any? : ⦃ DecEq A ⦄ {P : A Type} (P? : Decidable¹ P) (X : Set A) Dec (Any P X)
312-
313-
module _ {A : Type} ⦃ _ : DecEq A ⦄ where
318+
all? : {P : A Type} (P? : Decidable¹ P) {X : Set A} Dec (All P X)
319+
any? : {P : A Type} (P? : Decidable¹ P) (X : Set A) Dec (Any P X)
314320

315-
_∈ᵇ_ : A Set A Bool
316-
a ∈ᵇ X = ⌊ a ∈? X ⌋
317-
318-
instance
319-
Dec-∈ : _∈_ {A = A} ⁇²
320-
Dec-∈ = ⁇² _∈?_
321321

322-
module _ {P : A Type} ⦃ _ : P ⁇¹ ⦄ where instance
322+
module _ {A : Type} {P : A Type} where
323+
module _ ⦃ _ : P ⁇¹ ⦄ where instance
323324
Dec-Allˢ : All P ⁇¹
324325
Dec-Allˢ = ⁇¹ λ x all? dec¹ {x}
325326

326327
Dec-Anyˢ : Any P ⁇¹
327328
Dec-Anyˢ = ⁇¹ any? dec¹
328329

329-
module _ {P : A Type} (P? : Decidable¹ P) where
330+
module _ (P? : Decidable¹ P) where
330331
allᵇ anyᵇ : (X : Set A) Bool
331332
allᵇ X = ⌊ all? P? {X} ⌋
332333
anyᵇ X = ⌊ any? P? X ⌋
333334

335+
module _ {A : Type} ⦃ _ : DecEq A ⦄ where
336+
337+
_∈ᵇ_ : A Set A Bool
338+
a ∈ᵇ X = ⌊ a ∈? X ⌋
339+
340+
instance
341+
Dec-∈ : _∈_ {A = A} ⁇²
342+
Dec-∈ = ⁇² _∈?_
343+
334344
_ = _∈_ {A = A} ⁇² ∋ it
335345
_ = _⊆_ {A = A} ⁇² ∋ it
336346
_ = _≡ᵉ_ {A = A} ⁇² ∋ it

docs/agda-spec/src/Axiom/Set/Map.agda

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ mapPartial-uniq {f = f} prop {a} {b} {b'} p q =
289289
mapMaybeWithKeyᵐ : (A B Maybe B') Map A B Map A B'
290290
mapMaybeWithKeyᵐ f (rel , prop) = mapMaybeWithKey f rel , mapPartial-uniq {f = f} prop
291291

292+
mapFromPartialFun : (A Maybe B) Set A Map A B
293+
mapFromPartialFun f s = mapMaybeWithKeyᵐ (λ _ f) (idMap s)
294+
292295
module Restrictionᵐ (sp-∈ : spec-∈ A) where
293296
private module R = Restriction sp-∈
294297
open Unionᵐ sp-∈

docs/agda-spec/src/Interface/ComputationalRelation.agda

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,14 @@ module _ {STS : C → S → Sig → S → Type} (comp comp' : Computational STS
161161
compute-ext≡ = ExtendedRel-rightUnique comp
162162
(ExtendedRel-compute comp) (ExtendedRel-compute comp')
163163

164-
Computational⇒Dec' :
165-
⦃ _ : DecEq S ⦄ {STS : C S Sig S Type} ⦃ comp : Computational STS Err ⦄
166-
Dec (STS c s sig s')
167-
Computational⇒Dec' ⦃ comp = comp ⦄ = Computational⇒Dec comp
168-
169164
open Computational ⦃...⦄
170165

166+
Computational⇒Dec' :
167+
{STS : C S Sig S Type} ⦃ comp : Computational STS Err ⦄ Dec (∃[ s' ] STS c s sig s')
168+
Computational⇒Dec' with computeProof _ _ _ in eq
169+
... | success x = yes x
170+
... | failure x = no λ (_ , h) failure⇒∀¬STS (-, cong (map proj₁) eq) _ h
171+
171172
record InjectError Err₁ Err₂ : Type where
172173
field injectError : Err₁ Err₂
173174

docs/agda-spec/src/Interface/HasAdd/Instance.agda

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ module Interface.HasAdd.Instance where
44
open import Interface.HasAdd
55
open import Data.Integer as ℤ using (ℤ)
66
open import Data.Nat as ℕ using (ℕ)
7+
open import Data.String as Str
78

89
instance
910
addInt : HasAdd ℤ
1011
addInt ._+_ = ℤ._+_
1112

1213
addNat : HasAdd ℕ
1314
addNat ._+_ = ℕ._+_
15+
16+
addString : HasAdd String
17+
addString ._+_ = Str._++_

docs/agda-spec/src/Interface/HasOrder.agda

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module _ {a} {A : Set a} where
6464
<-asymmetric : Asymmetric _<_
6565
<-asymmetric = ≤-antisym⇒<-asym ≤-antisym
6666

67-
open IsEquivalence ≈-isEquivalence renaming (sym to ≈-sym)
67+
open IsEquivalence ≈-isEquivalence using () renaming (sym to ≈-sym) public
6868

6969
<-trans : Transitive _<_
7070
<-trans i<j j<k =
@@ -78,20 +78,51 @@ module _ {a} {A : Set a} where
7878
<⇒¬>⊎≈ x<y (inj₁ y<x) = <-asymmetric x<y y<x
7979
<⇒¬>⊎≈ x<y (inj₂ x≈y) = <-irrefl (≈-sym x≈y) x<y
8080

81+
≥⇒≮ : {x y} y ≤ x ¬ (x < y)
82+
≥⇒≮ y≤x x<y = contradiction (to ≤⇔<∨≈ y≤x) (<⇒¬>⊎≈ x<y)
83+
84+
open HasPartialOrder ⦃...⦄
85+
8186
record HasDecPartialOrder : Set (sucˡ a) where
8287
field
8388
⦃ hasPartialOrder ⦄ : HasPartialOrder
8489
⦃ dec-≤ ⦄ : _≤_ ⁇²
8590
⦃ dec-< ⦄ : _<_ ⁇²
8691

92+
record HasTotalOrder : Set (sucˡ a) where
93+
field
94+
⦃ hasPartialOrder ⦄ : HasPartialOrder
95+
≤-total : Total _≤_
96+
97+
≤-isTotalOrder : IsTotalOrder _≈_ _≤_
98+
≤-isTotalOrder = record { isPartialOrder = ≤-isPartialOrder ; total = ≤-total }
99+
100+
≮⇒≥ : Decidable _≈_ {x y} ¬ (x < y) y ≤ x
101+
≮⇒≥ _≈?_ {x} {y} x≮y with x ≈? y | ≤-total y x
102+
... | yes x≈y | _ = IsPreorder.reflexive ≤-isPreorder (≈-sym x≈y)
103+
... | _ | inj₁ y≤x = y≤x
104+
... | no x≉y | inj₂ x≤y = contradiction (≤∧≉⇒< (x≤y , x≉y)) x≮y
105+
106+
open HasTotalOrder ⦃...⦄
107+
108+
record HasDecTotalOrder : Set (sucˡ a) where
109+
field
110+
⦃ hasTotalOrder ⦄ : HasTotalOrder
111+
⦃ dec-≤ ⦄ : _≤_ ⁇²
112+
⦃ dec-< ⦄ : _<_ ⁇²
113+
87114
HasPreorder≡ = HasPreorder {_≈_ = _≡_}
88115
HasDecPreorder≡ = HasDecPreorder {_≈_ = _≡_}
89116
HasPartialOrder≡ = HasPartialOrder {_≈_ = _≡_}
90117
HasDecPartialOrder≡ = HasDecPartialOrder {_≈_ = _≡_}
118+
HasTotalOrder≡ = HasTotalOrder {_≈_ = _≡_}
119+
HasDecTotalOrder≡ = HasDecTotalOrder {_≈_ = _≡_}
91120

92121
open HasPreorder ⦃...⦄ public
93122
open HasPartialOrder ⦃...⦄ public hiding (hasPreorder)
94123
open HasDecPartialOrder ⦃...⦄ public hiding (hasPartialOrder)
124+
open HasTotalOrder ⦃...⦄ public hiding (hasPartialOrder)
125+
open HasDecTotalOrder ⦃...⦄ public hiding (hasTotalOrder)
95126

96127
module _ {a} {A : Set a} {_≈_ : Rel A a} where
97128

docs/agda-spec/src/Interface/HasOrder/Instance.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ instance
1616
ℕ-hasPartialOrder = HasPartialOrder ∋ record
1717
{ ≤-antisym = Nat.≤-antisym }
1818
ℕ-hasDecPartialOrder = HasDecPartialOrder {A = ℕ} ∋ record {}
19+
ℕ-hasTotalOrder = HasTotalOrder ∋ record
20+
{ ≤-total = Nat.≤-total }
21+
ℕ-hasDecTotalOrder = HasDecTotalOrder {A = ℕ} ∋ record {}
1922

2023
import Data.Integer.Properties as Int hiding (_≟_)
2124
ℤ-hasPreorder = HasPreorder ∋ record {Int; ≤⇔<∨≈ = let open Int in mk⇔
@@ -29,3 +32,6 @@ instance
2932
ℚ-hasPreorder = hasPreorderFromNonStrict Rat.≤-isPreorder _≟_
3033
ℚ-hasPartialOrder = HasPartialOrder ∋ record { ≤-antisym = Rat.≤-antisym }
3134
ℚ-hasDecPartialOrder = HasDecPartialOrder {A = ℚ} ∋ record {}
35+
ℚ-hasTotalOrder = HasTotalOrder ∋ record
36+
{ ≤-total = Rat.≤-total }
37+
ℚ-hasDecTotalOrder = HasDecTotalOrder {A = ℚ} ∋ record {}

docs/agda-spec/src/Interface/IsSet.agda

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ All-syntax : ∀ {A X} ⦃ _ : IsSet X A ⦄ → (A → Type) → X → Type
3131
All-syntax P X = All P (toSet X)
3232
syntax All-syntax (λ x P) l = ∀[ x ∈ l ] P
3333

34+
infix 2 Ex-syntax
35+
Ex-syntax : {A X} ⦃ _ : IsSet X A ⦄ (A Type) X Type
36+
Ex-syntax P X = Any P (toSet X)
37+
syntax Ex-syntax (λ x P) l = ∃[ x ∈ l ] P
38+
3439
module _ ⦃ _ : IsSet X (A × B) ⦄ where
3540
dom : X Set A
3641
dom = Rel.dom ∘ toSet

docs/agda-spec/src/InterfaceLibrary/Ledger.lagda

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ record LedgerInterface : Type₁ where
2727
NewEpochState : Type
2828
getPParams : NewEpochState → PParams
2929
getEpoch : NewEpochState → Epoch
30-
getPoolDistr : NewEpochState → PoolDistr
30+
getPoolDistr : NewEpochState → PoolDistr
3131
adoptGenesisDelegs : NewEpochState → Slot → NewEpochState
3232
_⊢_⇀⦇_,NEWEPOCH⦈_ : ⊤ → NewEpochState → Epoch → NewEpochState → Type
3333
\end{code}
34+
\begin{code}[hide]
35+
⦃ Computational-NEWEPOCH ⦄ : Computational _⊢_⇀⦇_,NEWEPOCH⦈_ String
36+
\end{code}
3437
\caption{Ledger interface}
3538
\label{fig:interface:ledger}
3639
\end{figure*}

docs/agda-spec/src/Ledger/BaseTypes.lagda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
\section{Base types}
1+
\section{Base Types}
22
\begin{code}[hide]
33
{-# OPTIONS --safe #-}
44

docs/agda-spec/src/Ledger/Crypto.lagda

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module Ledger.Crypto where
55

66
open import Ledger.Prelude hiding (T)
77

8+
module _ (M : Type↑) where
9+
_⁴ : ∀ {ℓ} {A B C D : Type ℓ} → (A → B → C → D → Type ℓ) → Type _
10+
_⁴ _~_~_~_ = ∀ {x y z w} → M (x ~ y ~ z ~ w)
11+
12+
_⁇⁴ = _⁇ ⁴
13+
814
\end{code}
915

1016
\subsection{Serialization}
@@ -20,7 +26,7 @@ record Serializer : Type₁ where
2026
\begin{code}
2127
Ser : Type
2228
encode : {T : Type} → T → Ser
23-
decode : {T : Type} → Ser → T
29+
decode : {T : Type} → Ser → Maybe T
2430
_∥_ : Ser → Ser → Ser
2531
\end{code}
2632
\emph{Properties}
@@ -29,7 +35,7 @@ record Serializer : Type₁ where
2935
enc-dec-correct :
3036
\end{code}
3137
\begin{code}
32-
∀ {T : Type} (x : T) → decode (encode x) ≡ x
38+
∀ {T : Type} (x : T) → decode (encode x) ≡ just x
3339
\end{code}
3440
\caption{Definitions for serialization}
3541
\label{fig:defs:serialization}
@@ -44,6 +50,7 @@ record isHashableSet (T : Type) : Set₁ where
4450
constructor mkIsHashableSet
4551
field THash : Type
4652
⦃ DecEq-THash ⦄ : DecEq THash
53+
⦃ Show-THash ⦄ : Show THash
4754
⦃ DecEq-T ⦄ : DecEq T
4855
⦃ T-Hashable ⦄ : Hashable T THash
4956
open isHashableSet
@@ -135,7 +142,7 @@ record KESScheme (srl : Serializer) : Type₁ where
135142
\end{code}
136143
\emph{Properties}
137144
\begin{code}[hide]
138-
field -- ⦃ Dec-isSigned ⦄ : isSigned ⁇⁴ -- TODO: Define ⁇⁴ if needed
145+
field ⦃ Dec-isSigned ⦄ : isSigned ⁇⁴
139146
isSigned-correct :
140147
\end{code}
141148
\begin{code}

0 commit comments

Comments
 (0)