/usr/share/doc/libghc-hledger-lib-doc/html/src/Hledger-Utils.html is in libghc-hledger-lib-doc 0.26-3build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>Hledger/Utils.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# LANGUAGE FlexibleContexts #-}</span>
<a name="line-2"></a><span class='hs-comment'>{-|
<a name="line-3"></a>
<a name="line-4"></a>Standard imports and utilities which are useful everywhere, or needed low
<a name="line-5"></a>in the module hierarchy. This is the bottom of hledger's module graph.
<a name="line-6"></a>
<a name="line-7"></a>-}</span>
<a name="line-8"></a>
<a name="line-9"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Hledger</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span> <span class='hs-layout'>(</span><span class='hs-comment'>---- provide these frequently used modules - or not, for clearer api:</span>
<a name="line-10"></a> <span class='hs-comment'>-- module Control.Monad,</span>
<a name="line-11"></a> <span class='hs-comment'>-- module Data.List,</span>
<a name="line-12"></a> <span class='hs-comment'>-- module Data.Maybe,</span>
<a name="line-13"></a> <span class='hs-comment'>-- module Data.Time.Calendar,</span>
<a name="line-14"></a> <span class='hs-comment'>-- module Data.Time.Clock,</span>
<a name="line-15"></a> <span class='hs-comment'>-- module Data.Time.LocalTime,</span>
<a name="line-16"></a> <span class='hs-comment'>-- module Data.Tree,</span>
<a name="line-17"></a> <span class='hs-comment'>-- module Text.RegexPR,</span>
<a name="line-18"></a> <span class='hs-comment'>-- module Test.HUnit,</span>
<a name="line-19"></a> <span class='hs-comment'>-- module Text.Printf,</span>
<a name="line-20"></a> <span class='hs-comment'>---- all of this one:</span>
<a name="line-21"></a> <span class='hs-keyword'>module</span> <span class='hs-conid'>Hledger</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span><span class='hs-layout'>,</span>
<a name="line-22"></a> <span class='hs-keyword'>module</span> <span class='hs-conid'>Hledger</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span><span class='hs-varop'>.</span><span class='hs-conid'>Debug</span><span class='hs-layout'>,</span>
<a name="line-23"></a> <span class='hs-keyword'>module</span> <span class='hs-conid'>Hledger</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span><span class='hs-varop'>.</span><span class='hs-conid'>Regex</span><span class='hs-layout'>,</span>
<a name="line-24"></a> <span class='hs-comment'>-- Debug.Trace.trace,</span>
<a name="line-25"></a> <span class='hs-comment'>-- module Data.PPrint,</span>
<a name="line-26"></a> <span class='hs-comment'>-- module Hledger.Utils.UTF8IOCompat</span>
<a name="line-27"></a> <span class='hs-conid'>SystemString</span><span class='hs-layout'>,</span><span class='hs-varid'>fromSystemString</span><span class='hs-layout'>,</span><span class='hs-varid'>toSystemString</span><span class='hs-layout'>,</span><span class='hs-varid'>error'</span><span class='hs-layout'>,</span><span class='hs-varid'>userError'</span><span class='hs-layout'>,</span>
<a name="line-28"></a> <span class='hs-comment'>-- the rest need to be done in each module I think</span>
<a name="line-29"></a> <span class='hs-layout'>)</span>
<a name="line-30"></a><span class='hs-keyword'>where</span>
<a name="line-31"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span> <span class='hs-layout'>(</span><span class='hs-varid'>liftM</span><span class='hs-layout'>)</span>
<a name="line-32"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>IO</span><span class='hs-varop'>.</span><span class='hs-conid'>Class</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadIO</span><span class='hs-layout'>,</span> <span class='hs-varid'>liftIO</span><span class='hs-layout'>)</span>
<a name="line-33"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Char</span>
<a name="line-34"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>List</span>
<a name="line-35"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>M</span>
<a name="line-36"></a><span class='hs-comment'>-- import Data.Maybe</span>
<a name="line-37"></a><span class='hs-comment'>-- import Data.PPrint</span>
<a name="line-38"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Time</span><span class='hs-varop'>.</span><span class='hs-conid'>Clock</span>
<a name="line-39"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Time</span><span class='hs-varop'>.</span><span class='hs-conid'>LocalTime</span>
<a name="line-40"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Tree</span>
<a name="line-41"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Directory</span> <span class='hs-layout'>(</span><span class='hs-varid'>getHomeDirectory</span><span class='hs-layout'>)</span>
<a name="line-42"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>FilePath</span><span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varop'></></span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-varid'>isRelative</span><span class='hs-layout'>)</span>
<a name="line-43"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>IO</span>
<a name="line-44"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Test</span><span class='hs-varop'>.</span><span class='hs-conid'>HUnit</span>
<a name="line-45"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Text</span><span class='hs-varop'>.</span><span class='hs-conid'>Parsec</span>
<a name="line-46"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Text</span><span class='hs-varop'>.</span><span class='hs-conid'>Printf</span>
<a name="line-47"></a><span class='hs-comment'>-- import qualified Data.Map as Map</span>
<a name="line-48"></a>
<a name="line-49"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Hledger</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span><span class='hs-varop'>.</span><span class='hs-conid'>Debug</span>
<a name="line-50"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Hledger</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span><span class='hs-varop'>.</span><span class='hs-conid'>Regex</span>
<a name="line-51"></a><span class='hs-comment'>-- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)</span>
<a name="line-52"></a><span class='hs-comment'>-- import Hledger.Utils.UTF8IOCompat (readFile,writeFile,appendFile,getContents,putStr,putStrLn)</span>
<a name="line-53"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Hledger</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span><span class='hs-varop'>.</span><span class='hs-conid'>UTF8IOCompat</span> <span class='hs-layout'>(</span><span class='hs-conid'>SystemString</span><span class='hs-layout'>,</span><span class='hs-varid'>fromSystemString</span><span class='hs-layout'>,</span><span class='hs-varid'>toSystemString</span><span class='hs-layout'>,</span><span class='hs-varid'>error'</span><span class='hs-layout'>,</span><span class='hs-varid'>userError'</span><span class='hs-layout'>)</span>
<a name="line-54"></a>
<a name="line-55"></a><span class='hs-comment'>-- strings</span>
<a name="line-56"></a>
<a name="line-57"></a><a name="lowercase"></a><span class='hs-definition'>lowercase</span><span class='hs-layout'>,</span> <span class='hs-varid'>uppercase</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-58"></a><span class='hs-definition'>lowercase</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>toLower</span>
<a name="line-59"></a><a name="uppercase"></a><span class='hs-definition'>uppercase</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>toUpper</span>
<a name="line-60"></a>
<a name="line-61"></a><a name="strip"></a><span class='hs-comment'>-- | Remove leading and trailing whitespace.</span>
<a name="line-62"></a><span class='hs-definition'>strip</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-63"></a><span class='hs-definition'>strip</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lstrip</span> <span class='hs-varop'>.</span> <span class='hs-varid'>rstrip</span>
<a name="line-64"></a>
<a name="line-65"></a><a name="lstrip"></a><span class='hs-comment'>-- | Remove leading whitespace.</span>
<a name="line-66"></a><span class='hs-definition'>lstrip</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-67"></a><span class='hs-definition'>lstrip</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>dropWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-str'>" \t"</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- XXX isSpace ?</span>
<a name="line-68"></a>
<a name="line-69"></a><a name="rstrip"></a><span class='hs-comment'>-- | Remove trailing whitespace.</span>
<a name="line-70"></a><span class='hs-definition'>rstrip</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-71"></a><span class='hs-definition'>rstrip</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>reverse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>lstrip</span> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span>
<a name="line-72"></a>
<a name="line-73"></a><a name="chomp"></a><span class='hs-comment'>-- | Remove trailing newlines/carriage returns.</span>
<a name="line-74"></a><span class='hs-definition'>chomp</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-75"></a><span class='hs-definition'>chomp</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>reverse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>dropWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-str'>"\r\n"</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span>
<a name="line-76"></a>
<a name="line-77"></a><a name="stripbrackets"></a><span class='hs-definition'>stripbrackets</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-78"></a><span class='hs-definition'>stripbrackets</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>dropWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-str'>"(["</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>dropWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-str'>"])"</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-79"></a>
<a name="line-80"></a><a name="elideLeft"></a><span class='hs-definition'>elideLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-81"></a><span class='hs-definition'>elideLeft</span> <span class='hs-varid'>width</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span>
<a name="line-82"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>length</span> <span class='hs-varid'>s</span> <span class='hs-varop'>></span> <span class='hs-varid'>width</span> <span class='hs-keyword'>then</span> <span class='hs-str'>".."</span> <span class='hs-varop'>++</span> <span class='hs-varid'>reverse</span> <span class='hs-layout'>(</span><span class='hs-varid'>take</span> <span class='hs-layout'>(</span><span class='hs-varid'>width</span> <span class='hs-comment'>-</span> <span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>reverse</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>s</span>
<a name="line-83"></a>
<a name="line-84"></a><a name="elideRight"></a><span class='hs-definition'>elideRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-85"></a><span class='hs-definition'>elideRight</span> <span class='hs-varid'>width</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span>
<a name="line-86"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>length</span> <span class='hs-varid'>s</span> <span class='hs-varop'>></span> <span class='hs-varid'>width</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>take</span> <span class='hs-layout'>(</span><span class='hs-varid'>width</span> <span class='hs-comment'>-</span> <span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>".."</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>s</span>
<a name="line-87"></a>
<a name="line-88"></a><a name="underline"></a><span class='hs-definition'>underline</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-89"></a><span class='hs-definition'>underline</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s'</span> <span class='hs-varop'>++</span> <span class='hs-varid'>replicate</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-chr'>'-'</span> <span class='hs-varop'>++</span> <span class='hs-str'>"\n"</span>
<a name="line-90"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>s'</span>
<a name="line-91"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>last</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'\n'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-92"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>"\n"</span>
<a name="line-93"></a>
<a name="line-94"></a><a name="quoteIfSpaced"></a><span class='hs-comment'>-- | Wrap a string in double quotes, and \-prefix any embedded single</span>
<a name="line-95"></a><span class='hs-comment'>-- quotes, if it contains whitespace and is not already single- or</span>
<a name="line-96"></a><span class='hs-comment'>-- double-quoted.</span>
<a name="line-97"></a><span class='hs-definition'>quoteIfSpaced</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-98"></a><span class='hs-definition'>quoteIfSpaced</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>isSingleQuoted</span> <span class='hs-varid'>s</span> <span class='hs-varop'>||</span> <span class='hs-varid'>isDoubleQuoted</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-99"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>not</span> <span class='hs-varop'>$</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-varid'>whitespacechars</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-100"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"'"</span><span class='hs-varop'>++</span><span class='hs-varid'>escapeSingleQuotes</span> <span class='hs-varid'>s</span><span class='hs-varop'>++</span><span class='hs-str'>"'"</span>
<a name="line-101"></a>
<a name="line-102"></a><a name="quoteIfNeeded"></a><span class='hs-comment'>-- | Double-quote this string if it contains whitespace, single quotes</span>
<a name="line-103"></a><span class='hs-comment'>-- or double-quotes, escaping the quotes as needed.</span>
<a name="line-104"></a><span class='hs-definition'>quoteIfNeeded</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-105"></a><span class='hs-definition'>quoteIfNeeded</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>quotechars</span><span class='hs-varop'>++</span><span class='hs-varid'>whitespacechars</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"\""</span> <span class='hs-varop'>++</span> <span class='hs-varid'>escapeDoubleQuotes</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>"\""</span>
<a name="line-106"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-107"></a>
<a name="line-108"></a><a name="singleQuoteIfNeeded"></a><span class='hs-comment'>-- | Single-quote this string if it contains whitespace or double-quotes.</span>
<a name="line-109"></a><span class='hs-comment'>-- No good for strings containing single quotes.</span>
<a name="line-110"></a><span class='hs-definition'>singleQuoteIfNeeded</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-111"></a><span class='hs-definition'>singleQuoteIfNeeded</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-varid'>whitespacechars</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"'"</span><span class='hs-varop'>++</span><span class='hs-varid'>s</span><span class='hs-varop'>++</span><span class='hs-str'>"'"</span>
<a name="line-112"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-113"></a>
<a name="line-114"></a><a name="quotechars"></a><span class='hs-definition'>quotechars</span><span class='hs-layout'>,</span> <span class='hs-varid'>whitespacechars</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span>
<a name="line-115"></a><span class='hs-definition'>quotechars</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"'\""</span>
<a name="line-116"></a><a name="whitespacechars"></a><span class='hs-definition'>whitespacechars</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>" \t\n\r"</span>
<a name="line-117"></a>
<a name="line-118"></a><a name="escapeDoubleQuotes"></a><span class='hs-definition'>escapeDoubleQuotes</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-119"></a><span class='hs-definition'>escapeDoubleQuotes</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>regexReplace</span> <span class='hs-str'>"\""</span> <span class='hs-str'>"\""</span>
<a name="line-120"></a>
<a name="line-121"></a><a name="escapeSingleQuotes"></a><span class='hs-definition'>escapeSingleQuotes</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-122"></a><span class='hs-definition'>escapeSingleQuotes</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>regexReplace</span> <span class='hs-str'>"'"</span> <span class='hs-str'>"\'"</span>
<a name="line-123"></a>
<a name="line-124"></a><a name="escapeQuotes"></a><span class='hs-definition'>escapeQuotes</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-125"></a><span class='hs-definition'>escapeQuotes</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>regexReplace</span> <span class='hs-str'>"([\"'])"</span> <span class='hs-str'>"\\1"</span>
<a name="line-126"></a>
<a name="line-127"></a><a name="words'"></a><span class='hs-comment'>-- | Quote-aware version of words - don't split on spaces which are inside quotes.</span>
<a name="line-128"></a><span class='hs-comment'>-- NB correctly handles "a'b" but not "''a''". Can raise an error if parsing fails.</span>
<a name="line-129"></a><span class='hs-definition'>words'</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span>
<a name="line-130"></a><span class='hs-definition'>words'</span> <span class='hs-str'>""</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-131"></a><span class='hs-definition'>words'</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>stripquotes</span> <span class='hs-varop'>$</span> <span class='hs-varid'>fromparse</span> <span class='hs-varop'>$</span> <span class='hs-varid'>parsewith</span> <span class='hs-varid'>p</span> <span class='hs-varid'>s</span>
<a name="line-132"></a> <span class='hs-keyword'>where</span>
<a name="line-133"></a> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span> <span class='hs-varid'>ss</span> <span class='hs-keyglyph'><-</span> <span class='hs-layout'>(</span><span class='hs-varid'>singleQuotedPattern</span> <span class='hs-varop'><|></span> <span class='hs-varid'>doubleQuotedPattern</span> <span class='hs-varop'><|></span> <span class='hs-varid'>pattern</span><span class='hs-layout'>)</span> <span class='hs-varop'>`sepBy`</span> <span class='hs-varid'>many1</span> <span class='hs-varid'>spacenonewline</span>
<a name="line-134"></a> <span class='hs-comment'>-- eof</span>
<a name="line-135"></a> <span class='hs-varid'>return</span> <span class='hs-varid'>ss</span>
<a name="line-136"></a> <span class='hs-varid'>pattern</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>many</span> <span class='hs-layout'>(</span><span class='hs-varid'>noneOf</span> <span class='hs-varid'>whitespacechars</span><span class='hs-layout'>)</span>
<a name="line-137"></a> <span class='hs-varid'>singleQuotedPattern</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>between</span> <span class='hs-layout'>(</span><span class='hs-varid'>char</span> <span class='hs-chr'>'\''</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>char</span> <span class='hs-chr'>'\''</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>many</span> <span class='hs-varop'>$</span> <span class='hs-varid'>noneOf</span> <span class='hs-str'>"'"</span><span class='hs-layout'>)</span>
<a name="line-138"></a> <span class='hs-varid'>doubleQuotedPattern</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>between</span> <span class='hs-layout'>(</span><span class='hs-varid'>char</span> <span class='hs-chr'>'"'</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>char</span> <span class='hs-chr'>'"'</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>many</span> <span class='hs-varop'>$</span> <span class='hs-varid'>noneOf</span> <span class='hs-str'>"\""</span><span class='hs-layout'>)</span>
<a name="line-139"></a>
<a name="line-140"></a><a name="unwords'"></a><span class='hs-comment'>-- | Quote-aware version of unwords - single-quote strings which contain whitespace</span>
<a name="line-141"></a><span class='hs-definition'>unwords'</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-142"></a><span class='hs-definition'>unwords'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwords</span> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-varid'>quoteIfNeeded</span>
<a name="line-143"></a>
<a name="line-144"></a><a name="stripquotes"></a><span class='hs-comment'>-- | Strip one matching pair of single or double quotes on the ends of a string.</span>
<a name="line-145"></a><span class='hs-definition'>stripquotes</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-146"></a><span class='hs-definition'>stripquotes</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>isSingleQuoted</span> <span class='hs-varid'>s</span> <span class='hs-varop'>||</span> <span class='hs-varid'>isDoubleQuoted</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>init</span> <span class='hs-varop'>$</span> <span class='hs-varid'>tail</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>s</span>
<a name="line-147"></a>
<a name="line-148"></a><a name="isSingleQuoted"></a><span class='hs-definition'>isSingleQuoted</span> <span class='hs-varid'>s</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>head</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'\''</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>last</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'\''</span>
<a name="line-149"></a><span class='hs-definition'>isSingleQuoted</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-150"></a>
<a name="line-151"></a><a name="isDoubleQuoted"></a><span class='hs-definition'>isDoubleQuoted</span> <span class='hs-varid'>s</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>head</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'"'</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>last</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'"'</span>
<a name="line-152"></a><span class='hs-definition'>isDoubleQuoted</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-153"></a>
<a name="line-154"></a><a name="unbracket"></a><span class='hs-definition'>unbracket</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-155"></a><span class='hs-definition'>unbracket</span> <span class='hs-varid'>s</span>
<a name="line-156"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>head</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'['</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>last</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>']'</span><span class='hs-layout'>)</span> <span class='hs-varop'>||</span> <span class='hs-layout'>(</span><span class='hs-varid'>head</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'('</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>last</span> <span class='hs-varid'>s</span> <span class='hs-varop'>==</span> <span class='hs-chr'>')'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>init</span> <span class='hs-varop'>$</span> <span class='hs-varid'>tail</span> <span class='hs-varid'>s</span>
<a name="line-157"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-158"></a>
<a name="line-159"></a><a name="concatTopPadded"></a><span class='hs-comment'>-- | Join multi-line strings as side-by-side rectangular strings of the same height, top-padded.</span>
<a name="line-160"></a><span class='hs-definition'>concatTopPadded</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-161"></a><span class='hs-definition'>concatTopPadded</span> <span class='hs-varid'>strs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>concat</span> <span class='hs-varop'>$</span> <span class='hs-varid'>transpose</span> <span class='hs-varid'>padded</span>
<a name="line-162"></a> <span class='hs-keyword'>where</span>
<a name="line-163"></a> <span class='hs-varid'>lss</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>lines</span> <span class='hs-varid'>strs</span>
<a name="line-164"></a> <span class='hs-varid'>h</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>length</span> <span class='hs-varid'>lss</span>
<a name="line-165"></a> <span class='hs-varid'>ypad</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>replicate</span> <span class='hs-layout'>(</span><span class='hs-varid'>difforzero</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>ls</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-str'>""</span> <span class='hs-varop'>++</span> <span class='hs-varid'>ls</span>
<a name="line-166"></a> <span class='hs-varid'>xpad</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>padleft</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span> <span class='hs-varid'>ls</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>w</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>null</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-167"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ls</span>
<a name="line-168"></a> <span class='hs-varid'>padded</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>xpad</span> <span class='hs-varop'>.</span> <span class='hs-varid'>ypad</span><span class='hs-layout'>)</span> <span class='hs-varid'>lss</span>
<a name="line-169"></a>
<a name="line-170"></a><a name="concatBottomPadded"></a><span class='hs-comment'>-- | Join multi-line strings as side-by-side rectangular strings of the same height, bottom-padded.</span>
<a name="line-171"></a><span class='hs-definition'>concatBottomPadded</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-172"></a><span class='hs-definition'>concatBottomPadded</span> <span class='hs-varid'>strs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>concat</span> <span class='hs-varop'>$</span> <span class='hs-varid'>transpose</span> <span class='hs-varid'>padded</span>
<a name="line-173"></a> <span class='hs-keyword'>where</span>
<a name="line-174"></a> <span class='hs-varid'>lss</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>lines</span> <span class='hs-varid'>strs</span>
<a name="line-175"></a> <span class='hs-varid'>h</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>length</span> <span class='hs-varid'>lss</span>
<a name="line-176"></a> <span class='hs-varid'>ypad</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ls</span> <span class='hs-varop'>++</span> <span class='hs-varid'>replicate</span> <span class='hs-layout'>(</span><span class='hs-varid'>difforzero</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>ls</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-str'>""</span>
<a name="line-177"></a> <span class='hs-varid'>xpad</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>padright</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span> <span class='hs-varid'>ls</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>w</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>null</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-178"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ls</span>
<a name="line-179"></a> <span class='hs-varid'>padded</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>xpad</span> <span class='hs-varop'>.</span> <span class='hs-varid'>ypad</span><span class='hs-layout'>)</span> <span class='hs-varid'>lss</span>
<a name="line-180"></a>
<a name="line-181"></a><a name="vConcatRightAligned"></a><span class='hs-comment'>-- | Compose strings vertically and right-aligned.</span>
<a name="line-182"></a><span class='hs-definition'>vConcatRightAligned</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-183"></a><span class='hs-definition'>vConcatRightAligned</span> <span class='hs-varid'>ss</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>showfixedwidth</span> <span class='hs-varid'>ss</span>
<a name="line-184"></a> <span class='hs-keyword'>where</span>
<a name="line-185"></a> <span class='hs-varid'>showfixedwidth</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>printf</span> <span class='hs-layout'>(</span><span class='hs-varid'>printf</span> <span class='hs-str'>"%%%ds"</span> <span class='hs-varid'>width</span><span class='hs-layout'>)</span>
<a name="line-186"></a> <span class='hs-varid'>width</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ss</span>
<a name="line-187"></a>
<a name="line-188"></a><a name="padtop"></a><span class='hs-comment'>-- | Convert a multi-line string to a rectangular string top-padded to the specified height.</span>
<a name="line-189"></a><span class='hs-definition'>padtop</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-190"></a><span class='hs-definition'>padtop</span> <span class='hs-varid'>h</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varid'>xpadded</span>
<a name="line-191"></a> <span class='hs-keyword'>where</span>
<a name="line-192"></a> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lines</span> <span class='hs-varid'>s</span>
<a name="line-193"></a> <span class='hs-varid'>sh</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ls</span>
<a name="line-194"></a> <span class='hs-varid'>sw</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>null</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-195"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ls</span>
<a name="line-196"></a> <span class='hs-varid'>ypadded</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>replicate</span> <span class='hs-layout'>(</span><span class='hs-varid'>difforzero</span> <span class='hs-varid'>h</span> <span class='hs-varid'>sh</span><span class='hs-layout'>)</span> <span class='hs-str'>""</span> <span class='hs-varop'>++</span> <span class='hs-varid'>ls</span>
<a name="line-197"></a> <span class='hs-varid'>xpadded</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>padleft</span> <span class='hs-varid'>sw</span><span class='hs-layout'>)</span> <span class='hs-varid'>ypadded</span>
<a name="line-198"></a>
<a name="line-199"></a><a name="padbottom"></a><span class='hs-comment'>-- | Convert a multi-line string to a rectangular string bottom-padded to the specified height.</span>
<a name="line-200"></a><span class='hs-definition'>padbottom</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-201"></a><span class='hs-definition'>padbottom</span> <span class='hs-varid'>h</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varid'>xpadded</span>
<a name="line-202"></a> <span class='hs-keyword'>where</span>
<a name="line-203"></a> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lines</span> <span class='hs-varid'>s</span>
<a name="line-204"></a> <span class='hs-varid'>sh</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ls</span>
<a name="line-205"></a> <span class='hs-varid'>sw</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>null</span> <span class='hs-varid'>ls</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-206"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ls</span>
<a name="line-207"></a> <span class='hs-varid'>ypadded</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ls</span> <span class='hs-varop'>++</span> <span class='hs-varid'>replicate</span> <span class='hs-layout'>(</span><span class='hs-varid'>difforzero</span> <span class='hs-varid'>h</span> <span class='hs-varid'>sh</span><span class='hs-layout'>)</span> <span class='hs-str'>""</span>
<a name="line-208"></a> <span class='hs-varid'>xpadded</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>padleft</span> <span class='hs-varid'>sw</span><span class='hs-layout'>)</span> <span class='hs-varid'>ypadded</span>
<a name="line-209"></a>
<a name="line-210"></a><a name="padleft"></a><span class='hs-comment'>-- | Convert a multi-line string to a rectangular string left-padded to the specified width.</span>
<a name="line-211"></a><span class='hs-definition'>padleft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-212"></a><span class='hs-definition'>padleft</span> <span class='hs-varid'>w</span> <span class='hs-str'>""</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concat</span> <span class='hs-varop'>$</span> <span class='hs-varid'>replicate</span> <span class='hs-varid'>w</span> <span class='hs-str'>" "</span>
<a name="line-213"></a><span class='hs-definition'>padleft</span> <span class='hs-varid'>w</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>printf</span> <span class='hs-layout'>(</span><span class='hs-varid'>printf</span> <span class='hs-str'>"%%%ds"</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>lines</span> <span class='hs-varid'>s</span>
<a name="line-214"></a>
<a name="line-215"></a><a name="padright"></a><span class='hs-comment'>-- | Convert a multi-line string to a rectangular string right-padded to the specified width.</span>
<a name="line-216"></a><span class='hs-definition'>padright</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-217"></a><span class='hs-definition'>padright</span> <span class='hs-varid'>w</span> <span class='hs-str'>""</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concat</span> <span class='hs-varop'>$</span> <span class='hs-varid'>replicate</span> <span class='hs-varid'>w</span> <span class='hs-str'>" "</span>
<a name="line-218"></a><span class='hs-definition'>padright</span> <span class='hs-varid'>w</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>printf</span> <span class='hs-layout'>(</span><span class='hs-varid'>printf</span> <span class='hs-str'>"%%-%ds"</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>lines</span> <span class='hs-varid'>s</span>
<a name="line-219"></a>
<a name="line-220"></a><a name="cliptopleft"></a><span class='hs-comment'>-- | Clip a multi-line string to the specified width and height from the top left.</span>
<a name="line-221"></a><span class='hs-definition'>cliptopleft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-222"></a><span class='hs-definition'>cliptopleft</span> <span class='hs-varid'>w</span> <span class='hs-varid'>h</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varop'>.</span> <span class='hs-varid'>take</span> <span class='hs-varid'>h</span> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>take</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>lines</span>
<a name="line-223"></a>
<a name="line-224"></a><a name="fitto"></a><span class='hs-comment'>-- | Clip and pad a multi-line string to fill the specified width and height.</span>
<a name="line-225"></a><span class='hs-definition'>fitto</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-226"></a><span class='hs-definition'>fitto</span> <span class='hs-varid'>w</span> <span class='hs-varid'>h</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>"\n"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>take</span> <span class='hs-varid'>h</span> <span class='hs-varop'>$</span> <span class='hs-varid'>rows</span> <span class='hs-varop'>++</span> <span class='hs-varid'>repeat</span> <span class='hs-varid'>blankline</span>
<a name="line-227"></a> <span class='hs-keyword'>where</span>
<a name="line-228"></a> <span class='hs-varid'>rows</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>fit</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>lines</span> <span class='hs-varid'>s</span>
<a name="line-229"></a> <span class='hs-varid'>fit</span> <span class='hs-varid'>w</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>take</span> <span class='hs-varid'>w</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varop'>++</span> <span class='hs-varid'>repeat</span> <span class='hs-chr'>' '</span><span class='hs-layout'>)</span>
<a name="line-230"></a> <span class='hs-varid'>blankline</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>replicate</span> <span class='hs-varid'>w</span> <span class='hs-chr'>' '</span>
<a name="line-231"></a>
<a name="line-232"></a><span class='hs-comment'>-- tuples</span>
<a name="line-233"></a>
<a name="line-234"></a><a name="first3"></a><span class='hs-definition'>first3</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-235"></a><a name="second3"></a><span class='hs-definition'>second3</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-236"></a><a name="third3"></a><span class='hs-definition'>third3</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-237"></a>
<a name="line-238"></a><a name="first4"></a><span class='hs-definition'>first4</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-239"></a><a name="second4"></a><span class='hs-definition'>second4</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-240"></a><a name="third4"></a><span class='hs-definition'>third4</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-241"></a><a name="fourth4"></a><span class='hs-definition'>fourth4</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-242"></a>
<a name="line-243"></a><a name="first5"></a><span class='hs-definition'>first5</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-244"></a><a name="second5"></a><span class='hs-definition'>second5</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-245"></a><a name="third5"></a><span class='hs-definition'>third5</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-246"></a><a name="fourth5"></a><span class='hs-definition'>fourth5</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-247"></a><a name="fifth5"></a><span class='hs-definition'>fifth5</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-248"></a>
<a name="line-249"></a><span class='hs-comment'>-- math</span>
<a name="line-250"></a>
<a name="line-251"></a><a name="difforzero"></a><span class='hs-definition'>difforzero</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Num</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-252"></a><span class='hs-definition'>difforzero</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-comment'>-</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-num'>0</span><span class='hs-keyglyph'>]</span>
<a name="line-253"></a>
<a name="line-254"></a><span class='hs-comment'>-- lists</span>
<a name="line-255"></a>
<a name="line-256"></a><a name="splitAtElement"></a><span class='hs-definition'>splitAtElement</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Eq</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span><span class='hs-keyglyph'>]</span>
<a name="line-257"></a><span class='hs-definition'>splitAtElement</span> <span class='hs-varid'>x</span> <span class='hs-varid'>l</span> <span class='hs-keyglyph'>=</span>
<a name="line-258"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>l</span> <span class='hs-keyword'>of</span>
<a name="line-259"></a> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>[]</span>
<a name="line-260"></a> <span class='hs-varid'>e</span><span class='hs-conop'>:</span><span class='hs-varid'>es</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>e</span><span class='hs-varop'>==</span><span class='hs-varid'>x</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>split</span> <span class='hs-varid'>es</span>
<a name="line-261"></a> <span class='hs-varid'>es</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>split</span> <span class='hs-varid'>es</span>
<a name="line-262"></a> <span class='hs-keyword'>where</span>
<a name="line-263"></a> <span class='hs-varid'>split</span> <span class='hs-varid'>es</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>first</span><span class='hs-layout'>,</span><span class='hs-varid'>rest</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>break</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-varop'>==</span><span class='hs-layout'>)</span> <span class='hs-varid'>es</span>
<a name="line-264"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>first</span> <span class='hs-conop'>:</span> <span class='hs-varid'>splitAtElement</span> <span class='hs-varid'>x</span> <span class='hs-varid'>rest</span>
<a name="line-265"></a>
<a name="line-266"></a><span class='hs-comment'>-- trees</span>
<a name="line-267"></a>
<a name="line-268"></a><span class='hs-comment'>-- standard tree helpers</span>
<a name="line-269"></a>
<a name="line-270"></a><a name="root"></a><span class='hs-definition'>root</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rootLabel</span>
<a name="line-271"></a><a name="subs"></a><span class='hs-definition'>subs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>subForest</span>
<a name="line-272"></a><a name="branches"></a><span class='hs-definition'>branches</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>subForest</span>
<a name="line-273"></a>
<a name="line-274"></a><a name="leaves"></a><span class='hs-comment'>-- | List just the leaf nodes of a tree</span>
<a name="line-275"></a><span class='hs-definition'>leaves</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-276"></a><span class='hs-definition'>leaves</span> <span class='hs-layout'>(</span><span class='hs-conid'>Node</span> <span class='hs-varid'>v</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span>
<a name="line-277"></a><span class='hs-definition'>leaves</span> <span class='hs-layout'>(</span><span class='hs-conid'>Node</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>branches</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>leaves</span> <span class='hs-varid'>branches</span>
<a name="line-278"></a>
<a name="line-279"></a><a name="subtreeat"></a><span class='hs-comment'>-- | get the sub-tree rooted at the first (left-most, depth-first) occurrence</span>
<a name="line-280"></a><span class='hs-comment'>-- of the specified node value</span>
<a name="line-281"></a><span class='hs-definition'>subtreeat</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Eq</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-282"></a><span class='hs-definition'>subtreeat</span> <span class='hs-varid'>v</span> <span class='hs-varid'>t</span>
<a name="line-283"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>root</span> <span class='hs-varid'>t</span> <span class='hs-varop'>==</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>t</span>
<a name="line-284"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>subtreeinforest</span> <span class='hs-varid'>v</span> <span class='hs-varop'>$</span> <span class='hs-varid'>subs</span> <span class='hs-varid'>t</span>
<a name="line-285"></a>
<a name="line-286"></a><a name="subtreeinforest"></a><span class='hs-comment'>-- | get the sub-tree for the specified node value in the first tree in</span>
<a name="line-287"></a><span class='hs-comment'>-- forest in which it occurs.</span>
<a name="line-288"></a><span class='hs-definition'>subtreeinforest</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Eq</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-289"></a><span class='hs-definition'>subtreeinforest</span> <span class='hs-keyword'>_</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-290"></a><span class='hs-definition'>subtreeinforest</span> <span class='hs-varid'>v</span> <span class='hs-layout'>(</span><span class='hs-varid'>t</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-layout'>(</span><span class='hs-varid'>subtreeat</span> <span class='hs-varid'>v</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyword'>of</span>
<a name="line-291"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>t'</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Just</span> <span class='hs-varid'>t'</span>
<a name="line-292"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>subtreeinforest</span> <span class='hs-varid'>v</span> <span class='hs-varid'>ts</span>
<a name="line-293"></a>
<a name="line-294"></a><a name="treeprune"></a><span class='hs-comment'>-- | remove all nodes past a certain depth</span>
<a name="line-295"></a><span class='hs-definition'>treeprune</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span>
<a name="line-296"></a><span class='hs-definition'>treeprune</span> <span class='hs-num'>0</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Node</span> <span class='hs-layout'>(</span><span class='hs-varid'>root</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-conid'>[]</span>
<a name="line-297"></a><span class='hs-definition'>treeprune</span> <span class='hs-varid'>d</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Node</span> <span class='hs-layout'>(</span><span class='hs-varid'>root</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>treeprune</span> <span class='hs-varop'>$</span> <span class='hs-varid'>d</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>branches</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-298"></a>
<a name="line-299"></a><a name="treemap"></a><span class='hs-comment'>-- | apply f to all tree nodes</span>
<a name="line-300"></a><span class='hs-definition'>treemap</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>b</span>
<a name="line-301"></a><span class='hs-definition'>treemap</span> <span class='hs-varid'>f</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Node</span> <span class='hs-layout'>(</span><span class='hs-varid'>f</span> <span class='hs-varop'>$</span> <span class='hs-varid'>root</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>treemap</span> <span class='hs-varid'>f</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>branches</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-302"></a>
<a name="line-303"></a><a name="treefilter"></a><span class='hs-comment'>-- | remove all subtrees whose nodes do not fulfill predicate</span>
<a name="line-304"></a><span class='hs-definition'>treefilter</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span>
<a name="line-305"></a><span class='hs-definition'>treefilter</span> <span class='hs-varid'>f</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Node</span>
<a name="line-306"></a> <span class='hs-layout'>(</span><span class='hs-varid'>root</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-307"></a> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>treefilter</span> <span class='hs-varid'>f</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>filter</span> <span class='hs-layout'>(</span><span class='hs-varid'>treeany</span> <span class='hs-varid'>f</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>branches</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-308"></a>
<a name="line-309"></a><a name="treeany"></a><span class='hs-comment'>-- | is predicate true in any node of tree ?</span>
<a name="line-310"></a><span class='hs-definition'>treeany</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-311"></a><span class='hs-definition'>treeany</span> <span class='hs-varid'>f</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>f</span> <span class='hs-layout'>(</span><span class='hs-varid'>root</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-varop'>||</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varid'>treeany</span> <span class='hs-varid'>f</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>branches</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-312"></a>
<a name="line-313"></a><span class='hs-comment'>-- treedrop -- remove the leaves which do fulfill predicate.</span>
<a name="line-314"></a><span class='hs-comment'>-- treedropall -- do this repeatedly.</span>
<a name="line-315"></a>
<a name="line-316"></a><a name="showtree"></a><span class='hs-comment'>-- | show a compact ascii representation of a tree</span>
<a name="line-317"></a><span class='hs-definition'>showtree</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Tree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-318"></a><span class='hs-definition'>showtree</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unlines</span> <span class='hs-varop'>.</span> <span class='hs-varid'>filter</span> <span class='hs-layout'>(</span><span class='hs-varid'>regexMatches</span> <span class='hs-str'>"[^ \\|]"</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>lines</span> <span class='hs-varop'>.</span> <span class='hs-varid'>drawTree</span> <span class='hs-varop'>.</span> <span class='hs-varid'>treemap</span> <span class='hs-varid'>show</span>
<a name="line-319"></a>
<a name="line-320"></a><a name="showforest"></a><span class='hs-comment'>-- | show a compact ascii representation of a forest</span>
<a name="line-321"></a><span class='hs-definition'>showforest</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Forest</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-322"></a><span class='hs-definition'>showforest</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>showtree</span>
<a name="line-323"></a>
<a name="line-324"></a>
<a name="line-325"></a><a name="FastTree"></a><span class='hs-comment'>-- | An efficient-to-build tree suggested by Cale Gibbard, probably</span>
<a name="line-326"></a><a name="FastTree"></a><span class='hs-comment'>-- better than accountNameTreeFrom.</span>
<a name="line-327"></a><a name="FastTree"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>FastTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-varid'>a</span> <span class='hs-layout'>(</span><span class='hs-conid'>FastTree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-328"></a> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Show</span><span class='hs-layout'>,</span> <span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span><span class='hs-layout'>)</span>
<a name="line-329"></a>
<a name="line-330"></a><a name="emptyTree"></a><span class='hs-definition'>emptyTree</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-conid'>M</span><span class='hs-varop'>.</span><span class='hs-varid'>empty</span>
<a name="line-331"></a>
<a name="line-332"></a><a name="mergeTrees"></a><span class='hs-definition'>mergeTrees</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>FastTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>FastTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>FastTree</span> <span class='hs-varid'>a</span>
<a name="line-333"></a><span class='hs-definition'>mergeTrees</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span><span class='hs-varop'>.</span><span class='hs-varid'>unionWith</span> <span class='hs-varid'>mergeTrees</span> <span class='hs-varid'>m</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-334"></a>
<a name="line-335"></a><a name="treeFromPath"></a><span class='hs-definition'>treeFromPath</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>FastTree</span> <span class='hs-varid'>a</span>
<a name="line-336"></a><span class='hs-definition'>treeFromPath</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-conid'>M</span><span class='hs-varop'>.</span><span class='hs-varid'>empty</span>
<a name="line-337"></a><span class='hs-definition'>treeFromPath</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span><span class='hs-varop'>.</span><span class='hs-varid'>singleton</span> <span class='hs-varid'>x</span> <span class='hs-layout'>(</span><span class='hs-varid'>treeFromPath</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-338"></a>
<a name="line-339"></a><a name="treeFromPaths"></a><span class='hs-definition'>treeFromPaths</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-keyglyph'>[</span><span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>FastTree</span> <span class='hs-varid'>a</span>
<a name="line-340"></a><span class='hs-definition'>treeFromPaths</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldl'</span> <span class='hs-varid'>mergeTrees</span> <span class='hs-varid'>emptyTree</span> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-varid'>treeFromPath</span>
<a name="line-341"></a>
<a name="line-342"></a>
<a name="line-343"></a><span class='hs-comment'>-- parsing</span>
<a name="line-344"></a>
<a name="line-345"></a><a name="choice'"></a><span class='hs-comment'>-- | Backtracking choice, use this when alternatives share a prefix.</span>
<a name="line-346"></a><span class='hs-comment'>-- Consumes no input if all choices fail.</span>
<a name="line-347"></a><span class='hs-definition'>choice'</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Stream</span> <span class='hs-varid'>s</span> <span class='hs-varid'>m</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>ParsecT</span> <span class='hs-varid'>s</span> <span class='hs-varid'>u</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsecT</span> <span class='hs-varid'>s</span> <span class='hs-varid'>u</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-348"></a><span class='hs-definition'>choice'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>choice</span> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-conid'>Text</span><span class='hs-varop'>.</span><span class='hs-conid'>Parsec</span><span class='hs-varop'>.</span><span class='hs-varid'>try</span>
<a name="line-349"></a>
<a name="line-350"></a><a name="parsewith"></a><span class='hs-definition'>parsewith</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Parsec</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-conid'>()</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>ParseError</span> <span class='hs-varid'>a</span>
<a name="line-351"></a><span class='hs-definition'>parsewith</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>runParser</span> <span class='hs-varid'>p</span> <span class='hs-conid'>()</span> <span class='hs-str'>""</span>
<a name="line-352"></a>
<a name="line-353"></a><a name="parseWithCtx"></a><span class='hs-definition'>parseWithCtx</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Stream</span> <span class='hs-varid'>s</span> <span class='hs-varid'>m</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>u</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsecT</span> <span class='hs-varid'>s</span> <span class='hs-varid'>u</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>ParseError</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-354"></a><span class='hs-definition'>parseWithCtx</span> <span class='hs-varid'>ctx</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>runParserT</span> <span class='hs-varid'>p</span> <span class='hs-varid'>ctx</span> <span class='hs-str'>""</span>
<a name="line-355"></a>
<a name="line-356"></a><a name="fromparse"></a><span class='hs-definition'>fromparse</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Either</span> <span class='hs-conid'>ParseError</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-357"></a><span class='hs-definition'>fromparse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>either</span> <span class='hs-varid'>parseerror</span> <span class='hs-varid'>id</span>
<a name="line-358"></a>
<a name="line-359"></a><a name="parseerror"></a><span class='hs-definition'>parseerror</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ParseError</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-360"></a><span class='hs-definition'>parseerror</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error'</span> <span class='hs-varop'>$</span> <span class='hs-varid'>showParseError</span> <span class='hs-varid'>e</span>
<a name="line-361"></a>
<a name="line-362"></a><a name="showParseError"></a><span class='hs-definition'>showParseError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ParseError</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-363"></a><span class='hs-definition'>showParseError</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"parse error at "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>e</span>
<a name="line-364"></a>
<a name="line-365"></a><a name="showDateParseError"></a><span class='hs-definition'>showDateParseError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ParseError</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-366"></a><span class='hs-definition'>showDateParseError</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>printf</span> <span class='hs-str'>"date parse error (%s)"</span> <span class='hs-layout'>(</span><span class='hs-varid'>intercalate</span> <span class='hs-str'>", "</span> <span class='hs-varop'>$</span> <span class='hs-varid'>tail</span> <span class='hs-varop'>$</span> <span class='hs-varid'>lines</span> <span class='hs-varop'>$</span> <span class='hs-varid'>show</span> <span class='hs-varid'>e</span><span class='hs-layout'>)</span>
<a name="line-367"></a>
<a name="line-368"></a><a name="nonspace"></a><span class='hs-definition'>nonspace</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Stream</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>m</span> <span class='hs-conid'>Char</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ParsecT</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>st</span> <span class='hs-varid'>m</span> <span class='hs-conid'>Char</span>
<a name="line-369"></a><span class='hs-definition'>nonspace</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>satisfy</span> <span class='hs-layout'>(</span><span class='hs-varid'>not</span> <span class='hs-varop'>.</span> <span class='hs-varid'>isSpace</span><span class='hs-layout'>)</span>
<a name="line-370"></a>
<a name="line-371"></a><a name="spacenonewline"></a><span class='hs-definition'>spacenonewline</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Stream</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>m</span> <span class='hs-conid'>Char</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ParsecT</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>st</span> <span class='hs-varid'>m</span> <span class='hs-conid'>Char</span>
<a name="line-372"></a><span class='hs-definition'>spacenonewline</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>satisfy</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-str'>" \v\f\t"</span><span class='hs-layout'>)</span>
<a name="line-373"></a>
<a name="line-374"></a><a name="restofline"></a><span class='hs-definition'>restofline</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Stream</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>m</span> <span class='hs-conid'>Char</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ParsecT</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>st</span> <span class='hs-varid'>m</span> <span class='hs-conid'>String</span>
<a name="line-375"></a><span class='hs-definition'>restofline</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>anyChar</span> <span class='hs-varop'>`manyTill`</span> <span class='hs-varid'>newline</span>
<a name="line-376"></a>
<a name="line-377"></a><a name="eolof"></a><span class='hs-definition'>eolof</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Stream</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>m</span> <span class='hs-conid'>Char</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ParsecT</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>st</span> <span class='hs-varid'>m</span> <span class='hs-conid'>()</span>
<a name="line-378"></a><span class='hs-definition'>eolof</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>newline</span> <span class='hs-varop'>>></span> <span class='hs-varid'>return</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-varop'><|></span> <span class='hs-varid'>eof</span>
<a name="line-379"></a>
<a name="line-380"></a><span class='hs-comment'>-- time</span>
<a name="line-381"></a>
<a name="line-382"></a><a name="getCurrentLocalTime"></a><span class='hs-definition'>getCurrentLocalTime</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>IO</span> <span class='hs-conid'>LocalTime</span>
<a name="line-383"></a><span class='hs-definition'>getCurrentLocalTime</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-384"></a> <span class='hs-varid'>t</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>getCurrentTime</span>
<a name="line-385"></a> <span class='hs-varid'>tz</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>getCurrentTimeZone</span>
<a name="line-386"></a> <span class='hs-varid'>return</span> <span class='hs-varop'>$</span> <span class='hs-varid'>utcToLocalTime</span> <span class='hs-varid'>tz</span> <span class='hs-varid'>t</span>
<a name="line-387"></a>
<a name="line-388"></a><span class='hs-comment'>-- testing</span>
<a name="line-389"></a>
<a name="line-390"></a><a name="testName"></a><span class='hs-comment'>-- | Get a Test's label, or the empty string.</span>
<a name="line-391"></a><span class='hs-definition'>testName</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Test</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-392"></a><span class='hs-definition'>testName</span> <span class='hs-layout'>(</span><span class='hs-conid'>TestLabel</span> <span class='hs-varid'>n</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>n</span>
<a name="line-393"></a><span class='hs-definition'>testName</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>""</span>
<a name="line-394"></a>
<a name="line-395"></a><a name="flattenTests"></a><span class='hs-comment'>-- | Flatten a Test containing TestLists into a list of single tests.</span>
<a name="line-396"></a><span class='hs-definition'>flattenTests</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Test</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Test</span><span class='hs-keyglyph'>]</span>
<a name="line-397"></a><span class='hs-definition'>flattenTests</span> <span class='hs-layout'>(</span><span class='hs-conid'>TestLabel</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>TestList</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>flattenTests</span> <span class='hs-varid'>t</span>
<a name="line-398"></a><span class='hs-definition'>flattenTests</span> <span class='hs-layout'>(</span><span class='hs-conid'>TestList</span> <span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>flattenTests</span> <span class='hs-varid'>ts</span>
<a name="line-399"></a><span class='hs-definition'>flattenTests</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-400"></a>
<a name="line-401"></a><a name="filterTests"></a><span class='hs-comment'>-- | Filter TestLists in a Test, recursively, preserving the structure.</span>
<a name="line-402"></a><span class='hs-definition'>filterTests</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Test</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Test</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Test</span>
<a name="line-403"></a><span class='hs-definition'>filterTests</span> <span class='hs-varid'>p</span> <span class='hs-layout'>(</span><span class='hs-conid'>TestLabel</span> <span class='hs-varid'>l</span> <span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>TestLabel</span> <span class='hs-varid'>l</span> <span class='hs-layout'>(</span><span class='hs-varid'>filterTests</span> <span class='hs-varid'>p</span> <span class='hs-varid'>ts</span><span class='hs-layout'>)</span>
<a name="line-404"></a><span class='hs-definition'>filterTests</span> <span class='hs-varid'>p</span> <span class='hs-layout'>(</span><span class='hs-conid'>TestList</span> <span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>TestList</span> <span class='hs-varop'>$</span> <span class='hs-varid'>filter</span> <span class='hs-layout'>(</span><span class='hs-varid'>any</span> <span class='hs-varid'>p</span> <span class='hs-varop'>.</span> <span class='hs-varid'>flattenTests</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>filterTests</span> <span class='hs-varid'>p</span><span class='hs-layout'>)</span> <span class='hs-varid'>ts</span>
<a name="line-405"></a><span class='hs-definition'>filterTests</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>t</span>
<a name="line-406"></a>
<a name="line-407"></a><a name="is"></a><span class='hs-comment'>-- | Simple way to assert something is some expected value, with no label.</span>
<a name="line-408"></a><span class='hs-definition'>is</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Assertion</span>
<a name="line-409"></a><a name="is"></a><span class='hs-definition'>a</span> <span class='hs-varop'>`is`</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>assertEqual</span> <span class='hs-str'>""</span> <span class='hs-varid'>e</span> <span class='hs-varid'>a</span>
<a name="line-410"></a>
<a name="line-411"></a><a name="assertParse"></a><span class='hs-comment'>-- | Assert a parse result is successful, printing the parse error on failure.</span>
<a name="line-412"></a><span class='hs-definition'>assertParse</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>ParseError</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Assertion</span>
<a name="line-413"></a><span class='hs-definition'>assertParse</span> <span class='hs-varid'>parse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>either</span> <span class='hs-layout'>(</span><span class='hs-varid'>assertFailure</span><span class='hs-varop'>.</span><span class='hs-varid'>show</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>const</span> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varid'>parse</span>
<a name="line-414"></a>
<a name="line-415"></a><a name="assertParseFailure"></a><span class='hs-comment'>-- | Assert a parse result is successful, printing the parse error on failure.</span>
<a name="line-416"></a><span class='hs-definition'>assertParseFailure</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>ParseError</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Assertion</span>
<a name="line-417"></a><span class='hs-definition'>assertParseFailure</span> <span class='hs-varid'>parse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>either</span> <span class='hs-layout'>(</span><span class='hs-varid'>const</span> <span class='hs-varop'>$</span> <span class='hs-varid'>return</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>const</span> <span class='hs-varop'>$</span> <span class='hs-varid'>assertFailure</span> <span class='hs-str'>"parse should not have succeeded"</span><span class='hs-layout'>)</span> <span class='hs-varid'>parse</span>
<a name="line-418"></a>
<a name="line-419"></a><a name="assertParseEqual"></a><span class='hs-comment'>-- | Assert a parse result is some expected value, printing the parse error on failure.</span>
<a name="line-420"></a><span class='hs-definition'>assertParseEqual</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Show</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Eq</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>ParseError</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Assertion</span>
<a name="line-421"></a><span class='hs-definition'>assertParseEqual</span> <span class='hs-varid'>parse</span> <span class='hs-varid'>expected</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>either</span> <span class='hs-layout'>(</span><span class='hs-varid'>assertFailure</span><span class='hs-varop'>.</span><span class='hs-varid'>show</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varop'>`is`</span> <span class='hs-varid'>expected</span><span class='hs-layout'>)</span> <span class='hs-varid'>parse</span>
<a name="line-422"></a>
<a name="line-423"></a><a name="printParseError"></a><span class='hs-definition'>printParseError</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Show</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span>
<a name="line-424"></a><span class='hs-definition'>printParseError</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span> <span class='hs-varid'>putStr</span> <span class='hs-str'>"parse error at "</span><span class='hs-layout'>;</span> <span class='hs-varid'>print</span> <span class='hs-varid'>e</span>
<a name="line-425"></a>
<a name="line-426"></a><span class='hs-comment'>-- misc</span>
<a name="line-427"></a>
<a name="line-428"></a><a name="isLeft"></a><span class='hs-definition'>isLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Either</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-429"></a><span class='hs-definition'>isLeft</span> <span class='hs-layout'>(</span><span class='hs-conid'>Left</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-430"></a><span class='hs-definition'>isLeft</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-431"></a>
<a name="line-432"></a><a name="isRight"></a><span class='hs-definition'>isRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Either</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-433"></a><span class='hs-definition'>isRight</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>not</span> <span class='hs-varop'>.</span> <span class='hs-varid'>isLeft</span>
<a name="line-434"></a>
<a name="line-435"></a><a name="applyN"></a><span class='hs-comment'>-- | Apply a function the specified number of times. Possibly uses O(n) stack ?</span>
<a name="line-436"></a><span class='hs-definition'>applyN</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-437"></a><span class='hs-definition'>applyN</span> <span class='hs-varid'>n</span> <span class='hs-varid'>f</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varop'>!!</span> <span class='hs-varid'>n</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>iterate</span> <span class='hs-varid'>f</span>
<a name="line-438"></a>
<a name="line-439"></a><a name="expandPath"></a><span class='hs-comment'>-- | Convert a possibly relative, possibly tilde-containing file path to an absolute one,</span>
<a name="line-440"></a><span class='hs-comment'>-- given the current directory. ~username is not supported. Leave "-" unchanged.</span>
<a name="line-441"></a><span class='hs-definition'>expandPath</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>FilePath</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>FilePath</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-conid'>FilePath</span> <span class='hs-comment'>-- general type sig for use in reader parsers</span>
<a name="line-442"></a><span class='hs-definition'>expandPath</span> <span class='hs-keyword'>_</span> <span class='hs-str'>"-"</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-str'>"-"</span>
<a name="line-443"></a><span class='hs-definition'>expandPath</span> <span class='hs-varid'>curdir</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-keyword'>if</span> <span class='hs-varid'>isRelative</span> <span class='hs-varid'>p</span> <span class='hs-keyword'>then</span> <span class='hs-layout'>(</span><span class='hs-varid'>curdir</span> <span class='hs-varop'></></span><span class='hs-layout'>)</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>id</span><span class='hs-layout'>)</span> <span class='hs-varop'>`liftM`</span> <span class='hs-varid'>expandPath'</span> <span class='hs-varid'>p</span>
<a name="line-444"></a> <span class='hs-keyword'>where</span>
<a name="line-445"></a> <span class='hs-varid'>expandPath'</span> <span class='hs-layout'>(</span><span class='hs-chr'>'~'</span><span class='hs-conop'>:</span><span class='hs-chr'>'/'</span><span class='hs-conop'>:</span><span class='hs-varid'>p</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-layout'>(</span><span class='hs-varop'></></span> <span class='hs-varid'>p</span><span class='hs-layout'>)</span> <span class='hs-varop'>`fmap`</span> <span class='hs-varid'>getHomeDirectory</span>
<a name="line-446"></a> <span class='hs-varid'>expandPath'</span> <span class='hs-layout'>(</span><span class='hs-chr'>'~'</span><span class='hs-conop'>:</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-varid'>p</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-layout'>(</span><span class='hs-varop'></></span> <span class='hs-varid'>p</span><span class='hs-layout'>)</span> <span class='hs-varop'>`fmap`</span> <span class='hs-varid'>getHomeDirectory</span>
<a name="line-447"></a> <span class='hs-varid'>expandPath'</span> <span class='hs-layout'>(</span><span class='hs-chr'>'~'</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error'</span> <span class='hs-str'>"~USERNAME in paths is not supported"</span>
<a name="line-448"></a> <span class='hs-varid'>expandPath'</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-varid'>p</span>
<a name="line-449"></a>
<a name="line-450"></a><a name="firstJust"></a><span class='hs-definition'>firstJust</span> <span class='hs-varid'>ms</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>dropWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span><span class='hs-conid'>Nothing</span><span class='hs-layout'>)</span> <span class='hs-varid'>ms</span> <span class='hs-keyword'>of</span>
<a name="line-451"></a> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Nothing</span>
<a name="line-452"></a> <span class='hs-layout'>(</span><span class='hs-varid'>md</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>md</span>
<a name="line-453"></a>
<a name="line-454"></a><a name="readFile'"></a><span class='hs-comment'>-- | Read a file in universal newline mode, handling whatever newline convention it may contain.</span>
<a name="line-455"></a><span class='hs-definition'>readFile'</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>FilePath</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>String</span>
<a name="line-456"></a><span class='hs-definition'>readFile'</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-457"></a> <span class='hs-varid'>h</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>openFile</span> <span class='hs-varid'>name</span> <span class='hs-conid'>ReadMode</span>
<a name="line-458"></a> <span class='hs-varid'>hSetNewlineMode</span> <span class='hs-varid'>h</span> <span class='hs-varid'>universalNewlineMode</span>
<a name="line-459"></a> <span class='hs-varid'>hGetContents</span> <span class='hs-varid'>h</span>
</pre></body>
</html>
|