|
| 1 | +{-# OPTIONS --guardedness #-} |
| 2 | + |
| 3 | +open import Data.Bool.Base using (Bool; true; false) |
| 4 | +open import Data.List.Base using (List; []; _∷_; zip; length) |
| 5 | +open import Data.Nat.Base using (ℕ; zero; suc; _≤′_; ≤′-reflexive; ≤′-step) |
| 6 | +open import Data.Nat.Properties using (_≤′?_) |
| 7 | +open import Data.Product.Base using (_×_; _,_) |
| 8 | +open import Data.Unit.Base using (⊤) |
| 9 | + |
| 10 | +open import Function.Base using (_$_) |
| 11 | + |
| 12 | +open import Relation.Binary.PropositionalEquality using (_≡_; refl) |
| 13 | +open import Relation.Nullary using (yes; no) |
| 14 | + |
| 15 | +variable |
| 16 | + A B C I O S T : Set |
| 17 | + |
| 18 | +------------------------------------------------------------------------ |
| 19 | +-- Adding indices to a list |
| 20 | + |
| 21 | +[_⋯_] : ℕ → ℕ → List ℕ |
| 22 | +[ m ⋯ n ] with m ≤′? n |
| 23 | +... | no ¬m≤n = [] |
| 24 | +... | yes m≤n = go [] _ _ m≤n where |
| 25 | + |
| 26 | + go : List ℕ → (m n : ℕ) (p : m ≤′ n) → List ℕ |
| 27 | + go acc m m (≤′-reflexive refl) = m ∷ acc |
| 28 | + go acc m (suc n) (≤′-step p) = go (suc n ∷ acc) m n p |
| 29 | + |
| 30 | + |
| 31 | +_ : [ 3 ⋯ 10 ] ≡ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ [] |
| 32 | +_ = refl |
| 33 | + |
| 34 | +-- Using zip |
| 35 | +addIndices : List A → List (ℕ × A) |
| 36 | +addIndices xs = zip [ 1 ⋯ length xs ] xs |
| 37 | + |
| 38 | +_ : (a b c : A) → addIndices (a ∷ b ∷ c ∷ []) |
| 39 | + ≡ (1 , a) ∷ (2 , b) ∷ (3 , c) ∷ [] |
| 40 | +_ = λ a b c → refl |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +------------------------------------------------------------------------ |
| 45 | +-- Adding indices to a list |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +record Stream (A : Set) : Set where |
| 50 | + coinductive |
| 51 | + field |
| 52 | + head : A |
| 53 | + tail : Stream A |
| 54 | +open Stream |
| 55 | + |
| 56 | +[_⋯] : ℕ → Stream ℕ |
| 57 | +[ n ⋯] .head = n |
| 58 | +[ n ⋯] .tail = [ suc n ⋯] |
| 59 | + |
| 60 | + |
| 61 | +∞zip : Stream A → List B → List (A × B) |
| 62 | +∞zip as [] = [] |
| 63 | +∞zip as (b ∷ bs) = (as .head , b) ∷ ∞zip (as .tail) bs |
| 64 | + |
| 65 | +addIndices′ : List A → List (ℕ × A) |
| 66 | +addIndices′ = ∞zip [ 1 ⋯] |
| 67 | + |
| 68 | +_ : (a b c : A) → addIndices′ (a ∷ b ∷ c ∷ []) |
| 69 | + ≡ (1 , a) ∷ (2 , b) ∷ (3 , c) ∷ [] |
| 70 | +_ = λ a b c → refl |
| 71 | + |
| 72 | + |
| 73 | +record ∞IOTree (I O A : Set) : Set |
| 74 | +data IOTree (I O : Set) (A : Set) : Set where |
| 75 | + pure : A → IOTree I O A |
| 76 | + ask : O → (I → ∞IOTree I O A) → IOTree I O A |
| 77 | + |
| 78 | +record ∞IOTree I O A where |
| 79 | + coinductive |
| 80 | + field force : IOTree I O A |
| 81 | + |
| 82 | +open ∞IOTree |
| 83 | + |
| 84 | + |
| 85 | +untilTrue : ℕ → IOTree Bool ⊤ ℕ |
| 86 | +untilTrue steps = ask _ reply where |
| 87 | + |
| 88 | + reply : Bool → ∞IOTree Bool ⊤ ℕ |
| 89 | + reply true .force = pure steps |
| 90 | + reply false .force = untilTrue (suc steps) |
| 91 | + |
| 92 | + |
| 93 | +open import Data.String.Base using (String) |
| 94 | + |
| 95 | +data Output : Set where |
| 96 | + Skip : Output |
| 97 | + Print : String → Output |
| 98 | + |
| 99 | +data Input : Set where |
| 100 | + Line : String → Input |
| 101 | + |
| 102 | +echo : IOTree Input Output ⊤ |
| 103 | +echo = ask Skip go where |
| 104 | + |
| 105 | + go : Input → ∞IOTree Input Output ⊤ |
| 106 | + go (Line str) .force = ask (Print str) go |
0 commit comments