Debian Codegraph @

src/Data/Tree/Rose/Show.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- 1 dimensional pretty printing of rose trees
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --sized-types #-}
8
9 module Data.Tree.Rose.Show where
10
11 open import Level using (Level)
12 open import Size
13 open import Data.Bool.Base using (Bool; true; false; if_then_else_; _∧_)
14 open import Data.DifferenceList as DList renaming (DiffList to DList) using ()
15 open import Data.List.Base as List using (List; []; [_]; _∷_; _∷ʳ_)
16 open import Data.Nat.Base using (ℕ; _∸_)
17 open import Data.Product.Base using (_×_; _,_)
18 open import Data.String.Base using (String; _++_)
19 open import Data.Tree.Rose using (Rose; node; map; fromBinary)
20 open import Function.Base using (flip; _∘′_; id)
21
22 private
23 variable
24 a : Level
25 A : Set a
26 i : Size
27
28 display : Rose (List String) i → List String
29 display t = DList.toList (go (([] , t) ∷ []))
30 where
31 padding : Bool → List Bool → String → String
32 padding dir? [] str = str
33 padding dir? (b ∷ bs) str =
34 (if dir? ∧ List.null bs
35 then if b then " ├ " else " └ "
36 else if b then " │ " else " ")
37 ++ padding dir? bs str
38
39 nodePrefixes : List A → List Bool
40 nodePrefixes as = true ∷ List.replicate (List.length as ∸ 1) false
41
42 childrenPrefixes : List A → List Bool
43 childrenPrefixes as = List.replicate (List.length as ∸ 1) true ∷ʳ false
44
45 go : List (List Bool × Rose (List String) i) → DList String
46 go [] = DList.[]
47 go ((bs , node a ts₁) ∷ ts) =
48 let bs′ = List.reverse bs in
49 DList.fromList (List.zipWith (flip padding bs′) (nodePrefixes a) a)
50 DList.++ go (List.zip (List.map (_∷ bs) (childrenPrefixes ts₁)) ts₁)
51 DList.++ go ts
52
53 show : (A → List String) → Rose A i → List String
54 show toString = display ∘′ map toString
55
56 showSimple : (A → String) → Rose A i → List String
57 showSimple toString = show ([_] ∘′ toString)