diff --git a/samples/ATS/basis_ssntype.sats b/samples/ATS/basis_ssntype.sats new file mode 100644 index 00000000..6da538d3 --- /dev/null +++ b/samples/ATS/basis_ssntype.sats @@ -0,0 +1,294 @@ +// Source: https://github.com/githwxi/ATS-Postiats-contrib/blob/201d635062d0ea64ff5ba5457a4ea0bb4d5ae202/contrib/libats-/hwxi/teaching/mysession-g/SATS/basis_ssntype.sats + +(* +** Basis for g-session types +*) + +(* ****** ****** *) +// +staload +"./basis_intset.sats" +// +(* ****** ****** *) +// +fun{} +channel_cap(): intGte(1) +// +(* ****** ****** *) +// +abstype +session_msg + (i:int, j:int, a:vt@ype) +// +(* ****** ****** *) + +abstype ssession_nil +abstype ssession_cons(a:type, ssn:type) + +(* ****** ****** *) +// +stadef msg = session_msg +// +stadef nil = ssession_nil +// +stadef :: = ssession_cons +stadef cons = ssession_cons +// +(* ****** ****** *) +// +abstype +session_append + (ssn1: type, ssn2: type) +// +stadef append = session_append +// +(* ****** ****** *) +// +abstype +session_choose +( + i:int, ssn1:type, ssn2:type +) (* session_choose *) +// +stadef choose = session_choose +// +(* ****** ****** *) +// +abstype +session_repeat +( + i:int, ssn:type(*body*) +) (* session_repeat *) +// +stadef repeat = session_repeat +// +(* ****** ****** *) +// +typedef +session_sing +( + i: int +, j: int +, a:vt@ype +) = cons(msg(i, j, a), nil) +// +(* ****** ****** *) +// +absvtype +channel1_vtype + (G:iset, n:int, ssn:type) = ptr +// +vtypedef +channel1 + (G:iset, n:int, ssn:type) = channel1_vtype(G, n, ssn) +// +vtypedef +cchannel1 + (G:iset, n:int, ssn:type) = channel1_vtype(ncomp(n, G), n, ssn) +// +(* ****** ****** *) +// +fun{} +channel1_get_nrole + {n:int}{ssn:type}{G:iset} + (chan: !channel1(G, n, ssn)): int(n) +// +fun{} +channel1_get_group + {n:int}{ssn:type}{G:iset} + (chan: !channel1(G, n, ssn)): intset(n,G) +// +(* ****** ****** *) +// +fun +{a:vt0p} +channel1_close + {n:int}{ssn:type}{G:iset}(chan: channel1(G, n, nil)): void +// +(* ****** ****** *) +// +fun{} +channel1_skipin + {a:vt0p} + {n:int}{ssn:type}{G:iset} + {i,j:nat | ismbr(G, i); ismbr(G, j)} +( + !channel1(G, n, msg(i, j, a)::ssn) >> channel1(G, n, ssn) +) : void // end-of-function +praxi +lemma_channel1_skipin + {a:vt0p} + {n:int}{ssn:type}{G:iset} + {i,j:nat | ismbr(G, i); ismbr(G, j)} +( + !channel1(G, n, msg(i, j, a)::ssn) >> channel1(G, n, ssn) +) : void // lemma_channel1_skipin +// +fun{} +channel1_skipex + {a:vt0p} + {n:int}{ssn:type}{G:iset} + {i,j:nat | ~ismbr(G, i); ~ismbr(G, j)} +( + !channel1(G, n, msg(i, j, a)::ssn) >> channel1(G, n, ssn) +) : void // end-of-function +praxi +lemma_channel1_skipex + {a:vt0p} + {n:int}{ssn:type}{G:iset} + {i,j:nat | ~ismbr(G, i); ~ismbr(G, j)} +( + !channel1(G, n, msg(i, j, a)::ssn) >> channel1(G, n, ssn) +) : void // lemma_channel1_skipex +// +(* ****** ****** *) +// +fun +{a:vt0p} +channel1_send + {n:int}{ssn:type}{G:iset} + {i,j:nat | i < n; j < n; ismbr(G, i); ~ismbr(G, j)} +( + !channel1(G, n, msg(i, j, a)::ssn) >> channel1(G, n, ssn), int(i), int(j), a +) : void // end of [channel1_send] +// +fun +{a:vt0p} +channel1_recv + {n:int}{ssn:type}{G:iset} + {i,j:nat | i < n; j < n; ~ismbr(G, i); ismbr(G, j)} +( + !channel1(G, n, msg(i, j, a)::ssn) >> channel1(G, n, ssn), int(i), int(j), &a? >> a +) : void // end of [channel1_recv] +// +fun +{a:vt0p} +channel1_recv_val + {n:int}{ssn:type}{G:iset} + {i,j:nat | i < n; j < n; ~ismbr(G, i); ismbr(G, j)} + (!channel1(G, n, msg(i, j, a)::ssn) >> channel1(G, n, ssn), int(i), int(j)): (a) +// +(* ****** ****** *) + +fun{} +channel1_append + {n:int} + {ssn1,ssn2:type} + {G:iset} +( + chan: !channel1(G, n, append(ssn1, ssn2)) >> channel1(G, n, ssn2) +, fserv: (!channel1(G, n, ssn1) >> channel1(G, n, nil)) - void +) : void // end of [channel1_append] + +(* ****** ****** *) +// +datatype +choosetag +( + a:type, b:type, c:type +) = + | choosetag_l(a, b, a) of () + | choosetag_r(a, b, b) of () +// +(* ****** ****** *) +// +fun{} +channel1_choose_l + {n:int} + {ssn1,ssn2:type} + {G:iset} + {i:nat | i < n; ismbr(G, i)} +( + !channel1(G, n, choose(i,ssn1,ssn2)) >> channel1(G, n, ssn1), i: int(i) +) : void // end of [channel1_choose_l] +// +fun{} +channel1_choose_r + {n:int} + {ssn1,ssn2:type} + {G:iset} + {i:nat | i < n; ismbr(G, i)} +( + !channel1(G, n, choose(i,ssn1,ssn2)) >> channel1(G, n, ssn2), i: int(i) +) : void // end of [channel1_choose_r] +// +fun{} +channel1_choose_tag + {n:int} + {ssn1,ssn2:type} + {G:iset} + {i:nat | i < n; ~isnil(G); ~ismbr(G, i)} +( + !channel1(G, n, choose(i,ssn1,ssn2)) >> channel1(G, n, ssn_chosen), i: int(i) +) : #[ssn_chosen:type] choosetag(ssn1, ssn2, ssn_chosen) +// +(* ****** ****** *) +// +fun{} +channel1_repeat_0 + {n:int} + {ssn:type} + {G:iset} + {i:nat | i < n; ismbr(G, i)} +( + !channel1(G, n, repeat(i,ssn)) >> channel1(G, n, nil), i: int(i) +) : void // end of [channel1_repeat_nil] +// +fun{} +channel1_repeat_1 + {n:int} + {ssn:type} + {G:iset} + {i:nat | i < n; ismbr(G, i)} +( + !channel1(G, n, repeat(i,ssn)) >> channel1(G, n, append(ssn,repeat(i,ssn))), i: int(i) +) : void // end of [channel1_repeat_more] +// +fun{} +channel1_repeat_tag + {n:int} + {ssn:type} + {G:iset} + {i:nat | i < n; ~isnil(G); ~ismbr(G, i)} +( + !channel1(G, n, repeat(i,ssn)) >> channel1(G, n, ssn_chosen), i: int(i) +) : #[ssn_chosen:type] choosetag(nil, append(ssn,repeat(i,ssn)), ssn_chosen) +// +(* ****** ****** *) +// +(* +// +// HX-2015-03-06: +// This one does not work with sschoose!!! +// +fun{} +channel1_link + {n:int}{ssn:type} + {G1,G2:iset | isnil(G1*G2)} + (channel1(G1, n, ssn), channel1(G2, n, ssn)): channel1(G1+G2, n, ssn) +*) +// +fun{} +channel1_link + {n:int}{ssn:type} + {G1,G2:iset | isful(G1+G2,n)} + (channel1(G1, n, ssn), channel1(G2, n, ssn)): channel1(G1*G2, n, ssn) +// +(* ****** ****** *) +// +fun{} +channel1_link_elim + {n:int}{ssn:type}{G:iset}(channel1(G, n, ssn), cchannel1(G, n, ssn)): void +// +(* ****** ****** *) +// +fun{} +cchannel1_create_exn + {n:nat}{ssn:type}{G:iset} +( + nrole: int(n), G: intset(n), fserv: channel1(G, n, ssn) - void +) : cchannel1(G, n, ssn) // end of [cchannel1_create_exn] +// +(* ****** ****** *) + +(* end of [basis_ssntype.sats] *) diff --git a/samples/ATS/intinf_vt.dats b/samples/ATS/intinf_vt.dats new file mode 100644 index 00000000..51d3af5e --- /dev/null +++ b/samples/ATS/intinf_vt.dats @@ -0,0 +1,694 @@ +// Source: https://github.com/githwxi/ATS-Postiats-contrib/blob/04a984d9c08c1831f7dda8a05ce356db01f81850/contrib/libats-/hwxi/intinf/DATS/intinf_vt.dats + +(***********************************************************************) +(* *) +(* ATS/contrib/atshwxi *) +(* *) +(***********************************************************************) + +(* +** Copyright (C) 2013 Hongwei Xi, ATS Trustful Software, Inc. +** +** Permission is hereby granted, free of charge, to any person obtaining a +** copy of this software and associated documentation files (the "Software"), +** to deal in the Software without restriction, including without limitation +** the rights to use, copy, modify, merge, publish, distribute, sublicense, +** and/or sell copies of the Software, and to permit persons to whom the +** Software is furnished to do so, subject to the following stated conditions: +** +** The above copyright notice and this permission notice shall be included in +** all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +** OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +** THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +** FROM OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +** IN THE SOFTWARE. +*) + +(* ****** ****** *) +// +// Author: Hongwei Xi +// Authoremail: hwxi AT gmail DOT com +// Start Time: April, 2013 +// +(* ****** ****** *) + +#include +"share/atspre_define.hats" + +(* ****** ****** *) + +staload +UN = "prelude/SATS/unsafe.sats" + +(* ****** ****** *) + +staload +GMP = "{$LIBGMP}/SATS/gmp.sats" + +(* ****** ****** *) + +vtypedef mpz = $GMP.mpz_vt0ype + +(* ****** ****** *) +// +staload "./../SATS/intinf.sats" +staload "./../SATS/intinf_vt.sats" +// +(* ****** ****** *) + +macdef i2u (x) = g1int2uint_int_uint (,(x)) + +(* ****** ****** *) + +local + +assume +intinf_vtype + (i: int) = // HX: [i] is a fake + [l:addr] (mpz @ l, mfree_gc_v (l) | ptr l) +// end of [intinf_vtype] + +in (* in of [local] *) + +implement{} +intinf_make_int + (i) = (x) where +{ +// +val x = ptr_alloc () +val () = $GMP.mpz_init_set_int (!(x.2), i) +// +} (* end of [intinf_make_int] *) + +implement{} +intinf_make_uint + (i) = (x) where +{ +// +val x = ptr_alloc () +val () = $GMP.mpz_init_set_uint (!(x.2), i) +// +} (* end of [intinf_make_uint] *) + +implement{} +intinf_make_lint + (i) = (x) where +{ +// +val x = ptr_alloc () +val () = $GMP.mpz_init_set_lint (!(x.2), i) +// +} (* end of [intinf_make_lint] *) + +implement{} +intinf_make_ulint + (i) = (x) where +{ +// +val x = ptr_alloc () +val () = $GMP.mpz_init_set_ulint (!(x.2), i) +// +} (* end of [intinf_make_ulint] *) + +(* ****** ****** *) + +implement{} +intinf_free (x) = let + val (pfat, pfgc | p) = x + val () = $GMP.mpz_clear (!p) in ptr_free (pfgc, pfat | p) +end (* end of [intinf_free] *) + +(* ****** ****** *) + +implement{} +intinf_get_int (x) = $GMP.mpz_get_int (!(x.2)) +implement{} +intinf_get_lint (x) = $GMP.mpz_get_lint (!(x.2)) + +(* ****** ****** *) + +implement{} +intinf_get_strptr + (x, base) = $GMP.mpz_get_str_null (base, !(x.2)) +// end of [intinf_get_strptr] + +(* ****** ****** *) + +implement{} +fprint_intinf_base + (out, x, base) = let + val nsz = $GMP.mpz_out_str (out, base, !(x.2)) +in +// +if (nsz = 0) then + exit_errmsg (1, "libgmp/gmp: fprint_intinf_base") +// end of [if] +// +end (* fprint_intinf_base *) + +(* ****** ****** *) + +implement{ +} neg_intinf0 + (x) = (x) where +{ +// +val () = $GMP.mpz_neg (!(x.2)) +// +} (* end of [neg_intinf0] *) + +implement{ +} neg_intinf1 + (x) = (y) where +{ +// +val y = ptr_alloc () +val () = $GMP.mpz_init (!(y.2)) +val () = $GMP.mpz_neg (!(y.2), !(x.2)) +// +} (* end of [neg_intinf1] *) + +(* ****** ****** *) + +implement{ +} abs_intinf0 + (x) = (x) where +{ +// +val () = $GMP.mpz_abs (!(x.2)) +// +} (* end of [abs_intinf0] *) + +implement{ +} abs_intinf1 + (x) = (y) where +{ +// +val y = ptr_alloc () +val () = $GMP.mpz_init (!(y.2)) +val () = $GMP.mpz_abs (!(y.2), !(x.2)) +// +} (* end of [abs_intinf1] *) + +(* ****** ****** *) + +implement{} +succ_intinf0 (x) = add_intinf0_int (x, 1) +implement{} +succ_intinf1 (x) = add_intinf1_int (x, 1) + +(* ****** ****** *) + +implement{} +pred_intinf0 (x) = sub_intinf0_int (x, 1) +implement{} +pred_intinf1 (x) = sub_intinf1_int (x, 1) + +(* ****** ****** *) + +implement{} +add_intinf0_int + (x, y) = (x) where +{ +// +val () = $GMP.mpz_add2_int (!(x.2), y) +// +} (* end of [add_intinf0_int] *) + +implement{} +add_intinf1_int + (x, y) = (z) where +{ +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +val () = $GMP.mpz_add3_int (!(z.2), !(x.2), y) +// +} (* end of [add_intinf1_int] *) + +(* ****** ****** *) + +implement{} +add_int_intinf0 (x, y) = add_intinf0_int (y, x) +implement{} +add_int_intinf1 (x, y) = add_intinf1_int (y, x) + +(* ****** ****** *) + +implement{} +add_intinf0_intinf1 + (x, y) = (x) where +{ +// +val () = $GMP.mpz_add2_mpz (!(x.2), !(y.2)) +// +} (* end of [add_intinf0_intinf1] *) + +implement{} +add_intinf1_intinf0 + (x, y) = (y) where +{ +// +val () = $GMP.mpz_add2_mpz (!(y.2), !(x.2)) +// +} (* end of [add_intinf1_intinf0] *) + +(* ****** ****** *) + +implement{} +add_intinf1_intinf1 + (x, y) = (z) where +{ +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +val () = $GMP.mpz_add3_mpz (!(z.2), !(x.2), !(y.2)) +// +} (* end of [add_intinf1_intinf1] *) + +(* ****** ****** *) + +implement{} +sub_intinf0_int + (x, y) = (x) where +{ +// +val () = $GMP.mpz_sub2_int (!(x.2), y) +// +} (* end of [sub_intinf0_int] *) + +implement{} +sub_intinf1_int + (x, y) = (z) where +{ +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +val () = $GMP.mpz_sub3_int (!(z.2), !(x.2), y) +// +} (* end of [sub_intinf1_int] *) + +(* ****** ****** *) + +implement{} +sub_int_intinf0 (x, y) = let + val z = sub_intinf0_int (y, x) in neg_intinf0 (z) +end (* end of [sub_int_intinf0] *) + +implement{} +sub_int_intinf1 (x, y) = let + val z = sub_intinf1_int (y, x) in neg_intinf0 (z) +end (* end of [sub_int_intinf1] *) + +(* ****** ****** *) + +implement{} +sub_intinf0_intinf1 + (x, y) = (x) where +{ +// +val () = $GMP.mpz_sub2_mpz (!(x.2), !(y.2)) +// +} (* end of [sub_intinf0_intinf1] *) + +implement{} +sub_intinf1_intinf0 + (x, y) = neg_intinf0 (sub_intinf0_intinf1 (y, x)) +// end of [sub_intinf1_intinf0] + +implement{} +sub_intinf1_intinf1 + (x, y) = (z) where +{ +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +val () = $GMP.mpz_sub3_mpz (!(z.2), !(x.2), !(y.2)) +// +} (* end of [sub_intinf1_intinf1] *) + +(* ****** ****** *) + +implement{} +mul_intinf0_int + (x, y) = (x) where +{ +// +val () = $GMP.mpz_mul2_int (!(x.2), y) +// +} (* end of [mul_intinf0_int] *) + +implement{} +mul_intinf1_int + (x, y) = (z) where +{ +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +val () = $GMP.mpz_mul3_int (!(z.2), !(x.2), y) +// +} (* end of [mul_intinf1_int] *) + +(* ****** ****** *) + +implement{} +mul_int_intinf0 (x, y) = mul_intinf0_int (y, x) +implement{} +mul_int_intinf1 (x, y) = mul_intinf1_int (y, x) + +(* ****** ****** *) + +implement{} +mul_intinf0_intinf1 + (x, y) = (x) where +{ +// +val () = $GMP.mpz_mul2_mpz (!(x.2), !(y.2)) +// +} (* end of [mul_intinf0_intinf1] *) + +implement{} +mul_intinf1_intinf0 + (x, y) = (y) where +{ +// +val () = $GMP.mpz_mul2_mpz (!(y.2), !(x.2)) +// +} (* end of [mul_intinf0_intinf1] *) + +(* ****** ****** *) + +implement{} +mul_intinf1_intinf1 + (x, y) = (z) where +{ +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +val () = $GMP.mpz_mul3_mpz (!(z.2), !(x.2), !(y.2)) +// +} (* end of [mul_intinf1_intinf1] *) + +(* ****** ****** *) + +implement{} +div_intinf0_int + {i,j} (x, y) = let +in +// +if y >= 0 then let + val () = $GMP.mpz_tdiv2_q_uint (!(x.2), i2u(y)) in x +end else let + val () = $GMP.mpz_tdiv2_q_uint (!(x.2), i2u(~y)) in neg_intinf0 (x) +end // end of [if] +// +end (* end of [div_intinf0_int] *) + +implement{} +div_intinf1_int + {i,j} (x, y) = let +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +// +in +// +if y >= 0 then let + val () = $GMP.mpz_tdiv3_q_uint (!(z.2), !(x.2), i2u(y)) in z +end else let + val () = $GMP.mpz_tdiv3_q_uint (!(z.2), !(x.2), i2u(~y)) in neg_intinf0 (z) +end // end of [if] +// +end (* end of [div_intinf1_int] *) + +(* ****** ****** *) + +implement{} +div_intinf0_intinf1 + (x, y) = (x) where +{ +// +val () = $GMP.mpz_tdiv2_q_mpz (!(x.2), !(y.2)) +// +} (* end of [div_intinf0_intinf1] *) + +(* ****** ****** *) + +implement{} +div_intinf1_intinf1 + (x, y) = (z) where +{ +// +val z = ptr_alloc () +val () = $GMP.mpz_init (!(z.2)) +val () = $GMP.mpz_tdiv3_q_mpz (!(z.2), !(x.2), !(y.2)) +// +} (* end of [div_intinf1_intinf1] *) + +(* ****** ****** *) + +implement{} +ndiv_intinf0_int (x, y) = div_intinf0_int (x, y) +implement{} +ndiv_intinf1_int (x, y) = div_intinf1_int (x, y) + +(* ****** ****** *) + +implement{} +nmod_intinf0_int + {i,j} (x, y) = let +// +val r = + $GMP.mpz_fdiv_uint (!(x.2), i2u(y)) +val () = intinf_free (x) +// +in + $UN.cast{intBtw(0,j)}(r) +end (* end of [nmod_intinf0_int] *) + +implement{} +nmod_intinf1_int + {i,j} (x, y) = let +// +val r = $GMP.mpz_fdiv_uint (!(x.2), i2u(y)) +// +in + $UN.cast{intBtw(0,j)}(r) +end (* end of [nmod_intinf1_int] *) + +(* ****** ****** *) +// +// comparison-functions +// +(* ****** ****** *) + +implement{} +lt_intinf_int + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(x.2), y) +val ans = (if sgn < 0 then true else false): bool +// +in + $UN.cast{bool(i < j)}(sgn) +end // end of [lt_intinf_int] + +implement{} +lt_intinf_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_mpz (!(x.2), !(y.2)) +val ans = (if sgn < 0 then true else false): bool +// +in + $UN.cast{bool(i < j)}(sgn) +end // end of [lt_intinf_intinf] + +(* ****** ****** *) + +implement{} +lte_intinf_int + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(x.2), y) +val ans = (if sgn <= 0 then true else false): bool +// +in + $UN.cast{bool(i <= j)}(sgn) +end // end of [lte_intinf_int] + +implement{} +lte_intinf_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_mpz (!(x.2), !(y.2)) +val ans = (if sgn <= 0 then true else false): bool +// +in + $UN.cast{bool(i <= j)}(sgn) +end // end of [lte_intinf_intinf] + +(* ****** ****** *) + +implement{} +gt_intinf_int + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(x.2), y) +val ans = (if sgn > 0 then true else false): bool +// +in + $UN.cast{bool(i > j)}(sgn) +end // end of [gt_intinf_int] + +implement{} +gt_intinf_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_mpz (!(x.2), !(y.2)) +val ans = (if sgn > 0 then true else false): bool +// +in + $UN.cast{bool(i > j)}(sgn) +end // end of [gt_intinf_intinf] + +(* ****** ****** *) + +implement{} +gte_intinf_int + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(x.2), y) +val ans = (if sgn >= 0 then true else false): bool +// +in + $UN.cast{bool(i >= j)}(sgn) +end // end of [gte_intinf_int] + +implement{} +gte_intinf_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_mpz (!(x.2), !(y.2)) +val ans = (if sgn >= 0 then true else false): bool +// +in + $UN.cast{bool(i >= j)}(sgn) +end // end of [gte_intinf_intinf] + +(* ****** ****** *) + +implement{} +eq_intinf_int + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(x.2), y) +val ans = (if sgn = 0 then true else false): bool +// +in + $UN.cast{bool(i == j)}(sgn) +end // end of [eq_intinf_int] + +implement{} +eq_intinf_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_mpz (!(x.2), !(y.2)) +val ans = (if sgn = 0 then true else false): bool +// +in + $UN.cast{bool(i == j)}(sgn) +end // end of [eq_intinf_intinf] + +(* ****** ****** *) + +implement{} +neq_intinf_int + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(x.2), y) +val ans = (if sgn != 0 then true else false): bool +// +in + $UN.cast{bool(i != j)}(sgn) +end // end of [neq_intinf_int] + +implement{} +neq_intinf_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_mpz (!(x.2), !(y.2)) +val ans = (if sgn != 0 then true else false): bool +// +in + $UN.cast{bool(i != j)}(sgn) +end // end of [neq_intinf_intinf] + +(* ****** ****** *) + +implement{} +compare_intinf_int + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(x.2), y) +val sgn = (if sgn < 0 then ~1 else (if sgn > 0 then 1 else 0)): int +// +in + $UN.cast{int(sgn(i-j))}(sgn) +end // end of [compare_intinf_int] + +implement{} +compare_int_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_int (!(y.2), x) +val sgn = (if sgn > 0 then ~1 else (if sgn < 0 then 1 else 0)): int +// +in + $UN.cast{int(sgn(i-j))}(sgn) +end // end of [compare_int_intinf] + +implement{} +compare_intinf_intinf + {i,j} (x, y) = let +// +val sgn = $GMP.mpz_cmp_mpz (!(x.2), !(y.2)) +val sgn = (if sgn < 0 then ~1 else (if sgn > 0 then 1 else 0)): int +// +in + $UN.cast{int(sgn(i-j))}(sgn) +end // end of [compare_intinf_intinf] + +(* ****** ****** *) + +implement{} +pow_intinf_int + (base, exp) = r where +{ +// +val r = ptr_alloc () +val () = $GMP.mpz_init (!(r.2)) +val () = $GMP.mpz_pow_uint (!(r.2), !(base.2), i2u(exp)) +// +} (* end of [pow_intinf_int] *) + +(* ****** ****** *) + +end // end of [local] + +(* ****** ****** *) + +implement{} +print_intinf (x) = fprint_intinf (stdout_ref, x) +implement{} +prerr_intinf (x) = fprint_intinf (stderr_ref, x) +implement{} +fprint_intinf (out, x) = fprint_intinf_base (out, x, 10(*base*)) + +(* ****** ****** *) + +(* end of [intinf_vt.dats] *) diff --git a/samples/ATS/linset_listord.dats b/samples/ATS/linset_listord.dats deleted file mode 100644 index eb7dd642..00000000 --- a/samples/ATS/linset_listord.dats +++ /dev/null @@ -1,504 +0,0 @@ -(***********************************************************************) -(* *) -(* Applied Type System *) -(* *) -(***********************************************************************) - -(* -** ATS/Postiats - Unleashing the Potential of Types! -** Copyright (C) 2011-2013 Hongwei Xi, ATS Trustful Software, Inc. -** All rights reserved -** -** ATS is free software; you can redistribute it and/or modify it under -** the terms of the GNU GENERAL PUBLIC LICENSE (GPL) as published by the -** Free Software Foundation; either version 3, 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, please write to the -** Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA -** 02110-1301, USA. -*) - -(* ****** ****** *) - -(* Author: Hongwei Xi *) -(* Authoremail: hwxi AT cs DOT bu DOT edu *) -(* Start time: February, 2013 *) - -(* ****** ****** *) -// -// HX-2013-08: -// a set is represented as a sorted list in descending order; -// note that descending order is chosen to faciliate set comparison -// -(* ****** ****** *) - -staload -UN = "prelude/SATS/unsafe.sats" - -(* ****** ****** *) - -staload "libats/SATS/linset_listord.sats" - -(* ****** ****** *) - -#include "./SHARE/linset.hats" // code reuse -#include "./SHARE/linset_node.hats" // code reuse - -(* ****** ****** *) - -assume -set_vtype (elt:t@ype) = List0_vt (elt) - -(* ****** ****** *) - -implement{} -linset_nil () = list_vt_nil () -implement{} -linset_make_nil () = list_vt_nil () - -(* ****** ****** *) - -implement -{a}(*tmp*) -linset_sing - (x) = list_vt_cons{a}(x, list_vt_nil) -// end of [linset_sing] -implement{a} -linset_make_sing - (x) = list_vt_cons{a}(x, list_vt_nil) -// end of [linset_make_sing] - -(* ****** ****** *) - -implement{} -linset_is_nil (xs) = list_vt_is_nil (xs) -implement{} -linset_isnot_nil (xs) = list_vt_is_cons (xs) - -(* ****** ****** *) - -implement{a} -linset_size (xs) = - let val n = list_vt_length(xs) in i2sz(n) end -// end of [linset_size] - -(* ****** ****** *) - -implement{a} -linset_is_member - (xs, x0) = let -// -fun aux - {n:nat} .. -( - xs: !list_vt (a, n) -) :<> bool = let -in -// -case+ xs of -| list_vt_cons (x, xs) => let - val sgn = compare_elt_elt (x0, x) in - if sgn > 0 then false else (if sgn < 0 then aux (xs) else true) - end // end of [list_vt_cons] -| list_vt_nil ((*void*)) => false -// -end // end of [aux] -// -in - aux (xs) -end // end of [linset_is_member] - -(* ****** ****** *) - -implement{a} -linset_copy (xs) = list_vt_copy (xs) -implement{a} -linset_free (xs) = list_vt_free (xs) - -(* ****** ****** *) - -implement{a} -linset_insert - (xs, x0) = let -// -fun -mynode_cons - {n:nat} .<>. -( - nx: mynode1 (a), xs: list_vt (a, n) -) : list_vt (a, n+1) = let -// -val xs1 = -$UN.castvwtp0{List1_vt(a)}(nx) -val+@list_vt_cons (_, xs2) = xs1 -prval () = $UN.cast2void (xs2); val () = (xs2 := xs) -// -in - fold@ (xs1); xs1 -end // end of [mynode_cons] -// -fun ins - {n:nat} .. // tail-recursive -( - xs: &list_vt (a, n) >> list_vt (a, n1) -) : #[n1:nat | n <= n1; n1 <= n+1] bool = -( -case+ xs of -| @list_vt_cons - (x, xs1) => let - val sgn = - compare_elt_elt (x0, x) - // end of [val] - in - if sgn > 0 then let - prval () = fold@ (xs) - val nx = mynode_make_elt (x0) - val ((*void*)) = xs := mynode_cons (nx, xs) - in - false - end else if sgn < 0 then let - val ans = ins (xs1) - prval () = fold@ (xs) - in - ans - end else let // [x0] is found - prval () = fold@ (xs) - in - true (* [x0] in [xs] *) - end (* end of [if] *) - end // end of [list_vt_cons] -| list_vt_nil () => let - val nx = mynode_make_elt (x0) - val ((*void*)) = xs := mynode_cons (nx, xs) - in - false - end // end of [list_vt_nil] -) (* end of [ins] *) -// -in - $effmask_all (ins (xs)) -end // end of [linset_insert] - -(* ****** ****** *) - -(* -// -HX-2013-08: -[linset_remove] moved up -// -implement{a} -linset_remove - (xs, x0) = let -// -fun rem - {n:nat} .. // tail-recursive -( - xs: &list_vt (a, n) >> list_vt (a, n1) -) : #[n1:nat | n1 <= n; n <= n1+1] bool = -( -case+ xs of -| @list_vt_cons - (x, xs1) => let - val sgn = - compare_elt_elt (x0, x) - // end of [val] - in - if sgn > 0 then let - prval () = fold@ (xs) - in - false - end else if sgn < 0 then let - val ans = rem (xs1) - prval () = fold@ (xs) - in - ans - end else let // x0 = x - val xs1_ = xs1 - val ((*void*)) = free@{a}{0}(xs) - val () = xs := xs1_ - in - true // [x0] in [xs] - end (* end of [if] *) - end // end of [list_vt_cons] -| list_vt_nil () => false -) (* end of [rem] *) -// -in - $effmask_all (rem (xs)) -end // end of [linset_remove] -*) - -(* ****** ****** *) -(* -** By Brandon Barker -*) -implement -{a}(*tmp*) -linset_choose - (xs, x0) = let -in -// -case+ xs of -| list_vt_cons - (x, xs1) => let - val () = x0 := x - prval () = opt_some{a}(x0) - in - true - end // end of [list_vt_cons] -| list_vt_nil () => let - prval () = opt_none{a}(x0) - in - false - end // end of [list_vt_nil] -// -end // end of [linset_choose] - -(* ****** ****** *) - -implement -{a}{env} -linset_foreach_env (xs, env) = let -// -implement -list_vt_foreach$fwork - (x, env) = linset_foreach$fwork (x, env) -// -in - list_vt_foreach_env (xs, env) -end // end of [linset_foreach_env] - -(* ****** ****** *) - -implement{a} -linset_listize (xs) = xs - -(* ****** ****** *) - -implement{a} -linset_listize1 (xs) = list_vt_copy (xs) - -(* ****** ****** *) -// -// HX: functions for processing mynodes -// -(* ****** ****** *) - -implement{ -} mynode_null{a} () = - $UN.castvwtp0{mynode(a,null)}(the_null_ptr) -// end of [mynode_null] - -(* ****** ****** *) - -implement -{a}(*tmp*) -mynode_make_elt - (x) = let -// -val nx = list_vt_cons{a}{0}(x, _ ) -// -in - $UN.castvwtp0{mynode1(a)}(nx) -end // end of [mynode_make_elt] - -(* ****** ****** *) - -implement{ -} mynode_free - {a}(nx) = () where { -val nx = - $UN.castvwtp0{List1_vt(a)}(nx) -// -val+~list_vt_cons (_, nx2) = nx -// -prval ((*void*)) = $UN.cast2void (nx2) -// -} (* end of [mynode_free] *) - -(* ****** ****** *) - -implement -{a}(*tmp*) -mynode_get_elt - (nx) = (x) where { -// -val nx1 = - $UN.castvwtp1{List1_vt(a)}(nx) -// -val+list_vt_cons (x, _) = nx1 -// -prval ((*void*)) = $UN.cast2void (nx1) -// -} (* end of [mynode_get_elt] *) - -(* ****** ****** *) - -implement -{a}(*tmp*) -mynode_set_elt - {l} (nx, x0) = -{ -// -val nx1 = - $UN.castvwtp1{List1_vt(a)}(nx) -// -val+@list_vt_cons (x, _) = nx1 -// -val () = x := x0 -// -prval () = fold@ (nx1) -prval () = $UN.cast2void (nx1) -// -prval () = __assert (nx) where -{ - extern praxi __assert (nx: !mynode(a?, l) >> mynode (a, l)): void -} (* end of [prval] *) -// -} (* end of [mynode_set_elt] *) - -(* ****** ****** *) - -implement -{a}(*tmp*) -mynode_getfree_elt - (nx) = (x) where { -// -val nx = - $UN.castvwtp0{List1_vt(a)}(nx) -// -val+~list_vt_cons (x, nx2) = nx -// -prval ((*void*)) = $UN.cast2void (nx2) -// -} (* end of [mynode_getfree_elt] *) - -(* ****** ****** *) - -(* -fun{a:t0p} -linset_takeout_ngc - (set: &set(INV(a)) >> _, x0: a): mynode0 (a) -// end of [linset_takeout_ngc] -*) -implement -{a}(*tmp*) -linset_takeout_ngc - (set, x0) = let -// -fun takeout -( - xs: &List0_vt (a) >> _ -) : mynode0(a) = let -in -// -case+ xs of -| @list_vt_cons - (x, xs1) => let - prval pf_x = view@x - prval pf_xs1 = view@xs1 - val sgn = - compare_elt_elt (x0, x) - // end of [val] - in - if sgn > 0 then let - prval () = fold@ (xs) - in - mynode_null{a}((*void*)) - end else if sgn < 0 then let - val res = takeout (xs1) - prval ((*void*)) = fold@ (xs) - in - res - end else let // x0 = x - val xs1_ = xs1 - val res = $UN.castvwtp0{mynode1(a)}((pf_x, pf_xs1 | xs)) - val () = xs := xs1_ - in - res // [x0] in [xs] - end (* end of [if] *) - end // end of [list_vt_cons] -| list_vt_nil () => mynode_null{a}((*void*)) -// -end (* end of [takeout] *) -// -in - $effmask_all (takeout (set)) -end // end of [linset_takeout_ngc] - -(* ****** ****** *) - -implement -{a}(*tmp*) -linset_takeoutmax_ngc - (xs) = let -in -// -case+ xs of -| @list_vt_cons - (x, xs1) => let - prval pf_x = view@x - prval pf_xs1 = view@xs1 - val xs_ = xs - val () = xs := xs1 - in - $UN.castvwtp0{mynode1(a)}((pf_x, pf_xs1 | xs_)) - end // end of [list_vt_cons] -| @list_vt_nil () => let - prval () = fold@ (xs) - in - mynode_null{a}((*void*)) - end // end of [list_vt_nil] -// -end // end of [linset_takeoutmax_ngc] - -(* ****** ****** *) - -implement -{a}(*tmp*) -linset_takeoutmin_ngc - (xs) = let -// -fun unsnoc - {n:pos} .. -( - xs: &list_vt (a, n) >> list_vt (a, n-1) -) : mynode1 (a) = let -// -val+@list_vt_cons (x, xs1) = xs -// -prval pf_x = view@x and pf_xs1 = view@xs1 -// -in -// -case+ xs1 of -| list_vt_cons _ => - let val res = unsnoc(xs1) in fold@xs; res end - // end of [list_vt_cons] -| list_vt_nil () => let - val xs_ = xs - val () = xs := list_vt_nil{a}() - in - $UN.castvwtp0{mynode1(a)}((pf_x, pf_xs1 | xs_)) - end // end of [list_vt_nil] -// -end // end of [unsnoc] -// -in -// -case+ xs of -| list_vt_cons _ => unsnoc (xs) -| list_vt_nil () => mynode_null{a}((*void*)) -// -end // end of [linset_takeoutmin_ngc] - -(* ****** ****** *) - -(* end of [linset_listord.dats] *) diff --git a/samples/ATS/linset_listord.sats b/samples/ATS/linset_listord.sats deleted file mode 100644 index 468a534f..00000000 --- a/samples/ATS/linset_listord.sats +++ /dev/null @@ -1,51 +0,0 @@ -(***********************************************************************) -(* *) -(* Applied Type System *) -(* *) -(***********************************************************************) - -(* -** ATS/Postiats - Unleashing the Potential of Types! -** Copyright (C) 2011-2013 Hongwei Xi, ATS Trustful Software, Inc. -** All rights reserved -** -** ATS is free software; you can redistribute it and/or modify it under -** the terms of the GNU GENERAL PUBLIC LICENSE (GPL) as published by the -** Free Software Foundation; either version 3, 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, please write to the -** Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA -** 02110-1301, USA. -*) - -(* ****** ****** *) -// -// Author: Hongwei Xi -// Authoremail: hwxiATcsDOTbuDOTedu -// Time: October, 2010 -// -(* ****** ****** *) - -#define ATS_PACKNAME "ATSLIB.libats.linset_listord" -#define ATS_STALOADFLAG 0 // no static loading at run-time - -(* ****** ****** *) - -#include "./SHARE/linset.hats" -#include "./SHARE/linset_node.hats" - -(* ****** ****** *) - -castfn -linset2list {a:t0p} (xs: set (INV(a))):<> List0_vt (a) - -(* ****** ****** *) - -(* end of [linset_listord.sats] *)