src/HOL/Tools/Nitpick/nitpick_peephole.ML
author blanchet
Mon, 03 Mar 2014 22:33:22 +0100
changeset 55889 6bfbec3dff62
parent 45398 7dbb7b044a11
permissions -rw-r--r--
tuned code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33982
1ae222745c4a fixed paths in Nitpick's ML file headers
blanchet
parents: 33705
diff changeset
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_peephole.ML
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34936
diff changeset
     3
    Copyright   2008, 2009, 2010
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     4
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     5
Peephole optimizer for Nitpick.
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     6
*)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     7
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     8
signature NITPICK_PEEPHOLE =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     9
sig
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    10
  type n_ary_index = Kodkod.n_ary_index
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    11
  type formula = Kodkod.formula
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    12
  type int_expr = Kodkod.int_expr
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    13
  type rel_expr = Kodkod.rel_expr
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    14
  type decl = Kodkod.decl
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    15
  type expr_assign = Kodkod.expr_assign
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    16
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    17
  type name_pool =
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    18
    {rels: n_ary_index list,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    19
     vars: n_ary_index list,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    20
     formula_reg: int,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    21
     rel_reg: int}
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    22
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    23
  val initial_pool : name_pool
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    24
  val not3_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    25
  val suc_rel : n_ary_index
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
    26
  val suc_rels_base : int
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    27
  val unsigned_bit_word_sel_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    28
  val signed_bit_word_sel_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    29
  val nat_add_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    30
  val int_add_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    31
  val nat_subtract_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    32
  val int_subtract_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    33
  val nat_multiply_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    34
  val int_multiply_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    35
  val nat_divide_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    36
  val int_divide_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    37
  val nat_less_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    38
  val int_less_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    39
  val gcd_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    40
  val lcm_rel : n_ary_index
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    41
  val norm_frac_rel : n_ary_index
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    42
  val atom_for_bool : int -> bool -> rel_expr
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    43
  val formula_for_bool : bool -> formula
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    44
  val atom_for_nat : int * int -> int -> int
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    45
  val min_int_for_card : int -> int
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    46
  val max_int_for_card : int -> int
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    47
  val int_for_atom : int * int -> int -> int
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    48
  val atom_for_int : int * int -> int -> int
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
    49
  val is_twos_complement_representable : int -> int -> bool
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
    50
  val suc_rel_for_atom_seq : (int * int) * bool -> n_ary_index
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
    51
  val atom_seq_for_suc_rel : n_ary_index -> (int * int) * bool
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    52
  val inline_rel_expr : rel_expr -> bool
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    53
  val empty_n_ary_rel : int -> rel_expr
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    54
  val num_seq : int -> int -> int_expr list
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    55
  val s_and : formula -> formula -> formula
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    56
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    57
  type kodkod_constrs =
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    58
    {kk_all: decl list -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    59
     kk_exist: decl list -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    60
     kk_formula_let: expr_assign list -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    61
     kk_formula_if: formula -> formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    62
     kk_or: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    63
     kk_not: formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    64
     kk_iff: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    65
     kk_implies: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    66
     kk_and: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    67
     kk_subset: rel_expr -> rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    68
     kk_rel_eq: rel_expr -> rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    69
     kk_no: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    70
     kk_lone: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    71
     kk_one: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    72
     kk_some: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    73
     kk_rel_let: expr_assign list -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    74
     kk_rel_if: formula -> rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    75
     kk_union: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    76
     kk_difference: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    77
     kk_override: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    78
     kk_intersect: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    79
     kk_product: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    80
     kk_join: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    81
     kk_closure: rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    82
     kk_reflexive_closure: rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    83
     kk_comprehension: decl list -> formula -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    84
     kk_project: rel_expr -> int_expr list -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    85
     kk_project_seq: rel_expr -> int -> int -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    86
     kk_not3: rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    87
     kk_nat_less: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    88
     kk_int_less: rel_expr -> rel_expr -> rel_expr}
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    89
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    90
  val kodkod_constrs : bool -> int -> int -> int -> kodkod_constrs
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    91
end;
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    92
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33192
diff changeset
    93
