Skip to content

Commit

Permalink
singleton types
Browse files Browse the repository at this point in the history
  • Loading branch information
winitzki committed Nov 2, 2024
1 parent 01615ce commit 848cdb7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions tutorial/programming_dhall.md
Original file line number Diff line number Diff line change
Expand Up @@ -5254,7 +5254,7 @@ This code defines a type `Text_abc` and a value `x` of that type:
```dhall
let Text_equals_abc = λ(text : Text) (text === "abc")
let Text_abc : Type = DependentPair Text Text_equals_abc
let x : Text_abc = makeDependentPair Text "abc" Text_equals_abc (assert : "abc === "abc")
let x : Text_abc = makeDependentPair Text "abc" Text_equals_abc (assert : "abc" === "abc")
```
We can then extract the `Text`-valued part of `x` and verify that it is equal to the string `"abc"`:

Expand All @@ -5264,15 +5264,15 @@ let _ = assert : dependentPairFirstValue Text Text_equals_abc x === "abc"

We can generalize this code to define a (dependent) type constructor for singleton types that are limited to a given `Text` value:
```dhall
let TextSigletonPredicate = λ(fixed : Text) → λ(text : Text) → (text === fixed)
let TextSingletonPredicate = λ(fixed : Text) λ(text : Text) (text === fixed)
let TextSingleton : Text Type
= λ(fixed : Text) → DependentPair Text (TextSigletonPredicate fixed)
= λ(fixed : Text) DependentPair Text (TextSingletonPredicate fixed)
let makeTextSingleton : (fixed : Text) TextSingleton fixed
= λ(fixed : Text) → makeDependentPair Text fixed (TestSingletonPredicate fixed) (assert : fixed === fixed)
= λ(fixed : Text) makeDependentPair Text fixed (TextSingletonPredicate fixed) (assert : fixed === fixed)
let x : TextSingleton "abc" = makeTextSingleton "abc"
-- let x : TextSingleton "abc" = makeTextSingleton "def" -- This will fail!
let x : TextSingleton "abc" = makeDependentPair Text "abc" (TextSigletonPredicate "abc") (assert : "abc" === "abc")
let _ = assert : dependentPairFirstValue Text (TextSigletonPredicate "abc") x === "abc"
let x : TextSingleton "abc" = makeDependentPair Text "abc" (TextSingletonPredicate "abc") (assert : "abc" === "abc")
let _ = assert : dependentPairFirstValue Text (TextSingletonPredicate "abc") x === "abc"
```

The repetition in this code can be reduced by using Dhall's built-in function `Text/replace`. -- Is this necessary?
Expand Down

0 comments on commit 848cdb7

Please sign in to comment.