Browse Source

Not sure if this is working.

master
Thomas Kerber 6 months ago
parent
commit
e9c05e676b
Signed by: Thomas Kerber <t.kerber@ed.ac.uk> GPG Key ID: 8489B911F9ED617B
1 changed files with 117 additions and 97 deletions
  1. 117
    97
      Yggdrasil/World.agda

+ 117
- 97
Yggdrasil/World.agda View File

@@ -1,17 +1,18 @@
1 1
 module Yggdrasil.World where
2 2
 
3 3
 open import Data.Bool using (Bool; T)
4
-open import Data.List using (List; _∷_; []; map)
4
+open import Data.List using (List; _∷_; []; map; _++_; length; lookup)
5 5
 open import Data.List.Any using (here; there)
6 6
 open import Data.List.Membership.Propositional using (_∈_)
7
+open import Data.Fin using (Fin)
7 8
 open import Data.Maybe using (Maybe; just; nothing) renaming (map to mmap)
8 9
 open import Data.Nat using (ℕ; suc)
9
-open import Data.Product as Π using (_×_; ∃; ∃-syntax; proj₁; proj₂) renaming (_,_ to ⟨_,_⟩)
10
+open import Data.Product as Π using (_×_; ∃; ∃-syntax; proj₁; proj₂; map₂) renaming (_,_ to ⟨_,_⟩)
10 11
 open import Data.String using (String)
11 12
 import Data.String.Unsafe as S
12 13
 open import Function using (id)
13 14
 open import Level using (Level; _⊔_; Lift) renaming (suc to lsuc; zero to lzero)
14
-open import Relation.Binary.PropositionalEquality using (_≡_; refl)
15
+open import Relation.Binary.PropositionalEquality using (_≡_; _≢_; refl)
15 16
 open import Relation.Nullary using (Dec; yes; no)
16 17
 
17 18
 -- Auto-lifted variant of ⊤.
@@ -21,128 +22,147 @@ data ⊤ {ℓ : Level} : Set ℓ where
21 22
 Table : ∀ {ℓ} → Set ℓ → Set ℓ
22 23
 Table A = List (String × A)
23 24
 
24
-lookup : ∀ {ℓ A} → Table {ℓ} A → String → Maybe A
25
-lookup [] _ = nothing
26
-lookup (⟨ s₁ , x ⟩ ∷ xs) s₂ with s₁ S.≟ s₂
27
-... | yes _ = just x
28
-... | no _  = lookup xs s₂
25
+_∪_ : ∀ {ℓ A} → Table {ℓ} A → Table {ℓ} A → Table {ℓ} A
26
+_∪_ = _++_
29 27
 
30 28
 module Weak where
29
+
31 30
   record WeakState {ℓ : Level} : Set (lsuc ℓ)
32 31
 
33
-  record WeakTransition {ℓ : Level} : Set (lsuc ℓ) where
32
+  record WeakTransition {ℓ : Level} (n : ℕ) : Set (lsuc ℓ) where
34 33
     inductive
35 34
     field
36 35
       A : Set ℓ
37 36
       B : A → Set ℓ
38
-      Σ : A → WeakState {ℓ}
37
+      Σ : A → Fin n
39 38
 
40 39
   record WeakState {ℓ} where
41 40
     inductive
42 41
     field
43
-      δ↑ : Table (WeakTransition {ℓ})
44
-      δ↓ : Table (WeakTransition {ℓ})
42
+      states : List (Set ℓ)
43
+      current : Fin (length states)
44
+      δ↑ : Fin (length states) → Table (WeakTransition {ℓ} (length states))
45
+      δ↓ : Fin (length states) → Table (WeakTransition {ℓ} (length states))
45 46
 
46 47
 open Weak using (WeakState; WeakTransition)
48
+open WeakState
47 49
 
48 50
 record State {ℓ : Level} (Σʷ : WeakState {ℓ}) : Set (lsuc ℓ)
49
-data Action {ℓ : Level} : {Σʷ₁ Σʷ₂ : WeakState {ℓ}} → (Σ₁ : Π.Σ (Set ℓ) id) →
51
+
52
+data Action {ℓ : Level} : {Σʷ₁ Σʷ₂ : WeakState {ℓ}} →
53
+    (Σ₁ : Fin (length (states Σʷ₁))) →
50 54
     (Σ₂ : State Σʷ₂) → Set ℓ → Set (lsuc ℓ)
