Debian Codegraph @

tests/data/list/Main.agda

raw
1 {-# OPTIONS --guardedness --sized-types #-}
2
3 module Main where
4
5 open import Level
6 open import Data.List.Base as List using (List; _∷_; []; _++_; reverse)
7 open import Data.List.Zipper
8 import Data.List.Sort as Sort
9 open import Data.Maybe.Base
10 open import Data.Nat.Base
11 open import Data.Nat.Show using (show)
12 import Data.Nat.Properties as ℕₚ
13 open import Data.String.Base as String using (String)
14 import Data.Vec.Base as Vec
15
16 open import Codata.Sized.Stream using (nats; take)
17
18 open Sort ℕₚ.≤-decTotalOrder
19
20 open import IO.Base
21 open import IO.Finite
22 open import Function.Base using (_$_; _∘_)
23
24 private
25 variable
26 a : Level
27 A : Set a
28
29 data Direction : Set where Left Right : Direction
30
31 turn : Direction → Zipper A → Zipper A
32 turn Left zip = fromMaybe zip (left zip)
33 turn Right zip = fromMaybe zip (right zip)
34
35 follow : List Direction → Zipper A → Zipper A
36 follow dirs init = go dirs init where
37
38 go : List Direction → Zipper A → Zipper A
39 go [] zip = zip
40 go (d ∷ dirs) zip = go dirs (turn d zip)
41
42 updateFocus : (A → A) → Zipper A → Zipper A
43 updateFocus f (mkZipper ctx (a ∷ val)) = mkZipper ctx (f a ∷ val)
44 updateFocus f zip = zip
45
46 updateAt : List Direction → (A → A) → Zipper A → Zipper A
47 updateAt dirs f = updateFocus f ∘ follow dirs
48
49 applyAt : List Direction → (A → A) → List A → List A
50 applyAt dirs f xs = toList
51 $ updateFocus f
52 $ follow dirs
53 $ fromList xs
54
55 someNats : List ℕ
56 someNats = Vec.toList $ take 20 $ nats
57
58 otherNats : List ℕ
59 otherNats
60 = applyAt (Right ∷ Right ∷ []) (3 +_)
61 $ applyAt (List.replicate 10 Right ++ Left ∷ []) (10 +_)
62 $ applyAt (List.replicate 10 Left) (_∸ 5)
63 $ applyAt (Left ∷ Right ∷ Right ∷ Left ∷ []) (2 *_)
64 $ applyAt (List.replicate 5 Right) (_^ 2)
65 $ List.reverse someNats
66
67 chaoticNats : List ℕ
68 chaoticNats
69 = toList
70 $ updateAt (Right ∷ Right ∷ []) (3 +_)
71 $ updateAt (List.replicate 10 Right ++ Left ∷ []) (10 +_)
72 $ updateAt (List.replicate 10 Left) (_∸ 5)
73 $ updateAt (Left ∷ Right ∷ Right ∷ Left ∷ []) (2 *_)
74 $ updateAt (List.replicate 5 Right) (_^ 2)
75 $ fromList
76 $ List.reverse someNats
77
78 showNats : List ℕ → String
79 showNats ns = String.concat
80 $ "["
81 ∷ String.intersperse ", " (List.map show ns)
82 ∷ "]" ∷ []
83
84 main : Main
85 main = run $ do
86 putStrLn $ showNats someNats
87 putStrLn $ showNats otherNats
88 putStrLn $ showNats $ sort otherNats
89 putStrLn $ showNats chaoticNats
90 putStrLn $ showNats $ sort chaoticNats