summaryrefslogtreecommitdiffhomepage
path: root/dhall.html.markdown
blob: 704a94eea3256e991765c658ad721ad3d7095840 (plain)
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
---
language: Dhall
filename: learndhall.dhall
contributors:
    - ["Gabriel Gonzalez", "http://www.haskellforall.com/"]
---

Dhall is a programmable configuration language that provides a non-repetitive
alternative to YAML.

You can think of Dhall as: JSON + functions + types + imports

Note that while Dhall is programmable, Dhall is not Turing-complete.  Many
of Dhall's features take advantage of this restriction to provide stronger
safety guarantees and more powerful tooling.

```haskell
-- Single-line comment

{- Multi-line comment

   Unicode is fine 🙂

   This file is a valid Dhall expression that evaluates to a large record
   collecting the results of each step.

   You can view the results by interpreting the file:

       $ dhall --file learndhall.dhall

   {- Comments can be nested -}
-}

let greeting = "Hello, world!"

let fruits = "🍋🍓🍍🍉🍌"

let interpolation = "Enjoy some delicious fruit: ${fruits}"

let multilineText {- Inline comments work, too -} =
        ''
        Leading whitespace is stripped from multi-line text literals.

        That means you can freely indent or dedent a text literal without
        changing the result.

            Relative indentation within the literal is still preserved.

        Other than that, the text literal is preserved verbatim, similar to a
        "literal" YAML multiline string.
        ''

let bool = True

-- Type annotations on bindings are optional, but helpful, so we'll use them
let annotation : Bool = True

let renderedBool : Text = if bool then "True" else "False"

-- Natural numbers are non-negative and are unsigned
let naturalNumber : Natural = 42

-- Integers may be negative, but require an explicit sign, even if positive
let positiveInteger : Integer = +1

let negativeInteger : Integer = -12

let pi : Double = 3.14159265359

{- You can use a wider character range for identifiers (such as quotation
   marks and whitespace) if you quote them using backticks
-}
let `Avogadro's Number` : Double = 6.0221409e+23

let origin : { x : Double, y : Double } = { x = 0.0, y = 0.0 }

let somePrimes : List Natural = [ 2, 3, 5, 7, 11 ]

{- A schema is the same thing as a type

   Types begin with an uppercase letter by convention, but this convention is
   not enforced
-}
let Profile : Type
        = { person :
              { name : Text
              , age  : Natural
              }
          , address :
              { country : Text
              , state   : Text
              , city    : Text
              }
          }

let john : Profile =
        { person =
            { name = "John Doe"
            , age  = 67
            }
        , address =
            { country = "United States"
            , state   = "Pennsylvania"
            , city    = "Philadelphia"
            }
        }

let philadelphia : Text = john.address.city

{- Enum alternatives also begin with an uppercase letter by convention.  This
   convention is not enforced
-}
let DNA : Type = < Adenine | Cytosine | Guanine | Thymine >

let dnaSequence : List DNA = [ DNA.Thymine, DNA.Guanine, DNA.Guanine ]

let compactDNASequence : List DNA =
        let a = DNA.Adenine
        let c = DNA.Cytosine
        let g = DNA.Guanine
        let t = DNA.Thymine
        in  [ c, t, t, a, t, c, g, g, c ]

-- You can transform enums by providing a record with one field per alternative
let theLetterG : Text =
            merge
            { Adenine  = "A"
            , Cytosine = "C"
            , Guanine  = "G"
            , Thymine  = "T"
            }
            DNA.Guanine

let presentOptionalValue : Optional Natural = Some 1

let absentOptionalValue : Optional Natural = None Natural

let points : List { x : Double, y : Double } =
        [ { x = 1.1, y = -4.2 }
        , { x = 4.4, y = -3.0 }
        , { x = 8.2, y = -5.5 }
        ]

{- `Natural -> List Natural` is the type of a function whose input type is a
   `Natural` and whose output type is a `List Natural`

   All functions in Dhall are anonymous functions (a.k.a. "lambdas"),
   which you can optionally give a name

   For example, the following function is equivalent to this Python code:

       lambda n : [ n, n + 1 ]

   ... and this JavaScript code:

       function (n) { return [ n, n + 1 ]; }
-}
let exampleFunction : Natural -> List Natural =
        \(n : Natural) -> [ n, n + 1 ]

-- Dhall also supports Unicode syntax, but this tutorial will stick to ASCII
let unicodeFunction : Natural → List Natural =
        λ(n : Natural) → [ n, n + 1 ]

-- You don't need to parenthesize function arguments
let exampleFunctionApplication : List Natural =
        exampleFunction 2

let functionOfMultipleArguments : Natural -> Natural -> List Natural =
        \(x : Natural) -> \(y : Natural) -> [ x, y ]

let functionAppliedToMultipleArguments : List Natural =
        functionOfMultipleArguments 2 3

{- Same as `exampleFunction` except we gave the function's input type a
   name: "n"
-}
let namedArgumentType : forall (n : Natural) -> List Natural =
        \(n : Natural) -> [ n, n + 1 ]

{- If you name a function's input type, you can use that name later within the
   same type

   This lets you write a function that works for more than one type of input
   (a.k.a. a "polymorphic" function)
-}
let duplicate : forall (a : Type) -> a -> List a =
        \(a : Type) -> \(x : a) -> [ x, x ] 