51 55
 
52
-Transition↑ : {ℓ : Level} {Σʷ : WeakState {ℓ}} → (Σ₁ : Π.Σ (Set ℓ) id) →
53
-  WeakTransition {ℓ} → Set (lsuc ℓ)
54
-Transition↑ {Σʷ = Σʷ} Σ₁ T = (x : A T) → Π.Σ (State (Σ T x))
56
+Transition↑ : {ℓ : Level} {Σʷ : WeakState {ℓ}} →
57
+  (Σ₁ : Fin (length (states Σʷ))) → WeakTransition {ℓ} (length (states Σʷ)) →
58
+  Set (lsuc ℓ)
59
+Transition↑ {Σʷ = Σʷ} Σ₁ T = (x : A T) → Π.Σ
60
+    (State (record Σʷ { current = Σ T x }))
55 61
     (λ Σ₂ → Action {Σʷ₁ = Σʷ} Σ₁ Σ₂ (B T x))
56 62
   where open WeakTransition using (A; B; Σ)
57 63
 
58
-Transition↓ : {ℓ : Level} → WeakTransition {ℓ} → Set (lsuc ℓ)
59
-Transition↓ T = (x : A T) → State (Σ T x)
64
+Transition↓ : {ℓ : Level} {Σʷ : WeakState {ℓ}} →
65
+  WeakTransition {ℓ} (length (states Σʷ)) → Set ℓ
66
+Transition↓ {Σʷ = Σʷ} T = (x : A T) → Π.Σ (Fin (length (states Σʷ)))
67
+    (λ n → lookup (states Σʷ) n)
60 68
   where open WeakTransition using (A; Σ)
61 69
 
62
-data Transitions↑ {ℓ : Level} (Σ : Π.Σ (Set ℓ) id) :
63
-    Table (WeakTransition {ℓ}) → Set (lsuc ℓ) where
64
-  [] : Transitions↑ Σ []
65
-  _∷_ : ∀ {Σʷ T Ts name} → Transition↑ {Σʷ = Σʷ} Σ T → Transitions↑ Σ Ts →
66
-    Transitions↑ Σ (⟨ name , T ⟩ ∷ Ts)
67
-
68
-data Transitions↓ {ℓ : Level} :
69
-    Table (WeakTransition {ℓ}) → Set (lsuc ℓ) where
70
-  [] : Transitions↓ []
71
-  _∷_ : ∀ {T Ts name} → Transition↓ T → Transitions↓ Ts →
72
-    Transitions↓ (⟨ name , T ⟩ ∷ Ts)
73
-
70
+--data Transitions↑ {ℓ : Level} (Σ : Π.Σ (Set ℓ) id) :
71
+--    Table (WeakTransition {ℓ}) → Set (lsuc ℓ) where
72
+--  [] : Transitions↑ Σ []
73
+--  _∷_ : ∀ {Σʷ T Ts name} → Transition↑ {Σʷ = Σʷ} Σ T → Transitions↑ Σ Ts →
74
+--    Transitions↑ Σ (⟨ name , T ⟩ ∷ Ts)
75
+--
76
+--data Transitions↓ {ℓ : Level} :
77
+--    Table (WeakTransition {ℓ}) → Set (lsuc ℓ) where
78
+--  [] : Transitions↓ []
79
+--  _∷_ : ∀ {T Ts name} → Transition↓ T → Transitions↓ Ts →
80
+--    Transitions↓ (⟨ name , T ⟩ ∷ Ts)
81
+--
74 82
 record State {ℓ} Σʷ where
75
-  inductive
83
+  open WeakState
76 84
   field
