-
Notifications
You must be signed in to change notification settings - Fork 35
/
VerySimpleExpressions.lhs
789 lines (589 loc) · 20.2 KB
/
VerySimpleExpressions.lhs
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
[[very-simple-expression-parsing]]
= Very simple expression parsing
In this tutorial we will develop a parser for a very simple expression
language, and start learning about the set of combinators which comes
with Parsec.
> import Text.Parsec (ParseError)
> import Text.Parsec.String (Parser)
> import Text.Parsec.String.Parsec (try)
> import Text.Parsec.String.Char (oneOf, char, digit, satisfy)
> import Text.Parsec.String.Combinator (many1, choice, chainl1)
> import Control.Applicative ((<|>), many)
> import Control.Monad (void)
> import Data.Char (isLetter, isDigit)
> import FunctionsAndTypesForParsing
== num
The first element we will have in this expression language is positive
integral numbers:
> numberExamples :: [(String,Integer)]
> numberExamples = [("1", 1)
> ,("23", 23)]
TODO: make examples with parsing failures for all of the example
scripts below?
To parse a number, we need to parse one or more digits, and then read
the resulting string. We can use the combinator `many1` to help with
this. We will also use do notation.
> num :: Parser Integer
> num = do
> n <- many1 digit
> return (read n)
Let's try it out.
```
*Main> regularParse num "1"
Right 1
*Main> regularParse num "123456"
Right 123456
*Main> regularParse num "aa"
Left (line 1, column 1):
unexpected "a"
expecting digit
```
How does it work? First, we parse one or more (`many1`) digits (`digit`),
and give the result the name 'n'. Then we convert the string to an
integer using `read`.
The `many1` function's type looks like this:
```
many1 :: Parser a -> Parser [a]
```
It applies the parser given one or more times, returning the result.
Let's see what happens when we use the `many` combinator which parses
zero or more items instead of one or more.
> num1 :: Parser Integer
> num1 = do
> n <- many digit
> return (read n)
```
*Main> regularParse num1 "1"
Right 1
*Main> regularParse num1 "123456"
Right 123456
*Main> regularParse num1 "aa"
Right *** Exception: Prelude.read: no parse
```
== var
For var, we have to decide on a syntax for the identifiers. Let's go
for a common choice: identifiers must start with a letter or
underscore, and then they can be followed by zero or more letters,
underscores or digits in any combination.
> varExamples :: [(String,String)]
> varExamples = [("test", "test")
> ,("_stuff", "_stuff")
> ,("_1234", "_1234")]
> var :: Parser String
> var = do
> fc <- firstChar
> rest <- many nonFirstChar
> return (fc:rest)
> where
> firstChar = satisfy (\a -> isLetter a || a == '_')
> nonFirstChar = satisfy (\a -> isDigit a || isLetter a || a == '_')
This time, we create two helper parsers: `firstChar`, which parses a
letter or underscore, and `nonFirstChar` which parses a digit, letter
or underscore. This time, we use the `many` function instead of
`many1`.
Try it out in ghci. I like to try things which you expect to work, and
also to try things which you expect to not work and make sure you get
an error.
== parens
The parens parser will eventually parse any expression inside
parentheses. First it will just parse integers inside parentheses.
> data Parentheses = Parentheses Integer
> deriving (Eq,Show)
>
> parensExamples :: [(String, Parentheses)]
> parensExamples = [("(1)", Parentheses 1)
> ,("(17)", Parentheses 17)]
> parens :: Parser Parentheses
> parens = do
> void $ char '('
> e <- many1 digit
> void $ char ')'
> return (Parentheses (read e))
There is a new function: `void`. This might be familiar to you
already. This is used to ignore the result of the `char` parser, since
we are not interested in this value. You can also write the function
without `void`, but ghc will give you a warning if you have warnings
turned on.
One way of turning warnings on in ghci is to enter `:set -Wall` at the
ghci prompt.
> parens' :: Parser Parentheses
> parens' = do
> char '('
> e <- many1 digit
> char ')'
> return (Parentheses (read e))
```
*Main> :set -Wall
*Main> :l "VerySimpleExpressions.lhs"
...
FirstRealParsing.lhs:140:7: Warning:
A do-notation statement discarded a result of type Char.
Suppress this warning by saying "_ <- char '('",
or by using the flag -fno-warn-unused-do-bind
FirstRealParsing.lhs:142:7: Warning:
A do-notation statement discarded a result of type Char.
Suppress this warning by saying "_ <- char ')'",
or by using the flag -fno-warn-unused-do-bind
...
```
As you can see, another way to suppress the warning is to use
`_ <- char '('`.
One issue with this parser is that it doesn't handle whitespace:
```
*Main> regularParse parens "(1)"
Right (Parentheses 1)
*Main> regularParse parens "( 1)"
Left (line 1, column 2):
unexpected " "
expecting digit
*Main> regularParse parens "(1 )"
Left (line 1, column 3):
unexpected " "
expecting digit or ")"
```
We will look at this issue below.
== add
Now we will write a little parser to parse strings like 'a+b' where a
and b are numbers.
> data SingleAdd = SingleAdd Integer Integer
> deriving (Eq,Show)
>
> singleAddExamples :: [(String, SingleAdd)]
> singleAddExamples = [("1+2", SingleAdd 1 2)
> ,("101+202", SingleAdd 101 202)]
> add :: Parser SingleAdd
> add = do
> e0 <- many1 digit
> void $ char '+'
> e1 <- many1 digit
> return (SingleAdd (read e0) (read e1))
It has the same whitespace issues as the parens parser.
```
*Main> regularParse add "1+2"
Right (SingleAdd 1 2)
*Main> regularParse add "1 +2"
Left (line 1, column 2):
unexpected " "
expecting digit or "+"
```
== whitespace
Here is a parser which will skip zero or more whitespace characters.
> whitespace :: Parser ()
> whitespace = void $ many $ oneOf " \n\t"
We can use this to make our parsers handle whitespace better.
```
*Main> regularParse whitespace " "
Right ()
*Main> regularParse whitespace " "
Right ()
*Main> regularParse whitespace "\t"
Right ()
*Main> regularParse whitespace " \n "
Right ()
*Main> regularParse whitespace ""
Right ()
```
Notice that it always succeeds.
Here is the parens parser rewritten with a common approach to
whitespace handling:
> parensW :: Parser Parentheses
> parensW = do
> whitespace
> void $ char '('
> whitespace
> e <- many1 digit
> whitespace
> void $ char ')'
> whitespace
> return (Parentheses (read e))
```
*Main> regularParse parensW "(1)"
Right (Parentheses 1)
*Main> regularParse parensW " (1)"
Right (Parentheses 1)
*Main> regularParse parensW " (1 )"
Right (Parentheses 1)
*Main> regularParse parensW " ( 1 ) "
Right (Parentheses 1)
```
Looks good.
In the original parsec documentation, one of the concepts mentioned is
the idea of 'lexeme' parsing. This is a style in which every token
parser should also consume and ignore any trailing whitespace.
This is a simple convention which with a bit of care allows skipping
whitespace exactly once wherever it needs to be skipped. To complete
the lexeme style, we should also always skip leading whitespace at the
top level only. This feels more elegant than spamming all the parsing
code with many calls to `whitespace`.
> lexeme :: Parser a -> Parser a
> lexeme p = do
> x <- p
> whitespace
> return x
> parseWithWhitespace :: Parser a -> String -> Either ParseError a
> parseWithWhitespace p = parseWithEof wrapper
> where
> wrapper = do
> whitespace
> p
Here is the parens parser rewritten to use lexeme:
> parensL :: Parser Parentheses
> parensL = do
> void $ lexeme $ char '('
> e <- lexeme $ many1 digit
> void $ lexeme $ char ')'
> return (Parentheses (read e))
```
*Main> parseWithWhitespace parensL "(1)"
Right (Parentheses 1)
*Main> parseWithWhitespace parensL " (1)"
Right (Parentheses 1)
*Main> parseWithWhitespace parensL " ( 1)"
Right (Parentheses 1)
*Main> parseWithWhitespace parensL " ( 1 ) "
Right (Parentheses 1)
```
The `parseWithWhitespace` function can also use `(>>)` to make it a
bit shorter, `wrapper = whiteSpace >> p`.
Here is the shorter version of this function using `(>>)`:
> parseWithWhitespace' :: Parser a -> String -> Either ParseError a
> parseWithWhitespace' p = parseWithEof (whitespace >> p)
Try rewriting the SingleAdd parser to use `lexeme`, and test it out to
convince yourself that it skips whitespace correctly.
== simple expr
Now we are ready to write a parser which parses simple expressions
made from these components. Here is the data type for these
expressions.
> data SimpleExpr = Num Integer
> | Var String
> | Add SimpleExpr SimpleExpr
> | Parens SimpleExpr
> deriving (Eq,Show)
It's so simple that it is almost useless at the moment.
> simpleExprExamples :: [(String,SimpleExpr)]
> simpleExprExamples =
> [("a", Var "a")
> ,("1", Num 1)
> ,("2 + 3", Add (Num 2) (Num 3))
> ,("(42)", Parens (Num 42))]
TODO: some more complex examples
Here are all our component parsers with `lexeme`, and with the
`SimpleExpr` constructors:
> numE :: Parser SimpleExpr
> numE = do
> n <- lexeme $ many1 digit
> return $ Num $ read n
There doesn't seem to be a unique obviously correct place to put the
lexeme call in the var parser:
> varE :: Parser SimpleExpr
> varE = lexeme $ do
> fc <- firstChar
> rest <- many nonFirstChar
> return $ Var (fc:rest)
> where
> firstChar = satisfy (\a -> isLetter a || a == '_')
> nonFirstChar = satisfy (\a -> isDigit a || isLetter a || a == '_')
Here is an alternative, with the call to lexeme in a different place,
but gives effectively the same function.
> varE' :: Parser SimpleExpr
> varE' = do
> fc <- firstChar
> rest <- lexeme $ many nonFirstChar
> return $ Var (fc:rest)
> where
> firstChar = satisfy (\a -> isLetter a || a == '_')
> nonFirstChar = satisfy (\a -> isDigit a || isLetter a || a == '_')
> parensE :: Parser SimpleExpr
> parensE = do
> void $ lexeme $ char '('
> e <- lexeme $ many1 digit
> void $ lexeme $ char ')'
> return $ Parens $ Num $ read e
In the parens parser, we can reuse the `numE` parser like this:
> parensE' :: Parser SimpleExpr
> parensE' = do
> void $ lexeme $ char '('
> e <- numE
> void $ lexeme $ char ')'
> return $ Parens e
Here is the add parser using `numE` also.
> addE :: Parser SimpleExpr
> addE = do
> e0 <- numE
> void $ lexeme $ char '+'
> e1 <- numE
> return $ Add e0 e1
=== choice
To combine these, we can use an operator called `(<|>)`:
> numOrVar :: Parser SimpleExpr
> numOrVar = numE <|> varE
It tries the first parser, and it if fails (without consuming any
input), it tries the second parser. More about the 'consuming input'
concept later.
Here is another way to write the numOrVar parser:
> numOrVar' :: Parser SimpleExpr
> numOrVar' = choice [numE,varE]
`choice` is just wrapper around `(<|>)`. You can choose which one to
use based on which is more readable in each particular case.
```
*Main> parseWithWhitespace numOrVar "a"
Right (Var "a")
*Main> parseWithWhitespace numOrVar "1"
Right (Num 1)
*Main> parseWithWhitespace numOrVar "!"
Left (line 1, column 1):
unexpected "!"
expecting digit
```
Here is the first version of the simpleExpr parser:
> simpleExpr :: Parser SimpleExpr
> simpleExpr = numE <|> varE <|> addE <|> parensE
```
*Main> parseWithWhitespace simpleExpr "12"
Right (Num 12)
*Main> parseWithWhitespace simpleExpr "aa"
Right (Var "aa")
*Main> parseWithWhitespace simpleExpr "1+2"
Left (line 1, column 2):
unexpected '+'
expecting digit or end of input
*Main> parseWithWhitespace simpleExpr "(1)"
Right (Parens (Num 1))
*Main> parseWithWhitespace simpleExpr "(aa)"
Left (line 1, column 2):
unexpected "a"
expecting digit
```
It works well for some of the parsers. One problem is that the `addE`
and `parensE` parsers don't parse general expressions as the
components, but just `numE`. Another problem is that the `addE`
doesn't work at all: the `numE` parser parses the first number, and
the `addE` parser is never tried. This is an example of `(<|>)` not
trying the second parser if the first parser succeeds, even if a later
alternative would consume more input or successfully parse the whole
input.
Let's try and rearrange the order:
> simpleExpr1 :: Parser SimpleExpr
> simpleExpr1 = addE <|> numE <|> varE <|> parensE
```
*Main> parseWithWhitespace simpleExpr1 "12"
Left (line 1, column 3):
unexpected end of input
expecting digit or "+"
*Main> parseWithWhitespace simpleExpr1 "aa"
Right (Var "aa")
*Main> parseWithWhitespace simpleExpr1 "1+2"
Right (Add (Num 1) (Num 2))
*Main> parseWithWhitespace simpleExpr1 "(1)"
Right (Parens (Num 1))
```
We swapped one problem for another. Let's fix this using the `try`
function.
> simpleExpr2 :: Parser SimpleExpr
> simpleExpr2 = try addE <|> numE <|> varE <|> parensE
```
*Main> parseWithWhitespace simpleExpr2 "12"
Right (Num 12)
*Main> parseWithWhitespace simpleExpr2 "aa"
Right (Var "aa")
*Main> parseWithWhitespace simpleExpr2 "1+2"
Right (Add (Num 1) (Num 2))
*Main> parseWithWhitespace simpleExpr2 "(1)"
Right (Parens (Num 1))
```
Now everything seems to work fine. The `try` function is very powerful
and easy to use, and can be used where in a more traditional parsing
approach you would have to use left factoring or something else.
The `try` function implements backtracking. When this is used with
`(<|>)`, it means that if the first parser fails, it will undo the
consumed input and carry on with the next option, instead of failing
completely. This works even if the `try` is nested deeply within the
first parser given to `(<|>)`.
`try` has its downsides (some of which we will see later), and I
usually try to minimise its use or eliminate it completely. I found I
often got into a complete mess when I used `try` too much when writing
parsers for something a little tricky like SQL, and that although
doing some left-factoring appeared at first to be tedious and appeared
to make the code less readable, I eventually decided that for me it
made the code more readable since what was happening was more
transparent.
Now we are going to fix this parser to parse arbitrarily nested
expressions. In a way, the method used will roughly mean we are left
factoring the `numE` and `addE` common prefix.
Here is the naive implementation:
> parensE3 :: Parser SimpleExpr
> parensE3 = do
> void $ lexeme $ char '('
> e <- simpleExpr3
> void $ lexeme $ char ')'
> return $ Parens e
> addE3 :: Parser SimpleExpr
> addE3 = do
> e0 <- simpleExpr3
> void $ lexeme $ char '+'
> e1 <- simpleExpr3
> return $ Add e0 e1
> simpleExpr3 :: Parser SimpleExpr
> simpleExpr3 = try addE3 <|> numE <|> varE <|> parensE3
If you run this parser, it will enter an infinite loop, since
`simpleExpr3` and `addE3` will keep calling each other recursively
without making any progress.
```
*Main> parseWithWhitespace simpleExpr3 "a+b"
C-c Interrupted.
```
Let's try without `add`.
> parensE4 :: Parser SimpleExpr
> parensE4 = do
> void $ lexeme $ char '('
> e <- simpleExpr4
> void $ lexeme $ char ')'
> return $ Parens e
> simpleExpr4 :: Parser SimpleExpr
> simpleExpr4 = numE <|> varE <|> parensE4
```
*Main> parseWithWhitespace simpleExpr4 "a"
Right (Var "a")
*Main> parseWithWhitespace simpleExpr4 "1"
Right (Num 1)
*Main> parseWithWhitespace simpleExpr4 "(1)"
Right (Parens (Num 1))
*Main> parseWithWhitespace simpleExpr4 "((a))"
Right (Parens (Parens (Var "a")))
```
At least this part seems to work OK.
Let's try to stop the add parser from calling itself indirectly:
Here is a parameterized parens parser where we supply the nested
expression parser as an argument. This is used here to try to make the
code easier to follow and avoid rewriting this parser out again and
again in full.
> parensEN :: Parser SimpleExpr -> Parser SimpleExpr
> parensEN simpleExprImpl = do
> void $ lexeme $ char '('
> e <- simpleExprImpl
> void $ lexeme $ char ')'
> return $ Parens e
Here is a new parser, which parses expressions except add.
> term :: Parser SimpleExpr -> Parser SimpleExpr
> term simpleExprImpl = numE <|> varE <|> parensEN simpleExprImpl
> term5 :: Parser SimpleExpr
> term5 = term simpleExpr5
> addE5 :: Parser SimpleExpr
> addE5 = do
> e0 <- term5
> void $ lexeme $ char '+'
> e1 <- term5
> return $ Add e0 e1
> simpleExpr5 :: Parser SimpleExpr
> simpleExpr5 = try addE5 <|> term5
```
*Main> parseWithWhitespace simpleExpr5 "1"
Right (Num 1)
*Main> parseWithWhitespace simpleExpr5 "a"
Right (Var "a")
*Main> parseWithWhitespace simpleExpr5 "(a)"
Right (Parens (Var "a"))
*Main> parseWithWhitespace simpleExpr5 "1+2"
Right (Add (Num 1) (Num 2))
*Main> parseWithWhitespace simpleExpr5 "1+a"
Right (Add (Num 1) (Var "a"))
*Main> parseWithWhitespace simpleExpr5 "(1+a)"
Right (Parens (Add (Num 1) (Var "a")))
*Main> parseWithWhitespace simpleExpr5 "1+a+b"
Left (line 1, column 4):
unexpected '+'
expecting end of input
```
Almost. Let's see what happens when the second `term` in `add` is
changed to the general expression parser.
> term6 :: Parser SimpleExpr
> term6 = term simpleExpr6
> addE6 :: Parser SimpleExpr
> addE6 = do
> e0 <- term6
> void $ lexeme $ char '+'
> e1 <- simpleExpr6
> return $ Add e0 e1
> simpleExpr6 :: Parser SimpleExpr
> simpleExpr6 = try addE6 <|> term6
```
*Main> parseWithWhitespace simpleExpr6 "a + b + c"
Right (Add (Var "a") (Add (Var "b") (Var "c")))
```
Maybe it looks like we've made it. But there is a problem. We've
parsed the + operator as if it has right associativity:
```
a + b + c -> a + (b + c)
```
But it should be left associative:
```
a + b + c -> (a + b) + c
```
Let's left factor the parsing and fix this:
> term7 :: Parser SimpleExpr
> term7 = term simpleExpr7
> simpleExpr7 :: Parser SimpleExpr
> simpleExpr7 = do
> -- first parse a term
> e <- term7
> -- then see if it is followed by an '+ expr' suffix
> maybeAddSuffix e
> where
> -- this function takes an expression, and parses a
> -- '+ expr' suffix, returning an Add expression
> -- it recursively calls itself via the maybeAddSuffix function
> addSuffix e0 = do
> void $ lexeme $ char '+'
> e1 <- term7
> maybeAddSuffix (Add e0 e1)
> -- this is the wrapper for addSuffix, which adapts it so that if
> -- addSuffix fails, it returns just the original expression
> maybeAddSuffix e = addSuffix e <|> return e
```
*Main> parseWithWhitespace simpleExpr7 "a + b + c"
Right (Add (Add (Var "a") (Var "b")) (Var "c"))
```
Now the parser seems to work for everything it should.
There is a combinator function in Parsec we can use which abstracts
this sort of pattern, `chainl1`.
> simpleExpr8 :: Parser SimpleExpr
> simpleExpr8 = chainl1 term8 op
> where
> op = do
> void $ lexeme $ char '+'
> return Add
> term8 = term simpleExpr8
How does this work? Here is the type of `chainl1`:
```haskell
chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 term op = ...
```
The type of the Add constructor in pseudocode is:
```haskell
Add :: SimpleExpr -> SimpleExpr -> SimpleExpr
```
The `op` parser here now just parses the operator itself, i.e. '+'
(and not the second expression like our simpleExpr7 parser). The
return from the `op` function is a function which accepts two elements
and combines them using the appropriate operator representation. In
this case, the represenation is the `Add` constructor.
You can look at the source
<http://hackage.haskell.org/package/parsec-3.1.3/docs/src/Text-Parsec-Combinator.html#chainl1>
and see if you can understand how it works. If you can't work it out,
you could come back to it later when you have more experience writing
parsing code.
== Testing with the examples
TODO: write a little manual tester that accepts a parser and a list of
examples, and checks they all parse correctly.
== Testing with quickcheck
Let's see if we can check with quickcheck. It's a bit tricky testing
parsers in this way, but one way to do something useful is to generate
random asts, convert them to concrete syntax, parse them, and check
the result. We can write a simple 'pretty printer' to convert an ast
to concrete syntax.
=== a pretty printer
TODO: a really simple pretty printer just pasting strings together, no
layout.
=== the quick check code
TODO: write a quickcheck property and arbitary instance and show
running it at the ghci prompt