structure Nitpick_Peephole : NITPICK_PEEPHOLE =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    94
struct
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    95
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    96
open Kodkod
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33192
diff changeset
    97
open Nitpick_Util
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    98
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
    99
type name_pool =
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   100
  {rels: n_ary_index list,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   101
   vars: n_ary_index list,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   102
   formula_reg: int,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   103
   rel_reg: int}
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   104
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   105
(* FIXME: needed? *)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   106
val initial_pool = {rels = [], vars = [], formula_reg = 10, rel_reg = 10}
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   107
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   108
val not3_rel = (2, ~1)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   109
val unsigned_bit_word_sel_rel = (2, ~2)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   110
val signed_bit_word_sel_rel = (2, ~3)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   111
val suc_rel = (2, ~4)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   112
val suc_rels_base = ~5 (* must be the last of the binary series *)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   113
val nat_add_rel = (3, ~1)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   114
val int_add_rel = (3, ~2)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   115
val nat_subtract_rel = (3, ~3)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   116
val int_subtract_rel = (3, ~4)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   117
val nat_multiply_rel = (3, ~5)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   118
val int_multiply_rel = (3, ~6)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   119
val nat_divide_rel = (3, ~7)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   120
val int_divide_rel = (3, ~8)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   121
val nat_less_rel = (3, ~9)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   122
val int_less_rel = (3, ~10)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   123
val gcd_rel = (3, ~11)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   124
val lcm_rel = (3, ~12)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   125
val norm_frac_rel = (4, ~1)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   126
35385
29f81babefd7 improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents: 35284
diff changeset
   127