let duplicatedNumber : List Natural =
        duplicate Natural 2

let duplicatedBool : List Bool =
        duplicate Bool False

{- The language also has some built-in polymorphic functions, such as:

       List/head : forall (a : Type) -> List a -> Optional a
-}
let firstPrime : Optional Natural = List/head Natural somePrimes

let functionOfARecord : { x : Natural, y : Natural } -> List Natural =
        \(args : { x : Natural, y : Natural }) -> [ args.x, args.y ]

let functionAppliedToARecord : List Natural =
        functionOfARecord { x = 2, y = 5 }

{- All type conversions are explicit

   `Natural/show` is a built-in function of the following type:

       Natural/show : Natural -> Text

   ... that converts `Natural` numbers to their `Text` representation
-}
let typeConversion : Natural -> Text =
        \(age : Natural) -> "I am ${Natural/show age} years old!"

-- A "template" is the same thing as a function whose output type is `Text`
let mitLicense : { year : Natural, copyrightHolder : Text } -> Text =
        \(args : { year : Natural, copyrightHolder : Text }) ->
''
Copyright ${Natural/show args.year} ${args.copyrightHolder}

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do 
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.
''

-- Template instantiation is the same thing as function application
let templatedLicense : Text =
        mitLicense { year = 2019, copyrightHolder = "Jane Smith" }

{- You can import expressions by URL

   Also, like Bash, you can import code from your local filesystem (not shown)

   Security-conscious users can pin remotely-imported expressions by adding a
   semantic integrity check.  The interpreter rejects any attempt to tamper with
   an expression pinned in this way.  However, behavior-preserving refactors
   of imported content will not perturb the hash.

   Imported expressions pinned in this way are also locally cached in a
   content-addressable store (typically underneath `~/.cache/dhall`)
-}
let Natural/sum : List Natural -> Natural =
      https://prelude.dhall-lang.org/Natural/sum
      sha256:33f7f4c3aff62e5ecf4848f964363133452d420dcde045784518fb59fa970037

let twentyEight : Natural = Natural/sum somePrimes

-- A "package" is the same thing as a (possibly nested) record that you can import
let Prelude = https://prelude.dhall-lang.org/package.dhall

let false : Bool = Prelude.Bool.not True

-- You can import the raw contents of a file by adding `as Text` to an import
let sourceCode : Text = https://prelude.dhall-lang.org/Bool/not as Text

-- You can import environment variables, too:
let presentWorkingDirectory = env:PWD as Text

-- You can provide a fallback expression if an import fails
let home : Optional Text = Some env:HOME ? None Text

-- Fallback expressions can contain alternative imports of their own
let possiblyCustomPrelude =
        env:DHALL_PRELUDE
      ? https://prelude.dhall-lang.org/package.dhall

{- Tie everything together by auto-generating configurations for 10 build users
   using the `generate` function:

       Prelude.List.generate
           : Natural -> forall (a : Type) -> (Natural -> a) -> List a
-}
let buildUsers =
        let makeUser = \(user : Text) ->
              let home       = "/home/${user}"
              let privateKey = "${home}/.ssh/id_ed25519"
              let publicKey  = "${privateKey}.pub"
              in  { home = home
                  , privateKey = privateKey
                  , publicKey = publicKey
                  }

        let buildUser =
                \(index : Natural) -> makeUser "build${Natural/show index}"

        let Config =
              { home : Text
              , privateKey : Text
              , publicKey : Text
              }

        in  Prelude.List.generate 10 Config buildUser

-- Present all of the results in a final record
in  { greeting = greeting
    , fruits = fruits
    , interpolation = interpolation
    , multilineText = multilineText
    , bool = bool
    , annotation = annotation
    , renderedBool = renderedBool
    , naturalNumber = naturalNumber
    , positiveInteger = positiveInteger
    , negativeInteger = negativeInteger
    , pi = pi
    , `Avogadro's Number` = `Avogadro's Number`
    , origin = origin
    , somePrimes = somePrimes
    , john = john
    , philadelphia = philadelphia
    , dnaSequence = dnaSequence
    , compactDNASequence = compactDNASequence
    , theLetterG = theLetterG
    , presentOptionalValue = presentOptionalValue
    , absentOptionalValue = absentOptionalValue
    , points = points
    , exampleFunction = exampleFunction
    , unicodeFunction = unicodeFunction
    , exampleFunctionApplication = exampleFunctionApplication
    , functionOfMultipleArguments = functionOfMultipleArguments
    , functionAppliedToMultipleArguments = functionAppliedToMultipleArguments
    , namedArgumentType = namedArgumentType
    , duplicate = duplicate
    , duplicatedNumber = duplicatedNumber
    , duplicatedBool = duplicatedBool
    , firstPrime = firstPrime
    , functionOfARecord = functionOfARecord
    , functionAppliedToARecord = functionAppliedToARecord
    , typeConversion = typeConversion
    , mitLicense = mitLicense
    , templatedLicense = templatedLicense
    , twentyEight = twentyEight
    , false = false
    , sourceCode = sourceCode
    , presentWorkingDirectory = presentWorkingDirectory
    , home = home
    , buildUsers = buildUsers
    }
```

To learn more, visit the official website, which also lets you try the
language live in your browser:

* [https://dhall-lang.org](http://dhall-lang.org/)