77
-    Σ : Set ℓ
78
-    σ : Σ
79
-    δ↑ : Transitions↑ ⟨ Σ , σ ⟩ (WeakState.δ↑ Σʷ)
80
-    δ↓ : Transitions↓ (WeakState.δ↓ Σʷ)
81
-
82
-⌊_⌋ : ∀ {ℓ Σʷ} → (Σ : State {ℓ} Σʷ) → Π.Σ (Set ℓ) id
83
-⌊ Σ ⌋ = ⟨ State.Σ Σ , State.σ Σ ⟩
84
-
85
-lookup↓ : ∀ {ℓ xs δ} → δ ∈ xs → (Ts : Transitions↓ {ℓ} xs) → Transition↓ (proj₂ δ)
86
-lookup↓ (here refl) (x ∷ _) = x
87
-lookup↓ (there ∈xs) (_ ∷ xs) = lookup↓ ∈xs xs
88
-
89
-∈↓ʷ⇒∈↓ : ∀ {ℓ Σʷ δ} {Σ : State Σʷ} → δ ∈ (WeakState.δ↓ Σʷ) →
90
-  Transition↓ {ℓ} (proj₂ δ)
91
-∈↓ʷ⇒∈↓ {Σ = Σ} δ∈ = lookup↓ δ∈ (State.δ↓ Σ)
85
+    n : Fin (length (states Σʷ))
86
+    σ : lookup (states Σʷ) n
87
+--    δ↑ : Transitions↑ ⟨ Σ , σ ⟩ (WeakState.δ↑ Σʷ)
88
+--    δ↓ : Transitions↓ (WeakState.δ↓ Σʷ)
92 89
 --
93
---open Weak
90
+--⌊_⌋ : ∀ {ℓ Σʷ} → (Σ : State {ℓ} Σʷ) → Π.Σ (Set ℓ) id
91
+--⌊ Σ ⌋ = ⟨ State.Σ Σ , State.σ Σ ⟩
92
+--
93
+--lookup↓ : ∀ {ℓ xs δ} → δ ∈ xs → (Ts : Transitions↓ {ℓ} xs) → Transition↓ (proj₂ δ)
94
+--lookup↓ (here refl) (x ∷ _) = x
95
+--lookup↓ (there ∈xs) (_ ∷ xs) = lookup↓ ∈xs xs
96
+--
97
+--∈↓ʷ⇒∈↓ : ∀ {ℓ Σʷ δ} {Σ : State Σʷ} → δ ∈ (WeakState.δ↓ Σʷ) →
98
+--  Transition↓ {ℓ} (proj₂ δ)
99
+--∈↓ʷ⇒∈↓ {Σ = Σ} δ∈ = lookup↓ δ∈ (State.δ↓ Σ)
100
+----
101
+----open Weak
102
+----
94 103
 --
95
-
96 104
 data Action {ℓ} where
