src/HOL/Tools/Nitpick/nitpick_kodkod.ML
author blanchet
Thu, 07 Jan 2010 12:24:35 +0100
changeset 34288 cf455b5880e1
parent 34123 8a2c5d7aff51
child 34923 c4f04bee79f3
permissions -rw-r--r--
reduced arity of Nitpick selectors associated with sets by 1, by using "Formula" instead of "Atom 2"
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_kodkod.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@33192
     3
    Copyright   2008, 2009
blanchet@33192
     4
blanchet@33192
     5
Kodkod problem generator part of Kodkod.
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33192
     8
signature NITPICK_KODKOD =
blanchet@33192
     9
sig
blanchet@33224
    10
  type extended_context = Nitpick_HOL.extended_context
blanchet@33224
    11
  type dtype_spec = Nitpick_Scope.dtype_spec
blanchet@33224
    12
  type kodkod_constrs = Nitpick_Peephole.kodkod_constrs
blanchet@33224
    13
  type nut = Nitpick_Nut.nut
blanchet@33192
    14
  type nfa_transition = Kodkod.rel_expr * typ
blanchet@33192
    15
  type nfa_entry = typ * nfa_transition list
blanchet@33192
    16
  type nfa_table = nfa_entry list
blanchet@33192
    17
blanchet@33192
    18
  structure NameTable : TABLE
blanchet@33192
    19
blanchet@33192
    20
  val univ_card :
blanchet@33192
    21
    int -> int -> int -> Kodkod.bound list -> Kodkod.formula -> int
blanchet@34121
    22
  val check_bits : int -> Kodkod.formula -> unit
blanchet@33192
    23
  val check_arity : int -> int -> unit
blanchet@33192
    24
  val kk_tuple : bool -> int -> int list -> Kodkod.tuple
blanchet@33192
    25
  val tuple_set_from_atom_schema : (int * int) list -> Kodkod.tuple_set
blanchet@33192
    26
  val sequential_int_bounds : int -> Kodkod.int_bound list
blanchet@34121
    27
  val pow_of_two_int_bounds : int -> int -> int -> Kodkod.int_bound list
blanchet@33192
    28
  val bounds_for_built_in_rels_in_formula :
blanchet@33192
    29
    bool -> int -> int -> int -> int -> Kodkod.formula -> Kodkod.bound list
blanchet@33192
    30
  val bound_for_plain_rel : Proof.context -> bool -> nut -> Kodkod.bound
blanchet@33192
    31
  val bound_for_sel_rel :
blanchet@33192
    32
    Proof.context -> bool -> dtype_spec list -> nut -> Kodkod.bound
blanchet@33192
    33
  val merge_bounds : Kodkod.bound list -> Kodkod.bound list
blanchet@33192
    34
  val declarative_axiom_for_plain_rel : kodkod_constrs -> nut -> Kodkod.formula
blanchet@33192
    35
  val declarative_axioms_for_datatypes :
blanchet@34121
    36
    extended_context -> int -> int Typtab.table -> kodkod_constrs
blanchet@33192
    37
    -> nut NameTable.table -> dtype_spec list -> Kodkod.formula list
blanchet@33192
    38
  val kodkod_formula_from_nut :
blanchet@34121
    39
    int -> int Typtab.table -> bool -> kodkod_constrs -> nut -> Kodkod.formula
blanchet@33192
    40
end;
blanchet@33192
    41
blanchet@33224
    42
structure Nitpick_Kodkod : NITPICK_KODKOD =
blanchet@33192
    43
struct
blanchet@33192
    44
blanchet@33224
    45
open Nitpick_Util
blanchet@33224
    46
open Nitpick_HOL
blanchet@33224
    47
open Nitpick_Scope
blanchet@33224
    48
open Nitpick_Peephole
blanchet@33224
    49
open Nitpick_Rep
blanchet@33224
    50
open Nitpick_Nut
blanchet@33192
    51
blanchet@34123
    52
structure KK = Kodkod
blanchet@34123
    53
blanchet@34123
    54
type nfa_transition = KK.rel_expr * typ
blanchet@33192
    55
type nfa_entry = typ * nfa_transition list
blanchet@33192
    56
type nfa_table = nfa_entry list
blanchet@33192
    57
blanchet@33192
    58
structure NfaGraph = Graph(type key = typ val ord = TermOrd.typ_ord)
blanchet@33192
    59
blanchet@34123
    60
(* int -> KK.int_expr list *)
blanchet@34123
    61
fun flip_nums n = index_seq 1 n @ [0] |> map KK.Num
blanchet@33192
    62
blanchet@34123
    63
(* int -> int -> int -> KK.bound list -> KK.formula -> int *)
blanchet@33192
    64
fun univ_card nat_card int_card main_j0 bounds formula =
blanchet@33192
    65
  let
blanchet@34123
    66
    (* KK.rel_expr -> int -> int *)
blanchet@33192
    67
    fun rel_expr_func r k =
