Common — library colibrilib.zarith — module Q

The rationals Q: fractions in lowest terms

72
module Q
73
use int.Int
74
use number.Gcd
75
use real.RealInfix
76
use real.FromInt
77
use real.Abs
78
use mach.int.Int63
79
Valid (377ms) (colibri2)
80
type
t
= private {
num
: int;
den
: int }
81
invariant { 0 < den /\ gcd num den = 1 }
82
83
function
real
(q:t) : real = from_int q.num /. from_int q.den
84
85
meta coercion function real

Equal values have the same fraction in lowest terms (proved in colibrilib/q_model.mlw)

89
axiom
equal_is_eq
: forall a b:t. real a = real b -> a = b
90
91
val
make
(n d:int) : t
92
requires { d <> 0 }
93
ensures { real result = from_int n /. from_int d }

{ num = n; den = d }, without normalization

96
val
make_exact
(n d:int) : t
97
requires { 0 < d /\ gcd n d = 1 }
98
ensures { result.num = n /\ result.den = d }
99
100
val function
of_bigint
(n:int) : t
101
ensures { result.num = n /\ result.den = 1 }
102
103
val
of_int
(n:int63) : t
104
ensures { result.num = n /\ result.den = 1 }
105
106
val
equal
(a b:t) : bool
107
ensures { result <-> real a = real b }
108
109
val
leq
(a b:t) : bool
110
ensures { result <-> real a <=. real b }
111
112
val
lt
(a b:t) : bool
113
ensures { result <-> real a <. real b }
114
115
val function
sign
(a:t) : int63
116
ensures { result = if real a >. 0. then 1 else if real a <. 0. then -1 else 0 }

The sign of a - b (Q.compare)

119
val
compare
(a b:t) : int63
120
ensures { result > 0 <-> real a >. real b }
121
ensures { result < 0 <-> real a <. real b }
122
123
val function
add
(a b:t) : t
124
ensures { real result = real a +. real b }
125
126
val function
sub
(a b:t) : t
127
ensures { real result = real a -. real b }
128
129
val function
mul
(a b:t) : t
130
ensures { real result = real a *. real b }
131
132
val function
div
(a b:t) : t
133
requires { real b <> 0. }
134
ensures { real result = real a /. real b }
135
136
val
neg
(a:t) : t
137
ensures { real result = -. real a }
138
139
val
abs
(a:t) : t
140
ensures { real result = abs (real a) }
141
end