Debian Codegraph @

tests/data/colist/Main.agda

raw
1 {-# OPTIONS --guardedness --sized-types #-}
2
3 module Main where
4
5 open import Level using (0ℓ)
6 open import Size
7 open import Data.Bool.Base using (Bool; true; false; if_then_else_)
8 open import Data.Nat.Base using (ℕ; zero; suc)
9 open import Data.Fin
10 import Data.Fin.Show as Fin
11 open import Data.String.Base using (String; _++_; parens)
12 open import Data.Unit.Polymorphic.Base using (⊤)
13 open import Codata.Sized.Thunk
14 open import Function.Base using (_$_; _∘_; id)
15 open import Relation.Nullary
16
17 open import Codata.Musical.Notation
18 open import Codata.Sized.Colist using (Colist; _∷_; [])
19 open import Codata.Musical.Colist renaming (Colist to Colist♩) using (_∷_; [])
20 open import Codata.Musical.Conversion
21
22 variable
23 i : Size
24 m n : ℕ
25 A : Set
26
27 data Lam (n : ℕ) : Set where
28 var : Fin n → Lam n
29 app : Lam n → Lam n → Lam n
30 lam : Lam (suc n) → Lam n
31
32 data Loc : Set where appL appR lam : Loc
33
34 appParens : Loc → String → String
35 appParens appR str = parens str
36 appParens _ str = str
37
38 lamParens : Loc → String → String
39 lamParens lam str = str
40 lamParens _ str = parens str
41
42 show : Loc → Lam n → String
43 show i (var v) = Fin.show v
44 show i (app f t) = appParens i $ show appL f ++ " " ++ show appR t
45 show i (lam b) = lamParens i $ "λ " ++ show lam b
46
47 variable
48 b f t : Lam n
49
50 _∙_ : (Fin m → A) → A → Fin (suc m) → A
51 (ρ ∙ v) zero = v
52 (ρ ∙ v) (suc k) = ρ k
53
54 rename : (Fin m → Fin n) → Lam m → Lam n
55 rename ρ (var v) = var (ρ v)
56 rename ρ (app f t) = app (rename ρ f) (rename ρ t)
57 rename ρ (lam b) = lam (rename ((suc ∘ ρ) ∙ zero) b)
58
59 subst : (Fin m → Lam n) → Lam m → Lam n
60 subst ρ (var v) = ρ v
61 subst ρ (app f t) = app (subst ρ f) (subst ρ t)
62 subst ρ (lam b) = lam (subst ((rename suc ∘ ρ) ∙ var zero) b)
63
64 data Redex {n} : Lam n → Set where
65 here : ∀ b t → Redex (app (lam b) t)
66 lam : Redex b → Redex (lam b)
67 appL : Redex f → ∀ t → Redex (app f t)
68 appR : ∀ f → Redex t → Redex (app f t)
69
70 redex : ∀ {n} (t : Lam n) → Dec (Redex t)
71 redex (var v) = no (λ ())
72 redex (app (lam b) t) = yes (here b t)
73 redex (app f@(var _) t) with redex t
74 ... | yes rt = yes (appR f rt)
75 ... | no nrt = no λ where (appR _ rt) → nrt rt
76 redex (app f@(app _ _) t) with redex f | redex t
77 ... | yes rf | _ = yes (appL rf t)
78 ... | _ | yes rt = yes (appR f rt)
79 ... | no nrf | no nrt = no λ where
80 (appL rf _) → nrf rf
81 (appR _ rt) → nrt rt
82 redex (lam b) with redex b
83 ... | yes rb = yes (lam rb)
84 ... | no nrb = no λ where (lam rb) → nrb rb
85
86 fire : Redex {n} t → Lam n
87 fire (here b t) = subst (var ∙ t) b
88 fire (lam rt) = lam (fire rt)
89 fire (appL rf t) = app (fire rf) t
90 fire (appR f rt) = app f (fire rt)
91
92 eval : Lam n → Colist (Lam n) i
93 eval t with redex t
94 ... | yes rt = t ∷ λ where .force → eval (fire rt)
95 ... | no nrt = t ∷ λ where .force → []
96
97 open import IO.Base
98 open import IO.Finite
99
100 trace : Colist♩ (Lam n) → IO {0ℓ} ⊤
101 trace [] = pure _
102 trace (t ∷ ts) = seq (♯ putStrLn (show lam t))
103 (♯ trace (♭ ts))
104
105 `id : Lam 0
106 `id = lam (var (# 0))
107
108 main : Main
109 main = run $ do
110 trace (Colist.toMusical $ eval `id)
111 trace (Colist.toMusical $ eval (app `id (app (app `id `id) (app `id `id))))