blanchet@33192
    68
      Int.max (k, case r of
blanchet@34123
    69
                    KK.Atom j => j + 1
blanchet@34123
    70
                  | KK.AtomSeq (k', j0) => j0 + k'
blanchet@33192
    71
                  | _ => 0)
blanchet@34123
    72
    (* KK.tuple -> int -> int *)
blanchet@33192
    73
    fun tuple_func t k =
blanchet@33192
    74
      case t of
blanchet@34123
    75
        KK.Tuple js => fold Integer.max (map (Integer.add 1) js) k
blanchet@33192
    76
      | _ => k
blanchet@34123
    77
    (* KK.tuple_set -> int -> int *)
blanchet@33192
    78
    fun tuple_set_func ts k =
blanchet@34123
    79
      Int.max (k, case ts of KK.TupleAtomSeq (k', j0) => j0 + k' | _ => 0)
blanchet@33192
    80
    val expr_F = {formula_func = K I, rel_expr_func = rel_expr_func,
blanchet@33192
    81
                  int_expr_func = K I}
blanchet@33192
    82
    val tuple_F = {tuple_func = tuple_func, tuple_set_func = tuple_set_func}
blanchet@34123
    83
    val card = fold (KK.fold_bound expr_F tuple_F) bounds 1
blanchet@34123
    84
               |> KK.fold_formula expr_F formula
blanchet@33192
    85
  in Int.max (main_j0 + fold Integer.max [2, nat_card, int_card] 0, card) end
blanchet@33192
    86
blanchet@34123
    87
(* int -> KK.formula -> unit *)
blanchet@34121
    88
fun check_bits bits formula =
blanchet@34121
    89
  let
blanchet@34123
    90
    (* KK.int_expr -> unit -> unit *)
blanchet@34123
    91
    fun int_expr_func (KK.Num k) () =
blanchet@34121
    92
        if is_twos_complement_representable bits k then
blanchet@34121
    93
          ()
blanchet@34121
    94
        else
blanchet@34121
    95
          raise TOO_SMALL ("Nitpick_Kodkod.check_bits",
blanchet@34121
    96
                           "\"bits\" value " ^ string_of_int bits ^
blanchet@34121
    97
                           " too small for problem")
blanchet@34121
    98
      | int_expr_func _ () = ()
blanchet@34121
    99
    val expr_F = {formula_func = K I, rel_expr_func = K I,
blanchet@34121
   100
                  int_expr_func = int_expr_func}
blanchet@34123
   101
  in KK.fold_formula expr_F formula () end
blanchet@33192
   102
blanchet@33192
   103
(* int -> int -> unit *)
blanchet@33192
   104
fun check_arity univ_card n =
blanchet@34123
   105
  if n > KK.max_arity univ_card then
blanchet@34121
   106
    raise TOO_LARGE ("Nitpick_Kodkod.check_arity",
blanchet@34121
   107
                     "arity " ^ string_of_int n ^ " too large for universe of \
blanchet@34121
   108
                     \cardinality " ^ string_of_int univ_card)
blanchet@33192
   109
  else
blanchet@33192
   110
    ()
blanchet@33192
   111
blanchet@34123
   112
(* bool -> int -> int list -> KK.tuple *)
blanchet@33192
   113
fun kk_tuple debug univ_card js =
blanchet@33192
   114
  if debug then
blanchet@34123
   115
    KK.Tuple js
blanchet@33192
   116
  else
blanchet@34123
   117
    KK.TupleIndex (length js,
blanchet@34123
   118
                   fold (fn j => fn accum => accum * univ_card + j) js 0)
blanchet@33192
   119
blanchet@34123
   120
(* (int * int) list -> KK.tuple_set *)
blanchet@34123
   121
val tuple_set_from_atom_schema = foldl1 KK.TupleProduct o map KK.TupleAtomSeq
blanchet@34123
   122
(* rep -> KK.tuple_set *)
blanchet@33192
   123
val upper_bound_for_rep = tuple_set_from_atom_schema o atom_schema_of_rep
blanchet@33192
   124
blanchet@34123
   125
(* int -> KK.tuple_set *)
blanchet@34123
   126
val single_atom = KK.TupleSet o single o KK.Tuple o single
blanchet@34123
   127
(* int -> KK.int_bound list *)
blanchet@34121
   128
fun sequential_int_bounds n = [(NONE, map single_atom (index_seq 0 n))]
blanchet@34123
   129
(* int -> int -> KK.int_bound list *)
blanchet@34121
   130
fun pow_of_two_int_bounds bits j0 univ_card =
blanchet@34121
   131
  let
blanchet@34123
   132
    (* int -> int -> int -> KK.int_bound list *)
blanchet@34121
   133
    fun aux 0  _ _ = []
blanchet@34121
   134
      | aux 1 pow_of_two j =
blanchet@34121
   135
        if j < univ_card then [(SOME (~ pow_of_two), [single_atom j])] else []
blanchet@34121
   136
      | aux iter pow_of_two j =
blanchet@34121
   137
        (SOME pow_of_two, [single_atom j]) ::
blanchet@34121
   138
        aux (iter - 1) (2 * pow_of_two) (j + 1)
blanchet@34121
   139
  in aux (bits + 1) 1 j0 end
blanchet@33192
   140
blanchet@34123
   141
(* KK.formula -> KK.n_ary_index list *)
blanchet@33192
   142
fun built_in_rels_in_formula formula =
blanchet@33192
   143
  let
blanchet@34123
   144
    (* KK.rel_expr -> KK.n_ary_index list -> KK.n_ary_index list *)
blanchet@34123
   145
    fun rel_expr_func (r as KK.Rel (x as (n, j))) =
blanchet@34121
   146
        if x = unsigned_bit_word_sel_rel orelse x = signed_bit_word_sel_rel then
blanchet@34121
   147
          I
blanchet@34121
   148
        else
blanchet@34121
   149
          (case AList.lookup (op =) (#rels initial_pool) n of
blanchet@34121
   150
             SOME k => j < k ? insert (op =) x
blanchet@34121
   151
           | NONE => I)
blanchet@34121
   152
      | rel_expr_func _ = I
blanchet@33192
   153
    val expr_F = {formula_func = K I, rel_expr_func = rel_expr_func,
blanchet@33192
   154
                  int_expr_func = K I}
blanchet@34123
   155
  in KK.fold_formula expr_F formula [] end
blanchet@33192
   156
blanchet@33192
   157
val max_table_size = 65536
blanchet@33192
   158
blanchet@33192
   159
(* int -> unit *)
blanchet@33192
   160
fun check_table_size k =
blanchet@33192
   161
  if k > max_table_size then
blanchet@34121
   162
    raise TOO_LARGE ("Nitpick_Kodkod.check_table_size",
blanchet@34121
   163
                     "precomputed table too large (" ^ string_of_int k ^ ")")
blanchet@33192
   164
  else
blanchet@33192
   165
    ()
blanchet@33192
   166
blanchet@34123
   167
(* bool -> int -> int * int -> (int -> int) -> KK.tuple list *)
blanchet@33192
   168
fun tabulate_func1 debug univ_card (k, j0) f =
blanchet@33192
   169
  (check_table_size k;
blanchet@33192
   170
   map_filter (fn j1 => let val j2 = f j1 in
blanchet@33192
   171
                          if j2 >= 0 then
blanchet@33192
   172
                            SOME (kk_tuple debug univ_card [j1 + j0, j2 + j0])
blanchet@33192
   173
                          else
blanchet@33192
   174
                            NONE
blanchet@33192
   175
                        end) (index_seq 0 k))
blanchet@34123
   176
(* bool -> int -> int * int -> int -> (int * int -> int) -> KK.tuple list *)
blanchet@33192
   177
fun tabulate_op2 debug univ_card (k, j0) res_j0 f =
blanchet@33192
   178
  (check_table_size (k * k);
blanchet@33192
   179
   map_filter (fn j => let
blanchet@33192
   180
                         val j1 = j div k
blanchet@33192
   181
                         val j2 = j - j1 * k
blanchet@33192
   182
                         val j3 = f (j1, j2)
blanchet@33192
   183
                       in
blanchet@33192
   184
                         if j3 >= 0 then
blanchet@33192
   185
                           SOME (kk_tuple debug univ_card
blanchet@33192
   186
                                          [j1 + j0, j2 + j0, j3 + res_j0])
blanchet@33192
   187
                         else
blanchet@33192
   188
                           NONE
blanchet@33192
   189
                       end) (index_seq 0 (k * k)))
blanchet@33192
   190
(* bool -> int -> int * int -> int -> (int * int -> int * int)
blanchet@34123
   191
   -> KK.tuple list *)
blanchet@33192
   192
fun tabulate_op2_2 debug univ_card (k, j0) res_j0 f =
blanchet@33192
   193
  (check_table_size (k * k);
blanchet@33192
   194
   map_filter (fn j => let
blanchet@33192
   195
                         val j1 = j div k
blanchet@33192
   196
                         val j2 = j - j1 * k
blanchet@33192
   197
                         val (j3, j4) = f (j1, j2)
blanchet@33192
   198
                       in
blanchet@33192
   199
                         if j3 >= 0 andalso j4 >= 0 then
blanchet@33192
   200
                           SOME (kk_tuple debug univ_card
blanchet@33192
   201
                                          [j1 + j0, j2 + j0, j3 + res_j0,
blanchet@33192
   202
                                           j4 + res_j0])
blanchet@33192
   203
                         else
blanchet@33192
   204
                           NONE
blanchet@33192
   205
                       end) (index_seq 0 (k * k)))
blanchet@34123
   206
(* bool -> int -> int * int -> (int * int -> int) -> KK.tuple list *)
blanchet@33192
   207
fun tabulate_nat_op2 debug univ_card (k, j0) f =
blanchet@33192
   208
  tabulate_op2 debug univ_card (k, j0) j0 (atom_for_nat (k, 0) o f)
blanchet@33192
   209
fun tabulate_int_op2 debug univ_card (k, j0) f =
blanchet@33192
   210
  tabulate_op2 debug univ_card (k, j0) j0
blanchet@33192
   211
               (atom_for_int (k, 0) o f o pairself (int_for_atom (k, 0)))
blanchet@34123
   212
(* bool -> int -> int * int -> (int * int -> int * int) -> KK.tuple list *)
blanchet@33192
   213
fun tabulate_int_op2_2 debug univ_card (k, j0) f =
blanchet@33192
   214
  tabulate_op2_2 debug univ_card (k, j0) j0
blanchet@33192
   215
                 (pairself (atom_for_int (k, 0)) o f
blanchet@33192
   216
                  o pairself (int_for_atom (k, 0)))
blanchet@33192
   217
blanchet@33192
   218
(* int * int -> int *)
blanchet@33192
   219
fun isa_div (m, n) = m div n handle General.Div => 0
blanchet@33192
   220
fun isa_mod (m, n) = m mod n handle General.Div => m
blanchet@33192
   221
fun isa_gcd (m, 0) = m
blanchet@33192
   222
  | isa_gcd (m, n) = isa_gcd (n, isa_mod (m, n))
blanchet@33192
   223
fun isa_lcm (m, n) = isa_div (m * n, isa_gcd (m, n))
blanchet@33192
   224
val isa_zgcd = isa_gcd o pairself abs
blanchet@33192
   225
(* int * int -> int * int *)
blanchet@33192
   226
fun isa_norm_frac (m, n) =
blanchet@33192
   227
  if n < 0 then isa_norm_frac (~m, ~n)
blanchet@33192
   228
  else if m = 0 orelse n = 0 then (0, 1)
blanchet@33192
   229
  else let val p = isa_zgcd (m, n) in (isa_div (m, p), isa_div (n, p)) end
blanchet@33192
   230
blanchet@33192
   231
(* bool -> int -> int -> int -> int -> int * int
blanchet@34123
   232
   -> string * bool * KK.tuple list *)
blanchet@33192
   233
fun tabulate_built_in_rel debug univ_card nat_card int_card j0 (x as (n, _)) =
blanchet@33192
   234
  (check_arity univ_card n;
blanchet@34121
   235
   if x = not3_rel then
blanchet@33192
   236
     ("not3", tabulate_func1 debug univ_card (2, j0) (curry (op -) 1))
blanchet@34121
   237
   else if x = suc_rel then
blanchet@33192
   238
     ("suc", tabulate_func1 debug univ_card (univ_card - j0 - 1, j0)
blanchet@33192
   239
                            (Integer.add 1))
blanchet@34121
   240
   else if x = nat_add_rel then
blanchet@33192
   241
     ("nat_add", tabulate_nat_op2 debug univ_card (nat_card, j0) (op +))
blanchet@34121
   242
   else if x = int_add_rel then
blanchet@33192
   243
     ("int_add", tabulate_int_op2 debug univ_card (int_card, j0) (op +))
blanchet@34121
   244
   else if x = nat_subtract_rel then
blanchet@33192
   245
     ("nat_subtract",
blanchet@33705
   246
      tabulate_op2 debug univ_card (nat_card, j0) j0 (uncurry nat_minus))
blanchet@34121
   247
   else if x = int_subtract_rel then
blanchet@33192
   248
     ("int_subtract", tabulate_int_op2 debug univ_card (int_card, j0) (op -))
blanchet@34121
   249
   else if x = nat_multiply_rel then
blanchet@33192
   250
     ("nat_multiply", tabulate_nat_op2 debug univ_card (nat_card, j0) (op * ))
blanchet@34121
   251
   else if x = int_multiply_rel then
blanchet@33192
   252
     ("int_multiply", tabulate_int_op2 debug univ_card (int_card, j0) (op * ))
blanchet@34121
   253
   else if x = nat_divide_rel then
blanchet@33192
   254
     ("nat_divide", tabulate_nat_op2 debug univ_card (nat_card, j0) isa_div)
blanchet@34121
   255
   else if x = int_divide_rel then
blanchet@33192
   256
     ("int_divide", tabulate_int_op2 debug univ_card (int_card, j0) isa_div)
blanchet@34121
   257
   else if x = nat_less_rel then
blanchet@33192
   258
     ("nat_less", tabulate_nat_op2 debug univ_card (nat_card, j0)
blanchet@33192
   259
                                   (int_for_bool o op <))
blanchet@34121
   260
   else if x = int_less_rel then
blanchet@33192
   261
     ("int_less", tabulate_int_op2 debug univ_card (int_card, j0)
blanchet@33192
   262
                                   (int_for_bool o op <))
blanchet@34121
   263
   else if x = gcd_rel then
blanchet@33192
   264
     ("gcd", tabulate_nat_op2 debug univ_card (nat_card, j0) isa_gcd)
blanchet@34121
   265
   else if x = lcm_rel then
blanchet@33192
   266
     ("lcm", tabulate_nat_op2 debug univ_card (nat_card, j0) isa_lcm)
blanchet@34121
   267
   else if x = norm_frac_rel then
blanchet@33192
   268
     ("norm_frac", tabulate_int_op2_2 debug univ_card (int_card, j0)
blanchet@33192
   269
                                      isa_norm_frac)
blanchet@33192
   270
   else
blanchet@33224
   271
     raise ARG ("Nitpick_Kodkod.tabulate_built_in_rel", "unknown relation"))
blanchet@33192
   272
blanchet@34123
   273
(* bool -> int -> int -> int -> int -> int * int -> KK.rel_expr -> KK.bound *)
blanchet@33192
   274
fun bound_for_built_in_rel debug univ_card nat_card int_card j0 x =
blanchet@33192
   275
  let
blanchet@33192
   276
    val (nick, ts) = tabulate_built_in_rel debug univ_card nat_card int_card
blanchet@33192
   277
                                           j0 x
blanchet@34123
   278
  in ([(x, nick)], [KK.TupleSet ts]) end
blanchet@33192
   279
blanchet@34123
   280
(* bool -> int -> int -> int -> int -> KK.formula -> KK.bound list *)
blanchet@33192
   281
fun bounds_for_built_in_rels_in_formula debug univ_card nat_card int_card j0 =
blanchet@33192
   282
  map (bound_for_built_in_rel debug univ_card nat_card int_card j0)
blanchet@33192
   283
  o built_in_rels_in_formula
blanchet@33192
   284
blanchet@34121
   285
(* Proof.context -> bool -> string -> typ -> rep -> string *)
blanchet@34121
   286
fun bound_comment ctxt debug nick T R =
blanchet@34121
   287
  short_name nick ^
blanchet@34121
   288
  (if debug then " :: " ^ plain_string_from_yxml (Syntax.string_of_typ ctxt T)
blanchet@34121
   289
   else "") ^ " : " ^ string_for_rep R
blanchet@34121
   290
blanchet@34123
   291
(* Proof.context -> bool -> nut -> KK.bound *)
blanchet@33192
   292
fun bound_for_plain_rel ctxt debug (u as FreeRel (x, T, R, nick)) =
blanchet@33192
   293
    ([(x, bound_comment ctxt debug nick T R)],
blanchet@33192
   294
     if nick = @{const_name bisim_iterator_max} then
blanchet@33192
   295
       case R of
blanchet@34121
   296
         Atom (k, j0) => [single_atom (k - 1 + j0)]
blanchet@33224
   297
       | _ => raise NUT ("Nitpick_Kodkod.bound_for_plain_rel", [u])
blanchet@33192
   298
     else
blanchet@34123
   299
       [KK.TupleSet [], upper_bound_for_rep R])
blanchet@33192
   300
  | bound_for_plain_rel _ _ u =
blanchet@33224
   301
    raise NUT ("Nitpick_Kodkod.bound_for_plain_rel", [u])
blanchet@33192
   302
blanchet@34123
   303
(* Proof.context -> bool -> dtype_spec list -> nut -> KK.bound *)
blanchet@33192
   304
fun bound_for_sel_rel ctxt debug dtypes
blanchet@33192
   305
        (FreeRel (x, T as Type ("fun", [T1, T2]), R as Func (Atom (_, j0), R2),
blanchet@33192
   306
                  nick)) =
blanchet@33192
   307
    let
blanchet@33192
   308
      val constr as {delta, epsilon, exclusive, explicit_max, ...} =
blanchet@33192
   309
        constr_spec dtypes (original_name nick, T1)
blanchet@33192
   310
    in
blanchet@33192
   311
      ([(x, bound_comment ctxt debug nick T R)],
blanchet@33192
   312
       if explicit_max = 0 then
blanchet@34123
   313
         [KK.TupleSet []]
blanchet@33192
   314
       else
blanchet@34123
   315
         let val ts = KK.TupleAtomSeq (epsilon - delta, delta + j0) in
blanchet@33192
   316
           if R2 = Formula Neut then
blanchet@34123
   317
             [ts] |> not exclusive ? cons (KK.TupleSet [])
blanchet@33192
   318
           else
blanchet@34123
   319
             [KK.TupleSet [], KK.TupleProduct (ts, upper_bound_for_rep R2)]
blanchet@33192
   320
         end)
blanchet@33192
   321
    end
blanchet@33192
   322
  | bound_for_sel_rel _ _ _ u =
blanchet@33224
   323
    raise NUT ("Nitpick_Kodkod.bound_for_sel_rel", [u])
blanchet@33192
   324
blanchet@34123
   325
(* KK.bound list -> KK.bound list *)
blanchet@33192
   326
fun merge_bounds bs =
blanchet@33192
   327
  let
blanchet@34123
   328
    (* KK.bound -> int *)
blanchet@33192
   329
    fun arity (zs, _) = fst (fst (hd zs))
blanchet@34123
   330
    (* KK.bound list -> KK.bound -> KK.bound list -> KK.bound list *)
blanchet@33192
   331
    fun add_bound ds b [] = List.revAppend (ds, [b])
blanchet@33192
   332
      | add_bound ds b (c :: cs) =
blanchet@33192
   333
        if arity b = arity c andalso snd b = snd c then
blanchet@33192
   334
          List.revAppend (ds, (fst c @ fst b, snd c) :: cs)
blanchet@33192
   335
        else
blanchet@33192
   336
          add_bound (c :: ds) b cs
blanchet@33192
   337
  in fold (add_bound []) bs [] end
blanchet@33192
   338
blanchet@34123
   339
(* int -> int -> KK.rel_expr list *)
blanchet@34123
   340
fun unary_var_seq j0 n = map (curry KK.Var 1) (index_seq j0 n)
blanchet@33192
   341
blanchet@34123
   342
(* int list -> KK.rel_expr *)
blanchet@34123
   343
val singleton_from_combination = foldl1 KK.Product o map KK.Atom
blanchet@34123
   344
(* rep -> KK.rel_expr list *)
blanchet@33192
   345
fun all_singletons_for_rep R =
blanchet@33192
   346
  if is_lone_rep R then
blanchet@33192
   347
    all_combinations_for_rep R |> map singleton_from_combination
blanchet@33192
   348
  else
blanchet@33224
   349
    raise REP ("Nitpick_Kodkod.all_singletons_for_rep", [R])
blanchet@33192
   350
blanchet@34123
   351
(* KK.rel_expr -> KK.rel_expr list *)
blanchet@34123
   352
fun unpack_products (KK.Product (r1, r2)) =
blanchet@33192
   353
    unpack_products r1 @ unpack_products r2
blanchet@33192
   354
  | unpack_products r = [r]
blanchet@34123
   355
fun unpack_joins (KK.Join (r1, r2)) = unpack_joins r1 @ unpack_joins r2
blanchet@33192
   356
  | unpack_joins r = [r]
blanchet@33192
   357
blanchet@34123
   358
(* rep -> KK.rel_expr *)
blanchet@33192
   359
val empty_rel_for_rep = empty_n_ary_rel o arity_of_rep
blanchet@33192
   360
fun full_rel_for_rep R =
blanchet@33192
   361
  case atom_schema_of_rep R of
blanchet@33224
   362
    [] => raise REP ("Nitpick_Kodkod.full_rel_for_rep", [R])
blanchet@34123
   363
  | schema => foldl1 KK.Product (map KK.AtomSeq schema)
blanchet@33192
   364
blanchet@34123
   365
(* int -> int list -> KK.decl list *)
blanchet@33192
   366
fun decls_for_atom_schema j0 schema =
blanchet@34123
   367
  map2 (fn j => fn x => KK.DeclOne ((1, j), KK.AtomSeq x))
blanchet@33192
   368
       (index_seq j0 (length schema)) schema
blanchet@33192
   369
blanchet@33192
   370
(* The type constraint below is a workaround for a Poly/ML bug. *)
blanchet@33192
   371
blanchet@34123
   372
(* kodkod_constrs -> rep -> KK.rel_expr -> KK.formula *)
blanchet@33192
   373
fun d_n_ary_function ({kk_all, kk_join, kk_lone, kk_one, ...} : kodkod_constrs)
blanchet@33192
   374
                     R r =
blanchet@33192
   375
  let val body_R = body_rep R in
blanchet@33192
   376
    if is_lone_rep body_R then
blanchet@33192
   377
      let
blanchet@33192
   378
        val binder_schema = atom_schema_of_reps (binder_reps R)
blanchet@33192
   379
        val body_schema = atom_schema_of_rep body_R
blanchet@33192
   380
        val one = is_one_rep body_R
blanchet@34123
   381
        val opt_x = case r of KK.Rel x => SOME x | _ => NONE
blanchet@33192
   382
      in
blanchet@33192
   383
        if opt_x <> NONE andalso length binder_schema = 1
blanchet@33192
   384
           andalso length body_schema = 1 then
blanchet@34123
   385
          (if one then KK.Function else KK.Functional)
blanchet@34123
   386
              (the opt_x, KK.AtomSeq (hd binder_schema),
blanchet@34123
   387
               KK.AtomSeq (hd body_schema))
blanchet@33192
   388
        else
blanchet@33192
   389
          let
blanchet@33192
   390
            val decls = decls_for_atom_schema ~1 binder_schema
blanchet@33192
   391
            val vars = unary_var_seq ~1 (length binder_schema)
blanchet@33192
   392
            val kk_xone = if one then kk_one else kk_lone
blanchet@33192
   393
          in kk_all decls (kk_xone (fold kk_join vars r)) end
blanchet@33192
   394
      end
blanchet@33192
   395
    else
blanchet@34123
   396
      KK.True
blanchet@33192
   397
  end
blanchet@34123
   398
fun kk_n_ary_function kk R (r as KK.Rel x) =
blanchet@33192
   399
    if not (is_opt_rep R) then
blanchet@34121
   400
      if x = suc_rel then
blanchet@34123
   401
        KK.False
blanchet@34121
   402
      else if x = nat_add_rel then
blanchet@33192
   403
        formula_for_bool (card_of_rep (body_rep R) = 1)
blanchet@34121
   404
      else if x = nat_multiply_rel then
blanchet@33192
   405
        formula_for_bool (card_of_rep (body_rep R) <= 2)
blanchet@33192
   406
      else
blanchet@33192
   407
        d_n_ary_function kk R r
blanchet@34121
   408
    else if x = nat_subtract_rel then
blanchet@34123
   409
      KK.True
blanchet@33192
   410
    else
blanchet@33192
   411
      d_n_ary_function kk R r
blanchet@33192
   412
  | kk_n_ary_function kk R r = d_n_ary_function kk R r
blanchet@33192
   413
blanchet@34123
   414
(* kodkod_constrs -> KK.rel_expr list -> KK.formula *)
blanchet@34123
   415
fun kk_disjoint_sets _ [] = KK.True
blanchet@33192
   416
  | kk_disjoint_sets (kk as {kk_and, kk_no, kk_intersect, ...} : kodkod_constrs)
blanchet@33192
   417
                     (r :: rs) =
blanchet@33192
   418
    fold (kk_and o kk_no o kk_intersect r) rs (kk_disjoint_sets kk rs)
blanchet@33192
   419
blanchet@34123
   420
(* int -> kodkod_constrs -> (KK.rel_expr -> KK.rel_expr) -> KK.rel_expr
blanchet@34123
   421
   -> KK.rel_expr *)
blanchet@34121
   422
fun basic_rel_rel_let j ({kk_rel_let, ...} : kodkod_constrs) f r =
blanchet@33192
   423
  if inline_rel_expr r then
blanchet@33192
   424
    f r
blanchet@33192
   425
  else
blanchet@34123
   426
    let val x = (KK.arity_of_rel_expr r, j) in
blanchet@34123
   427
      kk_rel_let [KK.AssignRelReg (x, r)] (f (KK.RelReg x))
blanchet@33192
   428
    end
blanchet@34123
   429
(* kodkod_constrs -> (KK.rel_expr -> KK.rel_expr) -> KK.rel_expr
blanchet@34123
   430
   -> KK.rel_expr *)
blanchet@34121
   431
val single_rel_rel_let = basic_rel_rel_let 0
blanchet@34123
   432
(* kodkod_constrs -> (KK.rel_expr -> KK.rel_expr -> KK.rel_expr) -> KK.rel_expr
blanchet@34123
   433
   -> KK.rel_expr -> KK.rel_expr *)
blanchet@34121
   434
fun double_rel_rel_let kk f r1 r2 =
blanchet@34121
   435
  single_rel_rel_let kk (fn r1 => basic_rel_rel_let 1 kk (f r1) r2) r1
blanchet@34123
   436
(* kodkod_constrs -> (KK.rel_expr -> KK.rel_expr -> KK.rel_expr -> KK.rel_expr)
blanchet@34123
   437
   -> KK.rel_expr -> KK.rel_expr -> KK.rel_expr -> KK.rel_expr *)
blanchet@34121
   438
fun tripl_rel_rel_let kk f r1 r2 r3 =
blanchet@34121
   439
  double_rel_rel_let kk
blanchet@34121
   440
      (fn r1 => fn r2 => basic_rel_rel_let 2 kk (f r1 r2) r3) r1 r2
blanchet@33192
   441
blanchet@34123
   442
(* kodkod_constrs -> int -> KK.formula -> KK.rel_expr *)
blanchet@33192
   443
fun atom_from_formula ({kk_rel_if, ...} : kodkod_constrs) j0 f =
blanchet@34123
   444
  kk_rel_if f (KK.Atom (j0 + 1)) (KK.Atom j0)
blanchet@34123
   445
(* kodkod_constrs -> rep -> KK.formula -> KK.rel_expr *)
blanchet@33192
   446
fun rel_expr_from_formula kk R f =
blanchet@33192
   447
  case unopt_rep R of
blanchet@33192
   448
    Atom (2, j0) => atom_from_formula kk j0 f
blanchet@33224
   449
  | _ => raise REP ("Nitpick_Kodkod.rel_expr_from_formula", [R])
blanchet@33192
   450
blanchet@34123
   451
(* kodkod_cotrs -> int -> int -> KK.rel_expr -> KK.rel_expr list *)
blanchet@33192
   452
fun unpack_vect_in_chunks ({kk_project_seq, ...} : kodkod_constrs) chunk_arity
blanchet@33192
   453
                          num_chunks r =
blanchet@33192
   454
  List.tabulate (num_chunks, fn j => kk_project_seq r (j * chunk_arity)
blanchet@33192
   455
                                                    chunk_arity)
blanchet@33192
   456
blanchet@34123
   457
(* kodkod_constrs -> bool -> rep -> rep -> KK.rel_expr -> KK.rel_expr
blanchet@34123
   458
   -> KK.rel_expr *)
blanchet@33192
   459
fun kk_n_fold_join
blanchet@33192
   460
        (kk as {kk_intersect, kk_product, kk_join, kk_project_seq, ...}) one R1
blanchet@33192
   461
        res_R r1 r2 =
blanchet@33192
   462
  case arity_of_rep R1 of
blanchet@33192
   463
    1 => kk_join r1 r2
blanchet@33192
   464
  | arity1 =>
blanchet@33192
   465
    let
blanchet@33192
   466
      val unpacked_rs1 =
blanchet@33192
   467
        if inline_rel_expr r1 then unpack_vect_in_chunks kk 1 arity1 r1
blanchet@33192
   468
        else unpack_products r1
blanchet@33192
   469
    in
blanchet@33192
   470
      if one andalso length unpacked_rs1 = arity1 then
blanchet@33192
   471
        fold kk_join unpacked_rs1 r2
blanchet@33192
   472
      else
blanchet@33192
   473
        kk_project_seq
blanchet@33192
   474
            (kk_intersect (kk_product r1 (full_rel_for_rep res_R)) r2)
blanchet@33192
   475
            arity1 (arity_of_rep res_R)
blanchet@33192
   476
    end
blanchet@33192
   477
blanchet@34123
   478
(* kodkod_constrs -> rep -> rep -> KK.rel_expr -> KK.rel_expr list
blanchet@34123
   479
   -> KK.rel_expr list -> KK.rel_expr *)
blanchet@33192
   480
fun kk_case_switch (kk as {kk_union, kk_product, ...}) R1 R2 r rs1 rs2 =
blanchet@33192
   481
  if rs1 = rs2 then r
blanchet@33192
   482
  else kk_n_fold_join kk true R1 R2 r (fold1 kk_union (map2 kk_product rs1 rs2))
blanchet@33192
   483
blanchet@33192
   484
val lone_rep_fallback_max_card = 4096
blanchet@33192
   485
val some_j0 = 0
blanchet@33192
   486
blanchet@34123
   487
(* kodkod_constrs -> rep -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   488
fun lone_rep_fallback kk new_R old_R r =
blanchet@33192
   489
  if old_R = new_R then
blanchet@33192
   490
    r
blanchet@33192
   491
  else
blanchet@33192
   492
    let val card = card_of_rep old_R in
blanchet@33192
   493
      if is_lone_rep old_R andalso is_lone_rep new_R
blanchet@33192
   494
         andalso card = card_of_rep new_R then
blanchet@33192
   495
        if card >= lone_rep_fallback_max_card then
blanchet@34121
   496
          raise TOO_LARGE ("Nitpick_Kodkod.lone_rep_fallback",
blanchet@34121
   497
                           "too high cardinality (" ^ string_of_int card ^ ")")
blanchet@33192
   498
        else
blanchet@33192
   499
          kk_case_switch kk old_R new_R r (all_singletons_for_rep old_R)
blanchet@33192
   500
                         (all_singletons_for_rep new_R)
blanchet@33192
   501
      else
blanchet@33224
   502
        raise REP ("Nitpick_Kodkod.lone_rep_fallback", [old_R, new_R])
blanchet@33192
   503
    end
blanchet@34123
   504
(* kodkod_constrs -> int * int -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   505
and atom_from_rel_expr kk (x as (k, j0)) old_R r =
blanchet@33192
   506
  case old_R of
blanchet@33192
   507
    Func (R1, R2) =>
blanchet@33192
   508
    let
blanchet@33192
   509
      val dom_card = card_of_rep R1
blanchet@33192
   510
      val R2' = case R2 of Atom _ => R2 | _ => Atom (card_of_rep R2, some_j0)
blanchet@33192
   511
    in
blanchet@33192
   512
      atom_from_rel_expr kk x (Vect (dom_card, R2'))
blanchet@33192
   513
                         (vect_from_rel_expr kk dom_card R2' old_R r)
blanchet@33192
   514
    end
blanchet@33224
   515
  | Opt _ => raise REP ("Nitpick_Kodkod.atom_from_rel_expr", [old_R])
blanchet@33192
   516
  | _ => lone_rep_fallback kk (Atom x) old_R r
blanchet@34123
   517
(* kodkod_constrs -> rep list -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   518
and struct_from_rel_expr kk Rs old_R r =
blanchet@33192
   519
  case old_R of
blanchet@33192
   520
    Atom _ => lone_rep_fallback kk (Struct Rs) old_R r
blanchet@33192
   521
  | Struct Rs' =>
blanchet@33192
   522
    let
blanchet@33192
   523
      val Rs = filter (not_equal Unit) Rs
blanchet@33192
   524
      val Rs' = filter (not_equal Unit) Rs'
blanchet@33192
   525
    in
blanchet@33192
   526
      if Rs' = Rs then
blanchet@33192
   527
        r
blanchet@33192
   528
      else if map card_of_rep Rs' = map card_of_rep Rs then
blanchet@33192
   529
        let
blanchet@33192
   530
          val old_arities = map arity_of_rep Rs'
blanchet@33192
   531
          val old_offsets = offset_list old_arities
blanchet@33192
   532
          val old_rs = map2 (#kk_project_seq kk r) old_offsets old_arities
blanchet@33192
   533
        in
blanchet@33192
   534
          fold1 (#kk_product kk)
blanchet@33192
   535
                (map3 (rel_expr_from_rel_expr kk) Rs Rs' old_rs)
blanchet@33192
   536
        end
blanchet@33192
   537
      else
blanchet@33192
   538
        lone_rep_fallback kk (Struct Rs) old_R r
blanchet@33192
   539
    end
blanchet@33224
   540
  | _ => raise REP ("Nitpick_Kodkod.struct_from_rel_expr", [old_R])
blanchet@34123
   541
(* kodkod_constrs -> int -> rep -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   542
and vect_from_rel_expr kk k R old_R r =
blanchet@33192
   543
  case old_R of
blanchet@33192
   544
    Atom _ => lone_rep_fallback kk (Vect (k, R)) old_R r
blanchet@33192
   545
  | Vect (k', R') =>
blanchet@33192
   546
    if k = k' andalso R = R' then r
blanchet@33192
   547
    else lone_rep_fallback kk (Vect (k, R)) old_R r
blanchet@33192
   548
  | Func (R1, Formula Neut) =>
blanchet@33192
   549
    if k = card_of_rep R1 then
blanchet@33192
   550
      fold1 (#kk_product kk)
blanchet@33192
   551
            (map (fn arg_r =>
blanchet@33192
   552
                     rel_expr_from_formula kk R (#kk_subset kk arg_r r))
blanchet@33192
   553
                 (all_singletons_for_rep R1))
blanchet@33192
   554
    else
blanchet@33224
   555
      raise REP ("Nitpick_Kodkod.vect_from_rel_expr", [old_R])
blanchet@33192
   556
  | Func (Unit, R2) => rel_expr_from_rel_expr kk R R2 r
blanchet@33192
   557
  | Func (R1, R2) =>
blanchet@33192
   558
    fold1 (#kk_product kk)
blanchet@33192
   559
          (map (fn arg_r =>
blanchet@33192
   560
                   rel_expr_from_rel_expr kk R R2
blanchet@33192
   561
                                         (kk_n_fold_join kk true R1 R2 arg_r r))
blanchet@33192
   562
               (all_singletons_for_rep R1))
blanchet@33224
   563
  | _ => raise REP ("Nitpick_Kodkod.vect_from_rel_expr", [old_R])
blanchet@34123
   564
(* kodkod_constrs -> rep -> rep -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   565
and func_from_no_opt_rel_expr kk R1 R2 (Atom x) r =
blanchet@33192
   566
    let
blanchet@33192
   567
      val dom_card = card_of_rep R1
blanchet@33192
   568
      val R2' = case R2 of Atom _ => R2 | _ => Atom (card_of_rep R2, some_j0)
blanchet@33192
   569
    in
blanchet@33192
   570
      func_from_no_opt_rel_expr kk R1 R2 (Vect (dom_card, R2'))
blanchet@33192
   571
                                (vect_from_rel_expr kk dom_card R2' (Atom x) r)
blanchet@33192
   572
    end
blanchet@33192
   573
  | func_from_no_opt_rel_expr kk Unit R2 old_R r =
blanchet@33192
   574
    (case old_R of
blanchet@33192
   575
       Vect (k, R') => rel_expr_from_rel_expr kk R2 R' r
blanchet@33192
   576
     | Func (Unit, R2') => rel_expr_from_rel_expr kk R2 R2' r
blanchet@33192
   577
     | Func (Atom (1, _), Formula Neut) =>
blanchet@33192
   578
       (case unopt_rep R2 of
blanchet@33192
   579
          Atom (2, j0) => atom_from_formula kk j0 (#kk_some kk r)
blanchet@33224
   580
        | _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr",
blanchet@33192
   581
                          [old_R, Func (Unit, R2)]))
blanchet@33192
   582
     | Func (R1', R2') =>
blanchet@33192
   583
       rel_expr_from_rel_expr kk R2 R2' (#kk_project_seq kk r (arity_of_rep R1')
blanchet@33192
   584
                              (arity_of_rep R2'))
blanchet@33224
   585
     | _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr",
blanchet@33192
   586
                       [old_R, Func (Unit, R2)]))
blanchet@33192
   587
  | func_from_no_opt_rel_expr kk R1 (Formula Neut) old_R r =
blanchet@33192
   588
    (case old_R of
blanchet@33192
   589
       Vect (k, Atom (2, j0)) =>
blanchet@33192
   590
       let
blanchet@33192
   591
         val args_rs = all_singletons_for_rep R1
blanchet@33192
   592
         val vals_rs = unpack_vect_in_chunks kk 1 k r
blanchet@34123
   593
         (* KK.rel_expr -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   594
         fun empty_or_singleton_set_for arg_r val_r =
blanchet@34123
   595
           #kk_join kk val_r (#kk_product kk (KK.Atom (j0 + 1)) arg_r)
blanchet@33192
   596
       in
blanchet@33192
   597
         fold1 (#kk_union kk) (map2 empty_or_singleton_set_for args_rs vals_rs)
blanchet@33192
   598
       end
blanchet@33192
   599
     | Func (R1', Formula Neut) =>
blanchet@33192
   600
       if R1 = R1' then
blanchet@33192
   601
         r
blanchet@33192
   602
       else
blanchet@33192
   603
         let
blanchet@33192
   604
           val schema = atom_schema_of_rep R1
blanchet@33192
   605
           val r1 = fold1 (#kk_product kk) (unary_var_seq ~1 (length schema))
blanchet@33192
   606
                    |> rel_expr_from_rel_expr kk R1' R1
blanchet@33573
   607
           val kk_xeq = (if is_one_rep R1' then #kk_subset else #kk_rel_eq) kk
blanchet@33192
   608
         in
blanchet@33573
   609
           #kk_comprehension kk (decls_for_atom_schema ~1 schema) (kk_xeq r1 r)
blanchet@33192
   610
         end
blanchet@33192
   611
     | Func (Unit, (Atom (2, j0))) =>
blanchet@34123
   612
       #kk_rel_if kk (#kk_rel_eq kk r (KK.Atom (j0 + 1)))
blanchet@33192
   613
                  (full_rel_for_rep R1) (empty_rel_for_rep R1)
blanchet@33192
   614
     | Func (R1', Atom (2, j0)) =>
blanchet@33192
   615
       func_from_no_opt_rel_expr kk R1 (Formula Neut)
blanchet@34123
   616
           (Func (R1', Formula Neut)) (#kk_join kk r (KK.Atom (j0 + 1)))
blanchet@33224
   617
     | _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr",
blanchet@33192
   618
                       [old_R, Func (R1, Formula Neut)]))
blanchet@33192
   619
  | func_from_no_opt_rel_expr kk R1 R2 old_R r =
blanchet@33192
   620
    case old_R of
blanchet@33192
   621
      Vect (k, R) =>
blanchet@33192
   622
      let
blanchet@33192
   623
        val args_rs = all_singletons_for_rep R1
blanchet@33192
   624
        val vals_rs = unpack_vect_in_chunks kk (arity_of_rep R) k r
blanchet@33192
   625
                      |> map (rel_expr_from_rel_expr kk R2 R)
blanchet@33192
   626
      in fold1 (#kk_union kk) (map2 (#kk_product kk) args_rs vals_rs) end
blanchet@33192
   627
    | Func (R1', Formula Neut) =>
blanchet@33192
   628
      (case R2 of
blanchet@33192
   629
         Atom (x as (2, j0)) =>
blanchet@33192
   630
         let val schema = atom_schema_of_rep R1 in
blanchet@33192
   631
           if length schema = 1 then
blanchet@34123
   632
             #kk_override kk (#kk_product kk (KK.AtomSeq (hd schema))
blanchet@34123
   633
                                             (KK.Atom j0))
blanchet@34123
   634
                             (#kk_product kk r (KK.Atom (j0 + 1)))
blanchet@33192
   635
           else
blanchet@33192
   636
             let
blanchet@33192
   637
               val r1 = fold1 (#kk_product kk) (unary_var_seq ~1 (length schema))
blanchet@33192
   638
                        |> rel_expr_from_rel_expr kk R1' R1
blanchet@34123
   639
               val r2 = KK.Var (1, ~(length schema) - 1)
blanchet@33192
   640
               val r3 = atom_from_formula kk j0 (#kk_subset kk r1 r)
blanchet@33192
   641
             in
blanchet@33192
   642
               #kk_comprehension kk (decls_for_atom_schema ~1 (schema @ [x]))
blanchet@33573
   643
                                 (#kk_subset kk r2 r3)
blanchet@33192
   644
             end
blanchet@33192
   645
           end
blanchet@33224
   646
         | _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr",
blanchet@33192
   647
                           [old_R, Func (R1, R2)]))
blanchet@33192
   648
    | Func (Unit, R2') =>
blanchet@33192
   649
      let val j0 = some_j0 in
blanchet@33192
   650
        func_from_no_opt_rel_expr kk R1 R2 (Func (Atom (1, j0), R2'))
blanchet@34123
   651
                                  (#kk_product kk (KK.Atom j0) r)
blanchet@33192
   652
      end
blanchet@33192
   653
    | Func (R1', R2') =>
blanchet@33192
   654
      if R1 = R1' andalso R2 = R2' then
blanchet@33192
   655
        r
blanchet@33192
   656
      else
blanchet@33192
   657
        let
blanchet@33192
   658
          val dom_schema = atom_schema_of_rep R1
blanchet@33192
   659
          val ran_schema = atom_schema_of_rep R2
blanchet@33192
   660
          val dom_prod = fold1 (#kk_product kk)
blanchet@33192
   661
                               (unary_var_seq ~1 (length dom_schema))
blanchet@33192
   662
                         |> rel_expr_from_rel_expr kk R1' R1
blanchet@33192
   663
          val ran_prod = fold1 (#kk_product kk)
blanchet@33192
   664
                               (unary_var_seq (~(length dom_schema) - 1)
blanchet@33192
   665
                                              (length ran_schema))
blanchet@33192
   666
                         |> rel_expr_from_rel_expr kk R2' R2
blanchet@33192
   667
          val app = kk_n_fold_join kk true R1' R2' dom_prod r
blanchet@33573
   668
          val kk_xeq = (if is_one_rep R2' then #kk_subset else #kk_rel_eq) kk
blanchet@33192
   669
        in
blanchet@33192
   670
          #kk_comprehension kk (decls_for_atom_schema ~1
blanchet@33192
   671
                                                      (dom_schema @ ran_schema))
blanchet@33573
   672
                               (kk_xeq ran_prod app)
blanchet@33192
   673
        end
blanchet@33224
   674
    | _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr",
blanchet@33192
   675
                      [old_R, Func (R1, R2)])
blanchet@34123
   676
(* kodkod_constrs -> rep -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   677
and rel_expr_from_rel_expr kk new_R old_R r =
blanchet@33192
   678
  let
blanchet@33192
   679
    val unopt_old_R = unopt_rep old_R
blanchet@33192
   680
    val unopt_new_R = unopt_rep new_R
blanchet@33192
   681
  in
blanchet@33192
   682
    if unopt_old_R <> old_R andalso unopt_new_R = new_R then
blanchet@33224
   683
      raise REP ("Nitpick_Kodkod.rel_expr_from_rel_expr", [old_R, new_R])
blanchet@33192
   684
    else if unopt_new_R = unopt_old_R then
blanchet@33192
   685
      r
blanchet@33192
   686
    else
blanchet@33192
   687
      (case unopt_new_R of
blanchet@33192
   688
         Atom x => atom_from_rel_expr kk x
blanchet@33192
   689
       | Struct Rs => struct_from_rel_expr kk Rs
blanchet@33192
   690
       | Vect (k, R') => vect_from_rel_expr kk k R'
blanchet@33192
   691
       | Func (R1, R2) => func_from_no_opt_rel_expr kk R1 R2
blanchet@33224
   692
       | _ => raise REP ("Nitpick_Kodkod.rel_expr_from_rel_expr",
blanchet@33192
   693
                         [old_R, new_R]))
blanchet@33192
   694
          unopt_old_R r
blanchet@33192
   695
  end
blanchet@34123
   696
(* kodkod_constrs -> rep -> rep -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   697
and rel_expr_to_func kk R1 R2 = rel_expr_from_rel_expr kk (Func (R1, R2))
blanchet@33192
   698
blanchet@34123
   699
(* kodkod_constrs -> typ -> KK.rel_expr -> KK.rel_expr *)
blanchet@34121
   700
fun bit_set_from_atom ({kk_join, ...} : kodkod_constrs) T r =
blanchet@34123
   701
  kk_join r (KK.Rel (if T = @{typ "unsigned_bit word"} then
blanchet@34123
   702
                       unsigned_bit_word_sel_rel
blanchet@34123
   703
                     else
blanchet@34123
   704
                       signed_bit_word_sel_rel))
blanchet@34123
   705
(* kodkod_constrs -> typ -> KK.rel_expr -> KK.int_expr *)
blanchet@34123
   706
val int_expr_from_atom = KK.SetSum ooo bit_set_from_atom
blanchet@34123
   707
(* kodkod_constrs -> typ -> rep -> KK.int_expr -> KK.rel_expr *)
blanchet@34121
   708
fun atom_from_int_expr (kk as {kk_rel_eq, kk_comprehension, ...}
blanchet@34121
   709
                        : kodkod_constrs) T R i =
blanchet@34121
   710
  kk_comprehension (decls_for_atom_schema ~1 (atom_schema_of_rep R))
blanchet@34123
   711
                   (kk_rel_eq (bit_set_from_atom kk T (KK.Var (1, ~1)))
blanchet@34123
   712
                              (KK.Bits i))
blanchet@34121
   713
blanchet@34123
   714
(* kodkod_constrs -> nut -> KK.formula *)
blanchet@33192
   715
fun declarative_axiom_for_plain_rel kk (FreeRel (x, _, R as Func _, nick)) =
blanchet@33192
   716
    kk_n_ary_function kk (R |> nick = @{const_name List.set} ? unopt_rep)
blanchet@34123
   717
                      (KK.Rel x)
blanchet@33192
   718
  | declarative_axiom_for_plain_rel ({kk_lone, kk_one, ...} : kodkod_constrs)
blanchet@33192
   719
                                    (FreeRel (x, _, R, _)) =
blanchet@34123
   720
    if is_one_rep R then kk_one (KK.Rel x)
blanchet@34123
   721
    else if is_lone_rep R andalso card_of_rep R > 1 then kk_lone (KK.Rel x)
blanchet@34123
   722
    else KK.True
blanchet@33192
   723
  | declarative_axiom_for_plain_rel _ u =
blanchet@33224
   724
    raise NUT ("Nitpick_Kodkod.declarative_axiom_for_plain_rel", [u])
blanchet@33192
   725
blanchet@34123
   726
(* nut NameTable.table -> styp -> KK.rel_expr * rep * int *)
blanchet@33192
   727
fun const_triple rel_table (x as (s, T)) =
blanchet@33192
   728
  case the_name rel_table (ConstName (s, T, Any)) of
blanchet@34123
   729
    FreeRel ((n, j), _, R, _) => (KK.Rel (n, j), R, n)
blanchet@33224
   730
  | _ => raise TERM ("Nitpick_Kodkod.const_triple", [Const x])
blanchet@33192
   731
blanchet@34123
   732
(* nut NameTable.table -> styp -> KK.rel_expr *)
blanchet@33192
   733
fun discr_rel_expr rel_table = #1 o const_triple rel_table o discr_for_constr
blanchet@33192
   734
blanchet@33192
   735
(* extended_context -> kodkod_constrs -> nut NameTable.table -> dtype_spec list
blanchet@33192
   736
   -> styp -> int -> nfa_transition list *)
blanchet@33192
   737
fun nfa_transitions_for_sel ext_ctxt ({kk_project, ...} : kodkod_constrs)
blanchet@33192
   738
                            rel_table (dtypes : dtype_spec list) constr_x n =
blanchet@33192
   739
  let
blanchet@33192
   740
    val x as (_, T) = boxed_nth_sel_for_constr ext_ctxt constr_x n
blanchet@33192
   741
    val (r, R, arity) = const_triple rel_table x
blanchet@33192
   742
    val type_schema = type_schema_of_rep T R
blanchet@33192
   743
  in
blanchet@33192
   744
    map_filter (fn (j, T) =>
blanchet@33192
   745
                   if forall (not_equal T o #typ) dtypes then NONE
blanchet@34123
   746
                   else SOME (kk_project r (map KK.Num [0, j]), T))
blanchet@33192
   747
               (index_seq 1 (arity - 1) ~~ tl type_schema)
blanchet@33192
   748
  end
blanchet@33192
   749
(* extended_context -> kodkod_constrs -> nut NameTable.table -> dtype_spec list
blanchet@33192
   750
   -> styp -> nfa_transition list *)
blanchet@33192
   751
fun nfa_transitions_for_constr ext_ctxt kk rel_table dtypes (x as (_, T)) =
blanchet@33192
   752
  maps (nfa_transitions_for_sel ext_ctxt kk rel_table dtypes x)
blanchet@33192
   753
       (index_seq 0 (num_sels_for_constr_type T))
blanchet@33192
   754
(* extended_context -> kodkod_constrs -> nut NameTable.table -> dtype_spec list
blanchet@33192
   755
   -> dtype_spec -> nfa_entry option *)
blanchet@33192
   756
fun nfa_entry_for_datatype _ _ _ _ ({co = true, ...} : dtype_spec) = NONE
blanchet@33549
   757
  | nfa_entry_for_datatype _ _ _ _ {shallow = true, ...} = NONE
blanchet@33549
   758
  | nfa_entry_for_datatype ext_ctxt kk rel_table dtypes {typ, constrs, ...} =
blanchet@33192
   759
    SOME (typ, maps (nfa_transitions_for_constr ext_ctxt kk rel_table dtypes
blanchet@33192
   760
                     o #const) constrs)
blanchet@33192
   761
blanchet@34123
   762
val empty_rel = KK.Product (KK.None, KK.None)
blanchet@33192
   763
blanchet@34123
   764
(* nfa_table -> typ -> typ -> KK.rel_expr list *)
blanchet@33192
   765
fun direct_path_rel_exprs nfa start final =
blanchet@33192
   766
  case AList.lookup (op =) nfa final of
blanchet@34118
   767
    SOME trans => map fst (filter (curry (op =) start o snd) trans)
blanchet@33192
   768
  | NONE => []
blanchet@34123
   769
(* kodkod_constrs -> nfa_table -> typ list -> typ -> typ -> KK.rel_expr *)
blanchet@33192
   770
and any_path_rel_expr ({kk_union, ...} : kodkod_constrs) nfa [] start final =
blanchet@33192
   771
    fold kk_union (direct_path_rel_exprs nfa start final)
blanchet@34123
   772
         (if start = final then KK.Iden else empty_rel)
blanchet@33192
   773
  | any_path_rel_expr (kk as {kk_union, ...}) nfa (q :: qs) start final =
blanchet@33192
   774
    kk_union (any_path_rel_expr kk nfa qs start final)
blanchet@33192
   775
             (knot_path_rel_expr kk nfa qs start q final)
blanchet@33192
   776
(* kodkod_constrs -> nfa_table -> typ list -> typ -> typ -> typ
blanchet@34123
   777
   -> KK.rel_expr *)
blanchet@33192
   778
and knot_path_rel_expr (kk as {kk_join, kk_reflexive_closure, ...}) nfa qs start
blanchet@33192
   779
                       knot final =
blanchet@33192
   780
  kk_join (kk_join (any_path_rel_expr kk nfa qs knot final)
blanchet@33192
   781
                   (kk_reflexive_closure (loop_path_rel_expr kk nfa qs knot)))
blanchet@33192
   782
          (any_path_rel_expr kk nfa qs start knot)
blanchet@34123
   783
(* kodkod_constrs -> nfa_table -> typ list -> typ -> KK.rel_expr *)
blanchet@33192
   784
and loop_path_rel_expr ({kk_union, ...} : kodkod_constrs) nfa [] start =
blanchet@33192
   785
    fold kk_union (direct_path_rel_exprs nfa start start) empty_rel
blanchet@33192
   786
  | loop_path_rel_expr (kk as {kk_union, kk_closure, ...}) nfa (q :: qs) start =
blanchet@33192
   787
    if start = q then
blanchet@33192
   788
      kk_closure (loop_path_rel_expr kk nfa qs start)
blanchet@33192
   789
    else
blanchet@33192
   790
      kk_union (loop_path_rel_expr kk nfa qs start)
blanchet@33192
   791
               (knot_path_rel_expr kk nfa qs start q start)
blanchet@33192
   792
blanchet@33192
   793
(* nfa_table -> unit NfaGraph.T *)
blanchet@33192
   794
fun graph_for_nfa nfa =
blanchet@33192
   795
  let
blanchet@33192
   796
    (* typ -> unit NfaGraph.T -> unit NfaGraph.T *)
blanchet@33192
   797
    fun new_node q = perhaps (try (NfaGraph.new_node (q, ())))
blanchet@33192
   798
    (* nfa_table -> unit NfaGraph.T -> unit NfaGraph.T *)
blanchet@33192
   799
    fun add_nfa [] = I
blanchet@33192
   800
      | add_nfa ((_, []) :: nfa) = add_nfa nfa
blanchet@33192
   801
      | add_nfa ((q, ((_, q') :: transitions)) :: nfa) =
blanchet@33192
   802
        add_nfa ((q, transitions) :: nfa) o NfaGraph.add_edge (q, q') o
blanchet@33192
   803
        new_node q' o new_node q
blanchet@33192
   804
  in add_nfa nfa NfaGraph.empty end
blanchet@33192
   805
blanchet@33192
   806
(* nfa_table -> nfa_table list *)
blanchet@33192
   807
fun strongly_connected_sub_nfas nfa =
blanchet@33192
   808
  nfa |> graph_for_nfa |> NfaGraph.strong_conn
blanchet@33192
   809
      |> map (fn keys => filter (member (op =) keys o fst) nfa)
blanchet@33192
   810
blanchet@34123
   811
(* dtype_spec list -> kodkod_constrs -> nfa_table -> typ -> KK.formula *)
blanchet@33192
   812
fun acyclicity_axiom_for_datatype dtypes kk nfa start =
blanchet@33192
   813
  #kk_no kk (#kk_intersect kk
blanchet@34123
   814
                 (loop_path_rel_expr kk nfa (map fst nfa) start) KK.Iden)
blanchet@33192
   815
(* extended_context -> kodkod_constrs -> nut NameTable.table -> dtype_spec list
blanchet@34123
   816
   -> KK.formula list *)
blanchet@33192
   817
fun acyclicity_axioms_for_datatypes ext_ctxt kk rel_table dtypes =
blanchet@33192
   818
  map_filter (nfa_entry_for_datatype ext_ctxt kk rel_table dtypes) dtypes
blanchet@33192
   819
  |> strongly_connected_sub_nfas
blanchet@33192
   820
  |> maps (fn nfa => map (acyclicity_axiom_for_datatype dtypes kk nfa o fst)
blanchet@33192
   821
                         nfa)
blanchet@33192
   822
blanchet@33192
   823
(* extended_context -> int -> kodkod_constrs -> nut NameTable.table
blanchet@34123
   824
   -> KK.rel_expr -> constr_spec -> int -> KK.formula *)
blanchet@33192
   825
fun sel_axiom_for_sel ext_ctxt j0
blanchet@33192
   826
        (kk as {kk_all, kk_implies, kk_formula_if, kk_subset, kk_rel_eq, kk_no,
blanchet@33982
   827
                kk_join, ...}) rel_table dom_r
blanchet@33192
   828
        ({const, delta, epsilon, exclusive, explicit_max, ...} : constr_spec)
blanchet@33192
   829
        n =
blanchet@33192
   830
  let
blanchet@33192
   831
    val x as (_, T) = boxed_nth_sel_for_constr ext_ctxt const n
blanchet@33192
   832
    val (r, R, arity) = const_triple rel_table x
blanchet@33192
   833
    val R2 = dest_Func R |> snd
blanchet@33192
   834
    val z = (epsilon - delta, delta + j0)
blanchet@33192
   835
  in
blanchet@33192
   836
    if exclusive then
blanchet@33192
   837
      kk_n_ary_function kk (Func (Atom z, R2)) r
blanchet@33192
   838
    else
blanchet@34123
   839
      let val r' = kk_join (KK.Var (1, 0)) r in
blanchet@34123
   840
        kk_all [KK.DeclOne ((1, 0), KK.AtomSeq z)]
blanchet@34123
   841
               (kk_formula_if (kk_subset (KK.Var (1, 0)) dom_r)
blanchet@34123
   842
                              (kk_n_ary_function kk R2 r') (kk_no r'))
blanchet@33192
   843
      end
blanchet@33192
   844
  end
blanchet@34121
   845
(* extended_context -> int -> int -> kodkod_constrs -> nut NameTable.table
blanchet@34123
   846
   -> constr_spec -> KK.formula list *)
blanchet@34121
   847
fun sel_axioms_for_constr ext_ctxt bits j0 kk rel_table
blanchet@33192
   848
        (constr as {const, delta, epsilon, explicit_max, ...}) =
blanchet@33192
   849
  let
blanchet@33192
   850
    val honors_explicit_max =
blanchet@33192
   851
      explicit_max < 0 orelse epsilon - delta <= explicit_max
blanchet@33192
   852
  in
blanchet@33192
   853
    if explicit_max = 0 then
blanchet@33192
   854
      [formula_for_bool honors_explicit_max]
blanchet@33192
   855
    else
blanchet@33192
   856
      let
blanchet@33192
   857
        val ran_r = discr_rel_expr rel_table const
blanchet@33192
   858
        val max_axiom =
blanchet@34121
   859
          if honors_explicit_max then
blanchet@34123
   860
            KK.True
blanchet@34121
   861
          else if is_twos_complement_representable bits (epsilon - delta) then
blanchet@34123
   862
            KK.LE (KK.Cardinality ran_r, KK.Num explicit_max)
blanchet@34121
   863
          else
blanchet@34121
   864
            raise TOO_SMALL ("Nitpick_Kodkod.sel_axioms_for_constr",
blanchet@34121
   865
                             "\"bits\" value " ^ string_of_int bits ^
blanchet@34121
   866
                             " too small for \"max\"")
blanchet@33192
   867
      in
blanchet@33192
   868
        max_axiom ::
blanchet@33192
   869
        map (sel_axiom_for_sel ext_ctxt j0 kk rel_table ran_r constr)
blanchet@33192
   870
            (index_seq 0 (num_sels_for_constr_type (snd const)))
blanchet@33192
   871
      end
blanchet@33192
   872
  end
blanchet@34121
   873
(* extended_context -> int -> int -> kodkod_constrs -> nut NameTable.table
blanchet@34123
   874
   -> dtype_spec -> KK.formula list *)
blanchet@34121
   875
fun sel_axioms_for_datatype ext_ctxt bits j0 kk rel_table
blanchet@33192
   876
                            ({constrs, ...} : dtype_spec) =
blanchet@34121
   877
  maps (sel_axioms_for_constr ext_ctxt bits j0 kk rel_table) constrs
blanchet@33192
   878
blanchet@33192
   879
(* extended_context -> kodkod_constrs -> nut NameTable.table -> constr_spec
blanchet@34123
   880
   -> KK.formula list *)
blanchet@33192
   881
fun uniqueness_axiom_for_constr ext_ctxt
blanchet@33192
   882
        ({kk_all, kk_implies, kk_and, kk_rel_eq, kk_lone, kk_join, ...}
blanchet@33192
   883
         : kodkod_constrs) rel_table ({const, ...} : constr_spec) =
blanchet@33192
   884
  let
blanchet@34123
   885
    (* KK.rel_expr -> KK.formula *)
blanchet@33192
   886
    fun conjunct_for_sel r =
blanchet@34123
   887
      kk_rel_eq (kk_join (KK.Var (1, 0)) r) (kk_join (KK.Var (1, 1)) r)
blanchet@33192
   888
    val num_sels = num_sels_for_constr_type (snd const)
blanchet@33192
   889
    val triples = map (const_triple rel_table
blanchet@33192
   890
                       o boxed_nth_sel_for_constr ext_ctxt const)
blanchet@33192
   891
                      (~1 upto num_sels - 1)
blanchet@33192
   892
    val j0 = case triples |> hd |> #2 of
blanchet@33192
   893
               Func (Atom (_, j0), _) => j0
blanchet@33224
   894
             | R => raise REP ("Nitpick_Kodkod.uniqueness_axiom_for_constr",
blanchet@33224
   895
                               [R])
blanchet@33192
   896
    val set_r = triples |> hd |> #1
blanchet@33192
   897
  in
blanchet@33192
   898
    if num_sels = 0 then
blanchet@33192
   899
      kk_lone set_r
blanchet@33192
   900
    else
blanchet@34123
   901
      kk_all (map (KK.DeclOne o rpair set_r o pair 1) [0, 1])
blanchet@33192
   902
             (kk_implies
blanchet@33192
   903
                  (fold1 kk_and (map (conjunct_for_sel o #1) (tl triples)))
blanchet@34123
   904
                  (kk_rel_eq (KK.Var (1, 0)) (KK.Var (1, 1))))
blanchet@33192
   905
  end
blanchet@33192
   906
(* extended_context -> kodkod_constrs -> nut NameTable.table -> dtype_spec
blanchet@34123
   907
   -> KK.formula list *)
blanchet@33192
   908
fun uniqueness_axioms_for_datatype ext_ctxt kk rel_table
blanchet@33192
   909
                                   ({constrs, ...} : dtype_spec) =
blanchet@33192
   910
  map (uniqueness_axiom_for_constr ext_ctxt kk rel_table) constrs
blanchet@33192
   911
blanchet@33192
   912
(* constr_spec -> int *)
blanchet@33192
   913
fun effective_constr_max ({delta, epsilon, ...} : constr_spec) = epsilon - delta
blanchet@33192
   914
(* int -> kodkod_constrs -> nut NameTable.table -> dtype_spec
blanchet@34123
   915
   -> KK.formula list *)
blanchet@33192
   916
fun partition_axioms_for_datatype j0 (kk as {kk_rel_eq, kk_union, ...})
blanchet@33192
   917
                                  rel_table
blanchet@33192
   918
                                  ({card, constrs, ...} : dtype_spec) =
blanchet@33192
   919
  if forall #exclusive constrs then
blanchet@33192
   920
    [Integer.sum (map effective_constr_max constrs) = card |> formula_for_bool]
blanchet@33192
   921
  else
blanchet@33192
   922
    let val rs = map (discr_rel_expr rel_table o #const) constrs in
blanchet@34123
   923
      [kk_rel_eq (fold1 kk_union rs) (KK.AtomSeq (card, j0)),
blanchet@33192
   924
       kk_disjoint_sets kk rs]
blanchet@33192
   925
    end
blanchet@33192
   926
blanchet@34121
   927
(* extended_context -> int -> int Typtab.table -> kodkod_constrs
blanchet@34123
   928
   -> nut NameTable.table -> dtype_spec -> KK.formula list *)
blanchet@34121
   929
fun other_axioms_for_datatype _ _ _ _ _ {shallow = true, ...} = []
blanchet@34121
   930
  | other_axioms_for_datatype ext_ctxt bits ofs kk rel_table
blanchet@34121
   931
                              (dtype as {typ, ...}) =
blanchet@33549
   932
    let val j0 = offset_of_type ofs typ in
blanchet@34121
   933
      sel_axioms_for_datatype ext_ctxt bits j0 kk rel_table dtype @
blanchet@33549
   934
      uniqueness_axioms_for_datatype ext_ctxt kk rel_table dtype @
blanchet@33549
   935
      partition_axioms_for_datatype j0 kk rel_table dtype
blanchet@33549
   936
    end
blanchet@33192
   937
blanchet@34121
   938
(* extended_context -> int -> int Typtab.table -> kodkod_constrs
blanchet@34123
   939
   -> nut NameTable.table -> dtype_spec list -> KK.formula list *)
blanchet@34121
   940
fun declarative_axioms_for_datatypes ext_ctxt bits ofs kk rel_table dtypes =
blanchet@33192
   941
  acyclicity_axioms_for_datatypes ext_ctxt kk rel_table dtypes @
blanchet@34121
   942
  maps (other_axioms_for_datatype ext_ctxt bits ofs kk rel_table) dtypes
blanchet@33192
   943
blanchet@34123
   944
(* int -> int Typtab.table -> bool -> kodkod_constrs -> nut -> KK.formula *)
blanchet@34121
   945
fun kodkod_formula_from_nut bits ofs liberal
blanchet@33192
   946
        (kk as {kk_all, kk_exist, kk_formula_let, kk_formula_if, kk_or, kk_not,
blanchet@33192
   947
                kk_iff, kk_implies, kk_and, kk_subset, kk_rel_eq, kk_no, kk_one,
blanchet@33192
   948
                kk_some, kk_rel_let, kk_rel_if, kk_union, kk_difference,
blanchet@33192
   949
                kk_intersect, kk_product, kk_join, kk_closure, kk_comprehension,
blanchet@33192
   950
                kk_project, kk_project_seq, kk_not3, kk_nat_less, kk_int_less,
blanchet@33192
   951
                ...}) u =
blanchet@33192
   952
  let
blanchet@33192
   953
    val main_j0 = offset_of_type ofs bool_T
blanchet@33192
   954
    val bool_j0 = main_j0
blanchet@33192
   955
    val bool_atom_R = Atom (2, main_j0)
blanchet@34123
   956
    val false_atom = KK.Atom bool_j0
blanchet@34123
   957
    val true_atom = KK.Atom (bool_j0 + 1)
blanchet@33192
   958
blanchet@34123
   959
    (* polarity -> int -> KK.rel_expr -> KK.formula *)
blanchet@33192
   960
    fun formula_from_opt_atom polar j0 r =
blanchet@33192
   961
      case polar of
blanchet@34123
   962
        Neg => kk_not (kk_rel_eq r (KK.Atom j0))
blanchet@34123
   963
      | _ => kk_rel_eq r (KK.Atom (j0 + 1))
blanchet@34123
   964
    (* int -> KK.rel_expr -> KK.formula *)
blanchet@33192
   965
    val formula_from_atom = formula_from_opt_atom Pos
blanchet@33192
   966
blanchet@34123
   967
    (* KK.formula -> KK.formula -> KK.formula *)
blanchet@33192
   968
    fun kk_notimplies f1 f2 = kk_and f1 (kk_not f2)
blanchet@34123
   969
    (* KK.rel_expr -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   970
    val kk_or3 =
blanchet@34121
   971
      double_rel_rel_let kk
blanchet@33192
   972
          (fn r1 => fn r2 =>
blanchet@33192
   973
              kk_rel_if (kk_subset true_atom (kk_union r1 r2)) true_atom
blanchet@33192
   974
                        (kk_intersect r1 r2))
blanchet@33192
   975
    val kk_and3 =
blanchet@34121
   976
      double_rel_rel_let kk
blanchet@33192
   977
          (fn r1 => fn r2 =>
blanchet@33192
   978
              kk_rel_if (kk_subset false_atom (kk_union r1 r2)) false_atom
blanchet@33192
   979
                        (kk_intersect r1 r2))
blanchet@33192
   980
    fun kk_notimplies3 r1 r2 = kk_and3 r1 (kk_not3 r2)
blanchet@33192
   981
blanchet@34123
   982
    (* int -> KK.rel_expr -> KK.formula list *)
blanchet@33192
   983
    val unpack_formulas =
blanchet@33192
   984
      map (formula_from_atom bool_j0) oo unpack_vect_in_chunks kk 1
blanchet@34123
   985
    (* (KK.formula -> KK.formula -> KK.formula) -> int -> KK.rel_expr
blanchet@34123
   986
       -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
   987
    fun kk_vect_set_op connective k r1 r2 =
blanchet@33192
   988
      fold1 kk_product (map2 (atom_from_formula kk bool_j0 oo connective)
blanchet@33192
   989
                             (unpack_formulas k r1) (unpack_formulas k r2))
blanchet@34123
   990
    (* (KK.formula -> KK.formula -> KK.formula) -> int -> KK.rel_expr
blanchet@34123
   991
       -> KK.rel_expr -> KK.formula *)
blanchet@33192
   992
    fun kk_vect_set_bool_op connective k r1 r2 =
blanchet@33192
   993
      fold1 kk_and (map2 connective (unpack_formulas k r1)
blanchet@33192
   994
                         (unpack_formulas k r2))
blanchet@33192
   995
blanchet@34123
   996
    (* nut -> KK.formula *)
blanchet@33192
   997
    fun to_f u =
blanchet@33192
   998
      case rep_of u of
blanchet@33192
   999
        Formula polar =>
blanchet@33192
  1000
        (case u of
blanchet@34123
  1001
           Cst (False, _, _) => KK.False
blanchet@34123
  1002
         | Cst (True, _, _) => KK.True
blanchet@33854
  1003
         | Op1 (Not, _, _, u1) =>
blanchet@33854
  1004
           kk_not (to_f_with_polarity (flip_polarity polar) u1)
blanchet@33192
  1005
         | Op1 (Finite, _, _, u1) =>
blanchet@33192
  1006
           let val opt1 = is_opt_rep (rep_of u1) in
blanchet@33192
  1007
             case polar of
blanchet@33192
  1008
               Neut => if opt1 then
blanchet@33224
  1009
                         raise NUT ("Nitpick_Kodkod.to_f (Finite)", [u])
blanchet@33192
  1010
                       else
blanchet@34123
  1011
                         KK.True
blanchet@33192
  1012
             | Pos => formula_for_bool (not opt1)
blanchet@34123
  1013
             | Neg => KK.True
blanchet@33192
  1014
           end
blanchet@33192
  1015
         | Op1 (Cast, _, _, u1) => to_f_with_polarity polar u1
blanchet@33854
  1016
         | Op2 (All, _, _, u1, u2) =>
blanchet@33854
  1017
           kk_all (untuple to_decl u1) (to_f_with_polarity polar u2)
blanchet@33854
  1018
         | Op2 (Exist, _, _, u1, u2) =>
blanchet@33854
  1019
           kk_exist (untuple to_decl u1) (to_f_with_polarity polar u2)
blanchet@33854
  1020
         | Op2 (Or, _, _, u1, u2) =>
blanchet@33854
  1021
           kk_or (to_f_with_polarity polar u1) (to_f_with_polarity polar u2)
blanchet@33854
  1022
         | Op2 (And, _, _, u1, u2) =>
blanchet@33854
  1023
           kk_and (to_f_with_polarity polar u1) (to_f_with_polarity polar u2)
blanchet@33192
  1024
         | Op2 (Less, T, Formula polar, u1, u2) =>
blanchet@33192
  1025
           formula_from_opt_atom polar bool_j0
blanchet@33192
  1026
               (to_r (Op2 (Less, T, Opt bool_atom_R, u1, u2)))
blanchet@33192
  1027
         | Op2 (Subset, _, _, u1, u2) =>
blanchet@33192
  1028
           let
blanchet@33192
  1029
             val dom_T = domain_type (type_of u1)
blanchet@33192
  1030
             val R1 = rep_of u1
blanchet@33192
  1031
             val R2 = rep_of u2
blanchet@33192
  1032
             val (dom_R, ran_R) =
blanchet@33192
  1033
               case min_rep R1 R2 of
blanchet@33192
  1034
                 Func (Unit, R') =>
blanchet@33192
  1035
                 (Atom (1, offset_of_type ofs dom_T), R')
blanchet@33192
  1036
               | Func Rp => Rp
blanchet@33192
  1037
               | R => (Atom (card_of_domain_from_rep 2 R,
blanchet@33192
  1038
                             offset_of_type ofs dom_T),
blanchet@33192
  1039
                       if is_opt_rep R then Opt bool_atom_R else Formula Neut)
blanchet@33192
  1040
             val set_R = Func (dom_R, ran_R)
blanchet@33192
  1041
           in
blanchet@33192
  1042
             if not (is_opt_rep ran_R) then
blanchet@33192
  1043
               to_set_bool_op kk_implies kk_subset u1 u2
blanchet@33192
  1044
             else if polar = Neut then
blanchet@33224
  1045
               raise NUT ("Nitpick_Kodkod.to_f (Subset)", [u])
blanchet@33192
  1046
             else
blanchet@33192
  1047
               let
blanchet@33886
  1048
                 (* FIXME: merge with similar code below *)
blanchet@34123
  1049
                 (* bool -> nut -> KK.rel_expr *)
blanchet@33192
  1050
                 fun set_to_r widen u =
blanchet@33192
  1051
                   if widen then
blanchet@33192
  1052
                     kk_difference (full_rel_for_rep dom_R)
blanchet@33192
  1053
                                   (kk_join (to_rep set_R u) false_atom)
blanchet@33192
  1054
                   else
blanchet@33192
  1055
                     kk_join (to_rep set_R u) true_atom
blanchet@33192
  1056
                 val widen1 = (polar = Pos andalso is_opt_rep R1)
blanchet@33192
  1057
                 val widen2 = (polar = Neg andalso is_opt_rep R2)
blanchet@33192
  1058
               in kk_subset (set_to_r widen1 u1) (set_to_r widen2 u2) end
blanchet@33192
  1059
           end
blanchet@33192
  1060
         | Op2 (DefEq, _, _, u1, u2) =>
blanchet@33192
  1061
           (case min_rep (rep_of u1) (rep_of u2) of
blanchet@34123
  1062
              Unit => KK.True
blanchet@33192
  1063
            | Formula polar =>
blanchet@33192
  1064
              kk_iff (to_f_with_polarity polar u1) (to_f_with_polarity polar u2)
blanchet@33192
  1065
            | min_R =>
blanchet@33192
  1066
              let
blanchet@33192
  1067
                (* nut -> nut list *)
blanchet@33192
  1068
                fun args (Op2 (Apply, _, _, u1, u2)) = u2 :: args u1
blanchet@33192
  1069
                  | args (Tuple (_, _, us)) = us
blanchet@33192
  1070
                  | args _ = []
blanchet@33192
  1071
                val opt_arg_us = filter (is_opt_rep o rep_of) (args u1)
blanchet@33192
  1072
              in
blanchet@33192
  1073
                if null opt_arg_us orelse not (is_Opt min_R)
blanchet@33192
  1074
                   orelse is_eval_name u1 then
blanchet@33192
  1075
                  fold (kk_or o (kk_no o to_r)) opt_arg_us
blanchet@33192
  1076
                       (kk_rel_eq (to_rep min_R u1) (to_rep min_R u2))
blanchet@33192
  1077
                else
blanchet@34118
  1078
                  kk_subset (to_rep min_R u1) (to_rep min_R u2)
blanchet@33192
  1079
              end)
blanchet@33192
  1080
         | Op2 (Eq, T, R, u1, u2) =>
blanchet@33192
  1081
           (case min_rep (rep_of u1) (rep_of u2) of
blanchet@34123
  1082
              Unit => KK.True
blanchet@33192
  1083
            | Formula polar =>
blanchet@33192
  1084
              kk_iff (to_f_with_polarity polar u1) (to_f_with_polarity polar u2)
blanchet@33192
  1085
            | min_R =>
blanchet@33192
  1086
              if is_opt_rep min_R then
blanchet@33192
  1087
                if polar = Neut then
blanchet@33192
  1088
                  (* continuation of hackish optimization *)
blanchet@33192
  1089
                  kk_rel_eq (to_rep min_R u1) (to_rep min_R u2)
blanchet@33192
  1090
                else if is_Cst Unrep u1 then
blanchet@33192
  1091
                  to_could_be_unrep (polar = Neg) u2
blanchet@33192
  1092
                else if is_Cst Unrep u2 then
blanchet@33192
  1093
                  to_could_be_unrep (polar = Neg) u1
blanchet@33192
  1094
                else
blanchet@33192
  1095
                  let
blanchet@33192
  1096
                    val r1 = to_rep min_R u1
blanchet@33192
  1097
                    val r2 = to_rep min_R u2
blanchet@33192
  1098
                    val both_opt = forall (is_opt_rep o rep_of) [u1, u2]
blanchet@33192
  1099
                  in
blanchet@33192
  1100
                    (if polar = Pos then
blanchet@33192
  1101
                       if not both_opt then
blanchet@33192
  1102
                         kk_rel_eq r1 r2
blanchet@33192
  1103
                       else if is_lone_rep min_R
blanchet@33192
  1104
                               andalso arity_of_rep min_R = 1 then
blanchet@33192
  1105
                         kk_some (kk_intersect r1 r2)
blanchet@33192
  1106
                       else
blanchet@33192
  1107
                         raise SAME ()
blanchet@33192
  1108
                     else
blanchet@33192
  1109
                       if is_lone_rep min_R then
blanchet@33192
  1110
                         if arity_of_rep min_R = 1 then
blanchet@34123
  1111
                           kk_subset (kk_product r1 r2) KK.Iden
blanchet@33192
  1112
                         else if not both_opt then
blanchet@33192
  1113
                           (r1, r2) |> is_opt_rep (rep_of u2) ? swap
blanchet@34118
  1114
                                    |-> kk_subset
blanchet@33192
  1115
                         else
blanchet@33192
  1116
                           raise SAME ()
blanchet@33192
  1117
                       else
blanchet@33192
  1118
                         raise SAME ())
blanchet@33192
  1119
                    handle SAME () =>
blanchet@33192
  1120
                           formula_from_opt_atom polar bool_j0
blanchet@33192
  1121
                               (to_guard [u1, u2] bool_atom_R
blanchet@33192
  1122
                                         (rel_expr_from_formula kk bool_atom_R
blanchet@33192
  1123
                                                            (kk_rel_eq r1 r2)))
blanchet@33192
  1124
                  end
blanchet@33192
  1125
              else
blanchet@33192
  1126
                let
blanchet@33192
  1127
                  val r1 = to_rep min_R u1
blanchet@33192
  1128
                  val r2 = to_rep min_R u2
blanchet@33192
  1129
                in
blanchet@33192
  1130
                  if is_one_rep min_R then
blanchet@33192
  1131
                    let
blanchet@33192
  1132
                      val rs1 = unpack_products r1
blanchet@33192
  1133
                      val rs2 = unpack_products r2
blanchet@33192
  1134
                    in
blanchet@33192
  1135
                      if length rs1 = length rs2
blanchet@34123
  1136
                         andalso map KK.arity_of_rel_expr rs1
blanchet@34123
  1137
                                 = map KK.arity_of_rel_expr rs2 then
blanchet@33192
  1138
                        fold1 kk_and (map2 kk_subset rs1 rs2)
blanchet@33192
  1139
                      else
blanchet@33192
  1140
                        kk_subset r1 r2
blanchet@33192
  1141
                    end
blanchet@33192
  1142
                  else
blanchet@33192
  1143
                    kk_rel_eq r1 r2
blanchet@33192
  1144
                end)
blanchet@33744
  1145
         | Op2 (The, T, _, u1, u2) =>
blanchet@33744
  1146
           to_f_with_polarity polar
blanchet@33744
  1147
                              (Op2 (The, T, Opt (Atom (2, bool_j0)), u1, u2))
blanchet@33744
  1148
         | Op2 (Eps, T, _, u1, u2) =>
blanchet@33744
  1149
           to_f_with_polarity polar
blanchet@33744
  1150
                              (Op2 (Eps, T, Opt (Atom (2, bool_j0)), u1, u2))
blanchet@33192
  1151
         | Op2 (Apply, T, _, u1, u2) =>
blanchet@33192
  1152
           (case (polar, rep_of u1) of
blanchet@33192
  1153
              (Neg, Func (R, Formula Neut)) => kk_subset (to_opt R u2) (to_r u1)
blanchet@33192
  1154
            | _ =>
blanchet@33192
  1155
              to_f_with_polarity polar
blanchet@33192
  1156
                 (Op2 (Apply, T, Opt (Atom (2, offset_of_type ofs T)), u1, u2)))
blanchet@33192
  1157
         | Op3 (Let, _, _, u1, u2, u3) =>
blanchet@33854
  1158
           kk_formula_let [to_expr_assign u1 u2] (to_f_with_polarity polar u3)
blanchet@33192
  1159
         | Op3 (If, _, _, u1, u2, u3) =>
blanchet@33854
  1160
           kk_formula_if (to_f u1) (to_f_with_polarity polar u2)
blanchet@33854
  1161
                         (to_f_with_polarity polar u3)
blanchet@34123
  1162
         | FormulaReg (j, _, _) => KK.FormulaReg j
blanchet@33224
  1163
         | _ => raise NUT ("Nitpick_Kodkod.to_f", [u]))
blanchet@33192
  1164
      | Atom (2, j0) => formula_from_atom j0 (to_r u)
blanchet@33224
  1165
      | _ => raise NUT ("Nitpick_Kodkod.to_f", [u])
blanchet@34123
  1166
    (* polarity -> nut -> KK.formula *)
blanchet@33192
  1167
    and to_f_with_polarity polar u =
blanchet@33192
  1168
      case rep_of u of
blanchet@33192
  1169
        Formula _ => to_f u
blanchet@33192
  1170
      | Atom (2, j0) => formula_from_atom j0 (to_r u)
blanchet@33192
  1171
      | Opt (Atom (2, j0)) => formula_from_opt_atom polar j0 (to_r u)
blanchet@33224
  1172
      | _ => raise NUT ("Nitpick_Kodkod.to_f_with_polarity", [u])
blanchet@34123
  1173
    (* nut -> KK.rel_expr *)
blanchet@33192
  1174
    and to_r u =
blanchet@33192
  1175
      case u of
blanchet@33192
  1176
        Cst (False, _, Atom _) => false_atom
blanchet@33192
  1177
      | Cst (True, _, Atom _) => true_atom
blanchet@33192
  1178
      | Cst (Iden, T, Func (Struct [R1, R2], Formula Neut)) =>
blanchet@33192
  1179
        if R1 = R2 andalso arity_of_rep R1 = 1 then
blanchet@34123
  1180
          kk_intersect KK.Iden (kk_product (full_rel_for_rep R1) KK.Univ)
blanchet@33192
  1181
        else
blanchet@33192
  1182
          let
blanchet@33192
  1183
            val schema1 = atom_schema_of_rep R1
blanchet@33192
  1184
            val schema2 = atom_schema_of_rep R2
blanchet@33192
  1185
            val arity1 = length schema1
blanchet@33192
  1186
            val arity2 = length schema2
blanchet@33192
  1187
            val r1 = fold1 kk_product (unary_var_seq 0 arity1)
blanchet@33192
  1188
            val r2 = fold1 kk_product (unary_var_seq arity1 arity2)
blanchet@33192
  1189
            val min_R = min_rep R1 R2
blanchet@33192
  1190
          in
blanchet@33192
  1191
            kk_comprehension
blanchet@33192
  1192
                (decls_for_atom_schema 0 (schema1 @ schema2))
blanchet@33192
  1193
                (kk_rel_eq (rel_expr_from_rel_expr kk min_R R1 r1)
blanchet@33192
  1194
                           (rel_expr_from_rel_expr kk min_R R2 r2))
blanchet@33192
  1195
          end
blanchet@34123
  1196
      | Cst (Iden, T, Func (Atom (1, j0), Formula Neut)) => KK.Atom j0
blanchet@33192
  1197
      | Cst (Iden, T as Type ("fun", [T1, _]), R as Func (R1, _)) =>
blanchet@33192
  1198
        to_rep R (Cst (Iden, T, Func (one_rep ofs T1 R1, Formula Neut)))
blanchet@34121
  1199
      | Cst (Num j, T, R) =>
blanchet@34121
  1200
        if is_word_type T then
blanchet@34123
  1201
          atom_from_int_expr kk T R (KK.Num j)
blanchet@34121
  1202
        else if T = int_T then
blanchet@34121
  1203
          case atom_for_int (card_of_rep R, offset_of_type ofs int_T) j of
blanchet@34123
  1204
            ~1 => if is_opt_rep R then KK.None
blanchet@33224
  1205
                  else raise NUT ("Nitpick_Kodkod.to_r (Num)", [u])
blanchet@34123
  1206
          | j' => KK.Atom j'
blanchet@34121
  1207
        else
blanchet@34123
  1208
          if j < card_of_rep R then KK.Atom (j + offset_of_type ofs T)
blanchet@34123
  1209
          else if is_opt_rep R then KK.None
blanchet@34121
  1210
          else raise NUT ("Nitpick_Kodkod.to_r (Num)", [u])
blanchet@33192
  1211
      | Cst (Unknown, _, R) => empty_rel_for_rep R
blanchet@33192
  1212
      | Cst (Unrep, _, R) => empty_rel_for_rep R
blanchet@34123
  1213
      | Cst (Suc, T as @{typ "unsigned_bit word => unsigned_bit word"}, R) =>
blanchet@34123
  1214
        to_bit_word_unary_op T R (curry KK.Add (KK.Num 1))
blanchet@34123
  1215
      | Cst (Suc, @{typ "nat => nat"}, Func (Atom x, _)) =>
blanchet@34123
  1216
        kk_intersect (KK.Rel suc_rel) (kk_product KK.Univ (KK.AtomSeq x))
blanchet@34123
  1217
      | Cst (Suc, _, Func (Atom x, _)) => KK.Rel suc_rel
blanchet@34123
  1218
      | Cst (Add, Type ("fun", [@{typ nat}, _]), _) => KK.Rel nat_add_rel
blanchet@34123
  1219
      | Cst (Add, Type ("fun", [@{typ int}, _]), _) => KK.Rel int_add_rel
blanchet@34121
  1220
      | Cst (Add, T as Type ("fun", [@{typ "unsigned_bit word"}, _]), R) =>
blanchet@34123
  1221
        to_bit_word_binary_op T R NONE (SOME (curry KK.Add))
blanchet@34121
  1222
      | Cst (Add, T as Type ("fun", [@{typ "signed_bit word"}, _]), R) =>
blanchet@34121
  1223
        to_bit_word_binary_op T R
blanchet@34121
  1224
            (SOME (fn i1 => fn i2 => fn i3 =>
blanchet@34123
  1225
                 kk_implies (KK.LE (KK.Num 0, KK.BitXor (i1, i2)))
blanchet@34123
  1226
                            (KK.LE (KK.Num 0, KK.BitXor (i2, i3)))))
blanchet@34123
  1227
            (SOME (curry KK.Add))
blanchet@34121
  1228
      | Cst (Subtract, Type ("fun", [@{typ nat}, _]), _) =>
blanchet@34123
  1229
        KK.Rel nat_subtract_rel
blanchet@34121
  1230
      | Cst (Subtract, Type ("fun", [@{typ int}, _]), _) =>
blanchet@34123
  1231
        KK.Rel int_subtract_rel
blanchet@34121
  1232
      | Cst (Subtract, T as Type ("fun", [@{typ "unsigned_bit word"}, _]), R) =>
blanchet@34121
  1233
        to_bit_word_binary_op T R NONE
blanchet@34121
  1234
            (SOME (fn i1 => fn i2 =>
blanchet@34123
  1235
                      KK.IntIf (KK.LE (i1, i2), KK.Num 0, KK.Sub (i1, i2))))
blanchet@34121
  1236
      | Cst (Subtract, T as Type ("fun", [@{typ "signed_bit word"}, _]), R) =>
blanchet@34121
  1237
        to_bit_word_binary_op T R
blanchet@34121
  1238
            (SOME (fn i1 => fn i2 => fn i3 =>
blanchet@34123
  1239
                 kk_implies (KK.LT (KK.BitXor (i1, i2), KK.Num 0))
blanchet@34123
  1240
                            (KK.LT (KK.BitXor (i2, i3), KK.Num 0))))
blanchet@34123
  1241
            (SOME (curry KK.Sub))
blanchet@34121
  1242
      | Cst (Multiply, Type ("fun", [@{typ nat}, _]), _) =>
blanchet@34123
  1243
        KK.Rel nat_multiply_rel
blanchet@34121
  1244
      | Cst (Multiply, Type ("fun", [@{typ int}, _]), _) =>
blanchet@34123
  1245
        KK.Rel int_multiply_rel
blanchet@34121
  1246
      | Cst (Multiply,
blanchet@34121
  1247
             T as Type ("fun", [Type (@{type_name word}, [bit_T]), _]), R) =>
blanchet@34121
  1248
        to_bit_word_binary_op T R
blanchet@34121
  1249
            (SOME (fn i1 => fn i2 => fn i3 =>
blanchet@34123
  1250
                kk_or (KK.IntEq (i2, KK.Num 0))
blanchet@34123
  1251
                      (KK.IntEq (KK.Div (i3, i2), i1)
blanchet@34121
  1252
                       |> bit_T = @{typ signed_bit}
blanchet@34123
  1253
                          ? kk_and (KK.LE (KK.Num 0,
blanchet@34123
  1254
                                           foldl1 KK.BitAnd [i1, i2, i3])))))
blanchet@34123
  1255
            (SOME (curry KK.Mult))
blanchet@34123
  1256
      | Cst (Divide, Type ("fun", [@{typ nat}, _]), _) => KK.Rel nat_divide_rel
blanchet@34123
  1257
      | Cst (Divide, Type ("fun", [@{typ int}, _]), _) => KK.Rel int_divide_rel
blanchet@34121
  1258
      | Cst (Divide, T as Type ("fun", [@{typ "unsigned_bit word"}, _]), R) =>
blanchet@34121
  1259
        to_bit_word_binary_op T R NONE
blanchet@34121
  1260
            (SOME (fn i1 => fn i2 =>
blanchet@34123
  1261
                      KK.IntIf (KK.IntEq (i2, KK.Num 0),
blanchet@34123
  1262
                                KK.Num 0, KK.Div (i1, i2))))
blanchet@34121
  1263
      | Cst (Divide, T as Type ("fun", [@{typ "signed_bit word"}, _]), R) =>
blanchet@34121
  1264
        to_bit_word_binary_op T R
blanchet@34121
  1265
            (SOME (fn i1 => fn i2 => fn i3 =>
blanchet@34123
  1266
                      KK.LE (KK.Num 0, foldl1 KK.BitAnd [i1, i2, i3])))
blanchet@34121
  1267
            (SOME (fn i1 => fn i2 =>
blanchet@34123
  1268
                 KK.IntIf (kk_and (KK.LT (i1, KK.Num 0))
blanchet@34123
  1269
                                  (KK.LT (KK.Num 0, i2)),
blanchet@34123
  1270
                     KK.Sub (KK.Div (KK.Add (i1, KK.Num 1), i2), KK.Num 1),
blanchet@34123
  1271
                     KK.IntIf (kk_and (KK.LT (KK.Num 0, i1))
blanchet@34123
  1272
                                      (KK.LT (i2, KK.Num 0)),
blanchet@34123
  1273
                         KK.Sub (KK.Div (KK.Sub (i1, KK.Num 1), i2), KK.Num 1),
blanchet@34123
  1274
                         KK.IntIf (KK.IntEq (i2, KK.Num 0),
blanchet@34123
  1275
                                   KK.Num 0, KK.Div (i1, i2))))))
blanchet@34123
  1276
      | Cst (Gcd, _, _) => KK.Rel gcd_rel
blanchet@34123
  1277
      | Cst (Lcm, _, _) => KK.Rel lcm_rel
blanchet@34123
  1278
      | Cst (Fracs, _, Func (Atom (1, _), _)) => KK.None
blanchet@33192
  1279
      | Cst (Fracs, _, Func (Struct _, _)) =>
blanchet@34123
  1280
        kk_project_seq (KK.Rel norm_frac_rel) 2 2
blanchet@34123
  1281
      | Cst (NormFrac, _, _) => KK.Rel norm_frac_rel
blanchet@34121
  1282
      | Cst (NatToInt, Type ("fun", [@{typ nat}, _]), Func (Atom _, Atom _)) =>
blanchet@34123
  1283
        KK.Iden
blanchet@34121
  1284
      | Cst (NatToInt, Type ("fun", [@{typ nat}, _]),
blanchet@33192
  1285
             Func (Atom (nat_k, nat_j0), Opt (Atom (int_k, int_j0)))) =>
blanchet@33192
  1286
        if nat_j0 = int_j0 then
blanchet@34123
  1287
          kk_intersect KK.Iden
blanchet@34123
  1288
              (kk_product (KK.AtomSeq (max_int_for_card int_k + 1, nat_j0))
blanchet@34123
  1289
                          KK.Univ)
blanchet@33192
  1290
        else
blanchet@33224
  1291
          raise BAD ("Nitpick_Kodkod.to_r (NatToInt)", "\"nat_j0 <> int_j0\"")
blanchet@34121
  1292
      | Cst (NatToInt, T as Type ("fun", [@{typ "unsigned_bit word"}, _]), R) =>
blanchet@34121
  1293
        to_bit_word_unary_op T R I
blanchet@34121
  1294
      | Cst (IntToNat, Type ("fun", [@{typ int}, _]),
blanchet@34121
  1295
             Func (Atom (int_k, int_j0), nat_R)) =>
blanchet@33192
  1296
        let
blanchet@33192
  1297
          val abs_card = max_int_for_card int_k + 1
blanchet@33192
  1298
          val (nat_k, nat_j0) = the_single (atom_schema_of_rep nat_R)
blanchet@33192
  1299
          val overlap = Int.min (nat_k, abs_card)
blanchet@33192
  1300
        in
blanchet@33192
  1301
          if nat_j0 = int_j0 then
blanchet@34123
  1302
            kk_union (kk_product (KK.AtomSeq (int_k - abs_card,
blanchet@34123
  1303
                                              int_j0 + abs_card))
blanchet@34123
  1304
                                 (KK.Atom nat_j0))
blanchet@34123
  1305
                     (kk_intersect KK.Iden
blanchet@34123
  1306
                          (kk_product (KK.AtomSeq (overlap, int_j0)) KK.Univ))
blanchet@33192
  1307
          else
blanchet@33224
  1308
            raise BAD ("Nitpick_Kodkod.to_r (IntToNat)", "\"nat_j0 <> int_j0\"")
blanchet@33192
  1309
        end
blanchet@34121
  1310
      | Cst (IntToNat, T as Type ("fun", [@{typ "signed_bit word"}, _]), R) =>
blanchet@34121
  1311
        to_bit_word_unary_op T R
blanchet@34123
  1312
            (fn i => KK.IntIf (KK.LE (i, KK.Num 0), KK.Num 0, i))
blanchet@33192
  1313
      | Op1 (Not, _, R, u1) => kk_not3 (to_rep R u1)
blanchet@34123
  1314
      | Op1 (Finite, _, Opt (Atom _), _) => KK.None
blanchet@33192
  1315
      | Op1 (Converse, T, R, u1) =>
blanchet@33192
  1316
        let
blanchet@33192
  1317
          val (b_T, a_T) = HOLogic.dest_prodT (domain_type T)
blanchet@33192
  1318
          val (b_R, a_R) =
blanchet@33192
  1319
            case R of
blanchet@33192
  1320
              Func (Struct [R1, R2], _) => (R1, R2)
blanchet@33192
  1321
            | Func (R1, _) =>
blanchet@33192
  1322
              if card_of_rep R1 <> 1 then
blanchet@33224
  1323
                raise REP ("Nitpick_Kodkod.to_r (Converse)", [R])
blanchet@33192
  1324
              else
blanchet@33192
  1325
                pairself (Atom o pair 1 o offset_of_type ofs) (b_T, a_T)
blanchet@33224
  1326
            | _ => raise REP ("Nitpick_Kodkod.to_r (Converse)", [R])
blanchet@33192
  1327
          val body_R = body_rep R
blanchet@33192
  1328
          val a_arity = arity_of_rep a_R
blanchet@33192
  1329
          val b_arity = arity_of_rep b_R
blanchet@33192
  1330
          val ab_arity = a_arity + b_arity
blanchet@33192
  1331
          val body_arity = arity_of_rep body_R
blanchet@33192
  1332
        in
blanchet@33192
  1333
          kk_project (to_rep (Func (Struct [a_R, b_R], body_R)) u1)
blanchet@34123
  1334
                     (map KK.Num (index_seq a_arity b_arity @
blanchet@34123
  1335
                                  index_seq 0 a_arity @
blanchet@34123
  1336
                                  index_seq ab_arity body_arity))
blanchet@33192
  1337
          |> rel_expr_from_rel_expr kk R (Func (Struct [b_R, a_R], body_R))
blanchet@33192
  1338
        end
blanchet@33192
  1339
      | Op1 (Closure, _, R, u1) =>
blanchet@33192
  1340
        if is_opt_rep R then
blanchet@33192
  1341
          let
blanchet@33192
  1342
            val T1 = type_of u1
blanchet@33192
  1343
            val R' = rep_to_binary_rel_rep ofs T1 (unopt_rep (rep_of u1))
blanchet@33192
  1344
            val R'' = opt_rep ofs T1 R'
blanchet@33192
  1345
          in
blanchet@34121
  1346
            single_rel_rel_let kk
blanchet@33192
  1347
                (fn r =>
blanchet@33192
  1348
                    let
blanchet@33192
  1349
                      val true_r = kk_closure (kk_join r true_atom)
blanchet@33192
  1350
                      val full_r = full_rel_for_rep R'
blanchet@33192
  1351
                      val false_r = kk_difference full_r
blanchet@33192
  1352
                                        (kk_closure (kk_difference full_r
blanchet@33192
  1353
                                                        (kk_join r false_atom)))
blanchet@33192
  1354
                    in
blanchet@33192
  1355
                      rel_expr_from_rel_expr kk R R''
blanchet@33192
  1356
                          (kk_union (kk_product true_r true_atom)
blanchet@33192
  1357
                                    (kk_product false_r false_atom))
blanchet@33192
  1358
                    end) (to_rep R'' u1)
blanchet@33192
  1359
          end
blanchet@33192
  1360
        else
blanchet@33192
  1361
          let val R' = rep_to_binary_rel_rep ofs (type_of u1) (rep_of u1) in
blanchet@33192
  1362
            rel_expr_from_rel_expr kk R R' (kk_closure (to_rep R' u1))
blanchet@33192
  1363
          end
blanchet@33192
  1364
      | Op1 (SingletonSet, _, Func (R1, Opt _), Cst (Unrep, _, _)) =>
blanchet@33192
  1365
        (if R1 = Unit then I else kk_product (full_rel_for_rep R1)) false_atom
blanchet@33192
  1366
      | Op1 (SingletonSet, _, R, u1) =>
blanchet@33192
  1367
        (case R of
blanchet@33192
  1368
           Func (R1, Formula Neut) => to_rep R1 u1
blanchet@33192
  1369
         | Func (Unit, Opt R) => to_guard [u1] R true_atom
blanchet@33192
  1370
         | Func (R1, R2 as Opt _) =>
blanchet@34121
  1371
           single_rel_rel_let kk
blanchet@33192
  1372
               (fn r => kk_rel_if (kk_no r) (empty_rel_for_rep R)
blanchet@33192
  1373
                            (rel_expr_to_func kk R1 bool_atom_R
blanchet@33192
  1374
                                              (Func (R1, Formula Neut)) r))
blanchet@33192
  1375
               (to_opt R1 u1)
blanchet@33224
  1376
         | _ => raise NUT ("Nitpick_Kodkod.to_r (SingletonSet)", [u]))
blanchet@33192
  1377
      | Op1 (Tha, T, R, u1) =>
blanchet@33192
  1378
        if is_opt_rep R then
blanchet@33192
  1379
          kk_join (to_rep (Func (unopt_rep R, Opt bool_atom_R)) u1) true_atom
blanchet@33192
  1380
        else
blanchet@33192
  1381
          to_rep (Func (R, Formula Neut)) u1
blanchet@33192
  1382
      | Op1 (First, T, R, u1) => to_nth_pair_sel 0 T R u1
blanchet@33192
  1383
      | Op1 (Second, T, R, u1) => to_nth_pair_sel 1 T R u1
blanchet@33192
  1384
      | Op1 (Cast, _, R, u1) =>
blanchet@33192
  1385
        ((case rep_of u1 of
blanchet@33192
  1386
            Formula _ =>
blanchet@33192
  1387
            (case unopt_rep R of
blanchet@33192
  1388
               Atom (2, j0) => atom_from_formula kk j0 (to_f u1)
blanchet@33192
  1389
             | _ => raise SAME ())
blanchet@33192
  1390
          | _ => raise SAME ())
blanchet@33192
  1391
         handle SAME () => rel_expr_from_rel_expr kk R (rep_of u1) (to_r u1))
blanchet@33192
  1392
      | Op2 (All, T, R as Opt _, u1, u2) =>
blanchet@33192
  1393
        to_r (Op1 (Not, T, R,
blanchet@33192
  1394
                   Op2 (Exist, T, R, u1, Op1 (Not, T, rep_of u2, u2))))
blanchet@33192
  1395
      | Op2 (Exist, T, Opt _, u1, u2) =>
blanchet@33192
  1396
        let val rs1 = untuple to_decl u1 in
blanchet@33192
  1397
          if not (is_opt_rep (rep_of u2)) then
blanchet@34123
  1398
            kk_rel_if (kk_exist rs1 (to_f u2)) true_atom KK.None
blanchet@33192
  1399
          else
blanchet@33192
  1400
            let val r2 = to_r u2 in
blanchet@33192
  1401
              kk_union (kk_rel_if (kk_exist rs1 (kk_rel_eq r2 true_atom))
blanchet@34123
  1402
                                  true_atom KK.None)
blanchet@33192
  1403
                       (kk_rel_if (kk_all rs1 (kk_rel_eq r2 false_atom))
blanchet@34123
  1404
                                  false_atom KK.None)
blanchet@33192
  1405
            end
blanchet@33192
  1406
        end
blanchet@33192
  1407
      | Op2 (Or, _, _, u1, u2) =>
blanchet@33192
  1408
        if is_opt_rep (rep_of u1) then kk_rel_if (to_f u2) true_atom (to_r u1)
blanchet@33192
  1409
        else kk_rel_if (to_f u1) true_atom (to_r u2)
blanchet@33192
  1410
      | Op2 (And, _, _, u1, u2) =>
blanchet@33192
  1411
        if is_opt_rep (rep_of u1) then kk_rel_if (to_f u2) (to_r u1) false_atom
blanchet@33192
  1412
        else kk_rel_if (to_f u1) (to_r u2) false_atom
blanchet@33192
  1413
      | Op2 (Less, _, _, u1, u2) =>
blanchet@34121
  1414
        (case type_of u1 of
blanchet@34121
  1415
           @{typ nat} =>
blanchet@34121
  1416
           if is_Cst Unrep u1 then to_compare_with_unrep u2 false_atom
blanchet@34121
  1417
           else if is_Cst Unrep u2 then to_compare_with_unrep u1 true_atom
blanchet@34121
  1418
           else kk_nat_less (to_integer u1) (to_integer u2)
blanchet@34121
  1419
         | @{typ int} => kk_int_less (to_integer u1) (to_integer u2)
blanchet@34121
  1420
         | _ => double_rel_rel_let kk
blanchet@34121
  1421
                    (fn r1 => fn r2 =>
blanchet@34121
  1422
                        kk_rel_if
blanchet@34121
  1423
                            (fold kk_and (map_filter (fn (u, r) =>
blanchet@34121
  1424
                                 if is_opt_rep (rep_of u) then SOME (kk_some r)
blanchet@34123
  1425
                                 else NONE) [(u1, r1), (u2, r2)]) KK.True)
blanchet@34123
  1426
                            (atom_from_formula kk bool_j0 (KK.LT (pairself
blanchet@34121
  1427
                                (int_expr_from_atom kk (type_of u1)) (r1, r2))))
blanchet@34123
  1428
                            KK.None)
blanchet@34121
  1429
                    (to_r u1) (to_r u2))
blanchet@33192
  1430
      | Op2 (The, T, R, u1, u2) =>
blanchet@33192
  1431
        if is_opt_rep R then
blanchet@33192
  1432
          let val r1 = to_opt (Func (unopt_rep R, bool_atom_R)) u1 in
blanchet@33192
  1433
            kk_rel_if (kk_one (kk_join r1 true_atom)) (kk_join r1 true_atom)
blanchet@33192
  1434
                      (kk_rel_if (kk_or (kk_some (kk_join r1 true_atom))
blanchet@33192
  1435
                                        (kk_subset (full_rel_for_rep R)
blanchet@33192
  1436
                                                   (kk_join r1 false_atom)))
blanchet@33892
  1437
                                 (to_rep R u2) (empty_rel_for_rep R))
blanchet@33192
  1438
          end
blanchet@33192
  1439
        else
blanchet@33192
  1440
          let val r1 = to_rep (Func (R, Formula Neut)) u1 in
blanchet@33192
  1441
            kk_rel_if (kk_one r1) r1 (to_rep R u2)
blanchet@33192
  1442
          end
blanchet@33192
  1443
      | Op2 (Eps, T, R, u1, u2) =>
blanchet@33192
  1444
        if is_opt_rep (rep_of u1) then
blanchet@33192
  1445
          let
blanchet@33192
  1446
            val r1 = to_rep (Func (unopt_rep R, Opt bool_atom_R)) u1
blanchet@33192
  1447
            val r2 = to_rep R u2
blanchet@33192
  1448
          in
blanchet@33192
  1449
            kk_union (kk_rel_if (kk_one (kk_join r1 true_atom))
blanchet@33892
  1450
                                (kk_join r1 true_atom) (empty_rel_for_rep R))
blanchet@33192
  1451
                     (kk_rel_if (kk_or (kk_subset r2 (kk_join r1 true_atom))
blanchet@33192
  1452
                                       (kk_subset (full_rel_for_rep R)
blanchet@33192
  1453
                                                  (kk_join r1 false_atom)))
blanchet@33892
  1454
                                r2 (empty_rel_for_rep R))
blanchet@33192
  1455
          end
blanchet@33192
  1456
        else
blanchet@33192
  1457
          let
blanchet@33192
  1458
            val r1 = to_rep (Func (unopt_rep R, Formula Neut)) u1
blanchet@33192
  1459
            val r2 = to_rep R u2
blanchet@33192
  1460
          in
blanchet@33892
  1461
            kk_union (kk_rel_if (kk_one r1) r1 (empty_rel_for_rep R))
blanchet@33192
  1462
                     (kk_rel_if (kk_or (kk_no r1) (kk_subset r2 r1))
blanchet@33892
  1463
                                r2 (empty_rel_for_rep R))
blanchet@33192
  1464
          end
blanchet@33192
  1465
      | Op2 (Triad, T, Opt (Atom (2, j0)), u1, u2) =>
blanchet@33192
  1466
        let
blanchet@33192
  1467
          val f1 = to_f u1
blanchet@33192
  1468
          val f2 = to_f u2
blanchet@33192
  1469
        in
blanchet@33192
  1470
          if f1 = f2 then
blanchet@33192
  1471
            atom_from_formula kk j0 f1
blanchet@33192
  1472
          else
blanchet@34123
  1473
            kk_union (kk_rel_if f1 true_atom KK.None)
blanchet@34123
  1474
                     (kk_rel_if f2 KK.None false_atom)
blanchet@33192
  1475
        end
blanchet@33192
  1476
      | Op2 (Union, _, R, u1, u2) =>
blanchet@33192
  1477
        to_set_op kk_or kk_or3 kk_union kk_union kk_intersect false R u1 u2
blanchet@33192
  1478
      | Op2 (SetDifference, _, R, u1, u2) =>
blanchet@33192
  1479
        to_set_op kk_notimplies kk_notimplies3 kk_difference kk_intersect
blanchet@33192
  1480
                  kk_union true R u1 u2
blanchet@33192
  1481
      | Op2 (Intersect, _, R, u1, u2) =>
blanchet@33192
  1482
        to_set_op kk_and kk_and3 kk_intersect kk_intersect kk_union false R
blanchet@33192
  1483
                  u1 u2
blanchet@33192
  1484
      | Op2 (Composition, _, R, u1, u2) =>
blanchet@33192
  1485
        let
blanchet@33863
  1486
          val (a_T, b_T) = HOLogic.dest_prodT (domain_type (type_of u1))
blanchet@33863
  1487
          val (_, c_T) = HOLogic.dest_prodT (domain_type (type_of u2))
blanchet@33863
  1488
          val ab_k = card_of_domain_from_rep 2 (rep_of u1)
blanchet@33863
  1489
          val bc_k = card_of_domain_from_rep 2 (rep_of u2)
blanchet@33192
  1490
          val ac_k = card_of_domain_from_rep 2 R
blanchet@33192
  1491
          val a_k = exact_root 2 (ac_k * ab_k div bc_k)
blanchet@33192
  1492
          val b_k = exact_root 2 (ab_k * bc_k div ac_k)
blanchet@33192
  1493
          val c_k = exact_root 2 (bc_k * ac_k div ab_k)
blanchet@33192
  1494
          val a_R = Atom (a_k, offset_of_type ofs a_T)
blanchet@33192
  1495
          val b_R = Atom (b_k, offset_of_type ofs b_T)
blanchet@33192
  1496
          val c_R = Atom (c_k, offset_of_type ofs c_T)
blanchet@33192
  1497
          val body_R = body_rep R
blanchet@33192
  1498
        in
blanchet@33192
  1499
          (case body_R of
blanchet@33192
  1500
             Formula Neut =>
blanchet@33863
  1501
             kk_join (to_rep (Func (Struct [a_R, b_R], Formula Neut)) u1)
blanchet@33863
  1502
                     (to_rep (Func (Struct [b_R, c_R], Formula Neut)) u2)
blanchet@33192
  1503
           | Opt (Atom (2, _)) =>
blanchet@33192
  1504
             let
blanchet@33886
  1505
               (* FIXME: merge with similar code above *)
blanchet@34123
  1506
               (* rep -> rep -> nut -> KK.rel_expr *)
blanchet@33886
  1507
               fun must R1 R2 u =
blanchet@33886
  1508
                 kk_join (to_rep (Func (Struct [R1, R2], body_R)) u) true_atom
blanchet@33886
  1509
               fun may R1 R2 u =
blanchet@33886
  1510
                 kk_difference
blanchet@33886
  1511
                     (full_rel_for_rep (Struct [R1, R2]))
blanchet@33886
  1512
                     (kk_join (to_rep (Func (Struct [R1, R2], body_R)) u)
blanchet@33886
  1513
                              false_atom)
blanchet@33886
  1514
             in
blanchet@33886
  1515
               kk_union
blanchet@33886
  1516
                   (kk_product (kk_join (must a_R b_R u1) (must b_R c_R u2))
blanchet@33886
  1517
                               true_atom)
blanchet@33886
  1518
                   (kk_product (kk_difference
blanchet@33886
  1519
                                   (full_rel_for_rep (Struct [a_R, c_R]))
blanchet@33886
  1520
                                   (kk_join (may a_R b_R u1) (may b_R c_R u2)))
blanchet@33886
  1521
                               false_atom)
blanchet@33886
  1522
             end
blanchet@33224
  1523
           | _ => raise NUT ("Nitpick_Kodkod.to_r (Composition)", [u]))
blanchet@33192
  1524
          |> rel_expr_from_rel_expr kk R (Func (Struct [a_R, c_R], body_R))
blanchet@33192
  1525
        end
blanchet@33192
  1526
      | Op2 (Product, T, R, u1, u2) =>
blanchet@33192
  1527
        let
blanchet@33192
  1528
          val (a_T, b_T) = HOLogic.dest_prodT (domain_type T)
blanchet@33192
  1529
          val a_k = card_of_domain_from_rep 2 (rep_of u1)
blanchet@33192
  1530
          val b_k = card_of_domain_from_rep 2 (rep_of u2)
blanchet@33192
  1531
          val a_R = Atom (a_k, offset_of_type ofs a_T)
blanchet@33192
  1532
          val b_R = Atom (b_k, offset_of_type ofs b_T)
blanchet@33192
  1533
          val body_R = body_rep R
blanchet@33192
  1534
        in
blanchet@33192
  1535
          (case body_R of
blanchet@33192
  1536
             Formula Neut =>
blanchet@33192
  1537
             kk_product (to_rep (Func (a_R, Formula Neut)) u1)
blanchet@33192
  1538
                        (to_rep (Func (b_R, Formula Neut)) u2)
blanchet@33192
  1539
           | Opt (Atom (2, _)) =>
blanchet@33192
  1540
             let
blanchet@34123
  1541
               (* KK.rel_expr -> rep -> nut -> KK.rel_expr *)
blanchet@33192
  1542
               fun do_nut r R u = kk_join (to_rep (Func (R, body_R)) u) r
blanchet@34123
  1543
               (* KK.rel_expr -> KK.rel_expr *)
blanchet@33192
  1544
               fun do_term r =
blanchet@33192
  1545
                 kk_product (kk_product (do_nut r a_R u1) (do_nut r b_R u2)) r
blanchet@33192
  1546
             in kk_union (do_term true_atom) (do_term false_atom) end
blanchet@33224
  1547
           | _ => raise NUT ("Nitpick_Kodkod.to_r (Product)", [u]))
blanchet@33192
  1548
          |> rel_expr_from_rel_expr kk R (Func (Struct [a_R, b_R], body_R))
blanchet@33192
  1549
        end
blanchet@33192
  1550
      | Op2 (Image, T, R, u1, u2) =>
blanchet@33192
  1551
        (case (rep_of u1, rep_of u2) of
blanchet@33192
  1552
           (Func (R11, R12), Func (R21, Formula Neut)) =>
blanchet@33192
  1553
           if R21 = R11 andalso is_lone_rep R12 then
blanchet@33192
  1554
             let
blanchet@34123
  1555
               (* KK.rel_expr -> KK.rel_expr *)
blanchet@33192
  1556
               fun big_join r = kk_n_fold_join kk false R21 R12 r (to_r u1)
blanchet@33192
  1557
               val core_r = big_join (to_r u2)
blanchet@33192
  1558
               val core_R = Func (R12, Formula Neut)
blanchet@33192
  1559
             in
blanchet@33192
  1560
               if is_opt_rep R12 then
blanchet@33192
  1561
                 let
blanchet@33192
  1562
                   val schema = atom_schema_of_rep R21
blanchet@33192
  1563
                   val decls = decls_for_atom_schema ~1 schema
blanchet@33192
  1564
                   val vars = unary_var_seq ~1 (length decls)
blanchet@33192
  1565
                   val f = kk_some (big_join (fold1 kk_product vars))
blanchet@33192
  1566
                 in
blanchet@33192
  1567
                   kk_rel_if (kk_all decls f)
blanchet@33192
  1568
                             (rel_expr_from_rel_expr kk R core_R core_r)
blanchet@33192
  1569
                             (rel_expr_from_rel_expr kk R (opt_rep ofs T core_R)
blanchet@33192
  1570
                                              (kk_product core_r true_atom))
blanchet@33192
  1571
                 end
blanchet@33192
  1572
               else
blanchet@33192
  1573
                 rel_expr_from_rel_expr kk R core_R core_r
blanchet@33192
  1574
             end
blanchet@33192
  1575
           else
blanchet@33224
  1576
             raise NUT ("Nitpick_Kodkod.to_r (Image)", [u1, u2])
blanchet@33224
  1577
         | _ => raise NUT ("Nitpick_Kodkod.to_r (Image)", [u1, u2]))
blanchet@33192
  1578
      | Op2 (Apply, @{typ nat}, _,
blanchet@33192
  1579
             Op2 (Apply, _, _, Cst (Subtract, _, _), u1), u2) =>
blanchet@33192
  1580
        if is_Cst Unrep u2 andalso not (is_opt_rep (rep_of u1)) then
blanchet@34123
  1581
          KK.Atom (offset_of_type ofs nat_T)
blanchet@33192
  1582
        else
blanchet@34123
  1583
          fold kk_join (map to_integer [u1, u2]) (KK.Rel nat_subtract_rel)
blanchet@33192
  1584
      | Op2 (Apply, _, R, u1, u2) =>
blanchet@33192
  1585
        if is_Cst Unrep u2 andalso is_set_type (type_of u1)
blanchet@33631
  1586
           andalso is_FreeName u1 then
blanchet@33192
  1587
          false_atom
blanchet@33192
  1588
        else
blanchet@33192
  1589
          to_apply R u1 u2
blanchet@33192
  1590
      | Op2 (Lambda, T, R as Opt (Atom (1, j0)), u1, u2) =>
blanchet@34123
  1591
        to_guard [u1, u2] R (KK.Atom j0)
blanchet@33192
  1592
      | Op2 (Lambda, T, Func (_, Formula Neut), u1, u2) =>
blanchet@33192
  1593
        kk_comprehension (untuple to_decl u1) (to_f u2)
blanchet@33192
  1594
      | Op2 (Lambda, T, Func (_, R2), u1, u2) =>
blanchet@33192
  1595
        let
blanchet@33192
  1596
          val dom_decls = untuple to_decl u1
blanchet@33192
  1597
          val ran_schema = atom_schema_of_rep R2
blanchet@33192
  1598
          val ran_decls = decls_for_atom_schema ~1 ran_schema
blanchet@33192
  1599
          val ran_vars = unary_var_seq ~1 (length ran_decls)
blanchet@33192
  1600
        in
blanchet@33192
  1601
          kk_comprehension (dom_decls @ ran_decls)
blanchet@33192
  1602
                           (kk_subset (fold1 kk_product ran_vars)
blanchet@33192
  1603
                                      (to_rep R2 u2))
blanchet@33192
  1604
        end
blanchet@33192
  1605
      | Op3 (Let, _, R, u1, u2, u3) =>
blanchet@33192
  1606
        kk_rel_let [to_expr_assign u1 u2] (to_rep R u3)
blanchet@33192
  1607
      | Op3 (If, _, R, u1, u2, u3) =>
blanchet@33192
  1608
        if is_opt_rep (rep_of u1) then
blanchet@34121
  1609
          tripl_rel_rel_let kk
blanchet@33192
  1610
              (fn r1 => fn r2 => fn r3 =>
blanchet@33192
  1611
                  let val empty_r = empty_rel_for_rep R in
blanchet@33192
  1612
                    fold1 kk_union
blanchet@33192
  1613
                          [kk_rel_if (kk_rel_eq r1 true_atom) r2 empty_r,
blanchet@33192
  1614
                           kk_rel_if (kk_rel_eq r1 false_atom) r3 empty_r,
blanchet@33192
  1615
                           kk_rel_if (kk_rel_eq r2 r3)
blanchet@33192
  1616
                                (if inline_rel_expr r2 then r2 else r3) empty_r]
blanchet@33192
  1617
                  end)
blanchet@33192
  1618
              (to_r u1) (to_rep R u2) (to_rep R u3)
blanchet@33192
  1619
        else
blanchet@33192
  1620
          kk_rel_if (to_f u1) (to_rep R u2) (to_rep R u3)
blanchet@33192
  1621
      | Tuple (_, R, us) =>
blanchet@33192
  1622
        (case unopt_rep R of
blanchet@33192
  1623
           Struct Rs => to_product Rs us
blanchet@33192
  1624
         | Vect (k, R) => to_product (replicate k R) us
blanchet@33192
  1625
         | Atom (1, j0) =>
blanchet@33192
  1626
           (case filter (not_equal Unit o rep_of) us of
blanchet@34123
  1627
              [] => KK.Atom j0
blanchet@34123
  1628
            | us' => kk_rel_if (kk_some (fold1 kk_product (map to_r us')))
blanchet@34123
  1629
                               (KK.Atom j0) KK.None)
blanchet@33224
  1630
         | _ => raise NUT ("Nitpick_Kodkod.to_r (Tuple)", [u]))
blanchet@33192
  1631
      | Construct ([u'], _, _, []) => to_r u'
blanchet@34288
  1632
      | Construct (discr_u :: sel_us, T, R, arg_us) =>
blanchet@33192
  1633
        let
blanchet@33192
  1634
          val set_rs =
blanchet@33192
  1635
            map2 (fn sel_u => fn arg_u =>
blanchet@33192
  1636
                     let
blanchet@33192
  1637
                       val (R1, R2) = dest_Func (rep_of sel_u)
blanchet@33192
  1638
                       val sel_r = to_r sel_u
blanchet@33192
  1639
                       val arg_r = to_opt R2 arg_u
blanchet@33192
  1640
                     in
blanchet@33192
  1641
                       if is_one_rep R2 then
blanchet@33192
  1642
                         kk_n_fold_join kk true R2 R1 arg_r
blanchet@33192
  1643
                              (kk_project sel_r (flip_nums (arity_of_rep R2)))
blanchet@33192
  1644
                       else
blanchet@34288
  1645
                         kk_comprehension [KK.DeclOne ((1, ~1), to_r discr_u)]
blanchet@34123
  1646
                             (kk_rel_eq (kk_join (KK.Var (1, ~1)) sel_r) arg_r)
blanchet@33192
  1647
                     end) sel_us arg_us
blanchet@33192
  1648
        in fold1 kk_intersect set_rs end
blanchet@34123
  1649
      | BoundRel (x, _, _, _) => KK.Var x
blanchet@34123
  1650
      | FreeRel (x, _, _, _) => KK.Rel x
blanchet@34123
  1651
      | RelReg (j, _, R) => KK.RelReg (arity_of_rep R, j)
blanchet@33224
  1652
      | u => raise NUT ("Nitpick_Kodkod.to_r", [u])
blanchet@34123
  1653
    (* nut -> KK.decl *)
blanchet@33192
  1654
    and to_decl (BoundRel (x, _, R, _)) =
blanchet@34123
  1655
        KK.DeclOne (x, KK.AtomSeq (the_single (atom_schema_of_rep R)))
blanchet@33224
  1656
      | to_decl u = raise NUT ("Nitpick_Kodkod.to_decl", [u])
blanchet@34123
  1657
    (* nut -> KK.expr_assign *)
blanchet@33192
  1658
    and to_expr_assign (FormulaReg (j, _, R)) u =
blanchet@34123
  1659
        KK.AssignFormulaReg (j, to_f u)
blanchet@33192
  1660
      | to_expr_assign (RelReg (j, _, R)) u =
blanchet@34123
  1661
        KK.AssignRelReg ((arity_of_rep R, j), to_r u)
blanchet@33224
  1662
      | to_expr_assign u1 _ = raise NUT ("Nitpick_Kodkod.to_expr_assign", [u1])
blanchet@34123
  1663
    (* int * int -> nut -> KK.rel_expr *)
blanchet@33192
  1664
    and to_atom (x as (k, j0)) u =
blanchet@33192
  1665
      case rep_of u of
blanchet@33192
  1666
        Formula _ => atom_from_formula kk j0 (to_f u)
blanchet@34123
  1667
      | Unit => if k = 1 then KK.Atom j0
blanchet@33224
  1668
                else raise NUT ("Nitpick_Kodkod.to_atom", [u])
blanchet@33192
  1669
      | R => atom_from_rel_expr kk x R (to_r u)
blanchet@34123
  1670
    (* rep list -> nut -> KK.rel_expr *)
blanchet@33192
  1671
    and to_struct Rs u =
blanchet@33192
  1672
      case rep_of u of
blanchet@33192
  1673
        Unit => full_rel_for_rep (Struct Rs)
blanchet@33192
  1674
      | R' => struct_from_rel_expr kk Rs R' (to_r u)
blanchet@34123
  1675
    (* int -> rep -> nut -> KK.rel_expr *)
blanchet@33192
  1676
    and to_vect k R u =
blanchet@33192
  1677
      case rep_of u of
blanchet@33192
  1678
        Unit => full_rel_for_rep (Vect (k, R))
blanchet@33192
  1679
      | R' => vect_from_rel_expr kk k R R' (to_r u)
blanchet@34123
  1680
    (* rep -> rep -> nut -> KK.rel_expr *)
blanchet@33192
  1681
    and to_func R1 R2 u =
blanchet@33192
  1682
      case rep_of u of
blanchet@33192
  1683
        Unit => full_rel_for_rep (Func (R1, R2))
blanchet@33192
  1684
      | R' => rel_expr_to_func kk R1 R2 R' (to_r u)
blanchet@34123
  1685
    (* rep -> nut -> KK.rel_expr *)
blanchet@33192
  1686
    and to_opt R u =
blanchet@33192
  1687
      let val old_R = rep_of u in
blanchet@33192
  1688
        if is_opt_rep old_R then
blanchet@33192
  1689
          rel_expr_from_rel_expr kk (Opt R) old_R (to_r u)
blanchet@33192
  1690
        else
blanchet@33192
  1691
          to_rep R u
blanchet@33192
  1692
      end
blanchet@34123
  1693
    (* rep -> nut -> KK.rel_expr *)
blanchet@33192
  1694
    and to_rep (Atom x) u = to_atom x u
blanchet@33192
  1695
      | to_rep (Struct Rs) u = to_struct Rs u
blanchet@33192
  1696
      | to_rep (Vect (k, R)) u = to_vect k R u
blanchet@33192
  1697
      | to_rep (Func (R1, R2)) u = to_func R1 R2 u
blanchet@33192
  1698
      | to_rep (Opt R) u = to_opt R u
blanchet@33224
  1699
      | to_rep R _ = raise REP ("Nitpick_Kodkod.to_rep", [R])
blanchet@34123
  1700
    (* nut -> KK.rel_expr *)
blanchet@33192
  1701
    and to_integer u = to_opt (one_rep ofs (type_of u) (rep_of u)) u
blanchet@34123
  1702
    (* nut list -> rep -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
  1703
    and to_guard guard_us R r =
blanchet@33192
  1704
      let
blanchet@33192
  1705
        val unpacked_rs = unpack_joins r
blanchet@33192
  1706
        val plain_guard_rs =
blanchet@33192
  1707
          map to_r (filter (is_Opt o rep_of) guard_us)
blanchet@33192
  1708
          |> filter_out (member (op =) unpacked_rs)
blanchet@33192
  1709
        val func_guard_us =
blanchet@33192
  1710
          filter ((is_Func andf is_opt_rep) o rep_of) guard_us
blanchet@33192
  1711
        val func_guard_rs = map to_r func_guard_us
blanchet@33192
  1712
        val guard_fs =
blanchet@33192
  1713
          map kk_no plain_guard_rs @
blanchet@33192
  1714
          map2 (kk_not oo kk_n_ary_function kk)
blanchet@33192
  1715
               (map (unopt_rep o rep_of) func_guard_us) func_guard_rs
blanchet@33192
  1716
      in
blanchet@33192
  1717
        if null guard_fs then
blanchet@33192
  1718
          r
blanchet@33192
  1719
        else
blanchet@33192
  1720
          kk_rel_if (fold1 kk_or guard_fs) (empty_rel_for_rep R) r
blanchet@33192
  1721
      end
blanchet@34123
  1722
    (* rep -> rep -> KK.rel_expr -> int -> KK.rel_expr *)
blanchet@33192
  1723
    and to_project new_R old_R r j0 =
blanchet@33192
  1724
      rel_expr_from_rel_expr kk new_R old_R
blanchet@33192
  1725
                             (kk_project_seq r j0 (arity_of_rep old_R))
blanchet@34123
  1726
    (* rep list -> nut list -> KK.rel_expr *)
blanchet@33192
  1727
    and to_product Rs us =
blanchet@33192
  1728
      case map (uncurry to_opt) (filter (not_equal Unit o fst) (Rs ~~ us)) of
blanchet@33224
  1729
        [] => raise REP ("Nitpick_Kodkod.to_product", Rs)
blanchet@33192
  1730
      | rs => fold1 kk_product rs
blanchet@34123
  1731
    (* int -> typ -> rep -> nut -> KK.rel_expr *)
blanchet@33192
  1732
    and to_nth_pair_sel n res_T res_R u =
blanchet@33192
  1733
      case u of
blanchet@33192
  1734
        Tuple (_, _, us) => to_rep res_R (nth us n)
blanchet@33192
  1735
      | _ => let
blanchet@33192
  1736
               val R = rep_of u
blanchet@33192
  1737
               val (a_T, b_T) = HOLogic.dest_prodT (type_of u)
blanchet@33192
  1738
               val Rs =
blanchet@33192
  1739
                 case unopt_rep R of
blanchet@33192
  1740
                   Struct (Rs as [_, _]) => Rs
blanchet@33192
  1741
                 | _ =>
blanchet@33192
  1742
                   let
blanchet@33192
  1743
                     val res_card = card_of_rep res_R
blanchet@33192
  1744
                     val other_card = card_of_rep R div res_card
blanchet@33192
  1745
                     val (a_card, b_card) = (res_card, other_card)
blanchet@33192
  1746
                                            |> n = 1 ? swap
blanchet@33192
  1747
                   in
blanchet@33192
  1748
                     [Atom (a_card, offset_of_type ofs a_T),
blanchet@33192
  1749
                      Atom (b_card, offset_of_type ofs b_T)]
blanchet@33192
  1750
                   end
blanchet@33192
  1751
               val nth_R = nth Rs n
blanchet@33192
  1752
               val j0 = if n = 0 then 0 else arity_of_rep (hd Rs)
blanchet@33192
  1753
             in
blanchet@33192
  1754
               case arity_of_rep nth_R of
blanchet@33192
  1755
                 0 => to_guard [u] res_R
blanchet@33192
  1756
                               (to_rep res_R (Cst (Unity, res_T, Unit)))
blanchet@33192
  1757
               | arity => to_project res_R nth_R (to_rep (Opt (Struct Rs)) u) j0
blanchet@33192
  1758
             end
blanchet@34123
  1759
    (* (KK.formula -> KK.formula -> KK.formula)
blanchet@34123
  1760
       -> (KK.rel_expr -> KK.rel_expr -> KK.formula) -> nut -> nut
blanchet@34123
  1761
       -> KK.formula *)
blanchet@33192
  1762
    and to_set_bool_op connective set_oper u1 u2 =
blanchet@33192
  1763
      let
blanchet@33192
  1764
        val min_R = min_rep (rep_of u1) (rep_of u2)
blanchet@33192
  1765
        val r1 = to_rep min_R u1
blanchet@33192
  1766
        val r2 = to_rep min_R u2
blanchet@33192
  1767
      in
blanchet@33192
  1768
        case min_R of
blanchet@33192
  1769
          Vect (k, Atom _) => kk_vect_set_bool_op connective k r1 r2
blanchet@33192
  1770
        | Func (R1, Formula Neut) => set_oper r1 r2
blanchet@33192
  1771
        | Func (Unit, Atom (2, j0)) =>
blanchet@33192
  1772
          connective (formula_from_atom j0 r1) (formula_from_atom j0 r2)
blanchet@33192
  1773
        | Func (R1, Atom _) => set_oper (kk_join r1 true_atom)
blanchet@33192
  1774
                                        (kk_join r2 true_atom)
blanchet@33224
  1775
        | _ => raise REP ("Nitpick_Kodkod.to_set_bool_op", [min_R])
blanchet@33192
  1776
      end
blanchet@34123
  1777
    (* (KK.formula -> KK.formula -> KK.formula)
blanchet@34123
  1778
       -> (KK.rel_expr -> KK.rel_expr -> KK.rel_expr)
blanchet@34123
  1779
       -> (KK.rel_expr -> KK.rel_expr -> KK.formula)
blanchet@34123
  1780
       -> (KK.rel_expr -> KK.rel_expr -> KK.formula)
blanchet@34123
  1781
       -> (KK.rel_expr -> KK.rel_expr -> KK.formula) -> bool -> rep -> nut
blanchet@34123
  1782
       -> nut -> KK.rel_expr *)
blanchet@33192
  1783
    and to_set_op connective connective3 set_oper true_set_oper false_set_oper
blanchet@33192
  1784
                  neg_second R u1 u2 =
blanchet@33192
  1785
      let
blanchet@33192
  1786
        val min_R = min_rep (rep_of u1) (rep_of u2)
blanchet@33192
  1787
        val r1 = to_rep min_R u1
blanchet@33192
  1788
        val r2 = to_rep min_R u2
blanchet@33192
  1789
        val unopt_R = unopt_rep R
blanchet@33192
  1790
      in
blanchet@33192
  1791
        rel_expr_from_rel_expr kk unopt_R (unopt_rep min_R)
blanchet@33192
  1792
            (case min_R of
blanchet@33192
  1793
               Opt (Vect (k, Atom _)) => kk_vect_set_op connective k r1 r2
blanchet@33192
  1794
             | Vect (k, Atom _) => kk_vect_set_op connective k r1 r2
blanchet@33192
  1795
             | Func (_, Formula Neut) => set_oper r1 r2
blanchet@33192
  1796
             | Func (Unit, _) => connective3 r1 r2
blanchet@33192
  1797
             | Func (R1, _) =>
blanchet@34121
  1798
               double_rel_rel_let kk
blanchet@33192
  1799
                   (fn r1 => fn r2 =>
blanchet@33192
  1800
                       kk_union
blanchet@33192
  1801
                           (kk_product
blanchet@33192
  1802
                                (true_set_oper (kk_join r1 true_atom)
blanchet@33192
  1803
                                     (kk_join r2 (atom_for_bool bool_j0
blanchet@33192
  1804
                                                             (not neg_second))))
blanchet@33192
  1805
                                true_atom)
blanchet@33192
  1806
                           (kk_product
blanchet@33192
  1807
                                (false_set_oper (kk_join r1 false_atom)
blanchet@33192
  1808
                                     (kk_join r2 (atom_for_bool bool_j0
blanchet@33192
  1809
                                                                neg_second)))
blanchet@33192
  1810
                                false_atom))
blanchet@33192
  1811
                   r1 r2
blanchet@33224
  1812
             | _ => raise REP ("Nitpick_Kodkod.to_set_op", [min_R]))
blanchet@33192
  1813
      end
blanchet@34123
  1814
    (* typ -> rep -> (KK.int_expr -> KK.int_expr) -> KK.rel_expr *)
blanchet@34121
  1815
    and to_bit_word_unary_op T R oper =
blanchet@34121
  1816
      let
blanchet@34121
  1817
        val Ts = strip_type T ||> single |> op @
blanchet@34123
  1818
        (* int -> KK.int_expr *)
blanchet@34123
  1819
        fun int_arg j = int_expr_from_atom kk (nth Ts j) (KK.Var (1, j))
blanchet@34121
  1820
      in
blanchet@34121
  1821
        kk_comprehension (decls_for_atom_schema 0 (atom_schema_of_rep R))
blanchet@34123
  1822
            (KK.FormulaLet
blanchet@34123
  1823
                 (map (fn j => KK.AssignIntReg (j, int_arg j)) (0 upto 1),
blanchet@34123
  1824
                  KK.IntEq (KK.IntReg 1, oper (KK.IntReg 0))))
blanchet@34121
  1825
      end
blanchet@34123
  1826
    (* typ -> rep -> (KK.int_expr -> KK.int_expr -> KK.int_expr -> bool) option
blanchet@34123
  1827
       -> (KK.int_expr -> KK.int_expr -> KK.int_expr) option -> KK.rel_expr *)
blanchet@34121
  1828
    and to_bit_word_binary_op T R opt_guard opt_oper =
blanchet@34121
  1829
      let
blanchet@34121
  1830
        val Ts = strip_type T ||> single |> op @
blanchet@34123
  1831
        (* int -> KK.int_expr *)
blanchet@34123
  1832
        fun int_arg j = int_expr_from_atom kk (nth Ts j) (KK.Var (1, j))
blanchet@34121
  1833
      in
blanchet@34121
  1834
        kk_comprehension (decls_for_atom_schema 0 (atom_schema_of_rep R))
blanchet@34123
  1835
            (KK.FormulaLet
blanchet@34123
  1836
                 (map (fn j => KK.AssignIntReg (j, int_arg j)) (0 upto 2),
blanchet@34121
  1837
                  fold1 kk_and
blanchet@34121
  1838
                        ((case opt_guard of
blanchet@34121
  1839
                            NONE => []
blanchet@34121
  1840
                          | SOME guard =>
blanchet@34123
  1841
                            [guard (KK.IntReg 0) (KK.IntReg 1) (KK.IntReg 2)]) @
blanchet@34121
  1842
                         (case opt_oper of
blanchet@34121
  1843
                            NONE => []
blanchet@34121
  1844
                          | SOME oper =>
blanchet@34123
  1845
                            [KK.IntEq (KK.IntReg 2,
blanchet@34123
  1846
                                       oper (KK.IntReg 0) (KK.IntReg 1))]))))
blanchet@34121
  1847
      end
blanchet@34123
  1848
    (* rep -> rep -> KK.rel_expr -> nut -> KK.rel_expr *)
blanchet@33192
  1849
    and to_apply res_R func_u arg_u =
blanchet@33192
  1850
      case unopt_rep (rep_of func_u) of
blanchet@33192
  1851
        Unit =>
blanchet@33192
  1852
        let val j0 = offset_of_type ofs (type_of func_u) in
blanchet@33192
  1853
          to_guard [arg_u] res_R
blanchet@34123
  1854
                   (rel_expr_from_rel_expr kk res_R (Atom (1, j0)) (KK.Atom j0))
blanchet@33192
  1855
        end
blanchet@33192
  1856
      | Atom (1, j0) =>
blanchet@33192
  1857
        to_guard [arg_u] res_R
blanchet@33192
  1858
                 (rel_expr_from_rel_expr kk res_R (Atom (1, j0)) (to_r func_u))
blanchet@33192
  1859
      | Atom (k, j0) =>
blanchet@33192
  1860
        let
blanchet@33192
  1861
          val dom_card = card_of_rep (rep_of arg_u)
blanchet@33192
  1862
          val ran_R = Atom (exact_root dom_card k,
blanchet@33192
  1863
                            offset_of_type ofs (range_type (type_of func_u)))
blanchet@33192
  1864
        in
blanchet@33192
  1865
          to_apply_vect dom_card ran_R res_R (to_vect dom_card ran_R func_u)
blanchet@33192
  1866
                        arg_u
blanchet@33192
  1867
        end
blanchet@33192
  1868
      | Vect (1, R') =>
blanchet@33192
  1869
        to_guard [arg_u] res_R
blanchet@33192
  1870
                 (rel_expr_from_rel_expr kk res_R R' (to_r func_u))
blanchet@33192
  1871
      | Vect (k, R') => to_apply_vect k R' res_R (to_r func_u) arg_u
blanchet@33192
  1872
      | Func (R, Formula Neut) =>
blanchet@33192
  1873
        to_guard [arg_u] res_R (rel_expr_from_formula kk res_R
blanchet@33192
  1874
                                    (kk_subset (to_opt R arg_u) (to_r func_u)))
blanchet@33192
  1875
      | Func (Unit, R2) =>
blanchet@33192
  1876
        to_guard [arg_u] res_R
blanchet@33192
  1877
                 (rel_expr_from_rel_expr kk res_R R2 (to_r func_u))
blanchet@33192
  1878
      | Func (R1, R2) =>
blanchet@33192
  1879
        rel_expr_from_rel_expr kk res_R R2
blanchet@33192
  1880
            (kk_n_fold_join kk true R1 R2 (to_opt R1 arg_u) (to_r func_u))
blanchet@33192
  1881
        |> body_rep R2 = Formula Neut ? to_guard [arg_u] res_R
blanchet@33224
  1882
      | _ => raise NUT ("Nitpick_Kodkod.to_apply", [func_u])
blanchet@34123
  1883
    (* int -> rep -> rep -> KK.rel_expr -> nut *)
blanchet@33192
  1884
    and to_apply_vect k R' res_R func_r arg_u =
blanchet@33192
  1885
      let
blanchet@33192
  1886
        val arg_R = one_rep ofs (type_of arg_u) (unopt_rep (rep_of arg_u))
blanchet@33192
  1887
        val vect_r = vect_from_rel_expr kk k res_R (Vect (k, R')) func_r
blanchet@33192
  1888
        val vect_rs = unpack_vect_in_chunks kk (arity_of_rep res_R) k vect_r
blanchet@33192
  1889
      in
blanchet@33192
  1890
        kk_case_switch kk arg_R res_R (to_opt arg_R arg_u)
blanchet@33192
  1891
                       (all_singletons_for_rep arg_R) vect_rs
blanchet@33192
  1892
      end
blanchet@34123
  1893
    (* bool -> nut -> KK.formula *)
blanchet@33192
  1894
    and to_could_be_unrep neg u =
blanchet@34123
  1895
      if neg andalso is_opt_rep (rep_of u) then kk_no (to_r u) else KK.False
blanchet@34123
  1896
    (* nut -> KK.rel_expr -> KK.rel_expr *)
blanchet@33192
  1897
    and to_compare_with_unrep u r =
blanchet@33892
  1898
      if is_opt_rep (rep_of u) then
blanchet@33892
  1899
        kk_rel_if (kk_some (to_r u)) r (empty_rel_for_rep (rep_of u))
blanchet@33892
  1900
      else
blanchet@33892
  1901
        r
blanchet@33192
  1902
  in to_f_with_polarity Pos u end
blanchet@33192
  1903
blanchet@33192
  1904
end;