Debian Codegraph @

doc/README/Text/Printf.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Examples of format strings and printf
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --safe --cubical-compatible #-}
8
9 module README.Text.Printf where
10
11 open import Data.Nat.Base
12 open import Data.Char.Base
13 open import Data.List.Base
14 open import Data.String.Base
15 open import Data.Sum.Base
16
17 open import Relation.Binary.PropositionalEquality
18
19 ------------------------------------------------------------------------
20 -- Format strings
21
22 open import Text.Format
23
24 -- We can specify a format by writing a string which will get interpreted
25 -- by a lexer into a list of formatting directives.
26
27 -- The specification types are always started with a '%' character:
28
29 -- Integers (%d or %i)
30 -- Naturals (%u)
31 -- Floats (%f)
32 -- Chars (%c)
33 -- Strings (%s)
34
35 -- Anything which is not a type specification is a raw string to be spliced
36 -- in the output of printf.
37
38 -- For instance the following format alternates types and raw strings
39
40 _ : lexer "%s: %u + %u ≡ %u"
41 ≡ inj₂ (`String ∷ Raw ": " ∷ `ℕ ∷ Raw " + " ∷ `ℕ ∷ Raw " ≡ " ∷ `ℕ ∷ [])
42 _ = refl
43
44 -- Lexing can fail. There are two possible errors:
45
46 -- If we start a specification type with a '%' but the string ends then
47 -- we get an UnexpectedEndOfString error
48
49 _ : lexer "%s: %u + %u ≡ %"
50 ≡ inj₁ (UnexpectedEndOfString "%s: %u + %u ≡ %")
51 _ = refl
52
53 -- If we start a specification type with a '%' and the following character
54 -- does not correspond to an existing type, we get an InvalidType error
55 -- together with a focus highlighting the position of the problematic type.
56
57 _ : lexer "%s: %u + %a ≡ %u"
58 ≡ inj₁ (InvalidType "%s: %u + %" 'a' " ≡ %u")
59 _ = refl
60
61 ------------------------------------------------------------------------
62 -- Printf
63
64 open import Text.Printf
65
66 -- printf is a function which takes a format string as an argument and
67 -- returns a function expecting a value for each type specification present
68 -- in the format and returns a string splicing in these values into the
69 -- format string.
70
71 -- For instance `printf "%s: %u + %u ≡ %u"` is a
72 -- `String → ℕ → ℕ → ℕ → String` function.
73
74 _ : String → ℕ → ℕ → ℕ → String
75 _ = printf "%s: %u + %u ≡ %u"
76
77 _ : printf "%s: %u + %u ≡ %u" "example" 3 2 5
78 ≡ "example: 3 + 2 ≡ 5"
79 _ = refl
80
81 -- If the format string str is invalid then `printf str` will have type
82 -- `Error e` where `e` is the lexing error.
83
84 _ : Text.Printf.Error (UnexpectedEndOfString "%s: %u + %u ≡ %")
85 _ = printf "%s: %u + %u ≡ %"
86
87 _ : Text.Printf.Error (InvalidType "%s: %u + %" 'a' " ≡ %u")
88 _ = printf "%s: %u + %a ≡ %u"
89
90 -- Trying to pass arguments to such an ̀Error` type will lead to a
91 -- unification error which hopefully makes the problem clear e.g.
92 -- `printf "%s: %u + %a ≡ %u" "example" 3 2 5` fails with the error:
93
94 -- Text.Printf.Error (InvalidType "%s: %u + %" 'a' " ≡ %u") should be
95 -- a function type, but it isn't
96 -- when checking that "example" 3 2 5 are valid arguments to a
97 -- function of type Text.Printf.Printf (lexer "%s: %u + %a ≡ %u")