/usr/lib/ats-anairiats-0.2.11/libats/DATS/intinf.dats is in ats-lang-anairiats 0.2.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | (***********************************************************************)
(* *)
(* Applied Type System *)
(* *)
(* Hongwei Xi *)
(* *)
(***********************************************************************)
(*
** ATS - Unleashing the Potential of Types!
** Copyright (C) 2002-2008 Hongwei Xi, Boston University
** All rights reserved
**
** ATS is free software; you can redistribute it and/or modify it under
** the terms of the GNU General Public License as published by the Free
** Software Foundation; either version 2.1, or (at your option) any later
** version.
**
** ATS is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
** for more details.
**
** You should have received a copy of the GNU General Public License
** along with ATS; see the file COPYING. If not, write to the Free
** Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*)
(* ****** ****** *)
(* author: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *)
(* ****** ****** *)
// infinite precision integers based on the gmp package
(* ****** ****** *)
%{^
#include "libc/CATS/gmp.cats"
%} // end of [%{^]
(* ****** ****** *)
staload "libc/SATS/gmp.sats"
staload "libats/SATS/intinf.sats"
(* ****** ****** *)
assume intinf (i: int) = // HX: [i] is a fake
[l:addr] (free_gc_v (mpz_vt?, l), mpz_vt @ l | ptr l)
// end of [intinf]
(* ****** ****** *)
implement
intinf_make_int {i} (i) = let
val (pf_gc, pf_at | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init_set_int (!p, i)
in
@(pf_gc, pf_at | p)
end // end of [intinf_make_int]
implement
intinf_make_lint (i) = let
val @(pf_gc, pf_at | p) =
ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val i = lint_of_lint1 (i) where {
extern castfn lint_of_lint1 {i:int} (x: lint i):<> lint
} // end of [val]
val () = mpz_init_set_lint (!p, i)
in
@(pf_gc, pf_at | p)
end // end of [intinf_make_lint]
implement
intinf_make_llint (i) = let
val @(pf_gc, pf_at | p) =
ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val i = llint_of_llint1 (i) where {
extern castfn llint_of_llint1 {i:int} (x: llint i):<> llint
} // end of [val]
val i = double_of_llint (i)
val () = mpz_init_set_double (!p, i)
in
@(pf_gc, pf_at | p)
end // end of [intinf_make_llint]
implement
intinf_make_double (d) = let
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init_set_double (!p, d)
in
#[0 | (pfgc, pfat | p)] // HX: [0] is just a place holder
end // end of [intinf_make_double]
implement
intinf_make_string (rep) = let
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init_set_str_exn (!p, rep, 10(*base*))
in
#[0 | (pfgc, pfat | p)] // HX: [0] is just a place holder
end // end of [intinf_make_double]
(* ****** ****** *)
implement
intinf_free @(pf_gc, pf_at | p) =
(mpz_clear (!p); ptr_free {mpz_vt?} (pf_gc, pf_at | p))
// end of [intinfptr_free]
(* ****** ****** *)
implement
fprint0_intinf
(out, intinf) = fprint0_intinf_base (out, 10, intinf)
// end of [fprint0_intinf]
implement
fprint1_intinf
(pf | fil, intinf) = fprint1_intinf_base (pf | fil, 10, intinf)
// end of [fprint1_intinf]
implement print_intinf (intinf) = print_mac (fprint1_intinf, intinf)
(* ****** ****** *)
implement
fprint0_intinf_base
(out, base, intinf) = let
val [l:addr]
(pffil | p) = __cast (out) where {
extern castfn __cast
(out: FILEref): [l:addr] (FILE(w) @ l | ptr l)
// end of [extern]
} // end of [val]
val () = fprint1_intinf_base (file_mode_lte_w_w | !p, base, intinf)
prval () = __free (pffil) where {
extern praxi __free (pf: FILE(w) @ l): void
} // end of [prval]
in
(*nothing*)
end // end of [fprint0_intinf_base]
implement
fprint1_intinf_base
(pf | fil, base, intinf) = () where {
prval pfat = intinf.1
val n = mpz_out_str (pf | fil, base, !(intinf.2))
prval () = intinf.1 := pfat
val () = assert_errmsg (n > 0, "exit(ATS): [fprint_intinf_base] failed.\n")
} // end of [fprint1_intinf_base]
implement
print_intinf_base (base, intinf) = let
val (pf_stdout | p_stdout) = stdout_get ()
val () = fprint1_intinf_base
(file_mode_lte_w_w | !p_stdout, base, intinf)
val () = stdout_view_set (pf_stdout | (*none*))
in
// empty
end // end of [print_intinf_base]
(* ****** ****** *)
implement
pred_intinf (intinf) = let
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_sub (!p, !(intinf.2), 1)
prval () = intinf.1 := pf
in
@(pfgc, pfat | p)
end // end of [pred_intinf]
implement
succ_intinf (intinf) = let
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_add (!p, !(intinf.2), 1)
prval () = intinf.1 := pf
in
@(pfgc, pfat | p)
end // end of [succ_intinf]
(* ****** ****** *)
implement
add_intinf_int (intinf, i) = let
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_add (!p, !(intinf.2), i)
prval () = intinf.1 := pf
in
@(pfgc, pfat | p)
end // end of [add_intinf_int]
implement
add_intinf_intinf (intinf1, intinf2) = let
prval pf1 = intinf1.1
prval pf2 = intinf2.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_add (!p, !(intinf1.2), !(intinf2.2))
prval () = intinf1.1 := pf1
prval () = intinf2.1 := pf2
in
@(pfgc, pfat | p)
end // end of [add_intinf_intinf]
(* ****** ****** *)
implement
sub_intinf_int (intinf, i) = let
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_sub (!p, !(intinf.2), i)
prval () = intinf.1 := pf
in
@(pfgc, pfat | p)
end // end of [sub_intinf_int]
implement
sub_intinf_intinf (intinf1, intinf2) = let
prval pf1 = intinf1.1
prval pf2 = intinf2.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_sub (!p, !(intinf1.2), !(intinf2.2))
prval () = intinf1.1 := pf1
prval () = intinf2.1 := pf2
in
@(pfgc, pfat | p)
end // end of [sub_intinf_intinf]
(* ****** ****** *)
implement
mul_int_intinf {m,n} (int, intinf) = let
prval pfmul = mul_make {m,n} ()
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_mul (!p, !(intinf.2), int)
prval () = intinf.1 := pf
in
@(pfmul | @(pfgc, pfat | p))
end // end of [mul_int_intinf]
implement
mul_intinf_int {m,n} (intinf, int) = let
prval pfmul = mul_make {m,n} ()
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_mul (!p, !(intinf.2), int)
prval () = intinf.1 := pf
in
@(pfmul | @(pfgc, pfat | p))
end // end of [mul_intinf_int]
implement
mul_intinf_intinf {m,n} (intinf1, intinf2) = let
prval pfmul = mul_make {m,n} ()
prval pf1 = intinf1.1
prval pf2 = intinf2.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p); val () = mpz_mul (!p, !(intinf1.2), !(intinf2.2))
prval () = intinf1.1 := pf1
prval () = intinf2.1 := pf2
in
@(pfmul | @(pfgc, pfat | p))
end // end of [mul_intinf_intinf]
(* ****** ****** *)
implement
square_intinf {n} (intinf) = let
prval pf = intinf.1
prval pfmul = mul_make {n,n} ()
val @(pfgc, pfat | p) =
ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init_set (!p, !(intinf.2)); val () = mpz_mul (!p, !(intinf.2))
prval () = intinf.1 := pf
in
@(pfmul | @(pfgc, pfat | p))
end // end of [square_intinf]
(* ****** ****** *)
implement
fdiv_intinf_int {m,n} (intinf, i) = let
prval [q,r:int] pfmul = lemma () where {
extern prfun lemma () : [q,r:int | 0 <= r; r < n] MUL (q, n, m-r)
} // end of [prval]
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p)
val ui = ulint_of_int i; val _(*remainder*) = mpz_fdiv_q (!p, !(intinf.2), ui)
prval () = intinf.1 := pf
in
#[q,r | @(pfmul | @(pfgc, pfat | p))]
end // end of [fdiv_intinf_int]
implement
fmod_intinf_int {m,n} (intinf, i) = let
prval [q,r:int] pfmul =
div_lemma {m,n} () where {
extern praxi div_lemma {m,n:int | n > 0} ()
: [q,r:int | 0 <= r; r < n] MUL (q, n, m-r)
} // end of [prval]
prval pf = intinf.1
val @(pfgc, pfat | p) = ptr_alloc_tsz {mpz_vt} (sizeof<mpz_vt>)
val () = mpz_init (!p)
val ui = ulint_of_int i; val _(*remainder*) = mpz_mod (!p, !(intinf.2), ui)
prval () = intinf.1 := pf
val r = mpz_get_int (!p)
val [r1:int] r = int1_of_int r
prval () = __assert () where { extern prfun __assert (): [r==r1] void }
val () = intinf_free @(pfgc, pfat | p)
in
#[q,r | @(pfmul | r)]
end // end of [fmod_intinf_int]
(* ****** ****** *)
(* end of [intinf.dats] *)
|