-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathTutorial.hs
2316 lines (2257 loc) · 72.1 KB
/
Tutorial.hs
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-| Dhall is a programming language specialized for configuration files. This
module contains a tutorial explaining how to author configuration files
using this language
You might also be interested in the
<https://docs.dhall-lang.org/tutorials/Language-Tour.html Language Tour>, a
Haskell-agnostic tour of Dhall's language features.
-}
module Dhall.Tutorial (
-- * Introduction
-- $introduction
-- * Types
-- $types
-- * Imports
-- $imports
-- * Lists
-- $lists
-- * Optional values
-- $optional0
-- * Records
-- $records
-- * Functions
-- $functions
-- * Compiler
-- $compiler
-- * Strings
-- $strings
-- * Combine
-- $combine
-- * Let expressions
-- $let
-- * Defaults
-- $defaults
-- * Unions
-- $unions
-- * Polymorphic functions
-- $polymorphic
-- * Total
-- $total
-- * Assertions
-- $assertions
-- * Headers
-- $headers
-- * Import integrity
-- $integrity
-- * Raw text
-- $rawText
-- * Formatting code
-- $format
-- * Built-in functions
-- $builtins
-- ** Caveats
-- $caveats
-- ** Extending the language
-- $extending
-- ** Substitutions
-- $substitutions
-- * Prelude
-- $prelude
-- * Limitations
-- $limitations
-- * Conclusion
-- $conclusion
-- * Frequently Asked Questions (FAQ)
-- $faq
) where
import Data.Vector (Vector)
import Dhall
-- $setup
--
-- >>> :set -XOverloadedStrings
-- $introduction
--
-- The simplest way to use Dhall is to ignore the programming language features
-- and use it as a strongly typed configuration format. For example, suppose
-- that you create the following configuration file:
--
-- > -- ./config.dhall
-- > { foo = 1
-- > , bar = [3.0, 4.0, 5.0]
-- > }
--
-- You can read the above configuration file into Haskell using the following
-- code:
--
-- > -- example.hs
-- >
-- > {-# LANGUAGE DeriveGeneric #-}
-- > {-# LANGUAGE OverloadedStrings #-}
-- >
-- > import Dhall
-- >
-- > data Example = Example { foo :: Natural, bar :: Vector Double }
-- > deriving (Generic, Show)
-- >
-- > instance FromDhall Example
-- >
-- > main :: IO ()
-- > main = do
-- > x <- input auto "./config.dhall"
-- > print (x :: Example)
--
-- __WARNING__: You must not instantiate FromDhall with a recursive type. See [Limitations](#limitations).
--
-- If you compile and run the above example, the program prints the corresponding
-- Haskell record:
--
-- > $ ./example
-- > Example {foo = 1, bar = [3.0,4.0,5.0]}
--
-- You can also load some types directly into Haskell without having to define a
-- record, like this:
--
-- >>> import Dhall
-- >>> :set -XOverloadedStrings
-- >>> input auto "True" :: IO Bool
-- True
--
-- The `Dhall.input` function can decode any value if we specify the value's
-- expected `Dhall.Type`:
--
-- > input
-- > :: Type a -- Expected type
-- > -> Text -- Dhall program
-- > -> IO a -- Decoded expression
--
-- ... and we can either specify an explicit type like `bool`:
--
-- > bool :: Type Bool
-- >
-- > input bool :: Text -> IO Bool
-- >
-- > input bool "True" :: IO Bool
--
-- >>> input bool "True"
-- True
--
-- ... or we can use `auto` to let the compiler infer what type to decode from
-- the expected return type:
--
-- > auto :: FromDhall a => Type a
-- >
-- > input auto :: FromDhall a => Text -> IO a
--
-- >>> input auto "True" :: IO Bool
-- True
--
-- You can see what types `auto` supports \"out-of-the-box\" by browsing the
-- instances for the `FromDhall` class. For example, the following instance
-- says that we can directly decode any Dhall expression that evaluates to a
-- @Bool@ into a Haskell `Prelude.Bool`:
--
-- > instance FromDhall Bool
--
-- ... which is why we could directly decode the string @\"True\"@ into the
-- value `True`.
--
-- There is also another instance that says that if we can decode a value of
-- type @a@, then we can also decode a @List@ of values as a `Vector` of @a@s:
--
-- > instance FromDhall a => FromDhall (Vector a)
--
-- Therefore, since we can decode a @Bool@, we must also be able to decode a
-- @List@ of @Bool@s, like this:
--
-- >>> input auto "[True, False]" :: IO (Vector Bool)
-- [True,False]
--
-- We could also specify what type to decode by providing an explicit
-- `Dhall.Type` instead of using `auto` with a type annotation:
--
-- >>> input (vector bool) "[True, False]"
-- [True,False]
--
-- __Exercise:__ Create a @./config.dhall@ file that the following program can
-- decode:
--
-- > {-# LANGUAGE DeriveAnyClass #-}
-- > {-# LANGUAGE DeriveGeneric #-}
-- > {-# LANGUAGE OverloadedStrings #-}
-- >
-- > import Dhall
-- >
-- > data Person = Person { age :: Natural, name :: Text }
-- > deriving (Generic, FromDhall, Show)
-- >
-- > main :: IO ()
-- > main = do
-- > x <- input auto "./config.dhall"
-- > print (x :: Person)
--
-- __Exercise:__ Create a @./config.dhall@ file that the following program can
-- decode:
--
-- > {-# LANGUAGE OverloadedStrings #-}
-- >
-- > import Data.Functor.Identity
-- > import Dhall
-- >
-- > instance FromDhall a => FromDhall (Identity a)
-- >
-- > main :: IO ()
-- > main = do
-- > x <- input auto "./config.dhall"
-- > print (x :: Identity Double)
-- $types
--
-- Suppose that we try to decode a value of the wrong type, like this:
--
-- >>> input auto "1" :: IO Bool
-- *** Exception:
-- ...Error...: Expression doesn't match annotation
-- ...
-- - Bool
-- + Natural
-- ...
-- 1│ 1 : Bool
-- ...
-- (input):1:1
-- ...
--
-- The interpreter complains because the string @\"1\"@ cannot be decoded into a
-- Haskell value of type `Prelude.Bool`. This part of the type error:
--
-- > - Bool
-- > + Natural
--
-- ... means that the expected type was @Bool@ but the inferred type of the
-- expression @1@ was @Natural@.
--
-- The code excerpt from the above error message has two components:
--
-- * the expression being type checked (i.e. @1@)
-- * the expression's expected type (i.e. @Bool@)
--
-- > Expression
-- > ⇩
-- > 1 : Bool
-- > ⇧
-- > Expected type
--
-- The @(:)@ symbol is how Dhall annotates values with their expected types.
-- This notation is equivalent to type annotations in Haskell using the @(::)@
-- symbol. Whenever you see:
--
-- > x : t
--
-- ... you should read that as \"we expect the expression @x@ to have type
-- @t@\". However, we might be wrong and if our expected type does not match the
-- expression's actual type then the type checker will complain.
--
-- In this case, the expression @1@ does not have type @Bool@ so type checking
-- fails with an exception.
--
-- __Exercise:__ Load the Dhall library into @ghci@ and run these commands to get
-- get a more detailed error message:
--
-- > >>> import Dhall
-- > >>> :set -XOverloadedStrings
-- > >>> detailed (input auto "1") :: IO Bool
-- > ...
--
-- ... then read the entire error message
--
-- __Exercise:__ Fix the type error, either by changing the value to decode or
-- changing the expected type
-- $imports
--
-- You might wonder why in some cases we can decode a configuration file:
--
-- >>> writeFile "bool" "True"
-- >>> input auto "./bool" :: IO Bool
-- True
--
-- ... and in other cases we can decode a value directly:
--
-- >>> input auto "True" :: IO Bool
-- True
--
-- This is because importing a configuration from a file is a special case of a
-- more general language feature: Dhall expressions can reference other
-- expressions by their file path.
--
-- To illustrate this, let's create three files:
--
-- > $ echo "True" > bool1
-- > $ echo "False" > bool2
-- > $ echo "./bool1 && ./bool2" > both
--
-- ... and read in all three files in a single expression:
--
-- >>> input auto "[ ./bool1 , ./bool2 , ./both ]" :: IO (Vector Bool)
-- [True,False,False]
--
-- Each file path is replaced with the Dhall expression contained within that
-- file. If that file contains references to other files then those references
-- are transitively resolved.
--
-- In other words: configuration files can reference other configuration files,
-- either by their relative or absolute paths. This means that we can split a
-- configuration file into multiple files, like this:
--
-- > -- ./config.dhall
-- > { foo = 1
-- > , bar = ./bar.dhall
-- > }
--
-- > -- ./bar.dhall
-- > [3.0, 4.0, 5.0]
--
-- ... which is equivalent to our original configuration:
--
-- > -- ./config.dhall
-- > { foo = 1
-- > , bar = [3.0, 4.0, 5.0]
-- > }
--
-- However, the Dhall language will forbid cycles in these file references. For
-- example, if we create the following cycle:
--
-- > $ echo './file1' > file2
-- > $ echo './file2' > file1
--
-- ... then the interpreter will reject the import:
--
-- >>> input auto "./file1" :: IO Natural
-- *** Exception:
-- ↳ ./file1
-- ↳ ./file2
-- ...
-- Cyclic import: ./file1
-- ...
--
-- You can also import expressions by URL. For example, you can find a Dhall
-- expression hosted at this GitHub URL:
--
-- <https://raw.githubusercontent.com/dhall-lang/dhall-haskell/18e4e9a18dc53271146df3ccf5b4177c3552236b/examples/True>
--
-- > $ curl https://raw.githubusercontent.com/dhall-lang/dhall-haskell/18e4e9a18dc53271146df3ccf5b4177c3552236b/examples/True
-- > True
--
-- ... and you can reference that expression either directly:
--
-- >>> input auto "https://raw.githubusercontent.com/dhall-lang/dhall-haskell/18e4e9a18dc53271146df3ccf5b4177c3552236b/examples/True" :: IO Bool
-- True
--
-- ... or inside of a larger expression:
--
-- >>> input auto "False == https://raw.githubusercontent.com/dhall-lang/dhall-haskell/18e4e9a18dc53271146df3ccf5b4177c3552236b/examples/True" :: IO Bool
-- False
--
-- You're not limited to hosting Dhall expressions on GitHub. You can host a
-- Dhall expression anywhere that you can host UTF8-encoded text on the web,
-- such as a pastebin, or your own web server.
--
-- You can also import Dhall expressions from environment variables, too:
--
-- >>> System.Environment.setEnv "FOO" "1"
-- >>> input auto "env:FOO" :: IO Natural
-- 1
--
-- You can import types, too. For example, we can change our @./bar@ file to:
--
-- > -- ./bar.dhall
-- > [3.0, 4.0, 5.0] : List ./type.dhall
--
-- ... then specify the type in a separate file:
--
-- > -- ./type.dhall
-- > Double
--
-- ... and everything still type checks:
--
-- > $ ./example
-- > Example {foo = 1, bar = [3.0,4.0,5.0]}
--
-- Note that imports should be terminated by whitespace or parentheses otherwise
-- you will get either an import error or a parse error, like this:
--
-- >>> writeFile "baz" "2.0"
-- >>> input auto "./baz: Double" :: IO Double
-- *** Exception:
-- ↳ ./baz:
-- ...
-- ...Error...: Missing file ...baz:
-- ...
--
-- This is because the parser thinks that @./baz:@ is a single token due to
-- the missing whitespace before the colon and tries to import a file named
-- @./baz:@, which does not exist. To fix the problem we have to add a space
-- after @./baz@:
--
-- >>> input auto "./baz : Double" :: IO Double
-- 2.0
--
-- __Exercise:__ There is a @not@ function hosted online here:
--
-- <https://prelude.dhall-lang.org/Bool/not>
--
-- Visit that link and read the documentation. Then try to guess what this
-- code returns:
--
-- > >>> input auto "https://prelude.dhall-lang.org/Bool/not https://raw.githubusercontent.com/dhall-lang/dhall-haskell/18e4e9a18dc53271146df3ccf5b4177c3552236b/examples/True" :: IO Bool
-- > ???
--
-- Run the code to test your guess
-- $lists
--
-- You can store 0 or more values of the same type in a list, like this:
--
-- > [1, 2, 3]
--
-- Every list can be followed by the type of the list. The type annotation is
-- required for empty lists but optional for non-empty lists. You will get a
-- parse error if you provide an empty list without a type annotation:
--
-- >>> input auto "[]" :: IO (Vector Natural)
-- *** Exception:
-- ...Error...: Invalid input
-- ...
-- (input):1:3:
-- |
-- 1 | []
-- | ^
-- Empty list literal without annotation
-- ...
--
-- Also, list elements must all have the same type. You will get an error if
-- you try to store elements of different types in a list:
--
-- >>> input auto "[1, True, 3]" :: IO (Vector Natural)
-- *** Exception:
-- ...Error...: List elements should all have the same type
-- ...
-- - Natural
-- + Bool
-- ...
-- 1│ True
-- ...
-- (input):1:5
-- ...
--
-- __Exercise:__ Replace the @\"???\"@ with an expression that successfully
-- decodes to the specified type:
--
-- > >>> input auto "???" :: IO (Vector (Vector Natural))
-- $optional0
--
-- @Optional@ values are either of the form @Some value@ or @None type@.
--
-- For example, these are valid @Optional@ values:
--
-- > Some 1
-- >
-- > None Natural
--
-- ... which both have type @Optional Natural@
--
-- An @Optional@ corresponds to Haskell's `Maybe` type for decoding purposes:
--
-- >>> input auto "Some 1" :: IO (Maybe Natural)
-- Just 1
-- >>> input auto "None Natural" :: IO (Maybe Natural)
-- Nothing
--
-- __Exercise:__ Replace the @\"???\"@ with an expression that successfully
-- decodes to the specified type:
--
-- > >>> input auto "???" :: IO (Maybe (Maybe (Maybe Natural)))
-- $records
--
-- Record literals are delimited by curly braces and their fields are separated
-- by commas. For example, this is a valid record literal:
--
-- > { foo = True
-- > , bar = 2
-- > , baz = 4.2
-- > }
--
-- A record type is like a record literal except instead of specifying each
-- field's value we specify each field's type. For example, the preceding
-- record literal has the following record type:
--
-- > { foo : Bool
-- > , bar : Natural
-- > , baz : Double
-- > }
--
-- If you want to specify an empty record literal, you must use @{=}@, which is
-- special syntax reserved for empty records. If you want to specify the empty
-- record type, then you use @{}@. If you forget which is which you can always
-- ask the @dhall@ compiler to remind you of the type for each one:
--
-- > $ dhall type <<< '{=}'
-- > {}
--
-- > $ dhall type <<< '{}'
-- > Type
--
-- This is our first use of the @dhall@ command-line tool (provided by this
-- package), which provides a @type@ subcommand for inferring an expression's
-- type. By default the tool reads the expression on standard input and outputs
-- the type to standard output.
--
-- Note that @<<<@ is a feature specific to the Bash shell to feed a string to
-- a command's standard input. If you are using another shell, then you can
-- instead do this:
--
-- > $ echo '{=}' | dhall type
-- > {}
--
-- __Exercise__: Use the @dhall type@ command to infer the type of this record:
--
-- > -- ./nested.dhall
-- > { foo = 1
-- > , bar =
-- > { baz = 2.0
-- > , qux = True
-- > }
-- > }
--
-- You can specify nested fields using dot-separated keys, like this:
--
-- > { foo = 1, bar.baz = 2.0, bar.qux = True }
--
-- ... which is equivalent to:
--
-- > { foo = 1, bar = { baz = 2.0, qux = True } }
--
-- You can also access a field of a record using the following syntax:
--
-- > record.fieldName
--
-- ... which means to access the value of the field named @fieldName@ from the
-- @record@. For example:
--
-- >>> input auto "{ foo = True, bar = 2, baz = 4.2 }.baz" :: IO Double
-- 4.2
--
-- ... and you can project out multiple fields into a new record using this
-- syntax:
--
-- > someRecord.{ field₀, field₁, … }
--
-- For example:
--
-- > $ dhall <<< '{ x = 1, y = True, z = "ABC" }.{ x, y }'
-- > { x = 1, y = True }
--
-- This is our first example of using the @dhall@ command-line tool with no
-- subcommand (like @type@), which evaluates the provided expression. By
-- default, this reads the expression on standard input and outputs the
-- evaluated result on standard output.
--
-- __Exercise__: Save the above @./nested.dhall@ file and then try to access the
-- value of the @baz@ field. Test that this works by interpreting your code
-- using the @dhall@ command.
--
-- You can convert a record to a list of key-value pairs (a.k.a. a \"Map\") by
-- using the @toMap@ keyword. For example:
--
-- > $ dhall <<< 'toMap { foo = 1, bar = 2 }'
-- > [ { mapKey = "bar", mapValue = 2 }, { mapKey = "foo", mapValue = 1 } ]
--
-- This conversion only works if all field of the record have the same type.
-- This comes in handy when you need to convert a Dhall record to the Dhall
-- equivalent of a homogeneous map (i.e. Haskell's @"Data.Map"@).
-- $functions
--
-- The Dhall programming language also supports user-defined anonymous
-- functions. For example, we can save the following anonymous function to a
-- file:
--
-- > -- ./makeBools.dhall
-- > \(n : Bool) ->
-- > [ n && True, n && False, n || True, n || False ]
--
-- ... or we can use Dhall's support for Unicode characters to use @λ@ (U+03BB)
-- instead of @\\@ and @→@ (U+2192) instead of @->@ (for people who are into
-- that sort of thing):
--
-- > $ -- ./makeBools.dhall
-- > λ(n : Bool) →
-- > [ n && True, n && False, n || True, n || False ]
-- > <Ctrl-D>
--
-- We'll be sticking to ASCII for the remainder of the tutorial, though, while
-- still pointing out Unicode equivalents as we go.
--
-- You can read this as a function of one argument named @n@ that has type
-- @Bool@. This function returns a @List@ of @Bool@s. Each element of the
-- @List@ depends on the input argument named @n@.
--
-- The (ASCII) syntax for anonymous functions resembles the syntax for anonymous
-- functions in Haskell. The only difference is that Dhall requires you to
-- annotate the type of the function's input.
--
-- You can import this function into Haskell, too:
--
-- >>> makeBools <- input auto "./makeBools.dhall" :: IO (Bool -> Vector Bool)
-- >>> makeBools True
-- [True,False,True,True]
--
-- The reason this works is that there is an `FromDhall` instance for simple
-- functions:
--
-- > instance (ToDhall a, FromDhall b) => FromDhall (a -> b)
--
-- Thanks to currying, this instance works for functions of multiple simple
-- arguments:
--
-- >>> dhallAnd <- input auto "\\(x : Bool) -> \\(y : Bool) -> x && y" :: IO (Bool -> Bool -> Bool)
-- >>> dhallAnd True False
-- False
--
-- However, you can't convert anything more complex than that (like a polymorphic
-- or higher-order function). You will need to apply those functions to their
-- arguments within Dhall before converting their result to a Haskell value.
--
-- Just like `FromDhall`, you can derive `ToDhall` for user-defined data types:
--
-- > {-# LANGUAGE DeriveAnyClass #-}
-- > {-# LANGUAGE DeriveGeneric #-}
-- > {-# LANGUAGE OverloadedStrings #-}
-- >
-- > module Main where
-- >
-- > import Dhall
-- >
-- > data Example0 = Example0 { foo :: Bool, bar :: Bool }
-- > deriving (Generic, ToDhall)
-- >
-- > main = do
-- > f <- input auto "\\(r : { foo : Bool, bar : Bool }) -> r.foo && r.bar"
-- > print (f (Example0 { foo = True, bar = False }) :: Bool)
-- > print (f (Example0 { foo = True, bar = True }) :: Bool)
--
-- The above program prints:
--
-- > False
-- > True
-- $compiler
--
-- We can also test our @makeBools@ function directly from the command line.
-- This library comes with a command-line executable program named @dhall@ that
-- you can use to both type-check files and convert them to a normal form. Our
-- compiler takes a program on standard input and then prints the program's type
-- to standard error followed by the program's normal form to standard output:
--
-- > $ dhall --annotate <<< './makeBools.dhall'
-- > (λ(n : Bool) → [ n, False, True, n ]) : ∀(n : Bool) → List Bool
--
-- The @dhall@ command emits Unicode output by default, but you can switch to
-- ASCII output using the @--ascii@ flag:
--
-- > $ dhall --annotate --ascii <<< './makeBools.dhall'
-- > (\(n : Bool) -> [ n, False, True, n ]) : forall (n : Bool) -> List Bool
--
-- The @--annotate@ flag adds a type signature to the output to let us know
-- what type the interpreter inferred for our expression. The type signature
-- is @∀(n : Bool) → List Bool@ which says that @makeBools@ is a function of one
-- argument named @n@ that has type @Bool@ and the function returns a @List@ of
-- @Bool@s. The @∀@ (U+2200) symbol is shorthand for the ASCII @forall@
-- keyword:
--
-- > ∀(x : a) → b -- This type ...
-- >
-- > forall (x : a) -> b -- ... is the same as this type
--
-- ... and Dhall's @forall@ keyword behaves the same way as Haskell's @forall@
-- keyword for input values that are @Type@s:
--
-- > forall (x : Type) -> b -- This Dhall type ...
--
-- > forall x . b -- ... is the same as this Haskell type
--
-- The part where Dhall differs from Haskell is that you can also use
-- @∀@/@forall@ to give names to non-@Type@ arguments (such as the first
-- argument to @makeBools@).
--
-- This expression is our program's normal form:
--
-- > λ(n : Bool) → [ n, False, True, n ]
--
-- ... and the interpreter was able to simplify our expression by noting that:
--
-- * @n && True = n@
-- * @n && False = False@
-- * @n || True = True@
-- * @n || False = n@
--
-- To apply a function to an argument you separate the function and argument by
-- whitespace (just like Haskell):
--
-- @f x@
--
-- You can read the above as \"apply the function @f@ to the argument @x@\". This
-- means that we can \"apply\" our @./makeBools@ function to a @Bool@ argument
-- like this:
--
-- > $ dhall <<< './makeBools.dhall True'
-- > [True, False, True, True]
--
-- Remember that file paths are synonymous with their contents, so the above
-- code is exactly equivalent to:
--
-- > $ dhall <<< '(\(n : Bool) -> [n && True, n && False, n || True, n || False]) True'
-- > [True, False, True, True]
--
-- When you apply an anonymous function to an argument, you substitute the
-- \"bound variable" with the function's argument:
--
-- > Bound variable
-- > ⇩
-- > (\(n : Bool) -> ...) True
-- > ⇧
-- > Function argument
--
-- So in our above example, we would replace all occurrences of @n@ with @True@,
-- like this:
--
-- > -- If we replace all of these `n`s with `True` ...
-- > [n && True, n && False, n || True, n || False]
-- >
-- > -- ... then we get this:
-- > [True && True, True && False, True || True, True || False]
-- >
-- > -- ... which reduces to the following normal form:
-- > [True, False, True, True]
--
-- Now that we've verified that our function type checks and works, we can use
-- the same function within Haskell:
--
-- >>> input auto "./makeBools.dhall True" :: IO (Vector Bool)
-- [True,False,True,True]
--
-- __Exercise__: Create a file named @getFoo@ that is a function of the following
-- type:
--
-- > forall (r : { foo : Bool, bar : Text }) -> Bool
--
-- This function should take a single input argument named @r@ that is a record
-- with two fields. The function should return the value of the @foo@ field.
--
-- __Exercise__: Use the @dhall@ command to infer the type of the function you
-- just created and verify that your function has the correct type
--
-- __Exercise__: Use the @dhall@ command to apply your function to a sample
-- record
-- $strings
-- Dhall supports ordinary string literals with Haskell-style escaping rules:
--
-- > $ dhall
-- > "Hello, \"world\"!"
-- > <Ctrl-D>
-- > "Hello, \"world\"!"
--
-- ... and Dhall also supports Nix-style multi-line string literals:
--
-- > $ dhall
-- > ''
-- > #!/bin/bash
-- >
-- > echo "Hi!"
-- > ''
-- > <Ctrl-D>
-- > "\n#!/bin/bash\n\necho \"Hi!\"\n"
--
-- These \"double single quote strings\" ignore all special characters, with one
-- exception: if you want to include a @''@ in the string, you will need to
-- escape it with a preceding @'@ (i.e. use @'''@ to insert @''@ into the final
-- string).
--
-- These strings also strip leading whitespace using the same rules as Nix.
-- Specifically: \"it strips from each line a number of spaces equal to the
-- minimal indentation of the string as a whole (disregarding the indentation
-- of empty lines).\"
--
-- You can also interpolate expressions into strings using @${...}@ syntax. For
-- example:
--
-- > $ dhall
-- > let name = "John Doe"
-- > let age = 21
-- > in "My name is ${name} and my age is ${Natural/show age}"
-- > <Ctrl-D>
-- > "My name is John Doe and my age is 21"
--
-- Note that you can only interpolate expressions of type @Text@
--
-- If you need to insert a @"${"@ into a string without interpolation then use
-- @"''${"@ (same as Nix)
--
-- > ''
-- > for file in *; do
-- > echo "Found ''${file}"
-- > done
-- > ''
-- $combine
--
-- You can combine two records, using either the @(//)@ operator or the
-- @(/\\)@ operator.
--
-- The @(//)@ operator (or @(⫽)@ U+2AFD) combines the fields of both records,
-- preferring fields from the right record if they share fields in common:
--
-- > $ dhall
-- > { foo = 1, bar = "ABC" } // { baz = True }
-- > <Ctrl-D>
-- > { bar = "ABC", baz = True, foo = 1 }
-- > $ dhall
-- > { foo = 1, bar = "ABC" } ⫽ { bar = True } -- Fancy unicode
-- > <Ctrl-D>
-- > { bar = True, foo = 1 }
--
-- Note that the order of record fields does not matter. The compiler
-- automatically sorts the fields.
--
-- If you need to set or add a deeply nested field you can use the @with@
-- keyword, like this:
--
-- > $ dhall <<< '{ x.y = 1 } with x.z = True'
-- > { x = { y = 1, z = True } }
--
-- > $ dhall <<< '{ x.y = 1 } with x.y = 2'
-- > { x.y = 2 }
--
-- The @with@ keyword is syntactic sugar for the @//@ operator which follows
-- these rules:
--
-- > -- Nested case
-- > record with k.ks… = value ⇒ record // { k = record.k with ks… = value }
-- >
-- > -- Base case
-- > record with k = value ⇒ record // { k = value }
--
-- The @(/\\)@ operator (or @(∧)@ U+2227) also lets you combine records, but
-- behaves differently if the records share fields in common. The operator
-- combines shared fields recursively if they are both records:
--
-- > $ dhall
-- > { foo = { bar = True }, baz = "ABC" } /\ { foo = { qux = 1.0 } }
-- > <Ctrl-D>
-- > { baz = "ABC", foo = { bar = True, qux = 1.0 } }
--
-- ... but fails with a type error if either shared field is not a record:
--
-- > $ dhall
-- > { foo = 1, bar = "ABC" } /\ { foo = True }
-- > <Ctrl-D>
-- > Use "dhall --explain" for detailed errors
-- >
-- > Error: Field collision
-- >
-- > { foo = 1, bar = "ABC" } ∧ { foo = True }
-- >
-- > (input):1:1
--
-- __Exercise__: Combine any record with the empty record. What do you expect
-- to happen?
--
-- You can analogously combine record types using the @//\\\\@ operator (or @(⩓)@ U+2A53):
--
-- > $ dhall
-- > { foo : Natural } //\\ { bar : Text }
-- > <Ctrl-D>
-- > { foo : Natural, bar : Text }
--
-- ... which behaves the exact same, except at the type level, meaning that the
-- operator descends recursively into record types:
--
-- > $ dhall
-- > { foo : { bar : Text } } //\\ { foo : { baz : Bool }, qux : Natural }
-- > <Ctrl-D>
-- > { foo : { bar : Text, baz : Bool }, qux : Natural }
-- $let
--
-- Dhall also supports @let@ expressions, which you can use to define
-- intermediate values in the course of a computation, like this:
--
-- > $ dhall
-- > let x = "ha" in x ++ x
-- > <Ctrl-D>
-- > "haha"
--
-- You can also annotate the types of values defined within a @let@ expression,
-- like this:
--
-- > $ dhall
-- > let x : Text = "ha" in x ++ x
-- > <Ctrl-D>
-- > "haha"
--
-- You need to nest @let@ expressions if you want to define more than one value
-- in this way:
--
-- > $ dhall
-- > let x = "Hello, "
-- > let y = "world!"
-- > in x ++ y
-- > <Ctrl-D>
-- > "Hello, world!"
--
-- Dhall is whitespace-insensitive, so feel free to format things over multiple
-- lines or indent in any way that you please.
--
-- If you want to define a named function, just give a name to an anonymous
-- function:
--
-- > $ dhall
-- > let twice = \(x : Text) -> x ++ x in twice "ha"
-- > <Ctrl-D>
-- > "haha"
--
-- Unlike Haskell, Dhall does not support function arguments on the left-hand
-- side of the equals sign, so this will not work:
--
-- > $ dhall
-- > let twice (x : Text) = x ++ x in twice "ha"
-- > <Ctrl-D>
-- > Error: Invalid input
-- >
-- > (input):1:11:
-- > |
-- > 1 | let twice (x : Text) = x ++ x in twice "ha"
-- > | ^
-- > unexpected '('
-- > expecting ':', '=', or the rest of label
--
-- The error message says that Dhall expected either a @(:)@ (i.e. the beginning
-- of a type annotation) or a @(=)@ (the beginning of the assignment) and not a
-- function argument.
--
-- You can also use @let@ expressions to rename imports, like this:
--
-- > $ dhall
-- > let not = https://prelude.dhall-lang.org/Bool/not
-- > in not True
-- > <Ctrl-D>
-- > False
--
-- ... or to define synonyms for types:
--
-- > $ dhall <<< 'let Name : Type = Text in [ "John", "Mary" ] : List Name'
-- > List Text
-- >
-- > [ "John", "Mary" ]
--
-- __Exercise:__ What do you think the following code will normalize to?
--
-- > let x = 1
-- > let x = 2
-- > in x
--
-- Test your guess using the @dhall@ compiler
--
-- __Exercise:__ Now try to guess what this code will normalize to:
--