| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Pretty Printing |
| 5 |
-- This module is based on Jean-Philippe Bernardy's functional pearl |
| 6 |
-- "A Pretty But Not Greedy Printer" |
| 7 |
------------------------------------------------------------------------ |
| 8 |
|
| 9 |
{-# OPTIONS --with-K #-} |
| 10 |
|
| 11 |
module Text.Pretty.Core where |
| 12 |
|
| 13 |
import Level |
| 14 |
|
| 15 |
open import Data.Bool.Base using (Bool) |
| 16 |
open import Data.Irrelevant as Irrelevant using (Irrelevant) hiding (module Irrelevant) |
| 17 |
open import Data.List.Base as List using (List; []; _∷_) |
| 18 |
open import Data.Nat.Base using (ℕ; zero; suc; _+_; _⊔_; _≤_; z≤n) |
| 19 |
open import Data.Nat.Properties using (≤-refl; ≤-trans; +-identityʳ; |
| 20 |
module ≤-Reasoning; m≤n⊔m; +-monoʳ-≤; m≤m⊔n; +-comm; _≤?_) |
| 21 |
open import Data.Product.Base as Prod using (_×_; _,_; uncurry; proj₁; proj₂) |
| 22 |
import Data.Product.Relation.Unary.All as Allᴾ |
| 23 |
|
| 24 |
open import Data.Tree.Binary as Tree using (Tree; leaf; node; #nodes; mapₙ) |
| 25 |
open import Data.Tree.Binary.Relation.Unary.All as Allᵀ using (leaf; node) |
| 26 |
open import Data.Unit.Base using (⊤; tt) |
| 27 |
import Data.Tree.Binary.Relation.Unary.All.Properties as Allᵀ |
| 28 |
import Data.Tree.Binary.Properties as Tree |
| 29 |
|
| 30 |
open import Data.Maybe.Base as Maybe using (Maybe; nothing; just; maybe′) |
| 31 |
open import Data.Maybe.Relation.Unary.All as Allᴹ using (nothing; just) |
| 32 |
|
| 33 |
open import Data.String.Base as String |
| 34 |
using (String; length; replicate; _++_; unlines) |
| 35 |
import Data.String.Unsafe as String |
| 36 |
open import Function.Base using (_∘_; flip; _on_; id; _∘′_; _$_) |
| 37 |
open import Relation.Nullary.Decidable.Core using (Dec) |
| 38 |
open import Relation.Unary using (IUniversal; _⇒_; U) |
| 39 |
open import Relation.Binary.PropositionalEquality.Core |
| 40 |
using (_≡_; refl; sym; cong; cong₂; subst) |
| 41 |
open import Relation.Binary.PropositionalEquality.Properties |
| 42 |
using (module ≡-Reasoning) |
| 43 |
|
| 44 |
open import Data.Refinement using (Refinement-syntax; _,_; value; proof) |
| 45 |
import Data.Refinement.Relation.Unary.All as Allᴿ |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Block of text |
| 49 |
|
| 50 |
-- Content is a representation of the first line and the middle of the |
| 51 |
-- block. We use a tree rather than a list for the middle of the block |
| 52 |
-- so that we can extend it with lines on the left and on the line for |
| 53 |
-- free. We will ultimately render the block by traversing the tree left |
| 54 |
-- to right in a depth-first manner. |
| 55 |
|
| 56 |
Content : Set |
| 57 |
Content = Maybe (String × Tree String ⊤) |
| 58 |
|
| 59 |
size : Content → ℕ |
| 60 |
size = maybe′ (suc ∘ #nodes ∘ proj₂) 0 |
| 61 |
|
| 62 |
All : ∀ {p} (P : String → Set p) → (Content → Set p) |
| 63 |
All P = Allᴹ.All (Allᴾ.All P (Allᵀ.All P U)) |
| 64 |
|
| 65 |
All≤ : ℕ → Content → Set |
| 66 |
All≤ n = All (λ s → length s ≤ n) |
| 67 |
|
| 68 |
record Block : Set where |
| 69 |
field |
| 70 |
height : ℕ |
| 71 |
block : [ xs ∈ Content ∣ size xs ≡ height ] |
| 72 |
-- last line |
| 73 |
lastWidth : ℕ |
| 74 |
last : [ s ∈ String ∣ length s ≡ lastWidth ] |
| 75 |
-- max of all the widths |
| 76 |
maxWidth : [ n ∈ ℕ ∣ lastWidth ≤ n × All≤ n (block .value) ] |
| 77 |
|
| 78 |
------------------------------------------------------------------------ |
| 79 |
-- Raw string |
| 80 |
|
| 81 |
text : String → Block |
| 82 |
text s = record |
| 83 |
{ height = 0 |
| 84 |
; block = nothing , ⦇ refl ⦈ |
| 85 |
; lastWidth = width |
| 86 |
; last = s , ⦇ refl ⦈ |
| 87 |
; maxWidth = width , ⦇ (≤-refl , nothing) ⦈ |
| 88 |
} where width = length s; open Irrelevant |
| 89 |
|
| 90 |
------------------------------------------------------------------------ |
| 91 |
-- Empty |
| 92 |
|
| 93 |
empty : Block |
| 94 |
empty = text "" |
| 95 |
|
| 96 |
------------------------------------------------------------------------ |
| 97 |
-- Helper functions |
| 98 |
|
| 99 |
node? : Content → String → Tree String ⊤ → Content |
| 100 |
node? (just (x , xs)) y ys = just (x , node xs y ys) |
| 101 |
node? nothing y ys = just (y , ys) |
| 102 |
|
| 103 |
∣node?∣ : ∀ b y ys → size (node? b y ys) |
| 104 |
≡ size b + suc (#nodes ys) |
| 105 |
∣node?∣ (just (x , xs)) y ys = refl |
| 106 |
∣node?∣ nothing y ys = refl |
| 107 |
|
| 108 |
≤-Content : ∀ {m n} {b : Content} → m ≤ n → All≤ m b → All≤ n b |
| 109 |
≤-Content {m} {n} m≤n = Allᴹ.map (Prod.map step (Allᵀ.mapₙ step)) |
| 110 |
|
| 111 |
where |
| 112 |
|
| 113 |
step : ∀ {p} → p ≤ m → p ≤ n |
| 114 |
step = flip ≤-trans m≤n |
| 115 |
|
| 116 |
All≤-node? : ∀ {l m r n} → |
| 117 |
All≤ n l → length m ≤ n → Allᵀ.All (λ s → length s ≤ n) U r → |
| 118 |
All≤ n (node? l m r) |
| 119 |
All≤-node? nothing py pys = just (py , pys) |
| 120 |
All≤-node? (just (px , pxs)) py pys = just (px , node pxs py pys) |
| 121 |
|
| 122 |
------------------------------------------------------------------------ |
| 123 |
-- Appending two documents |
| 124 |
|
| 125 |
private |
| 126 |
module append (x y : Block) where |
| 127 |
|
| 128 |
module x = Block x |
| 129 |
module y = Block y |
| 130 |
|
| 131 |
blockx = x.block .value |
| 132 |
blocky = y.block .value |
| 133 |
|
| 134 |
widthx = x.maxWidth .value |
| 135 |
widthy = y.maxWidth .value |
| 136 |
|
| 137 |
lastx = x.last .value |
| 138 |
lasty = y.last .value |
| 139 |
|
| 140 |
height : ℕ |
| 141 |
height = (_+_ on Block.height) x y |
| 142 |
|
| 143 |
lastWidth : ℕ |
| 144 |
lastWidth = (_+_ on Block.lastWidth) x y |
| 145 |
|
| 146 |
pad : Maybe String |
| 147 |
pad with x.lastWidth |
| 148 |
... | 0 = nothing |
| 149 |
... | l@(suc _) = just (replicate l ' ') |
| 150 |
|
| 151 |
size-pad : maybe′ length 0 pad ≡ x.lastWidth |
| 152 |
size-pad with x.lastWidth |
| 153 |
... | 0 = refl |
| 154 |
... | l@(suc _) = String.length-replicate l |
| 155 |
|
| 156 |
indent : Maybe String → String → String |
| 157 |
indent = maybe′ _++_ id |
| 158 |
|
| 159 |
size-indent : ∀ ma str → length (indent ma str) |
| 160 |
≡ maybe′ length 0 ma + length str |
| 161 |
size-indent nothing str = refl |
| 162 |
size-indent (just pad) str = String.length-++ pad str |
| 163 |
|
| 164 |
indents : Maybe String → Tree String ⊤ → Tree String ⊤ |
| 165 |
indents = maybe′ (mapₙ ∘ _++_) id |
| 166 |
|
| 167 |
size-indents : ∀ ma t → #nodes (indents ma t) ≡ #nodes t |
| 168 |
size-indents nothing t = refl |
| 169 |
size-indents (just pad) t = Tree.#nodes-mapₙ (pad ++_) t |
| 170 |
|
| 171 |
unfold-indents : ∀ ma t → indents ma t ≡ mapₙ (indent ma) t |
| 172 |
unfold-indents nothing t = sym (Tree.map-id t) |
| 173 |
unfold-indents (just pad) t = refl |
| 174 |
|
| 175 |
vContent : Content × String |
| 176 |
vContent with blocky |
| 177 |
... | nothing = blockx |
| 178 |
, lastx ++ lasty |
| 179 |
... | just (hd , tl) = node? |
| 180 |
{-,--------------,-} |
| 181 |
{-|-} blockx {-|-} |
| 182 |
{-|-} {-'---,-} {-,------------------,-} |
| 183 |
{-|-} (lastx {-|-} ++ {-|-} hd) {-|-} |
| 184 |
{-'------------------'-} {-|-} {-|-} |
| 185 |
(indents pad {-|-} tl) {-,----'-} |
| 186 |
, indent pad {-|-} lasty {-|-} |
| 187 |
{-'-------------'-} |
| 188 |
|
| 189 |
vBlock = proj₁ vContent |
| 190 |
vLast = proj₂ vContent |
| 191 |
|
| 192 |
isBlock : size blockx ≡ x.height → size blocky ≡ y.height → |
| 193 |
size vBlock ≡ height |
| 194 |
isBlock ∣x∣ ∣y∣ with blocky |
| 195 |
... | nothing = begin |
| 196 |
size blockx ≡⟨ ∣x∣ ⟩ |
| 197 |
x.height ≡⟨ +-identityʳ x.height ⟨ |
| 198 |
x.height + 0 ≡⟨ cong (_ +_) ∣y∣ ⟩ |
| 199 |
x.height + y.height ∎ where open ≡-Reasoning |
| 200 |
... | just (hd , tl) = begin |
| 201 |
∣node∣ ≡⟨ ∣node?∣ blockx middle rest ⟩ |
| 202 |
∣blockx∣ + suc (#nodes rest) ≡⟨ cong ((size blockx +_) ∘′ suc) ∣rest∣ ⟩ |
| 203 |
∣blockx∣ + suc (#nodes tl) ≡⟨ cong₂ _+_ ∣x∣ ∣y∣ ⟩ |
| 204 |
x.height + y.height ∎ where |
| 205 |
|
| 206 |
open ≡-Reasoning |
| 207 |
∣blockx∣ = size blockx |
| 208 |
middle = lastx ++ hd |
| 209 |
rest = indents pad tl |
| 210 |
∣rest∣ = size-indents pad tl |
| 211 |
∣node∣ = size (node? blockx middle rest) |
| 212 |
|
| 213 |
block : [ xs ∈ Content ∣ size xs ≡ height ] |
| 214 |
block .value = vBlock |
| 215 |
block .proof = ⦇ isBlock (Block.block x .proof) (Block.block y .proof) ⦈ |
| 216 |
where open Irrelevant |
| 217 |
|
| 218 |
isLastLine : length lastx ≡ x.lastWidth → |
| 219 |
length lasty ≡ y.lastWidth → |
| 220 |
length vLast ≡ lastWidth |
| 221 |
isLastLine ∣x∣ ∣y∣ with blocky |
| 222 |
... | nothing = begin |
| 223 |
length (lastx ++ lasty) ≡⟨ String.length-++ lastx lasty ⟩ |
| 224 |
length lastx + length lasty ≡⟨ cong₂ _+_ ∣x∣ ∣y∣ ⟩ |
| 225 |
x.lastWidth + y.lastWidth ∎ where open ≡-Reasoning |
| 226 |
... | just (hd , tl) = begin |
| 227 |
length (indent pad lasty) ≡⟨ size-indent pad lasty ⟩ |
| 228 |
maybe′ length 0 pad + length lasty ≡⟨ cong₂ _+_ size-pad ∣y∣ ⟩ |
| 229 |
x.lastWidth + y.lastWidth ∎ where open ≡-Reasoning |
| 230 |
|
| 231 |
last : [ s ∈ String ∣ length s ≡ lastWidth ] |
| 232 |
last .value = vLast |
| 233 |
last .proof = ⦇ isLastLine (Block.last x .proof) (Block.last y .proof) ⦈ |
| 234 |
where open Irrelevant |
| 235 |
|
| 236 |
vMaxWidth : ℕ |
| 237 |
vMaxWidth = widthx ⊔ (x.lastWidth + widthy) |
| 238 |
|
| 239 |
isMaxWidth₁ : y.lastWidth ≤ widthy → lastWidth ≤ vMaxWidth |
| 240 |
isMaxWidth₁ p = begin |
| 241 |
lastWidth ≤⟨ +-monoʳ-≤ x.lastWidth p ⟩ |
| 242 |
x.lastWidth + widthy ≤⟨ m≤n⊔m _ _ ⟩ |
| 243 |
vMaxWidth ∎ where open ≤-Reasoning |
| 244 |
|
| 245 |
isMaxWidth₂ : length lastx ≡ x.lastWidth → |
| 246 |
x.lastWidth ≤ widthx → |
| 247 |
All≤ widthx blockx → |
| 248 |
All≤ widthy blocky → |
| 249 |
All≤ vMaxWidth vBlock |
| 250 |
isMaxWidth₂ ∣x∣≡ ∣x∣≤ ∣xs∣ ∣ys∣ with blocky |
| 251 |
... | nothing = ≤-Content (m≤m⊔n _ _) ∣xs∣ |
| 252 |
isMaxWidth₂ ∣x∣≡ ∣x∣≤ ∣xs∣ (just (∣hd∣ , ∣tl∣)) |
| 253 |
| just (hd , tl) = |
| 254 |
All≤-node? (≤-Content (m≤m⊔n _ _) ∣xs∣) |
| 255 |
middle |
| 256 |
(subst (Allᵀ.All _ U) (sym $ unfold-indents pad tl) |
| 257 |
$ Allᵀ.mapₙ⁺ (indent pad) (Allᵀ.mapₙ (indented _) ∣tl∣)) |
| 258 |
where |
| 259 |
|
| 260 |
middle : length (lastx ++ hd) ≤ vMaxWidth |
| 261 |
middle = begin |
| 262 |
length (lastx ++ hd) ≡⟨ String.length-++ lastx hd ⟩ |
| 263 |
length lastx + length hd ≡⟨ cong (_+ _) ∣x∣≡ ⟩ |
| 264 |
x.lastWidth + length hd ≤⟨ +-monoʳ-≤ x.lastWidth ∣hd∣ ⟩ |
| 265 |
x.lastWidth + widthy ≤⟨ m≤n⊔m _ _ ⟩ |
| 266 |
vMaxWidth ∎ where open ≤-Reasoning |
| 267 |
|
| 268 |
indented : ∀ s → length s ≤ widthy → |
| 269 |
length (indent pad s) ≤ vMaxWidth |
| 270 |
indented s ∣s∣ = begin |
| 271 |
length (indent pad s) ≡⟨ size-indent pad s ⟩ |
| 272 |
maybe′ length 0 pad + length s ≡⟨ cong (_+ _) size-pad ⟩ |
| 273 |
x.lastWidth + length s ≤⟨ +-monoʳ-≤ x.lastWidth ∣s∣ ⟩ |
| 274 |
x.lastWidth + widthy ≤⟨ m≤n⊔m (widthx) _ ⟩ |
| 275 |
vMaxWidth ∎ where open ≤-Reasoning |
| 276 |
|
| 277 |
maxWidth : [ n ∈ ℕ ∣ lastWidth ≤ n × All≤ n vBlock ] |
| 278 |
maxWidth .value = vMaxWidth |
| 279 |
maxWidth .proof = |
| 280 |
⦇ _,_ ⦇ isMaxWidth₁ (map proj₁ (Block.maxWidth y .proof)) ⦈ |
| 281 |
⦇ isMaxWidth₂ (Block.last x .proof) |
| 282 |
(map proj₁ (Block.maxWidth x .proof)) |
| 283 |
(map proj₂ (Block.maxWidth x .proof)) |
| 284 |
(map proj₂ (Block.maxWidth y .proof)) |
| 285 |
⦈ |
| 286 |
⦈ where open Irrelevant |
| 287 |
|
| 288 |
infixl 4 _<>_ |
| 289 |
_<>_ : Block → Block → Block |
| 290 |
x <> y = record { append x y } |
| 291 |
|
| 292 |
------------------------------------------------------------------------ |
| 293 |
-- Flush (introduces a new line) |
| 294 |
|
| 295 |
private |
| 296 |
module flush (x : Block) where |
| 297 |
|
| 298 |
module x = Block x |
| 299 |
blockx = x.block .value |
| 300 |
lastx = x.last .value |
| 301 |
widthx = x.maxWidth .value |
| 302 |
heightx = x.height |
| 303 |
|
| 304 |
height = suc heightx |
| 305 |
lastWidth = 0 |
| 306 |
vMaxWidth = widthx |
| 307 |
|
| 308 |
last : [ s ∈ String ∣ length s ≡ lastWidth ] |
| 309 |
last = "" , ⦇ refl ⦈ where open Irrelevant |
| 310 |
|
| 311 |
vContent = node? blockx lastx (leaf tt) |
| 312 |
|
| 313 |
isBlock : size blockx ≡ heightx → size vContent ≡ height |
| 314 |
isBlock ∣x∣ = begin |
| 315 |
size vContent ≡⟨ ∣node?∣ blockx lastx (leaf tt) ⟩ |
| 316 |
size blockx + 1 ≡⟨ cong (_+ 1) ∣x∣ ⟩ |
| 317 |
heightx + 1 ≡⟨ +-comm heightx 1 ⟩ |
| 318 |
height ∎ where open ≡-Reasoning |
| 319 |
|
| 320 |
block : [ xs ∈ Content ∣ size xs ≡ height ] |
| 321 |
block .value = vContent |
| 322 |
block .proof = Irrelevant.map isBlock $ Block.block x .proof |
| 323 |
|
| 324 |
maxWidth : [ n ∈ ℕ ∣ lastWidth ≤ n × All≤ n vContent ] |
| 325 |
maxWidth .value = widthx |
| 326 |
maxWidth .proof = map (z≤n ,_) |
| 327 |
⦇ All≤-node? ⦇ proj₂ (Block.maxWidth x .proof) ⦈ |
| 328 |
⦇ middle (Block.last x .proof) ⦇ proj₁ (Block.maxWidth x .proof) ⦈ ⦈ |
| 329 |
(pure (leaf tt)) |
| 330 |
⦈ where |
| 331 |
|
| 332 |
open Irrelevant |
| 333 |
|
| 334 |
middle : length lastx ≡ x.lastWidth → x.lastWidth ≤ vMaxWidth → |
| 335 |
length lastx ≤ vMaxWidth |
| 336 |
middle p q = begin |
| 337 |
length lastx ≡⟨ p ⟩ |
| 338 |
x.lastWidth ≤⟨ q ⟩ |
| 339 |
vMaxWidth ∎ where open ≤-Reasoning |
| 340 |
|
| 341 |
flush : Block → Block |
| 342 |
flush x = record { flush x } |
| 343 |
|
| 344 |
------------------------------------------------------------------------ |
| 345 |
-- Other functions |
| 346 |
|
| 347 |
render : Block → String |
| 348 |
render x = unlines |
| 349 |
$ maybe′ (uncurry (λ hd tl → hd ∷ Tree.Infix.toList tl)) [] |
| 350 |
$ node? (Block.block x .value) (Block.last x .value) (leaf tt) |
| 351 |
|
| 352 |
valid : (width : ℕ) (b : Block) → Dec (Block.maxWidth b .value ≤ width) |
| 353 |
valid width b = Block.maxWidth b .value ≤? width |