fun atom_for_bool j0 = Atom o Integer.add j0 o int_from_bool
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   128
fun formula_for_bool b = if b then True else False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   129
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   130
fun atom_for_nat (k, j0) n = if n < 0 orelse n >= k then ~1 else n + j0
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   131
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   132
fun min_int_for_card k = ~k div 2 + 1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   133
fun max_int_for_card k = k div 2
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   134
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   135
fun int_for_atom (k, j0) j =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   136
  let val j = j - j0 in if j <= max_int_for_card k then j else j - k end
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   137
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   138
fun atom_for_int (k, j0) n =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   139
  if n < min_int_for_card k orelse n > max_int_for_card k then ~1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   140
  else if n < 0 then n + k + j0
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   141
  else n + j0
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   142
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   143
fun is_twos_complement_representable bits n =
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   144
  let val max = reasonable_power 2 bits in n >= ~ max andalso n < max end
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   145
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   146
val max_squeeze_card = 49
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   147
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   148
fun squeeze (m, n) =
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   149
  if n > max_squeeze_card then
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   150
    raise TOO_LARGE ("Nitpick_Peephole.squeeze",
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   151
                     "too large cardinality (" ^ string_of_int n ^ ")")
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   152
  else
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   153
    (max_squeeze_card + 1) * m + n
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   154
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   155
fun unsqueeze p = (p div (max_squeeze_card + 1), p mod (max_squeeze_card + 1))
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   156
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   157
fun boolify (j, b) = 2 * j + (if b then 0 else 1)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   158
fun unboolify j = (j div 2, j mod 2 = 0)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   159
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   160
fun suc_rel_for_atom_seq (x, tabulate) =
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   161
  (2, suc_rels_base - boolify (squeeze x, tabulate))
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   162
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   163
fun atom_seq_for_suc_rel (_, j) = unboolify (~ j + suc_rels_base) |>> unsqueeze
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   164
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   165
fun is_none_product (Product (r1, r2)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   166
    is_none_product r1 orelse is_none_product r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   167
  | is_none_product None = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   168
  | is_none_product _ = false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   169
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   170
fun is_one_rel_expr (Atom _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   171
  | is_one_rel_expr (AtomSeq (1, _)) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   172
  | is_one_rel_expr (Var _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   173
  | is_one_rel_expr _ = false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   174
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   175
fun inline_rel_expr (Product (r1, r2)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   176
    inline_rel_expr r1 andalso inline_rel_expr r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   177
  | inline_rel_expr Iden = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   178
  | inline_rel_expr Ints = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   179
  | inline_rel_expr None = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   180
  | inline_rel_expr Univ = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   181
  | inline_rel_expr (Atom _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   182
  | inline_rel_expr (AtomSeq _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   183
  | inline_rel_expr (Rel _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   184
  | inline_rel_expr (Var _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   185
  | inline_rel_expr (RelReg _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   186
  | inline_rel_expr _ = false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   187
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   188
fun rel_expr_equal None (Atom _) = SOME false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   189
  | rel_expr_equal None (AtomSeq (k, _)) = SOME (k = 0)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   190
  | rel_expr_equal (Atom _) None = SOME false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   191
  | rel_expr_equal (AtomSeq (k, _)) None = SOME (k = 0)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   192
  | rel_expr_equal (Atom j1) (Atom j2) = SOME (j1 = j2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   193
  | rel_expr_equal (Atom j) (AtomSeq (k, j0)) = SOME (j = j0 andalso k = 1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   194
  | rel_expr_equal (AtomSeq (k, j0)) (Atom j) = SOME (j = j0 andalso k = 1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   195
  | rel_expr_equal (AtomSeq x1) (AtomSeq x2) = SOME (x1 = x2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   196
  | rel_expr_equal r1 r2 = if r1 = r2 then SOME true else NONE
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   197
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   198
fun rel_expr_intersects (Atom j1) (Atom j2) = SOME (j1 = j2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   199
  | rel_expr_intersects (Atom j) (AtomSeq (k, j0)) = SOME (j < j0 + k)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   200
  | rel_expr_intersects (AtomSeq (k, j0)) (Atom j) = SOME (j < j0 + k)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   201
  | rel_expr_intersects (AtomSeq (k1, j01)) (AtomSeq (k2, j02)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   202
    SOME (k1 > 0 andalso k2 > 0 andalso j01 + k1 > j02 andalso j02 + k2 > j01)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   203
  | rel_expr_intersects r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   204
    if is_none_product r1 orelse is_none_product r2 then SOME false else NONE
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   205
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33192
diff changeset
   206
fun empty_n_ary_rel 0 = raise ARG ("Nitpick_Peephole.empty_n_ary_rel", "0")
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   207
  | empty_n_ary_rel n = funpow (n - 1) (curry Product None) None
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   208
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   209
fun decl_one_set (DeclOne (_, r)) = r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   210
  | decl_one_set _ =
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33192
diff changeset
   211
    raise ARG ("Nitpick_Peephole.decl_one_set", "not \"DeclOne\"")
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   212
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   213
fun is_Num (Num _) = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   214
  | is_Num _ = false
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   215
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   216
fun dest_Num (Num k) = k
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33192
diff changeset
   217
  | dest_Num _ = raise ARG ("Nitpick_Peephole.dest_Num", "not \"Num\"")
55889
6bfbec3dff62 tuned code
blanchet
parents: 45398
diff changeset
   218
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   219
fun num_seq j0 n = map Num (index_seq j0 n)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   220
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   221
fun occurs_in_union r (Union (r1, r2)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   222
    occurs_in_union r r1 orelse occurs_in_union r r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   223
  | occurs_in_union r r' = (r = r')
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   224
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   225
fun s_and True f2 = f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   226
  | s_and False _ = False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   227
  | s_and f1 True = f1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   228
  | s_and _ False = False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   229
  | s_and f1 f2 = And (f1, f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   230
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   231
type kodkod_constrs =
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   232
  {kk_all: decl list -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   233
   kk_exist: decl list -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   234
   kk_formula_let: expr_assign list -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   235
   kk_formula_if: formula -> formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   236
   kk_or: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   237
   kk_not: formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   238
   kk_iff: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   239
   kk_implies: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   240
   kk_and: formula -> formula -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   241
   kk_subset: rel_expr -> rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   242
   kk_rel_eq: rel_expr -> rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   243
   kk_no: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   244
   kk_lone: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   245
   kk_one: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   246
   kk_some: rel_expr -> formula,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   247
   kk_rel_let: expr_assign list -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   248
   kk_rel_if: formula -> rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   249
   kk_union: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   250
   kk_difference: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   251
   kk_override: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   252
   kk_intersect: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   253
   kk_product: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   254
   kk_join: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   255
   kk_closure: rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   256
   kk_reflexive_closure: rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   257
   kk_comprehension: decl list -> formula -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   258
   kk_project: rel_expr -> int_expr list -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   259
   kk_project_seq: rel_expr -> int -> int -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   260
   kk_not3: rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   261
   kk_nat_less: rel_expr -> rel_expr -> rel_expr,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36385
diff changeset
   262
   kk_int_less: rel_expr -> rel_expr -> rel_expr}
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   263
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   264
(* We assume throughout that Kodkod variables have a "one" constraint. This is
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   265
   always the case if Kodkod's skolemization is disabled. *)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   266
fun kodkod_constrs optim nat_card int_card main_j0 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   267
  let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   268
    val from_bool = atom_for_bool main_j0
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   269
    fun from_nat n = Atom (n + main_j0)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   270
    fun to_nat j = j - main_j0
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   271
    val to_int = int_for_atom (int_card, main_j0)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   272
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   273
    val exists_empty_decl = exists (fn DeclOne (_, None) => true | _ => false)
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   274
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   275
    fun s_all _ True = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   276
      | s_all _ False = False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   277
      | s_all [] f = f
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   278
      | s_all ds (All (ds', f)) = s_all (ds @ ds') f
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   279
      | s_all ds f = if exists_empty_decl ds then True else All (ds, f)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   280
    fun s_exist _ True = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   281
      | s_exist _ False = False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   282
      | s_exist [] f = f
38126
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   283
      | s_exist ds (Exist (ds', f)) = s_exist (ds @ ds') f
8031d099379a added manual symmetry breaking for datatypes
blanchet
parents: 36390
diff changeset
   284
      | s_exist ds f = if exists_empty_decl ds then False else Exist (ds, f)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   285
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   286
    fun s_formula_let _ True = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   287
      | s_formula_let _ False = False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   288
      | s_formula_let assigns f = FormulaLet (assigns, f)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   289
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   290
    fun s_not True = False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   291
      | s_not False = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   292
      | s_not (All (ds, f)) = Exist (ds, s_not f)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   293
      | s_not (Exist (ds, f)) = All (ds, s_not f)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   294
      | s_not (Or (f1, f2)) = And (s_not f1, s_not f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   295
      | s_not (Implies (f1, f2)) = And (f1, s_not f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   296
      | s_not (And (f1, f2)) = Or (s_not f1, s_not f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   297
      | s_not (Not f) = f
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   298
      | s_not (No r) = Some r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   299
      | s_not (Some r) = No r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   300
      | s_not f = Not f
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   301
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   302
    fun s_or True _ = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   303
      | s_or False f2 = f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   304
      | s_or _ True = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   305
      | s_or f1 False = f1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   306
      | s_or f1 f2 = if f1 = f2 then f1 else Or (f1, f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   307
    fun s_iff True f2 = f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   308
      | s_iff False f2 = s_not f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   309
      | s_iff f1 True = f1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   310
      | s_iff f1 False = s_not f1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   311
      | s_iff f1 f2 = if f1 = f2 then True else Iff (f1, f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   312
    fun s_implies True f2 = f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   313
      | s_implies False _ = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   314
      | s_implies _ True = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   315
      | s_implies f1 False = s_not f1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   316
      | s_implies f1 f2 = if f1 = f2 then True else Implies (f1, f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   317
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   318
    fun s_formula_if True f2 _ = f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   319
      | s_formula_if False _ f3 = f3
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   320
      | s_formula_if f1 True f3 = s_or f1 f3
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   321
      | s_formula_if f1 False f3 = s_and (s_not f1) f3
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   322
      | s_formula_if f1 f2 True = s_implies f1 f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   323
      | s_formula_if f1 f2 False = s_and f1 f2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   324
      | s_formula_if f f1 f2 = FormulaIf (f, f1, f2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   325
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   326
    fun s_project r is =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   327
      (case r of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   328
         Project (r1, is') =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   329
         if forall is_Num is then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   330
           s_project r1 (map (nth is' o dest_Num) is)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   331
         else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   332
           raise SAME ()
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   333
       | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   334
      handle SAME () =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   335
             let val n = length is in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   336
               if arity_of_rel_expr r = n andalso is = num_seq 0 n then r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   337
               else Project (r, is)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   338
             end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   339
35284
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   340
    fun s_xone xone r =
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   341
      if is_one_rel_expr r then
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   342
        True
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   343
      else case arity_of_rel_expr r of
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   344
        1 => xone r
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   345
      | arity => foldl1 And (map (xone o s_project r o single o Num)
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   346
                                 (index_seq 0 arity))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   347
    fun s_no None = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   348
      | s_no (Product (r1, r2)) = s_or (s_no r1) (s_no r2)
34126
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   349
      | s_no (Intersect (Closure (Rel x), Iden)) = Acyclic x
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   350
      | s_no r = if is_one_rel_expr r then False else No r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   351
    fun s_lone None = True
35284
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   352
      | s_lone r = s_xone Lone r
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   353
    fun s_one None = False
35284
9edc2bd6d2bd enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents: 35280
diff changeset
   354
      | s_one r = s_xone One r
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   355
    fun s_some None = False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   356
      | s_some (Atom _) = True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   357
      | s_some (Product (r1, r2)) = s_and (s_some r1) (s_some r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   358
      | s_some r = if is_one_rel_expr r then True else Some r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   359
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   360
    fun s_not3 (Atom j) = Atom (if j = main_j0 then j + 1 else j - 1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   361
      | s_not3 (r as Join (r1, r2)) =
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   362
        if r2 = Rel not3_rel then r1 else Join (r, Rel not3_rel)
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   363
      | s_not3 r = Join (r, Rel not3_rel)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   364
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   365
    fun s_rel_eq r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   366
      (case (r1, r2) of
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   367
         (Join (r11, Rel x), _) =>
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   368
         if x = not3_rel then s_rel_eq r11 (s_not3 r2) else raise SAME ()
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   369
       | (RelIf (f, r11, r12), _) =>
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   370
         if inline_rel_expr r2 then
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   371
           s_formula_if f (s_rel_eq r11 r2) (s_rel_eq r12 r2)
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   372
         else
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   373
           raise SAME ()
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   374
       | (_, RelIf (f, r21, r22)) =>
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   375
         if inline_rel_expr r1 then
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   376
           s_formula_if f (s_rel_eq r1 r21) (s_rel_eq r1 r22)
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   377
         else
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   378
           raise SAME ()
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   379
       | (RelLet (bs, r1'), Atom _) => s_formula_let bs (s_rel_eq r1' r2)
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   380
       | (Atom _, RelLet (bs, r2')) => s_formula_let bs (s_rel_eq r1 r2')
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   381
       | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   382
      handle SAME () =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   383
             case rel_expr_equal r1 r2 of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   384
               SOME true => True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   385
             | SOME false => False
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   386
             | NONE =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   387
               case (r1, r2) of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   388
                 (_, RelIf (f, r21, r22)) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   389
                  if inline_rel_expr r1 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   390
                    s_formula_if f (s_rel_eq r1 r21) (s_rel_eq r1 r22)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   391
                  else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   392
                    RelEq (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   393
               | (RelIf (f, r11, r12), _) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   394
                  if inline_rel_expr r2 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   395
                    s_formula_if f (s_rel_eq r11 r2) (s_rel_eq r12 r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   396
                  else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   397
                    RelEq (r1, r2)
34126
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   398
               | (_, None) => s_no r1
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   399
               | (None, _) => s_no r2
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   400
               | _ => RelEq (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   401
    fun s_subset (Atom j1) (Atom j2) = formula_for_bool (j1 = j2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   402
      | s_subset (Atom j) (AtomSeq (k, j0)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   403
        formula_for_bool (j >= j0 andalso j < j0 + k)
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 34982
diff changeset
   404
      | s_subset (Union (r11, r12)) r2 =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   405
        s_and (s_subset r11 r2) (s_subset r12 r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   406
      | s_subset r1 (r2 as Union (r21, r22)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   407
        if is_one_rel_expr r1 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   408
          s_or (s_subset r1 r21) (s_subset r1 r22)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   409
        else
34936
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   410
          if s_subset r1 r21 = True orelse s_subset r1 r22 = True orelse
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   411
             r1 = r2 then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   412
            True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   413
          else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   414
            Subset (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   415
      | s_subset r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   416
        if r1 = r2 orelse is_none_product r1 then True
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   417
        else if is_none_product r2 then s_no r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   418
        else if forall is_one_rel_expr [r1, r2] then s_rel_eq r1 r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   419
        else Subset (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   420
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   421
    fun s_rel_let [b as AssignRelReg (x', r')] (r as RelReg x) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   422
        if x = x' then r' else RelLet ([b], r)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   423
      | s_rel_let bs r = RelLet (bs, r)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   424
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   425
    fun s_rel_if f r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   426
      (case (f, r1, r2) of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   427
         (True, _, _) => r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   428
       | (False, _, _) => r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   429
       | (No r1', None, RelIf (One r2', r3', r4')) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   430
         if r1' = r2' andalso r2' = r3' then s_rel_if (Lone r1') r1' r4'
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   431
         else raise SAME ()
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   432
       | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   433
      handle SAME () => if r1 = r2 then r1 else RelIf (f, r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   434
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   435
    fun s_union r1 (Union (r21, r22)) = s_union (s_union r1 r21) r22
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   436
      | s_union r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   437
        if is_none_product r1 then r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   438
        else if is_none_product r2 then r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   439
        else if r1 = r2 then r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   440
        else if occurs_in_union r2 r1 then r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   441
        else Union (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   442
    fun s_difference r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   443
      if is_none_product r1 orelse is_none_product r2 then r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   444
      else if r1 = r2 then empty_n_ary_rel (arity_of_rel_expr r1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   445
      else Difference (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   446
    fun s_override r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   447
      if is_none_product r2 then r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   448
      else if is_none_product r1 then r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   449
      else Override (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   450
    fun s_intersect r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   451
      case rel_expr_intersects r1 r2 of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   452
        SOME true => if r1 = r2 then r1 else Intersect (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   453
      | SOME false => empty_n_ary_rel (arity_of_rel_expr r1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   454
      | NONE => if is_none_product r1 then r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   455
                else if is_none_product r2 then r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   456
                else Intersect (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   457
    fun s_product r1 r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   458
      if is_none_product r1 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   459
        Product (r1, empty_n_ary_rel (arity_of_rel_expr r2))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   460
      else if is_none_product r2 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   461
        Product (empty_n_ary_rel (arity_of_rel_expr r1), r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   462
      else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   463
        Product (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   464
    fun s_join r1 (Product (Product (r211, r212), r22)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   465
        Product (s_join r1 (Product (r211, r212)), r22)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   466
      | s_join (Product (r11, Product (r121, r122))) r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   467
        Product (r11, s_join (Product (r121, r122)) r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   468
      | s_join None r = empty_n_ary_rel (arity_of_rel_expr r - 1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   469
      | s_join r None = empty_n_ary_rel (arity_of_rel_expr r - 1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   470
      | s_join (Product (None, None)) r = empty_n_ary_rel (arity_of_rel_expr r)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   471
      | s_join r (Product (None, None)) = empty_n_ary_rel (arity_of_rel_expr r)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   472
      | s_join Iden r2 = r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   473
      | s_join r1 Iden = r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   474
      | s_join (Product (r1, r2)) Univ =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   475
        if arity_of_rel_expr r2 = 1 then r1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   476
        else Product (r1, s_join r2 Univ)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   477
      | s_join Univ (Product (r1, r2)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   478
        if arity_of_rel_expr r1 = 1 then r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   479
        else Product (s_join Univ r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   480
      | s_join r1 (r2 as Product (r21, r22)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   481
        if arity_of_rel_expr r1 = 1 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   482
          case rel_expr_intersects r1 r21 of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   483
            SOME true => r22
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   484
          | SOME false => empty_n_ary_rel (arity_of_rel_expr r2 - 1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   485
          | NONE => Join (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   486
        else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   487
          Join (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   488
      | s_join (r1 as Product (r11, r12)) r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   489
        if arity_of_rel_expr r2 = 1 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   490
          case rel_expr_intersects r2 r12 of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   491
            SOME true => r11
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   492
          | SOME false => empty_n_ary_rel (arity_of_rel_expr r1 - 1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   493
          | NONE => Join (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   494
        else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   495
          Join (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   496
      | s_join r1 (r2 as RelIf (f, r21, r22)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   497
        if inline_rel_expr r1 then s_rel_if f (s_join r1 r21) (s_join r1 r22)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   498
        else Join (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   499
      | s_join (r1 as RelIf (f, r11, r12)) r2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   500
        if inline_rel_expr r2 then s_rel_if f (s_join r11 r2) (s_join r12 r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   501
        else Join (r1, r2)
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 34982
diff changeset
   502
      | s_join (r1 as Atom j1) (r2 as Rel (x as (2, _))) =
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   503
        if x = suc_rel then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   504
          let val n = to_nat j1 + 1 in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   505
            if n < nat_card then from_nat n else None
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   506
          end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   507
        else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   508
          Join (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   509
      | s_join r1 (r2 as Project (r21, Num k :: is)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   510
        if k = arity_of_rel_expr r21 - 1 andalso arity_of_rel_expr r1 = 1 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   511
          s_project (s_join r21 r1) is
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   512
        else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   513
          Join (r1, r2)
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 34982
diff changeset
   514
      | s_join r1 (Join (r21, r22 as Rel (x as (3, _)))) =
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   515
        ((if x = nat_add_rel then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   516
            case (r21, r1) of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   517
              (Atom j1, Atom j2) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   518
              let val n = to_nat j1 + to_nat j2 in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   519
                if n < nat_card then from_nat n else None
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   520
              end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   521
            | (Atom j, r) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   522
              (case to_nat j of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   523
                 0 => r
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   524
               | 1 => s_join r (Rel suc_rel)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   525
               | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   526
            | (r, Atom j) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   527
              (case to_nat j of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   528
                 0 => r
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   529
               | 1 => s_join r (Rel suc_rel)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   530
               | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   531
            | _ => raise SAME ()
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   532
          else if x = nat_subtract_rel then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   533
            case (r21, r1) of
33705
947184dc75c9 removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents: 33232
diff changeset
   534
              (Atom j1, Atom j2) => from_nat (nat_minus (to_nat j1) (to_nat j2))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   535
            | _ => raise SAME ()
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   536
          else if x = nat_multiply_rel then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   537
            case (r21, r1) of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   538
              (Atom j1, Atom j2) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   539
              let val n = to_nat j1 * to_nat j2 in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   540
                if n < nat_card then from_nat n else None
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   541
              end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   542
            | (Atom j, r) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   543
              (case to_nat j of 0 => Atom j | 1 => r | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   544
            | (r, Atom j) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   545
              (case to_nat j of 0 => Atom j | 1 => r | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   546
            | _ => raise SAME ()
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   547
          else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   548
            raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   549
         handle SAME () => List.foldr Join r22 [r1, r21])
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   550
      | s_join r1 r2 = Join (r1, r2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   551
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   552
    fun s_closure Iden = Iden
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   553
      | s_closure r = if is_none_product r then r else Closure r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   554
    fun s_reflexive_closure Iden = Iden
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   555
      | s_reflexive_closure r =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   556
        if is_none_product r then Iden else ReflexiveClosure r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   557
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   558
    fun s_comprehension ds False = empty_n_ary_rel (length ds)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   559
      | s_comprehension ds True = fold1 s_product (map decl_one_set ds)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   560
      | s_comprehension [d as DeclOne ((1, j1), r)]
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   561
                        (f as RelEq (Var (1, j2), Atom j)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   562
        if j1 = j2 andalso rel_expr_intersects (Atom j) r = SOME true then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   563
          Atom j
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   564
        else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   565
          Comprehension ([d], f)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   566
      | s_comprehension ds f = Comprehension (ds, f)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   567
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   568
    fun s_project_seq r =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   569
      let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   570
        fun aux arity r j0 n =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   571
          if j0 = 0 andalso arity = n then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   572
            r
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   573
          else case r of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   574
            RelIf (f, r1, r2) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   575
            s_rel_if f (aux arity r1 j0 n) (aux arity r2 j0 n)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   576
          | Product (r1, r2) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   577
            let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   578
              val arity2 = arity_of_rel_expr r2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   579
              val arity1 = arity - arity2
33705
947184dc75c9 removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents: 33232
diff changeset
   580
              val n1 = Int.min (nat_minus arity1 j0, n)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   581
              val n2 = n - n1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   582
              fun one () = aux arity1 r1 j0 n1
33705
947184dc75c9 removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents: 33232
diff changeset
   583
              fun two () = aux arity2 r2 (nat_minus j0 arity1) n2
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   584
            in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   585
              case (n1, n2) of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   586
                (0, _) => s_rel_if (s_some r1) (two ()) (empty_n_ary_rel n2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   587
              | (_, 0) => s_rel_if (s_some r2) (one ()) (empty_n_ary_rel n1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   588
              | _ => s_product (one ()) (two ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   589
            end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   590
          | _ => s_project r (num_seq j0 n)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   591
      in aux (arity_of_rel_expr r) r end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   592
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   593
    fun s_nat_less (Atom j1) (Atom j2) = from_bool (j1 < j2)
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   594
      | s_nat_less r1 r2 = fold s_join [r1, r2] (Rel nat_less_rel)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   595
    fun s_int_less (Atom j1) (Atom j2) = from_bool (to_int j1 < to_int j2)
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   596
      | s_int_less r1 r2 = fold s_join [r1, r2] (Rel int_less_rel)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   597
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   598
    fun d_project_seq r j0 n = Project (r, num_seq j0 n)
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   599
    fun d_not3 r = Join (r, Rel not3_rel)
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   600
    fun d_nat_less r1 r2 = List.foldl Join (Rel nat_less_rel) [r1, r2]
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 33982
diff changeset
   601
    fun d_int_less r1 r2 = List.foldl Join (Rel int_less_rel) [r1, r2]
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   602
  in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   603
    if optim then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   604
      {kk_all = s_all, kk_exist = s_exist, kk_formula_let = s_formula_let,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   605
       kk_formula_if = s_formula_if, kk_or = s_or, kk_not = s_not,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   606
       kk_iff = s_iff, kk_implies = s_implies, kk_and = s_and,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   607
       kk_subset = s_subset, kk_rel_eq = s_rel_eq, kk_no = s_no,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   608
       kk_lone = s_lone, kk_one = s_one, kk_some = s_some,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   609
       kk_rel_let = s_rel_let, kk_rel_if = s_rel_if, kk_union = s_union,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   610
       kk_difference = s_difference, kk_override = s_override,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   611
       kk_intersect = s_intersect, kk_product = s_product, kk_join = s_join,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   612
       kk_closure = s_closure, kk_reflexive_closure = s_reflexive_closure,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   613
       kk_comprehension = s_comprehension, kk_project = s_project,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   614
       kk_project_seq = s_project_seq, kk_not3 = s_not3,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   615
       kk_nat_less = s_nat_less, kk_int_less = s_int_less}
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   616
    else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   617
      {kk_all = curry All, kk_exist = curry Exist,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   618
       kk_formula_let = curry FormulaLet, kk_formula_if = curry3 FormulaIf,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   619
       kk_or = curry Or,kk_not = Not, kk_iff = curry Iff, kk_implies = curry
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   620
       Implies, kk_and = curry And, kk_subset = curry Subset, kk_rel_eq = curry
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   621
       RelEq, kk_no = No, kk_lone = Lone, kk_one = One, kk_some = Some,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   622
       kk_rel_let = curry RelLet, kk_rel_if = curry3 RelIf, kk_union = curry
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   623
       Union, kk_difference = curry Difference, kk_override = curry Override,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   624
       kk_intersect = curry Intersect, kk_product = curry Product,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   625
       kk_join = curry Join, kk_closure = Closure,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   626
       kk_reflexive_closure = ReflexiveClosure, kk_comprehension = curry
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   627
       Comprehension, kk_project = curry Project,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   628
       kk_project_seq = d_project_seq, kk_not3 = d_not3,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   629
       kk_nat_less = d_nat_less, kk_int_less = d_int_less}
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   630
  end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   631
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   632
end;