-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathChar.lean
More file actions
184 lines (121 loc) · 4.07 KB
/
Copy pathChar.lean
File metadata and controls
184 lines (121 loc) · 4.07 KB
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
/-
Copyright (c) 2025 Lean FRO LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: David Thrane Christiansen
-/
import VersoManual
import Manual.Meta
import Manual.BasicTypes.Array.Subarray
import Manual.BasicTypes.Array.FFI
open Manual.FFIDocType
open Verso.Genre Manual
open Verso.Genre.Manual.InlineLean
set_option pp.rawOnError true
#doc (Manual) "Characters" =>
%%%
tag := "Char"
%%%
Characters are represented by the type {name}`Char`, which may be any Unicode [scalar value](http://www.unicode.org/glossary/#unicode_scalar_value).
While {ref "String"}[strings] are UTF-8-encoded arrays of bytes, characters are represented by full 32-bit values.
Lean provides special {ref "char-syntax"}[syntax] for character literals.
# Logical Model
%%%
tag := "char-model"
%%%
From the perspective of Lean's logic, characters consist of a 32-bit unsigned integer paired with a proof that it is a valid Unicode scalar value.
{docstring Char}
# Run-Time Representation
%%%
tag := "char-runtime"
%%%
As a {ref "inductive-types-trivial-wrappers"}[trivial wrapper], characters are represented identically to {lean}`UInt32`.
In particular, characters are represented as 32-bit immediate values in monomorphic contexts.
In other words, a field of a constructor or structure of type {lean}`Char` does not require indirection to access.
In polymorphic contexts, characters are {tech}[boxed].
# Syntax
%%%
tag := "char-syntax"
%%%
Character literals consist of a single character or an escape sequence enclosed in single quotes (`'`, Unicode `'APOSTROPHE' (U+0027)`).
Between these single quotes, the character literal may contain any character other than `'`, including newlines, which are included literally (with the caveat that all newlines in a Lean source file are interpreted as `'\n'`, regardless of file encoding and platform).
Special characters may be escaped with a backslash, so `'\''` is a character literal that contains a single quote.
The following forms of escape sequences are accepted:
: `\r`, `\n`, `\t`, `\\`, `\"`, `\'`
These escape sequences have the usual meaning, mapping to `CR`, `LF`, tab, backslash, double quote, and single quote, respectively.
: `\xNN`
When `NN` is a sequence of two hexadecimal digits, this escape denotes the character whose Unicode code point is indicated by the two-digit hexadecimal code.
: `\uNNNN`
When `NN` is a sequence of two hexadecimal digits, this escape denotes the character whose Unicode code point is indicated by the four-digit hexadecimal code.
# API Reference
%%%
tag := "char-api"
%%%
## Conversions
{docstring Char.ofNat}
{docstring Char.toNat}
{docstring Char.isValidCharNat}
{docstring Char.ofUInt8}
{docstring Char.toUInt8}
There are two ways to convert a character to a string.
{name}`Char.toString` converts a character to a singleton string that consists of only that character, while {name}`Char.quote` converts the character to a string representation of the corresponding character literal.
{docstring Char.toString}
{docstring Char.quote}
:::example "From Characters to Strings"
{name}`Char.toString` produces a string that contains only the character in question:
```lean (name := e)
#eval 'e'.toString
```
```leanOutput e
"e"
```
```lean (name := e')
#eval '\x65'.toString
```
```leanOutput e'
"e"
```
```lean (name := n')
#eval '"'.toString
```
```leanOutput n'
"\""
```
{name}`Char.quote` produces a string that contains a character literal, suitably escaped:
```lean (name := eq)
#eval 'e'.quote
```
```leanOutput eq
"'e'"
```
```lean (name := eq')
#eval '\x65'.quote
```
```leanOutput eq'
"'e'"
```
```lean (name := nq')
#eval '"'.quote
```
```leanOutput nq'
"'\\\"'"
```
:::
## Character Classes
%%%
tag := "char-api-classes"
%%%
{docstring Char.isAlpha}
{docstring Char.isAlphanum}
{docstring Char.isDigit}
{docstring Char.isLower}
{docstring Char.isUpper}
{docstring Char.isWhitespace}
## Case Conversion
{docstring Char.toUpper}
{docstring Char.toLower}
## Comparisons
{docstring Char.le}
{docstring Char.lt}
## Unicode
{docstring Char.utf8Size}
{docstring Char.utf16Size}