Common — library colibrilib.flint — module CA

The exact real numbers of Flint (Flint.CA, with the context of colibri2). Trusted: each operation is exact.

Flint can fail to decide a comparison, even on algebraic numbers: then flint_ca.ml prints a message and exits, so that the comparisons, floor, ceil and truncate return only exact results.

Known false assumption, see agents/journal/soundness–review–2026–11–05.md (finding F1): the OCaml check_is_rational is for now a stand–in that never answers None, and answers Some false when to_q fails.

18
module CA
19
use int.Int
20
use real.RealInfix
21
use real.FromInt
22
use real.Abs
23
use real.Square
24
use real.Truncate
25
use mach.int.Int63
26
use option.Option
27
use colibrilib.zarith.Q as Q
28
use colibrilib.q.Real as Real
29
30
type
t
31
32
function
real
t : real
33
34
35
val function
of_q
(q:Q.t) : t
36
ensures { real result = Q.real q }

The rational value when Flint finds one (ca_get_fmpq). None proves nothing: Flint also fails on some rationals.

40
val function
to_q
(a:t) : option Q.t
41
ensures { match result with
42
| Some q -> Q.real q = real a
43
| None -> true
44
end }

Is the number rational (ca_check_is_rational)? None when Flint did not finish.

48
val function
check_is_rational
(a:t) : option bool
49
ensures { match result with
50
| Some b -> b <-> exists q:Q.t. Q.real q = real a
51
| None -> true
52
end }

Prints a message and stops the program: it never returns. For a case where Flint did not finish.

56
val
give_up
(_a:t) : 'a
57
returns { _ -> false }

There are irrational numbers (sqrt 2)

60
axiom
irrational
: exists c:t. forall q:Q.t. Q.real q <> real c

The comparisons answer -1, 0 or 1

63
val function
compare
(a b:t) : int63
64
ensures { result = if real a >. real b then 1 else if real a <. real b then -1 else 0 }
65
66
val function
compare_q
(a:t) (q:Q.t) : int63
67
ensures { result = if real a >. Q.real q then 1 else if real a <. Q.real q then -1 else 0 }
68
69
val function
sign
(a:t) : int63
70
ensures { result = if real a >. 0. then 1 else if real a <. 0. then -1 else 0 }
71
72
val function
floor
(a:t) : int
73
ensures { result = Truncate.floor (real a) }
74
75
val function
ceil
(a:t) : int
76
ensures { result = Truncate.ceil (real a) }
77
78
val function
truncate
(a:t) : int
79
ensures { result = Truncate.truncate (real a) }
80
81
val function
add
(a b:t) : t
82
ensures { real result = real a +. real b }
83
84
val function
sub
(a b:t) : t
85
ensures { real result = real a -. real b }
86
87
val function
mul
(a b:t) : t
88
ensures { real result = real a *. real b }
89
90
val function
div
(a b:t) : t
91
requires { real b <> 0. }
92
ensures { real result = real a /. real b }
93
94
val function
neg
(a:t) : t
95
ensures { real result = -. real a }
96
97
val function
inv
(a:t) : t
98
requires { real a <> 0. }
99
ensures { real result = 1. /. real a }
100
101
val function
abs
(a:t) : t
102
ensures { real result = Abs.abs (real a) }
103
104
val function
sqrt
(a:t) : t
105
requires { 0. <=. real a }
106
ensures { real result = Square.sqrt (real a) }
107
108
val function
div_e
(a b:t) : int
109
requires { real b <> 0. }
110
ensures { result = Real.div_e (real a) (real b) }
111
112
val function
div_t
(a b:t) : int
113
requires { real b <> 0. }
114
ensures { result = Real.div_t (real a) (real b) }
115
116
val function
div_f
(a b:t) : int
117
requires { real b <> 0. }
118
ensures { result = Real.div_f (real a) (real b) }
119
120
val function
mod_e
(a b:t) : t
121
requires { real b <> 0. }
122
ensures { real result = Real.mod_e (real a) (real b) }
123
124
val function
mod_t
(a b:t) : t
125
requires { real b <> 0. }
126
ensures { real result = Real.mod_t (real a) (real b) }
127
128
val function
mod_f
(a b:t) : t
129
requires { real b <> 0. }
130
ensures { real result = Real.mod_f (real a) (real b) }
131
132
use real.PowerInt as RP
133
134
val function
of_z
(z:int) : t
135
ensures { real result = from_int z }

a^n (Flint.CA.pow_int)

138
val function
pow_int
(a:t) (n:int63) : t
139
requires { 0 <= n \/ real a <> 0. }
140
ensures { real result = if 0 <= n then RP.power (real a) n
141
else 1. /. RP.power (real a) (- n) }

a^e for a rational exponent e = num/den (Flint.CA.pow, the non negative root): r^den = a^num

145
val function
pow
(a:t) (e:Q.t) : t
146
requires { 0. <. real a \/ (real a = 0. /\ 0 < e.Q.num) }
147
ensures { 0. <=. real result }
148
ensures { 0 <= e.Q.num ->
149
RP.power (real result) e.Q.den = RP.power (real a) e.Q.num }
150
ensures { e.Q.num < 0 ->
151
RP.power (real result) e.Q.den *. RP.power (real a) (- e.Q.num) = 1. }

The length of the printed number, a measure of its size (no specification)

155
val function
size
(a:t) : int63
156
end