97
-  get    : ∀ {Σʷ Σ} → Action {ℓ} {Σʷ} {Σʷ} ⌊ Σ ⌋ Σ (State.Σ Σ)
98
-  set    : ∀ {Σʷ₁ Σʷ₂ Σ₁ Σ₂} → State.Σ Σ₂ → Action {ℓ} {Σʷ₁} {Σʷ₂} Σ₁ Σ₂ ⊤
99
-  call   : ∀ {Σʷ δ} {Σ : State Σʷ} → (δ∈ : δ ∈ WeakState.δ↓ Σʷ) →
100
-    (x : WeakTransition.A (proj₂ δ)) →
101
-    Action {ℓ} {Σʷ} {WeakTransition.Σ (proj₂ δ) x} ⌊ Σ ⌋
102
-      (∈↓ʷ⇒∈↓ {Σ = Σ} δ∈ x)
103
-      (WeakTransition.B (proj₂ δ) x)
104
-  return : ∀ {Σʷ Σ A} → Action {ℓ} {Σʷ} {Σʷ} ⌊ Σ ⌋ Σ A
105
-  _>>=_  : ∀ {Σʷ₁ Σʷ₂ Σʷ₃ Σ₁ Σ₂ Σ₃ A B} →
106
-    Action {ℓ} {Σʷ₁} {Σʷ₂} (⌊_⌋ {Σʷ = Σʷ₁} Σ₁) Σ₂ A →
107
-    (A → Action {ℓ} {Σʷ₂} {Σʷ₃} ⌊ Σ₂ ⌋ Σ₃ B) →
108
-    Action {ℓ} {Σʷ₁} {Σʷ₃} ⌊ Σ₁ ⌋ Σ₃ B
109
-
110
-record ParallelComposable {ℓ₁ ℓ₂ : Level} (A : Set ℓ₁) {P : A → A → Set ℓ₂} :
111
-    Set (ℓ₁ ⊔ ℓ₂) where
112
-  field
113
-    _||_ : A → A → A
114
-    _∘_  : (x : A) → (y : A) → {_ : P x y} → A
115
-
116
-data InterfaceMatches {ℓ : Level} : ℕ → (Σʷ₁ Σʷ₂ : WeakState {ℓ}) → Set (lsuc ℓ) where
117
-  unreachable : ∀ {Σʷ₁ Σʷ₂} → InterfaceMatches 0 Σʷ₁ Σʷ₂
118
-  match : ∀ {n Σʷ₁ Σʷ₂ δ₁} →
119
-    -- For each interface in the outgoing to Σʷ₁
120
-    δ₁ ∈ WeakState.δ↓ Σʷ₁ →
121
-    Π.Σ (String × WeakTransition {ℓ}) (λ δ₂ →
122
-      -- A corresponding interface exists in the incoming of Σʷ₂
123
-      (δ₂ ∈ WeakState.δ↑ Σʷ₂) ×
124
-      -- With the same name
125
-      (proj₁ δ₁ ≡ proj₁ δ₂) ×
126
-      -- The same input type
127
-      Π.Σ (WeakTransition.A (proj₂ δ₁) ≡ WeakTransition.A (proj₂ δ₂)) (λ{
128
-        refl → (x : WeakTransition.A (proj₂ δ₁)) →
129
-          -- The same output type
130
-          WeakTransition.B (proj₂ δ₁) x ≡ WeakTransition.B (proj₂ δ₂) x ×
131
-          -- And preserving the fact that interfaces keep matching.
132
-          InterfaceMatches n (WeakTransition.Σ (proj₂ δ₁) x)
133
-            (WeakTransition.Σ (proj₂ δ₂) x)
134
-    })) →
135
-    InterfaceMatches (suc n) Σʷ₁ Σʷ₂
136
-
137
-instance
138
-  WeakStateParallelComposable : ∀ {ℓ : Level} → ParallelComposable {lsuc ℓ} (WeakState {ℓ}) {λ Σʷ₁ Σʷ₂ → ∀ n → InterfaceMatches n Σʷ₁ Σʷ₂}
139
-  WeakStateParallelComposable = record
140
-    { _||_ = ?
141
-    ; _∘_  = λ Σʷ₁ Σʷ₂ → record
142
-      { δ↑ = ?
143
-      ; δ↓ = ?
144
-      }
145
-    }
105
+--  get    : ∀ {Σʷ Σ} → Action {ℓ} {Σʷ} {Σʷ} ⌊ Σ ⌋ Σ (State.Σ Σ)
106
+--  set    : ∀ {Σʷ₁ Σʷ₂ Σ₁ Σ₂} → State.Σ Σ₂ → Action {ℓ} {Σʷ₁} {Σʷ₂} Σ₁ Σ₂ ⊤
107
+--  call   : ∀ {Σʷ δ} {Σ : State Σʷ} → (δ∈ : δ ∈ WeakState.δ↓ Σʷ) →
108
+--    (x : WeakTransition.A (proj₂ δ)) →
109
+--    Action {ℓ} {Σʷ} {WeakTransition.Σ (proj₂ δ) x} ⌊ Σ ⌋
110
+--      (∈↓ʷ⇒∈↓ {Σ = Σ} δ∈ x)
111
+--      (WeakTransition.B (proj₂ δ) x)
112
+--  return : ∀ {Σʷ Σ A} → Action {ℓ} {Σʷ} {Σʷ} ⌊ Σ ⌋ Σ A
113
+--  _>>=_  : ∀ {Σʷ₁ Σʷ₂ Σʷ₃ Σ₁ Σ₂ Σ₃ A B} →
114
+--    Action {ℓ} {Σʷ₁} {Σʷ₂} (⌊_⌋ {Σʷ = Σʷ₁} Σ₁) Σ₂ A →
115
+--    (A → Action {ℓ} {Σʷ₂} {Σʷ₃} ⌊ Σ₂ ⌋ Σ₃ B) →
116
+--    Action {ℓ} {Σʷ₁} {Σʷ₃} ⌊ Σ₁ ⌋ Σ₃ B
117
+--
118
+--record ParallelComposable {ℓ₁ ℓ₂ : Level} (A : Set ℓ₁) {P : A → A → Set ℓ₂} :
119
+--    Set (ℓ₁ ⊔ ℓ₂) where
120
+--  field
121
+--    _||_ : A → A → A
122
+--    _∘_  : (x : A) → (y : A) → {_ : P x y} → A
123
+--
124
+--data InterfaceMatches {ℓ : Level} : ℕ → (Σʷ₁ Σʷ₂ : WeakState {ℓ}) → Set (lsuc ℓ) where
125
+--  unreachable : ∀ {Σʷ₁ Σʷ₂} → InterfaceMatches 0 Σʷ₁ Σʷ₂
126
+--  match : ∀ {n Σʷ₁ Σʷ₂ δ₁} →
127
+--    -- For each interface in the outgoing to Σʷ₁
128
+--    δ₁ ∈ WeakState.δ↓ Σʷ₁ →
129
+--    Π.Σ (String × WeakTransition {ℓ}) (λ δ₂ →
130
+--      -- A corresponding interface exists in the incoming of Σʷ₂
131
+--      (δ₂ ∈ WeakState.δ↑ Σʷ₂) ×
132
+--      -- With the same name
133
+--      (proj₁ δ₁ ≡ proj₁ δ₂) ×
134
+--      -- The same input type
135
+--      Π.Σ (WeakTransition.A (proj₂ δ₁) ≡ WeakTransition.A (proj₂ δ₂)) (λ{
136
+--        refl → (x : WeakTransition.A (proj₂ δ₁)) →
137
+--          -- The same output type
138
+--          WeakTransition.B (proj₂ δ₁) x ≡ WeakTransition.B (proj₂ δ₂) x ×
139
+--          -- And preserving the fact that interfaces keep matching.
140
+--          InterfaceMatches n (WeakTransition.Σ (proj₂ δ₁) x)
141
+--            (WeakTransition.Σ (proj₂ δ₂) x)
142
+--    })) →
143
+--    InterfaceMatches (suc n) Σʷ₁ Σʷ₂
144
+--
145
+--instance
146
+--  WeakStateParallelComposable : ∀ {ℓ : Level} → ParallelComposable {lsuc ℓ} (WeakState {ℓ}) {λ Σʷ₁ Σʷ₂ → ∀ n → InterfaceMatches n Σʷ₁ Σʷ₂}
147
+--  WeakStateParallelComposable = record
148
+--    { _||_ = _||_
149
+--    ; _∘_  = λ Σʷ₁ Σʷ₂ → record
150
+--      { δ↑ = ?
151
+--      ; δ↓ = ?
152
+--      }
153
+--    }
154
+--    where
155
+--      open WeakState
156
+--      open WeakTransition
157
+--      invariant₁ : ∀ {ℓ} → WeakState {ℓ} → WeakTransition {ℓ} → WeakTransition {ℓ}
158
+--      invariant₂ : ∀ {ℓ} → WeakState {ℓ} → WeakTransition {ℓ} → WeakTransition {ℓ}
159
+--      _||_ : ∀ {ℓ} → WeakState {ℓ} → WeakState {ℓ} → WeakState {ℓ}
160
+--      Σʷ₁ || Σʷ₂ = record
161
+--        { δ↑ = map (map₂ (invariant₂ Σʷ₂)) (δ↑ Σʷ₁) ∪ map (map₂ (invariant₁ Σʷ₁)) (δ↑ Σʷ₂)
162
+--        ; δ↓ = map (map₂ (invariant₂ Σʷ₂)) (δ↓ Σʷ₁) ∪ map (map₂ (invariant₁ Σʷ₁)) (δ↓ Σʷ₂)
163
+--        }
164
+--      invariant₁ Σʷ δ = record { A = A δ ; B = B δ ; Σ = λ x → Σʷ || Σ δ x }
165
+--      invariant₂ Σʷ δ = record { A = A δ ; B = B δ ; Σ = λ x → Σ δ x || Σʷ }
146 166
 --  ActionParallelComposable : ∀ {ℓ : Level} {Σʷ : WeakState {ℓ}} → (Σ : State Σʷ) → (A : Set ℓ) → ParallelComposable {ℓ} {P = InterfaceMatches
147 167
 --ParallelComposable 
148 168
 

Loading…
Cancel
Save