| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Examples of pretty printing |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --sized-types #-} |
| 8 |
|
| 9 |
module README.Text.Pretty where |
| 10 |
|
| 11 |
open import Size |
| 12 |
|
| 13 |
open import Data.Bool.Base |
| 14 |
open import Data.List.Base as List |
| 15 |
open import Data.List.NonEmpty as List⁺ |
| 16 |
open import Data.Nat.Base |
| 17 |
open import Data.Product.Base using (_×_; uncurry; _,_) |
| 18 |
open import Data.String.Base hiding (parens; _<+>_) |
| 19 |
open import Data.Vec.Base as Vec |
| 20 |
open import Function.Base |
| 21 |
|
| 22 |
-- We import the pretty printer and pass 80 to say that we do not want to |
| 23 |
-- have lines longer than 80 characters |
| 24 |
open import Text.Pretty 80 |
| 25 |
|
| 26 |
open import Relation.Binary.PropositionalEquality |
| 27 |
|
| 28 |
------------------------------------------------------------------------ |
| 29 |
-- A small declarative programming language |
| 30 |
------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- We define a small programming language where definitions are |
| 33 |
-- introduced by providing a non-empty list of equations with: |
| 34 |
-- * the same number of patterns on the LHS |
| 35 |
-- * a term on the RHS of each equation |
| 36 |
|
| 37 |
-- A pattern is either a variable or a constructor applied to a |
| 38 |
-- list of subpatterns |
| 39 |
|
| 40 |
data Pattern (i : Size) : Set where |
| 41 |
var : String → Pattern i |
| 42 |
con : ∀ {j : Size< i} → String → List (Pattern j) → Pattern i |
| 43 |
|
| 44 |
-- A term is either a (bound) variable, the application of a |
| 45 |
-- named definition / constructor to a list of arguments or a |
| 46 |
-- lambda abstraction |
| 47 |
|
| 48 |
data Term (i : Size) : Set where |
| 49 |
var : String → Term i |
| 50 |
app : ∀ {j : Size< i} → String → List (Term j) → Term i |
| 51 |
lam : ∀ {j : Size< i} → String → Term j → Term i |
| 52 |
|
| 53 |
-- As explained before, a definitions is given by a list of equations |
| 54 |
|
| 55 |
infix 1 _by_ |
| 56 |
record Def : Set where |
| 57 |
constructor _by_ |
| 58 |
field name : String |
| 59 |
{arity} : ℕ |
| 60 |
equations : List⁺ (Vec (Pattern _) arity × (Term _)) |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- A pretty printer for this language |
| 64 |
------------------------------------------------------------------------ |
| 65 |
|
| 66 |
-- First we print patterns. We only wrap a pattern in parentheses if it |
| 67 |
-- is compound: i.e. if it is a constructor applied to a non-empty list |
| 68 |
-- of subpatterns |
| 69 |
-- Lists of patterns are printed separated by a single space. |
| 70 |
|
| 71 |
prettyPattern : ∀ {i} → Pattern i → Doc |
| 72 |
prettyPatterns : ∀ {i} → List (Pattern i) → Doc |
| 73 |
|
| 74 |
prettyPattern (var v) = text v |
| 75 |
prettyPattern (con c []) = text c |
| 76 |
prettyPattern (con c ps) = parens $ text c <+> prettyPatterns ps |
| 77 |
|
| 78 |
prettyPatterns = hsep ∘ List.map prettyPattern |
| 79 |
|
| 80 |
-- Next we print terms. The Bool argument tells us whether we are on |
| 81 |
-- the RHS of an application (in which case it is sensible to wrap |
| 82 |
-- complex subterms in parentheses). |
| 83 |
|
| 84 |
prettyTerm : ∀ {i} → Bool → Term i → Doc |
| 85 |
prettyTerm l (var v) = text v |
| 86 |
prettyTerm l (app f []) = text f |
| 87 |
prettyTerm l (app f es) = if l then parens else id |
| 88 |
$ text f <+> sep (List.map (prettyTerm true) es) |
| 89 |
prettyTerm l (lam x b) = if l then parens else id |
| 90 |
$ text "λ" <+> text x <> text "." <+> prettyTerm false b |
| 91 |
|
| 92 |
-- We now have all the pieces to print definitions. |
| 93 |
-- We print the equations below each other by using vcat. |
| 94 |
-- |
| 95 |
-- The LHS is printed as follows: the name of the function followed by |
| 96 |
-- the space-separated list of patterns (if any) and then an equal sign. |
| 97 |
-- |
| 98 |
-- The RHS is printed as a term which is *not* on the RHS of an application. |
| 99 |
-- |
| 100 |
-- Finally we can layout the definition in two different manners: |
| 101 |
-- * either LHS followed by RHS |
| 102 |
-- * or LHS followed and the RHS as a relative block (indented by 2 spaces) |
| 103 |
-- on the next line |
| 104 |
|
| 105 |
prettyDef : Def → Doc |
| 106 |
prettyDef (fun by eqs) = |
| 107 |
vcat $ List⁺.toList $ flip List⁺.map eqs $ uncurry $ λ ps e → |
| 108 |
let lhs = text fun <+> (case ps of λ where |
| 109 |
[] → text "=" |
| 110 |
_ → prettyPatterns (Vec.toList ps) <+> text "=") |
| 111 |
rhs = prettyTerm false e |
| 112 |
in lhs <+> rhs <|> lhs $$ (spaces 2 <> rhs) |
| 113 |
|
| 114 |
-- The pretty printer is obtained by using the renderer. |
| 115 |
|
| 116 |
pretty : Def → String |
| 117 |
pretty = render ∘ prettyDef |
| 118 |
|
| 119 |
------------------------------------------------------------------------ |
| 120 |
-- Some examples |
| 121 |
------------------------------------------------------------------------ |
| 122 |
|
| 123 |
-- Our first example is the identity function defined as a λ-abstraction |
| 124 |
|
| 125 |
`id : Def |
| 126 |
`id = "id" by ([] , lam "x" (var "x")) ∷ [] |
| 127 |
|
| 128 |
_ : pretty `id ≡ "id = λ x. x" |
| 129 |
_ = refl |
| 130 |
|
| 131 |
-- If we were to assume that this definition also takes a level (a) and |
| 132 |
-- a Set at that level (A) as arguments, we can have a slightly more complex |
| 133 |
-- definition like so. |
| 134 |
|
| 135 |
`explicitid : Def |
| 136 |
`explicitid = "id" by (var "a" ∷ var "A" ∷ [] , lam "x" (var "x")) ∷ [] |
| 137 |
|
| 138 |
_ : pretty `explicitid ≡ "id a A = λ x. x" |
| 139 |
_ = refl |
| 140 |
|
| 141 |
-- A more complex example: boolFilter, a function that takes a boolean |
| 142 |
-- predicate and a list as arguments and returns a list containing only |
| 143 |
-- the values that satisfy the predicate. |
| 144 |
-- We use nil and con for [] and _∷_ as our little toy language does not |
| 145 |
-- support infix notations. |
| 146 |
|
| 147 |
`filter : Def |
| 148 |
`filter = "boolFilter" |
| 149 |
by ( var "P?" ∷ con "nil" [] ∷ [] |
| 150 |
, app "nil" [] |
| 151 |
) |
| 152 |
∷ ( var "P?" ∷ con "con" (var "x" ∷ var "xs" ∷ []) ∷ [] |
| 153 |
, let rec = app "filter" (var "P?" ∷ var "xs" ∷ []) in |
| 154 |
app "if" (app "P?" (var "x" ∷ []) |
| 155 |
∷ app "con" (var "x" ∷ rec ∷ []) |
| 156 |
∷ rec |
| 157 |
∷ []) |
| 158 |
) |
| 159 |
∷ [] |
| 160 |
|
| 161 |
_ : pretty `filter ≡ |
| 162 |
"boolFilter P? nil = nil |
| 163 |
\ \boolFilter P? (con x xs) = if (P? x) (con x (filter P? xs)) (filter P? xs)" |
| 164 |
_ = refl |
| 165 |
|
| 166 |
-- We can once more revisit this example with its more complex counterpart: |
| 167 |
-- boolFilter taking its level and set arguments explicitly (idem for the |
| 168 |
-- list constructors nil and con). |
| 169 |
-- This time laying out the second equation on a single line would produce a |
| 170 |
-- string larger than 80 characters long. So the pretty printer decides to |
| 171 |
-- make the RHS a relative block indented by 2 spaces. |
| 172 |
|
| 173 |
`explicitfilter : Def |
| 174 |
`explicitfilter = "boolFilter" |
| 175 |
by ( var "a" ∷ var "A" ∷ var "P?" ∷ con "nil" [] ∷ [] |
| 176 |
, app "nil" (var "a" ∷ var "A" ∷ []) |
| 177 |
) |
| 178 |
∷ ( var "a" ∷ var "A" ∷ var "P?" ∷ con "con" (var "x" ∷ var "xs" ∷ []) ∷ [] |
| 179 |
, let rec = app "filter" (var "a" ∷ var "A" ∷ var "P?" ∷ var "xs" ∷ []) in |
| 180 |
app "if" (app "P?" (var "x" ∷ []) |
| 181 |
∷ app "con" (var "a" ∷ var "A" ∷ var "x" ∷ rec ∷ []) |
| 182 |
∷ rec |
| 183 |
∷ []) |
| 184 |
) |
| 185 |
∷ [] |
| 186 |
|
| 187 |
_ : pretty `explicitfilter |
| 188 |
≡ "boolFilter a A P? nil = nil a A |
| 189 |
\ \boolFilter a A P? (con x xs) = |
| 190 |
\ \ if (P? x) (con a A x (filter a A P? xs)) (filter a A P? xs)" |
| 191 |
_ = refl |