src/HOL/Tools/Nitpick/nitpick_model.ML
author blanchet
Tue, 01 Jun 2010 10:31:18 +0200
changeset 37256 0dca1ec52999
parent 37170 38ba15040455
child 37260 dde817e6dfb1
permissions -rw-r--r--
thread along context instead of theory for typedef lookup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33982
1ae222745c4a fixed paths in Nitpick's ML file headers
blanchet
parents: 33884
diff changeset
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_model.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: 34974
diff changeset
     3
    Copyright   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
Model reconstruction 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_MODEL =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     9
sig
33705
947184dc75c9 removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents: 33580
diff changeset
    10
  type styp = Nitpick_Util.styp
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    11
  type scope = Nitpick_Scope.scope
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    12
  type rep = Nitpick_Rep.rep
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    13
  type nut = Nitpick_Nut.nut
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    14
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
    15
  type params =
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
    16
    {show_datatypes: bool,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
    17
     show_consts: bool}
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
    18
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    19
  type term_postprocessor =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    20
    Proof.context -> string -> (typ -> term list) -> typ -> term -> term
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    21
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    22
  structure NameTable : TABLE
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    23
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    24
  val irrelevant : string
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    25
  val unknown : string
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    26
  val unrep : string
35711
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    27
  val register_term_postprocessor :
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    28
    typ -> term_postprocessor -> theory -> theory
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    29
  val unregister_term_postprocessor : typ -> theory -> theory
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    30
  val tuple_list_for_name :
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    31
    nut NameTable.table -> Kodkod.raw_bound list -> nut -> int list list
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    32
  val dest_plain_fun : term -> bool * (term list * term list)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    33
  val reconstruct_hol_model :
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    34
    params -> scope -> (term option * int list) list -> styp list -> nut list
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    35
    -> nut list -> nut list -> nut NameTable.table -> Kodkod.raw_bound list
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    36
    -> Pretty.T * bool
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    37
  val prove_hol_model :
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    38
    scope -> Time.time option -> nut list -> nut list -> nut NameTable.table
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    39
    -> Kodkod.raw_bound list -> term -> bool option
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    40
end;
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    41
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    42
structure Nitpick_Model : NITPICK_MODEL =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    43
struct
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    44
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    45
open Nitpick_Util
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    46
open Nitpick_HOL
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    47
open Nitpick_Scope
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    48
open Nitpick_Peephole
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    49
open Nitpick_Rep
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
    50
open Nitpick_Nut
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    51
34126
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
    52
structure KK = Kodkod
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
    53
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
    54
type params =
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
    55
  {show_datatypes: bool,
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
    56
   show_consts: bool}
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    57
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    58
type term_postprocessor =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    59
  Proof.context -> string -> (typ -> term list) -> typ -> term -> term
35711
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    60
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    61
structure Data = Theory_Data(
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    62
  type T = (typ * term_postprocessor) list
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    63
  val empty = []
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    64
  val extend = I
36607
e5f7235f39c5 made sml/nj happy about Sledgehammer and Nitpick (cf. 6f11c9b1fb3e, 3c2438efe224)
krauss
parents: 36391
diff changeset
    65
  fun merge (x, y) = AList.merge (op =) (K true) (x, y))
35711
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
    66
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
    67
val irrelevant = "_"
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    68
val unknown = "?"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    69
val unrep = "\<dots>"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    70
val maybe_mixfix = "_\<^sup>?"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    71
val base_mixfix = "_\<^bsub>base\<^esub>"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    72
val step_mixfix = "_\<^bsub>step\<^esub>"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    73
val abs_mixfix = "\<guillemotleft>_\<guillemotright>"
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    74
val arg_var_prefix = "x"
35179
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
    75
val cyclic_co_val_name = "\<omega>"
35188
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
    76
val cyclic_const_prefix = "\<xi>"
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
    77
val cyclic_type_name = nitpick_prefix ^ cyclic_const_prefix
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
    78
val opt_flag = nitpick_prefix ^ "opt"
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
    79
val non_opt_flag = nitpick_prefix ^ "non_opt"
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    80
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
    81
type atom_pool = ((string * int) * int list) list
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
    82
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    83
fun add_wacky_syntax ctxt =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    84
  let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    85
    val name_of = fst o dest_Const
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    86
    val thy = ProofContext.theory_of ctxt |> Context.reject_draft
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    87
    val (maybe_t, thy) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    88
      Sign.declare_const ((@{binding nitpick_maybe}, @{typ "'a => 'a"}),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    89
                          Mixfix (maybe_mixfix, [1000], 1000)) thy
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    90
    val (abs_t, thy) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    91
      Sign.declare_const ((@{binding nitpick_abs}, @{typ "'a => 'b"}),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    92
                          Mixfix (abs_mixfix, [40], 40)) thy
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    93
    val (base_t, thy) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    94
      Sign.declare_const ((@{binding nitpick_base}, @{typ "'a => 'a"}),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    95
                          Mixfix (base_mixfix, [1000], 1000)) thy
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    96
    val (step_t, thy) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    97
      Sign.declare_const ((@{binding nitpick_step}, @{typ "'a => 'a"}),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    98
                          Mixfix (step_mixfix, [1000], 1000)) thy
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
    99
  in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   100
    (pairself (pairself name_of) ((maybe_t, abs_t), (base_t, step_t)),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   101
     ProofContext.transfer_syntax thy ctxt)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   102
  end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   103
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   104
(** Term reconstruction **)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   105
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   106
fun nth_atom_suffix pool s j k =
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   107
  (case AList.lookup (op =) (!pool) (s, k) of
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   108
     SOME js =>
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   109
     (case find_index (curry (op =) j) js of
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   110
        ~1 => (Unsynchronized.change pool (cons ((s, k), j :: js));
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   111
               length js + 1)
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   112
      | n => length js - n)
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   113
   | NONE => (Unsynchronized.change pool (cons ((s, k), [j])); 1))
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   114
  |> nat_subscript
33884
a0c43f185fef generate clearer atom names in Nitpick for types that end with a digit;
blanchet
parents: 33705
diff changeset
   115
  |> (s <> "" andalso Symbol.is_ascii_digit (List.last (explode s)))
a0c43f185fef generate clearer atom names in Nitpick for types that end with a digit;
blanchet
parents: 33705
diff changeset
   116
     ? prefix "\<^isub>,"
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   117
fun nth_atom_name pool prefix (Type (s, _)) j k =
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   118
    let val s' = shortest_name s in
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   119
      prefix ^ (if String.isPrefix "\\" s' then s' else substring (s', 0, 1)) ^
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   120
      nth_atom_suffix pool s j k
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   121
    end
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   122
  | nth_atom_name pool prefix (TFree (s, _)) j k =
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   123
    prefix ^ perhaps (try (unprefix "'")) s ^ nth_atom_suffix pool s j k
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   124
  | nth_atom_name _ _ T _ _ =
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   125
    raise TYPE ("Nitpick_Model.nth_atom_name", [T], [])
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   126
fun nth_atom pool for_auto T j k =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   127
  if for_auto then
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   128
    Free (nth_atom_name pool (hd (space_explode "." nitpick_prefix)) T j k, T)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   129
  else
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   130
    Const (nth_atom_name pool "" T j k, T)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   131
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35086
diff changeset
   132
fun extract_real_number (Const (@{const_name divide}, _) $ t1 $ t2) =
34126
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   133
    real (snd (HOLogic.dest_number t1)) / real (snd (HOLogic.dest_number t2))
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   134
  | extract_real_number t = real (snd (HOLogic.dest_number t))
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   135
fun nice_term_ord (Abs (_, _, t1), Abs (_, _, t2)) = nice_term_ord (t1, t2)
34126
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   136
  | nice_term_ord tp = Real.compare (pairself extract_real_number tp)
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   137
    handle TERM ("dest_number", _) =>
34126
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   138
           case tp of
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   139
             (t11 $ t12, t21 $ t22) =>
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   140
             (case nice_term_ord (t11, t21) of
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   141
                EQUAL => nice_term_ord (t12, t22)
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   142
              | ord => ord)
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 35402
diff changeset
   143
           | _ => Term_Ord.fast_term_ord tp
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   144
35711
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   145
fun register_term_postprocessor T p = Data.map (cons (T, p))
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   146
fun unregister_term_postprocessor T = Data.map (AList.delete (op =) T)
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   147
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   148
fun tuple_list_for_name rel_table bounds name =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   149
  the (AList.lookup (op =) bounds (the_rel rel_table name)) handle NUT _ => [[]]
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   150
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   151
fun unarize_unbox_etc_term (Const (@{const_name FinFun}, _) $ t1) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   152
    unarize_unbox_etc_term t1
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   153
  | unarize_unbox_etc_term (Const (@{const_name FunBox}, _) $ t1) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   154
    unarize_unbox_etc_term t1
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   155
  | unarize_unbox_etc_term
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   156
        (Const (@{const_name PairBox},
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   157
                Type (@{type_name fun}, [T1, Type (@{type_name fun}, [T2, _])]))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   158
         $ t1 $ t2) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   159
    let val Ts = map uniterize_unarize_unbox_etc_type [T1, T2] in
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   160
      Const (@{const_name Pair}, Ts ---> Type (@{type_name "*"}, Ts))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   161
      $ unarize_unbox_etc_term t1 $ unarize_unbox_etc_term t2
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   162
    end
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   163
  | unarize_unbox_etc_term (Const (s, T)) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   164
    Const (s, uniterize_unarize_unbox_etc_type T)
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   165
  | unarize_unbox_etc_term (t1 $ t2) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   166
    unarize_unbox_etc_term t1 $ unarize_unbox_etc_term t2
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   167
  | unarize_unbox_etc_term (Free (s, T)) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   168
    Free (s, uniterize_unarize_unbox_etc_type T)
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   169
  | unarize_unbox_etc_term (Var (x, T)) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   170
    Var (x, uniterize_unarize_unbox_etc_type T)
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   171
  | unarize_unbox_etc_term (Bound j) = Bound j
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   172
  | unarize_unbox_etc_term (Abs (s, T, t')) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   173
    Abs (s, uniterize_unarize_unbox_etc_type T, unarize_unbox_etc_term t')
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   174
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   175
fun factor_out_types (T1 as Type (@{type_name "*"}, [T11, T12]))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   176
                     (T2 as Type (@{type_name "*"}, [T21, T22])) =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   177
    let val (n1, n2) = pairself num_factors_in_type (T11, T21) in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   178
      if n1 = n2 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   179
        let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   180
          val ((T11', opt_T12'), (T21', opt_T22')) = factor_out_types T12 T22
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   181
        in
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   182
          ((Type (@{type_name "*"}, [T11, T11']), opt_T12'),
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   183
           (Type (@{type_name "*"}, [T21, T21']), opt_T22'))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   184
        end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   185
      else if n1 < n2 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   186
        case factor_out_types T1 T21 of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   187
          (p1, (T21', NONE)) => (p1, (T21', SOME T22))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   188
        | (p1, (T21', SOME T22')) =>
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   189
          (p1, (T21', SOME (Type (@{type_name "*"}, [T22', T22]))))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   190
      else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   191
        swap (factor_out_types T2 T1)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   192
    end
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   193
  | factor_out_types (Type (@{type_name "*"}, [T11, T12])) T2 =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   194
    ((T11, SOME T12), (T2, NONE))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   195
  | factor_out_types T1 (Type (@{type_name "*"}, [T21, T22])) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   196
    ((T1, NONE), (T21, SOME T22))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   197
  | factor_out_types T1 T2 = ((T1, NONE), (T2, NONE))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   198
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   199
fun make_plain_fun maybe_opt T1 T2 =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   200
  let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   201
    fun aux T1 T2 [] =
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   202
        Const (if maybe_opt then opt_flag else non_opt_flag, T1 --> T2)
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   203
      | aux T1 T2 ((t1, t2) :: tps) =
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   204
        Const (@{const_name fun_upd}, (T1 --> T2) --> T1 --> T2 --> T1 --> T2)
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   205
        $ aux T1 T2 tps $ t1 $ t2
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   206
  in aux T1 T2 o rev end
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   207
fun is_plain_fun (Const (s, _)) = (s = opt_flag orelse s = non_opt_flag)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   208
  | is_plain_fun (Const (@{const_name fun_upd}, _) $ t0 $ _ $ _) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   209
    is_plain_fun t0
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   210
  | is_plain_fun _ = false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   211
val dest_plain_fun =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   212
  let
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   213
    fun aux (Abs (_, _, Const (s, _))) = (s <> irrelevant, ([], []))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   214
      | aux (Const (s, _)) = (s <> non_opt_flag, ([], []))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   215
      | aux (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   216
        let val (maybe_opt, (ts1, ts2)) = aux t0 in
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   217
          (maybe_opt, (t1 :: ts1, t2 :: ts2))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   218
        end
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   219
      | aux t = raise TERM ("Nitpick_Model.dest_plain_fun", [t])
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   220
  in apsnd (pairself rev) o aux end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   221
33565
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   222
fun break_in_two T T1 T2 t =
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   223
  let
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   224
    val ps = HOLogic.flat_tupleT_paths T
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   225
    val cut = length (HOLogic.strip_tupleT T1)
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   226
    val (ps1, ps2) = pairself HOLogic.flat_tupleT_paths (T1, T2)
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   227
    val (ts1, ts2) = t |> HOLogic.strip_ptuple ps |> chop cut
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   228
  in (HOLogic.mk_ptuple ps1 T1 ts1, HOLogic.mk_ptuple ps2 T2 ts2) end
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   229
fun pair_up (Type (@{type_name "*"}, [T1', T2']))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   230
            (t1 as Const (@{const_name Pair},
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   231
                          Type (@{type_name fun},
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   232
                                [_, Type (@{type_name fun}, [_, T1])]))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   233
             $ t11 $ t12) t2 =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   234
    if T1 = T1' then HOLogic.mk_prod (t1, t2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   235
    else HOLogic.mk_prod (t11, pair_up T2' t12 t2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   236
  | pair_up _ t1 t2 = HOLogic.mk_prod (t1, t2)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   237
fun multi_pair_up T1 t1 (ts2, ts3) = map2 (pair o pair_up T1 t1) ts2 ts3
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   238
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   239
fun typecast_fun (Type (@{type_name fun}, [T1', T2'])) T1 T2 t =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   240
    let
33565
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   241
      fun do_curry T1 T1a T1b T2 t =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   242
        let
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   243
          val (maybe_opt, tsp) = dest_plain_fun t
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   244
          val tps =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   245
            tsp |>> map (break_in_two T1 T1a T1b)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   246
                |> uncurry (map2 (fn (t1a, t1b) => fn t2 => (t1a, (t1b, t2))))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   247
                |> AList.coalesce (op =)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   248
                |> map (apsnd (make_plain_fun maybe_opt T1b T2))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   249
        in make_plain_fun maybe_opt T1a (T1b --> T2) tps end
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   250
      and do_uncurry T1 T2 t =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   251
        let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   252
          val (maybe_opt, tsp) = dest_plain_fun t
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   253
          val tps =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   254
            tsp |> op ~~
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   255
                |> maps (fn (t1, t2) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   256
                            multi_pair_up T1 t1 (snd (dest_plain_fun t2)))
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   257
        in make_plain_fun maybe_opt T1 T2 tps end
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   258
      and do_arrow T1' T2' _ _ (Const (s, _)) = Const (s, T1' --> T2')
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   259
        | do_arrow T1' T2' T1 T2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   260
                   (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   261
          Const (@{const_name fun_upd},
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   262
                 (T1' --> T2') --> T1' --> T2' --> T1' --> T2')
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   263
          $ do_arrow T1' T2' T1 T2 t0 $ do_term T1' T1 t1 $ do_term T2' T2 t2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   264
        | do_arrow _ _ _ _ t =
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   265
          raise TERM ("Nitpick_Model.typecast_fun.do_arrow", [t])
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   266
      and do_fun T1' T2' T1 T2 t =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   267
        case factor_out_types T1' T1 of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   268
          ((_, NONE), (_, NONE)) => t |> do_arrow T1' T2' T1 T2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   269
        | ((_, NONE), (T1a, SOME T1b)) =>
33565
5fad8e36dfb1 fixed error in Nitpick's display of uncurried constants, which resulted in an exception
blanchet
parents: 33558
diff changeset
   270
          t |> do_curry T1 T1a T1b T2 |> do_arrow T1' T2' T1a (T1b --> T2)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   271
        | ((T1a', SOME T1b'), (_, NONE)) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   272
          t |> do_arrow T1a' (T1b' --> T2') T1 T2 |> do_uncurry T1' T2'
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   273
        | _ => raise TYPE ("Nitpick_Model.typecast_fun.do_fun", [T1, T1'], [])
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   274
      and do_term (Type (@{type_name fun}, [T1', T2']))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   275
                  (Type (@{type_name fun}, [T1, T2])) t =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   276
          do_fun T1' T2' T1 T2 t
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   277
        | do_term (T' as Type (@{type_name "*"}, Ts' as [T1', T2']))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   278
                  (Type (@{type_name "*"}, [T1, T2]))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   279
                  (Const (@{const_name Pair}, _) $ t1 $ t2) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   280
          Const (@{const_name Pair}, Ts' ---> T')
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   281
          $ do_term T1' T1 t1 $ do_term T2' T2 t2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   282
        | do_term T' T t =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   283
          if T = T' then t
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   284
          else raise TYPE ("Nitpick_Model.typecast_fun.do_term", [T, T'], [])
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   285
    in if T1' = T1 andalso T2' = T2 then t else do_fun T1' T2' T1 T2 t end
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   286
  | typecast_fun T' _ _ _ =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   287
    raise TYPE ("Nitpick_Model.typecast_fun", [T'], [])
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   288
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   289
fun truth_const_sort_key @{const True} = "0"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   290
  | truth_const_sort_key @{const False} = "2"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   291
  | truth_const_sort_key _ = "1"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   292
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   293
fun mk_tuple (Type (@{type_name "*"}, [T1, T2])) ts =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   294
    HOLogic.mk_prod (mk_tuple T1 ts,
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   295
        mk_tuple T2 (List.drop (ts, length (HOLogic.flatten_tupleT T1))))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   296
  | mk_tuple _ (t :: _) = t
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   297
  | mk_tuple T [] = raise TYPE ("Nitpick_Model.mk_tuple", [T], [])
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   298
35711
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   299
fun varified_type_match thy (candid_T, pat_T) =
35845
e5980f0ad025 renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents: 35807
diff changeset
   300
  strict_type_match thy (candid_T, Logic.varifyT_global pat_T)
35711
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   301
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   302
fun all_values_of_type pool wacky_names (scope as {card_assigns, ...} : scope)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   303
                       sel_names rel_table bounds card T =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   304
  let
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   305
    val card = if card = 0 then card_of_type card_assigns T else card
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   306
    fun nth_value_of_type n =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   307
      let
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   308
        fun term unfold =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   309
          reconstruct_term unfold pool wacky_names scope sel_names rel_table
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   310
                           bounds T T (Atom (card, 0)) [[n]]
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   311
      in
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   312
        case term false of
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   313
          t as Const (s, _) =>
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   314
          if String.isPrefix cyclic_const_prefix s then
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   315
            HOLogic.mk_eq (t, term true)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   316
          else
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   317
            t
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   318
        | t => t
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   319
      end
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   320
  in index_seq 0 card |> map nth_value_of_type |> sort nice_term_ord end
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   321
and reconstruct_term unfold pool (wacky_names as ((maybe_name, abs_name), _))
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37170
diff changeset
   322
        (scope as {hol_ctxt as {thy, ctxt, stds, ...}, binarize, card_assigns,
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   323
                   bits, datatypes, ofs, ...}) sel_names rel_table bounds =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   324
  let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   325
    val for_auto = (maybe_name = "")
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   326
    fun value_of_bits jss =
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   327
      let
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   328
        val j0 = offset_of_type ofs @{typ unsigned_bit}
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   329
        val js = map (Integer.add (~ j0) o the_single) jss
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   330
      in
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   331
        fold (fn j => Integer.add (reasonable_power 2 j |> j = bits ? op ~))
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   332
             js 0
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   333
      end
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   334
    val all_values =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   335
      all_values_of_type pool wacky_names scope sel_names rel_table bounds 0
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   336
    fun postprocess_term (Type (@{type_name fun}, _)) = I
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   337
      | postprocess_term T =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   338
        if null (Data.get thy) then
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   339
          I
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   340
        else case AList.lookup (varified_type_match thy) (Data.get thy) T of
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   341
          SOME postproc => postproc ctxt maybe_name all_values T
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   342
        | NONE => I
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   343
    fun postprocess_subterms Ts (t1 $ t2) =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   344
        let val t = postprocess_subterms Ts t1 $ postprocess_subterms Ts t2 in
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   345
          postprocess_term (fastype_of1 (Ts, t)) t
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   346
        end
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   347
      | postprocess_subterms Ts (Abs (s, T, t')) =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   348
        Abs (s, T, postprocess_subterms (T :: Ts) t')
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   349
      | postprocess_subterms Ts t = postprocess_term (fastype_of1 (Ts, t)) t
35388
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   350
    fun make_set maybe_opt T1 T2 tps =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   351
      let
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35388
diff changeset
   352
        val empty_const = Const (@{const_abbrev Set.empty}, T1 --> T2)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   353
        val insert_const = Const (@{const_name insert},
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   354
                                  T1 --> (T1 --> T2) --> T1 --> T2)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   355
        fun aux [] =
35385
29f81babefd7 improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents: 35280
diff changeset
   356
            if maybe_opt andalso not (is_complete_type datatypes false T1) then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   357
              insert_const $ Const (unrep, T1) $ empty_const
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   358
            else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   359
              empty_const
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   360
          | aux ((t1, t2) :: zs) =
35388
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   361
            aux zs
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   362
            |> t2 <> @{const False}
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   363
               ? curry (op $)
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   364
                       (insert_const
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   365
                        $ (t1 |> t2 <> @{const True}
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   366
                                 ? curry (op $)
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   367
                                         (Const (maybe_name, T1 --> T1))))
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   368
      in
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   369
        if forall (fn (_, t) => t <> @{const True} andalso t <> @{const False})
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   370
                  tps then
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   371
          Const (unknown, T1 --> T2)
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   372
        else
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   373
          aux tps
42d39948cace use SAT4J for "Tests_Nits.thy" for safety (this should solve the Isatest failures) + minor changes
blanchet
parents: 35385
diff changeset
   374
      end
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   375
    fun make_map maybe_opt T1 T2 T2' =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   376
      let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   377
        val update_const = Const (@{const_name fun_upd},
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   378
                                  (T1 --> T2) --> T1 --> T2 --> T1 --> T2)
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35388
diff changeset
   379
        fun aux' [] = Const (@{const_abbrev Map.empty}, T1 --> T2)
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   380
          | aux' ((t1, t2) :: tps) =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   381
            (case t2 of
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   382
               Const (@{const_name None}, _) => aux' tps
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   383
             | _ => update_const $ aux' tps $ t1 $ t2)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   384
        fun aux tps =
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   385
          if maybe_opt andalso not (is_complete_type datatypes false T1) then
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   386
            update_const $ aux' tps $ Const (unrep, T1)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   387
            $ (Const (@{const_name Some}, T2' --> T2) $ Const (unknown, T2'))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   388
          else
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   389
            aux' tps
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   390
      in aux end
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   391
    fun polish_funs Ts t =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   392
      (case fastype_of1 (Ts, t) of
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   393
         Type (@{type_name fun}, [T1, T2]) =>
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   394
         if is_plain_fun t then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   395
           case T2 of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   396
             @{typ bool} =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   397
             let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   398
               val (maybe_opt, ts_pair) =
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   399
                 dest_plain_fun t ||> pairself (map (polish_funs Ts))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   400
             in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   401
               make_set maybe_opt T1 T2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   402
                        (sort_wrt (truth_const_sort_key o snd) (op ~~ ts_pair))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   403
             end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   404
           | Type (@{type_name option}, [T2']) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   405
             let
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   406
               val (maybe_opt, ts_pair) =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   407
                 dest_plain_fun t ||> pairself (map (polish_funs Ts))
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   408
             in make_map maybe_opt T1 T2 T2' (rev (op ~~ ts_pair)) end
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   409
           | _ => raise SAME ()
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   410
         else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   411
           raise SAME ()
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   412
       | _ => raise SAME ())
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   413
      handle SAME () =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   414
             case t of
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   415
               (t1 as Const (@{const_name fun_upd}, _) $ t11 $ _)
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   416
               $ (t2 as Const (s, _)) =>
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   417
               if s = unknown then polish_funs Ts t11
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   418
               else polish_funs Ts t1 $ polish_funs Ts t2
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   419
             | t1 $ t2 => polish_funs Ts t1 $ polish_funs Ts t2
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   420
             | Abs (s, T, t') => Abs (s, T, polish_funs (T :: Ts) t')
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   421
             | Const (s, Type (@{type_name fun}, [T1, T2])) =>
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   422
               if s = opt_flag orelse s = non_opt_flag then
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   423
                 Abs ("x", T1,
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   424
                      Const (if is_complete_type datatypes false T1 then
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   425
                               irrelevant
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   426
                             else
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   427
                               unknown, T2))
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   428
               else
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   429
                 t
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   430
             | t => t
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   431
    fun make_fun maybe_opt T1 T2 T' ts1 ts2 =
34126
8a2c5d7aff51 polished Nitpick's binary integer support etc.;
blanchet
parents: 34124
diff changeset
   432
      ts1 ~~ ts2 |> sort (nice_term_ord o pairself fst)
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   433
                 |> make_plain_fun maybe_opt T1 T2
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   434
                 |> unarize_unbox_etc_term
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   435
                 |> typecast_fun (uniterize_unarize_unbox_etc_type T')
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   436
                                 (uniterize_unarize_unbox_etc_type T1)
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   437
                                 (uniterize_unarize_unbox_etc_type T2)
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   438
    fun term_for_atom seen (T as Type (@{type_name fun}, [T1, T2])) T' j _ =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   439
        let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   440
          val k1 = card_of_type card_assigns T1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   441
          val k2 = card_of_type card_assigns T2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   442
        in
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   443
          term_for_rep true seen T T' (Vect (k1, Atom (k2, 0)))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   444
                       [nth_combination (replicate k1 (k2, 0)) j]
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   445
          handle General.Subscript =>
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   446
                 raise ARG ("Nitpick_Model.reconstruct_term.term_for_atom",
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   447
                            signed_string_of_int j ^ " for " ^
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   448
                            string_for_rep (Vect (k1, Atom (k2, 0))))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   449
        end
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   450
      | term_for_atom seen (Type (@{type_name "*"}, [T1, T2])) _ j k =
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   451
        let
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   452
          val k1 = card_of_type card_assigns T1
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   453
          val k2 = k div k1
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   454
        in
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   455
          list_comb (HOLogic.pair_const T1 T2,
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   456
                     map3 (fn T => term_for_atom seen T T) [T1, T2]
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   457
                          [j div k2, j mod k2] [k1, k2]) (* ### k2 or k1? FIXME *)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   458
        end
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   459
      | term_for_atom seen @{typ prop} _ j k =
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   460
        HOLogic.mk_Trueprop (term_for_atom seen bool_T bool_T j k)
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   461
      | term_for_atom _ @{typ bool} _ j _ =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   462
        if j = 0 then @{const False} else @{const True}
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   463
      | term_for_atom _ @{typ unit} _ _ _ = @{const Unity}
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   464
      | term_for_atom seen T _ j k =
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   465
        if T = nat_T andalso is_standard_datatype thy stds nat_T then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   466
          HOLogic.mk_number nat_T j
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   467
        else if T = int_T then
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   468
          HOLogic.mk_number int_T (int_for_atom (k, 0) j)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   469
        else if is_fp_iterator_type T then
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   470
          HOLogic.mk_number nat_T (k - j - 1)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   471
        else if T = @{typ bisim_iterator} then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   472
          HOLogic.mk_number nat_T j
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   473
        else case datatype_spec datatypes T of
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   474
          NONE => nth_atom pool for_auto T j k
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   475
        | SOME {deep = false, ...} => nth_atom pool for_auto T j k
35179
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   476
        | SOME {co, standard, constrs, ...} =>
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   477
          let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   478
            fun tuples_for_const (s, T) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   479
              tuple_list_for_name rel_table bounds (ConstName (s, T, Any))
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   480
            fun cyclic_atom () =
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   481
              nth_atom pool for_auto (Type (cyclic_type_name, [])) j k
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   482
            fun cyclic_var () = Var ((nth_atom_name pool "" T j k, 0), T)
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   483
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   484
            val discr_jsss = map (tuples_for_const o discr_for_constr o #const)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   485
                                 constrs
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   486
            val real_j = j + offset_of_type ofs T
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   487
            val constr_x as (constr_s, constr_T) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   488
              get_first (fn (jss, {const, ...}) =>
34121
5e831d805118 get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents: 33982
diff changeset
   489
                            if member (op =) jss [real_j] then SOME const
5e831d805118 get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents: 33982
diff changeset
   490
                            else NONE)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   491
                        (discr_jsss ~~ constrs) |> the
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   492
            val arg_Ts = curried_binder_types constr_T
35190
ce653cc27a94 make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents: 35188
diff changeset
   493
            val sel_xs =
ce653cc27a94 make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents: 35188
diff changeset
   494
              map (binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize
ce653cc27a94 make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents: 35188
diff changeset
   495
                                                          constr_x)
ce653cc27a94 make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents: 35188
diff changeset
   496
                  (index_seq 0 (length arg_Ts))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   497
            val sel_Rs =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   498
              map (fn x => get_first
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   499
                               (fn ConstName (s', T', R) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   500
                                   if (s', T') = x then SOME R else NONE
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   501
                                 | u => raise NUT ("Nitpick_Model.reconstruct_\
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   502
                                                   \term.term_for_atom", [u]))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   503
                               sel_names |> the) sel_xs
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   504
            val arg_Rs = map (snd o dest_Func) sel_Rs
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   505
            val sel_jsss = map tuples_for_const sel_xs
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   506
            val arg_jsss =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   507
              map (map_filter (fn js => if hd js = real_j then SOME (tl js)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   508
                                        else NONE)) sel_jsss
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   509
            val uncur_arg_Ts = binder_types constr_T
35179
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   510
            val maybe_cyclic = co orelse not standard
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   511
          in
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   512
            if maybe_cyclic andalso not (null seen) andalso
35188
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   513
               member (op =) (seen |> unfold ? (fst o split_last)) (T, j) then
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   514
              cyclic_var ()
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   515
            else if constr_s = @{const_name Word} then
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   516
              HOLogic.mk_number
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   517
                  (if T = @{typ "unsigned_bit word"} then nat_T else int_T)
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   518
                  (value_of_bits (the_single arg_jsss))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   519
            else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   520
              let
35179
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   521
                val seen = seen |> maybe_cyclic ? cons (T, j)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   522
                val ts =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   523
                  if length arg_Ts = 0 then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   524
                    []
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   525
                  else
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   526
                    map3 (fn Ts =>
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   527
                             term_for_rep (constr_s <> @{const_name FinFun})
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   528
                                          seen Ts Ts) arg_Ts arg_Rs arg_jsss
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   529
                    |> mk_tuple (HOLogic.mk_tupleT uncur_arg_Ts)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   530
                    |> dest_n_tuple (length uncur_arg_Ts)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   531
                val t =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   532
                  if constr_s = @{const_name Abs_Frac} then
35711
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   533
                    case ts of
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   534
                      [Const (@{const_name Pair}, _) $ t1 $ t2] =>
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   535
                      frac_from_term_pair (body_type T) t1 t2
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   536
                    | _ => raise TERM ("Nitpick_Model.reconstruct_term.\
548d3f16404b added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents: 35665
diff changeset
   537
                                       \term_for_atom (Abs_Frac)", ts)
34936
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   538
                  else if not for_auto andalso
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37170
diff changeset
   539
                          (is_abs_fun ctxt constr_x orelse
34936
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   540
                           constr_s = @{const_name Quot}) then
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   541
                    Const (abs_name, constr_T) $ the_single ts
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   542
                  else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   543
                    list_comb (Const constr_x, ts)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   544
              in
35179
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   545
                if maybe_cyclic then
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   546
                  let val var = cyclic_var () in
35188
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   547
                    if unfold andalso not standard andalso
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   548
                       length seen = 1 andalso
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   549
                       exists_subterm (fn Const (s, _) =>
35188
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   550
                                          String.isPrefix cyclic_const_prefix s
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   551
                                        | t' => t' = var) t then
35188
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   552
                      subst_atomic [(var, cyclic_atom ())] t
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   553
                    else if exists_subterm (curry (op =) var) t then
35179
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   554
                      if co then
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   555
                        Const (@{const_name The}, (T --> bool_T) --> T)
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   556
                        $ Abs (cyclic_co_val_name, T,
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   557
                               Const (@{const_name "op ="}, T --> T --> bool_T)
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   558
                               $ Bound 0 $ abstract_over (var, t))
35179
4b198af5beb5 redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents: 35177
diff changeset
   559
                      else
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   560
                        cyclic_atom ()
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   561
                    else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   562
                      t
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   563
                  end
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
                  t
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   566
              end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   567
          end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   568
    and term_for_vect seen k R T1 T2 T' js =
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   569
      make_fun true T1 T2 T'
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   570
               (map (fn j => term_for_atom seen T1 T1 j k) (index_seq 0 k))
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   571
               (map (term_for_rep true seen T2 T2 R o single)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   572
                    (batch_list (arity_of_rep R) js))
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   573
    and term_for_rep _ seen T T' Unit [[]] = term_for_atom seen T T' 0 1
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   574
      | term_for_rep _ seen T T' (R as Atom (k, j0)) [[j]] =
35075
888802be2019 handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents: 35070
diff changeset
   575
        if j >= j0 andalso j < j0 + k then term_for_atom seen T T' (j - j0) k
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   576
        else raise REP ("Nitpick_Model.reconstruct_term.term_for_rep", [R])
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   577
      | term_for_rep _ seen (Type (@{type_name "*"}, [T1, T2])) _
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   578
                     (Struct [R1, R2]) [js] =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   579
        let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   580
          val arity1 = arity_of_rep R1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   581
          val (js1, js2) = chop arity1 js
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   582
        in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   583
          list_comb (HOLogic.pair_const T1 T2,
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   584
                     map3 (fn T => term_for_rep true seen T T) [T1, T2] [R1, R2]
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   585
                          [[js1], [js2]])
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   586
        end
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   587
      | term_for_rep _ seen (Type (@{type_name fun}, [T1, T2])) T'
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   588
                     (Vect (k, R')) [js] =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   589
        term_for_vect seen k R' T1 T2 T' js
37170
38ba15040455 Nitpick: show "..." in datatype values (e.g., [{0::nat, ...}]), since these are really equivalence classes
blanchet
parents: 36607
diff changeset
   590
      | term_for_rep maybe_opt seen (Type (@{type_name fun}, [T1, T2])) T'
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   591
                     (Func (R1, Formula Neut)) jss =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   592
        let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   593
          val jss1 = all_combinations_for_rep R1
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   594
          val ts1 = map (term_for_rep true seen T1 T1 R1 o single) jss1
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   595
          val ts2 =
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   596
            map (fn js => term_for_rep true seen T2 T2 (Atom (2, 0))
35385
29f81babefd7 improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents: 35280
diff changeset
   597
                                       [[int_from_bool (member (op =) jss js)]])
34121
5e831d805118 get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents: 33982
diff changeset
   598
                jss1
37170
38ba15040455 Nitpick: show "..." in datatype values (e.g., [{0::nat, ...}]), since these are really equivalence classes
blanchet
parents: 36607
diff changeset
   599
        in make_fun maybe_opt T1 T2 T' ts1 ts2 end
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   600
      | term_for_rep maybe_opt seen (Type (@{type_name fun}, [T1, T2])) T'
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   601
                     (Func (R1, R2)) jss =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   602
        let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   603
          val arity1 = arity_of_rep R1
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   604
          val jss1 = all_combinations_for_rep R1
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   605
          val ts1 = map (term_for_rep false seen T1 T1 R1 o single) jss1
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   606
          val grouped_jss2 = AList.group (op =) (map (chop arity1) jss)
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   607
          val ts2 = map (term_for_rep false seen T2 T2 R2 o the_default []
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   608
                         o AList.lookup (op =) grouped_jss2) jss1
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   609
        in make_fun maybe_opt T1 T2 T' ts1 ts2 end
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   610
      | term_for_rep _ seen T T' (Opt R) jss =
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   611
        if null jss then Const (unknown, T)
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   612
        else term_for_rep true seen T T' R jss
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   613
      | term_for_rep _ _ T _ R jss =
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   614
        raise ARG ("Nitpick_Model.reconstruct_term.term_for_rep",
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   615
                   Refute.string_of_typ T ^ " " ^ string_for_rep R ^ " " ^
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   616
                   string_of_int (length jss))
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   617
  in
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   618
    postprocess_subterms [] o polish_funs [] o unarize_unbox_etc_term
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   619
    oooo term_for_rep true []
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   620
  end
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   621
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   622
(** Constant postprocessing **)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   623
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   624
fun dest_n_tuple_type 1 T = [T]
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   625
  | dest_n_tuple_type n (Type (_, [T1, T2])) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   626
    T1 :: dest_n_tuple_type (n - 1) T2
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   627
  | dest_n_tuple_type _ T =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   628
    raise TYPE ("Nitpick_Model.dest_n_tuple_type", [T], [])
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   629
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   630
fun const_format thy def_table (x as (s, T)) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   631
  if String.isPrefix unrolled_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   632
    const_format thy def_table (original_name s, range_type T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   633
  else if String.isPrefix skolem_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   634
    let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   635
      val k = unprefix skolem_prefix s
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   636
              |> strip_first_name_sep |> fst |> space_explode "@"
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   637
              |> hd |> Int.fromString |> the
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   638
    in [k, num_binder_types T - k] end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   639
  else if original_name s <> s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   640
    [num_binder_types T]
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   641
  else case def_of_const thy def_table x of
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   642
    SOME t' => if fixpoint_kind_of_rhs t' <> NoFp then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   643
                 let val k = length (strip_abs_vars t') in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   644
                   [k, num_binder_types T - k]
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   645
                 end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   646
               else
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   647
                 [num_binder_types T]
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   648
  | NONE => [num_binder_types T]
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   649
fun intersect_formats _ [] = []
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   650
  | intersect_formats [] _ = []
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   651
  | intersect_formats ks1 ks2 =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   652
    let val ((ks1', k1), (ks2', k2)) = pairself split_last (ks1, ks2) in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   653
      intersect_formats (ks1' @ (if k1 > k2 then [k1 - k2] else []))
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   654
                        (ks2' @ (if k2 > k1 then [k2 - k1] else [])) @
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   655
      [Int.min (k1, k2)]
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   656
    end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   657
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   658
fun lookup_format thy def_table formats t =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   659
  case AList.lookup (fn (SOME x, SOME y) =>
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   660
                        (term_match thy) (x, y) | _ => false)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   661
                    formats (SOME t) of
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   662
    SOME format => format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   663
  | NONE => let val format = the (AList.lookup (op =) formats NONE) in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   664
              case t of
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   665
                Const x => intersect_formats format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   666
                                             (const_format thy def_table x)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   667
              | _ => format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   668
            end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   669
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   670
fun format_type default_format format T =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   671
  let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   672
    val T = uniterize_unarize_unbox_etc_type T
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   673
    val format = format |> filter (curry (op <) 0)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   674
  in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   675
    if forall (curry (op =) 1) format then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   676
      T
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   677
    else
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   678
      let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   679
        val (binder_Ts, body_T) = strip_type T
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   680
        val batched =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   681
          binder_Ts
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   682
          |> map (format_type default_format default_format)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   683
          |> rev |> chunk_list_unevenly (rev format)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   684
          |> map (HOLogic.mk_tupleT o rev)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   685
      in List.foldl (op -->) body_T batched end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   686
  end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   687
fun format_term_type thy def_table formats t =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   688
  format_type (the (AList.lookup (op =) formats NONE))
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   689
              (lookup_format thy def_table formats t) (fastype_of t)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   690
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   691
fun repair_special_format js m format =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   692
  m - 1 downto 0 |> chunk_list_unevenly (rev format)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   693
                 |> map (rev o filter_out (member (op =) js))
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   694
                 |> filter_out null |> map length |> rev
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   695
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   696
fun user_friendly_const ({thy, evals, def_table, skolems, special_funs, ...}
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   697
                         : hol_context) (base_name, step_name) formats =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   698
  let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   699
    val default_format = the (AList.lookup (op =) formats NONE)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   700
    fun do_const (x as (s, T)) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   701
      (if String.isPrefix special_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   702
         let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   703
           val do_term = map_aterms (fn Const x => fst (do_const x) | t' => t')
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   704
           val (x' as (_, T'), js, ts) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   705
             AList.find (op =) (!special_funs) (s, unarize_unbox_etc_type T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   706
             |> the_single
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   707
           val max_j = List.last js
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   708
           val Ts = List.take (binder_types T', max_j + 1)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   709
           val missing_js = filter_out (member (op =) js) (0 upto max_j)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   710
           val missing_Ts = filter_indices missing_js Ts
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   711
           fun nth_missing_var n =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   712
             ((arg_var_prefix ^ nat_subscript (n + 1), 0), nth missing_Ts n)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   713
           val missing_vars = map nth_missing_var (0 upto length missing_js - 1)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   714
           val vars = special_bounds ts @ missing_vars
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   715
           val ts' =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   716
             map (fn j =>
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   717
                     case AList.lookup (op =) (js ~~ ts) j of
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   718
                       SOME t => do_term t
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   719
                     | NONE =>
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   720
                       Var (nth missing_vars
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   721
                                (find_index (curry (op =) j) missing_js)))
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   722
                 (0 upto max_j)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   723
           val t = do_const x' |> fst
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   724
           val format =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   725
             case AList.lookup (fn (SOME t1, SOME t2) => term_match thy (t1, t2)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   726
                                 | _ => false) formats (SOME t) of
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   727
               SOME format =>
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   728
               repair_special_format js (num_binder_types T') format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   729
             | NONE =>
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   730
               const_format thy def_table x'
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   731
               |> repair_special_format js (num_binder_types T')
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   732
               |> intersect_formats default_format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   733
         in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   734
           (list_comb (t, ts') |> fold_rev abs_var vars,
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   735
            format_type default_format format T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   736
         end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   737
       else if String.isPrefix uncurry_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   738
         let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   739
           val (ss, s') = unprefix uncurry_prefix s
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   740
                          |> strip_first_name_sep |>> space_explode "@"
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   741
         in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   742
           if String.isPrefix step_prefix s' then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   743
             do_const (s', T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   744
           else
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   745
             let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   746
               val k = the (Int.fromString (hd ss))
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   747
               val j = the (Int.fromString (List.last ss))
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   748
               val (before_Ts, (tuple_T, rest_T)) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   749
                 strip_n_binders j T ||> (strip_n_binders 1 #>> hd)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   750
               val T' = before_Ts ---> dest_n_tuple_type k tuple_T ---> rest_T
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   751
             in do_const (s', T') end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   752
         end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   753
       else if String.isPrefix unrolled_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   754
         let val t = Const (original_name s, range_type T) in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   755
           (lambda (Free (iter_var_prefix, nat_T)) t,
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   756
            format_type default_format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   757
                        (lookup_format thy def_table formats t) T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   758
         end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   759
       else if String.isPrefix base_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   760
         (Const (base_name, T --> T) $ Const (unprefix base_prefix s, T),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   761
          format_type default_format default_format T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   762
       else if String.isPrefix step_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   763
         (Const (step_name, T --> T) $ Const (unprefix step_prefix s, T),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   764
          format_type default_format default_format T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   765
       else if String.isPrefix quot_normal_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   766
         let val t = Const (nitpick_prefix ^ "normalize quotient type", T) in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   767
           (t, format_term_type thy def_table formats t)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   768
         end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   769
       else if String.isPrefix skolem_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   770
         let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   771
           val ss = the (AList.lookup (op =) (!skolems) s)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   772
           val (Ts, Ts') = chop (length ss) (binder_types T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   773
           val frees = map Free (ss ~~ Ts)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   774
           val s' = original_name s
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   775
         in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   776
           (fold lambda frees (Const (s', Ts' ---> T)),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   777
            format_type default_format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   778
                        (lookup_format thy def_table formats (Const x)) T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   779
         end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   780
       else if String.isPrefix eval_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   781
         let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   782
           val t = nth evals (the (Int.fromString (unprefix eval_prefix s)))
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   783
         in (t, format_term_type thy def_table formats t) end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   784
       else if s = @{const_name undefined_fast_The} then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   785
         (Const (nitpick_prefix ^ "The fallback", T),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   786
          format_type default_format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   787
                      (lookup_format thy def_table formats
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   788
                           (Const (@{const_name The}, (T --> bool_T) --> T))) T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   789
       else if s = @{const_name undefined_fast_Eps} then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   790
         (Const (nitpick_prefix ^ "Eps fallback", T),
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   791
          format_type default_format
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   792
                      (lookup_format thy def_table formats
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   793
                           (Const (@{const_name Eps}, (T --> bool_T) --> T))) T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   794
       else
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   795
         let val t = Const (original_name s, T) in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   796
           (t, format_term_type thy def_table formats t)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   797
         end)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   798
      |>> map_types uniterize_unarize_unbox_etc_type
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   799
      |>> shorten_names_in_term |>> Term.map_abs_vars shortest_name
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   800
  in do_const end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   801
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   802
fun assign_operator_for_const (s, T) =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   803
  if String.isPrefix ubfp_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   804
    if is_fun_type T then "\<subseteq>" else "\<le>"
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   805
  else if String.isPrefix lbfp_prefix s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   806
    if is_fun_type T then "\<supseteq>" else "\<ge>"
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   807
  else if original_name s <> s then
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   808
    assign_operator_for_const (strip_first_name_sep s |> snd, T)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   809
  else
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   810
    "="
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   811
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   812
(** Model reconstruction **)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35712
diff changeset
   813
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   814
fun term_for_name pool scope sel_names rel_table bounds name =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   815
  let val T = type_of name in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   816
    tuple_list_for_name rel_table bounds name
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   817
    |> reconstruct_term false pool (("", ""), ("", "")) scope sel_names
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   818
                        rel_table bounds T T (rep_of name)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   819
  end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   820
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   821
fun unfold_outer_the_binders (t as Const (@{const_name The}, _)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   822
                                   $ Abs (s, T, Const (@{const_name "op ="}, _)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   823
                                                $ Bound 0 $ t')) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   824
    betapply (Abs (s, T, t'), t) |> unfold_outer_the_binders
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   825
  | unfold_outer_the_binders t = t
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   826
fun bisimilar_values _ 0 _ = true
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   827
  | bisimilar_values coTs max_depth (t1, t2) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   828
    let val T = fastype_of t1 in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   829
      if exists_subtype (member (op =) coTs) T then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   830
        let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   831
          val ((head1, args1), (head2, args2)) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   832
            pairself (strip_comb o unfold_outer_the_binders) (t1, t2)
34121
5e831d805118 get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents: 33982
diff changeset
   833
          val max_depth = max_depth - (if member (op =) coTs T then 1 else 0)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   834
        in
34936
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   835
          head1 = head2 andalso
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   836
          forall (bisimilar_values coTs max_depth) (args1 ~~ args2)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   837
        end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   838
      else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   839
        t1 = t2
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   840
    end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   841
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
   842
fun reconstruct_hol_model {show_datatypes, show_consts}
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   843
        ({hol_ctxt = {thy, ctxt, max_bisim_depth, boxes, stds, wfs, user_axioms,
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   844
                      debug, binary_ints, destroy_constrs, specialize,
36389
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   845
                      star_linear_preds, fast_descrs, tac_timeout, evals,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   846
                      case_names, def_table, nondef_table, user_nondefs,
36388
30f7ce76712d removed Nitpick's "uncurry" option
blanchet
parents: 36385
diff changeset
   847
                      simp_table, psimp_table, choice_spec_table, intro_table,
30f7ce76712d removed Nitpick's "uncurry" option
blanchet
parents: 36385
diff changeset
   848
                      ground_thm_table, ersatz_table, skolems, special_funs,
30f7ce76712d removed Nitpick's "uncurry" option
blanchet
parents: 36385
diff changeset
   849
                      unrolled_preds, wf_cache, constr_cache},
35190
ce653cc27a94 make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents: 35188
diff changeset
   850
         binarize, card_assigns, bits, bisim_depth, datatypes, ofs} : scope)
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   851
        formats all_frees free_names sel_names nonsel_names rel_table bounds =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   852
  let
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   853
    val pool = Unsynchronized.ref []
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   854
    val (wacky_names as (_, base_step_names), ctxt) =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   855
      add_wacky_syntax ctxt
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents: 34998
diff changeset
   856
    val hol_ctxt =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   857
      {thy = thy, ctxt = ctxt, max_bisim_depth = max_bisim_depth, boxes = boxes,
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   858
       stds = stds, wfs = wfs, user_axioms = user_axioms, debug = debug,
34124
c4628a1dcf75 added support for binary nat/int representation to Nitpick
blanchet
parents: 34123
diff changeset
   859
       binary_ints = binary_ints, destroy_constrs = destroy_constrs,
36389
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   860
       specialize = specialize, star_linear_preds = star_linear_preds,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   861
       fast_descrs = fast_descrs, tac_timeout = tac_timeout, evals = evals,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   862
       case_names = case_names, def_table = def_table,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   863
       nondef_table = nondef_table, user_nondefs = user_nondefs,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   864
       simp_table = simp_table, psimp_table = psimp_table,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   865
       choice_spec_table = choice_spec_table, intro_table = intro_table,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   866
       ground_thm_table = ground_thm_table, ersatz_table = ersatz_table,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   867
       skolems = skolems, special_funs = special_funs,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   868
       unrolled_preds = unrolled_preds, wf_cache = wf_cache,
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
   869
       constr_cache = constr_cache}
36388
30f7ce76712d removed Nitpick's "uncurry" option
blanchet
parents: 36385
diff changeset
   870
    val scope =
30f7ce76712d removed Nitpick's "uncurry" option
blanchet
parents: 36385
diff changeset
   871
      {hol_ctxt = hol_ctxt, binarize = binarize, card_assigns = card_assigns,
30f7ce76712d removed Nitpick's "uncurry" option
blanchet
parents: 36385
diff changeset
   872
       bits = bits, bisim_depth = bisim_depth, datatypes = datatypes, ofs = ofs}
35188
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   873
    fun term_for_rep unfold =
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   874
      reconstruct_term unfold pool wacky_names scope sel_names rel_table bounds
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   875
    fun nth_value_of_type card T n =
35188
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   876
      let
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   877
        fun aux unfold = term_for_rep unfold T T (Atom (card, 0)) [[n]]
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   878
      in
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   879
        case aux false of
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   880
          t as Const (s, _) =>
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   881
          if String.isPrefix cyclic_const_prefix s then
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   882
            HOLogic.mk_eq (t, aux true)
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   883
          else
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   884
            t
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   885
        | t => t
8c70a34931b1 improve Nitpick's "Datatypes" rendering for elements containing cycles
blanchet
parents: 35180
diff changeset
   886
      end
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   887
    val all_values =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   888
      all_values_of_type pool wacky_names scope sel_names rel_table bounds
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   889
    fun is_codatatype_wellformed (cos : dtype_spec list)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   890
                                 ({typ, card, ...} : dtype_spec) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   891
      let
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   892
        val ts = all_values card typ
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   893
        val max_depth = Integer.sum (map #card cos)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   894
      in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   895
        forall (not o bisimilar_values (map #typ cos) max_depth)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   896
               (all_distinct_unordered_pairs_of ts)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   897
      end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   898
    fun pretty_for_assign name =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   899
      let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   900
        val (oper, (t1, T'), T) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   901
          case name of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   902
            FreeName (s, T, _) =>
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   903
            let val t = Free (s, uniterize_unarize_unbox_etc_type T) in
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   904
              ("=", (t, format_term_type thy def_table formats t), T)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   905
            end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   906
          | ConstName (s, T, _) =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   907
            (assign_operator_for_const (s, T),
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   908
             user_friendly_const hol_ctxt base_step_names formats (s, T), T)
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   909
          | _ => raise NUT ("Nitpick_Model.reconstruct_hol_model.\
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   910
                            \pretty_for_assign", [name])
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   911
        val t2 = if rep_of name = Any then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   912
                   Const (@{const_name undefined}, T')
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   913
                 else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   914
                   tuple_list_for_name rel_table bounds name
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
   915
                   |> term_for_rep false T T' (rep_of name)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   916
      in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   917
        Pretty.block (Pretty.breaks
33571
3655e51f9958 minor cleanup in Nitpick
blanchet
parents: 33565
diff changeset
   918
            [setmp_show_all_types (Syntax.pretty_term ctxt) t1,
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   919
             Pretty.str oper, Syntax.pretty_term ctxt t2])
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   920
      end
34123
c4988215a691 distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents: 34121
diff changeset
   921
    fun pretty_for_datatype ({typ, card, complete, ...} : dtype_spec) =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   922
      Pretty.block (Pretty.breaks
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   923
          (Syntax.pretty_typ ctxt (uniterize_unarize_unbox_etc_type typ) ::
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   924
           (case typ of
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   925
              Type (@{type_name fin_fun}, _) => [Pretty.str "[finite]"]
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   926
            | Type (@{type_name fun_box}, _) => [Pretty.str "[boxed]"]
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   927
            | Type (@{type_name pair_box}, _) => [Pretty.str "[boxed]"]
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   928
            | _ => []) @
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   929
           [Pretty.str "=",
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   930
            Pretty.enum "," "{" "}"
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35711
diff changeset
   931
                (map (Syntax.pretty_term ctxt) (all_values card typ) @
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   932
                 (if fun_from_pair complete false then []
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   933
                  else [Pretty.str unrep]))]))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   934
    fun integer_datatype T =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   935
      [{typ = T, card = card_of_type card_assigns T, co = false,
35385
29f81babefd7 improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents: 35280
diff changeset
   936
        standard = true, complete = (false, false), concrete = (true, true),
29f81babefd7 improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents: 35280
diff changeset
   937
        deep = true, constrs = []}]
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   938
      handle TYPE ("Nitpick_HOL.card_of_type", _, _) => []
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   939
    val (codatatypes, datatypes) =
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 34974
diff changeset
   940
      datatypes |> filter #deep |> List.partition #co
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   941
                ||> append (integer_datatype int_T
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   942
                            |> is_standard_datatype thy stds nat_T
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   943
                               ? append (integer_datatype nat_T))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   944
    val block_of_datatypes =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   945
      if show_datatypes andalso not (null datatypes) then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   946
        [Pretty.big_list ("Datatype" ^ plural_s_for_list datatypes ^ ":")
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   947
                         (map pretty_for_datatype datatypes)]
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   948
      else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   949
        []
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   950
    val block_of_codatatypes =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   951
      if show_datatypes andalso not (null codatatypes) then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   952
        [Pretty.big_list ("Codatatype" ^ plural_s_for_list codatatypes ^ ":")
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   953
                         (map pretty_for_datatype codatatypes)]
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   954
      else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   955
        []
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   956
    fun block_of_names show title names =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   957
      if show andalso not (null names) then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   958
        Pretty.str (title ^ plural_s_for_list names ^ ":")
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   959
        :: map (Pretty.indent indent_size o pretty_for_assign)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   960
               (sort_wrt (original_name o nickname_of) names)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   961
      else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   962
        []
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   963
    val (skolem_names, nonskolem_nonsel_names) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   964
      List.partition is_skolem_name nonsel_names
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   965
    val (eval_names, noneval_nonskolem_nonsel_names) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   966
      List.partition (String.isPrefix eval_prefix o nickname_of)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   967
                     nonskolem_nonsel_names
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   968
      ||> filter_out (member (op =) [@{const_name bisim},
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   969
                                     @{const_name bisim_iterator_max}]
34121
5e831d805118 get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents: 33982
diff changeset
   970
                      o nickname_of)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   971
    val free_names =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   972
      map (fn x as (s, T) =>
34121
5e831d805118 get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents: 33982
diff changeset
   973
              case filter (curry (op =) x
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   974
                       o pairf nickname_of
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35625
diff changeset
   975
                               (uniterize_unarize_unbox_etc_type o type_of))
35190
ce653cc27a94 make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents: 35188
diff changeset
   976
                       free_names of
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   977
                [name] => name
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   978
              | [] => FreeName (s, T, Any)
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33202
diff changeset
   979
              | _ => raise TERM ("Nitpick_Model.reconstruct_hol_model",
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   980
                                 [Const x])) all_frees
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   981
    val chunks = block_of_names true "Free variable" free_names @
36390
eee4ee6a5cbe remove "show_skolems" option and change style of record declarations
blanchet
parents: 36389
diff changeset
   982
                 block_of_names true "Skolem constant" skolem_names @
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   983
                 block_of_names true "Evaluated term" eval_names @
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   984
                 block_of_datatypes @ block_of_codatatypes @
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   985
                 block_of_names show_consts "Constant"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   986
                                noneval_nonskolem_nonsel_names
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   987
  in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   988
    (Pretty.chunks (if null chunks then [Pretty.str "Empty assignment"]
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   989
                    else chunks),
34936
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   990
     bisim_depth >= 0 orelse
c4f04bee79f3 some work on Nitpick's support for quotient types;
blanchet
parents: 34126
diff changeset
   991
     forall (is_codatatype_wellformed codatatypes) codatatypes)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   992
  end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   993
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   994
fun prove_hol_model (scope as {hol_ctxt = {thy, ctxt, debug, ...},
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   995
                               card_assigns, ...})
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   996
                    auto_timeout free_names sel_names rel_table bounds prop =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   997
  let
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
   998
    val pool = Unsynchronized.ref []
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   999
    fun free_type_assm (T, k) =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1000
      let
35076
cc19e2aef17e added hotel key card example for Nitpick, and renumber atoms in Nitpick's output for increased readability
blanchet
parents: 35075
diff changeset
  1001
        fun atom j = nth_atom pool true T j k
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1002
        fun equation_for_atom j = HOLogic.eq_const T $ Bound 0 $ atom j
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1003
        val eqs = map equation_for_atom (index_seq 0 k)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1004
        val compreh_assm =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1005
          Const (@{const_name All}, (T --> bool_T) --> bool_T)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1006
              $ Abs ("x", T, foldl1 HOLogic.mk_disj eqs)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1007
        val distinct_assm = distinctness_formula T (map atom (index_seq 0 k))
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1008
      in s_conj (compreh_assm, distinct_assm) end
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1009
    fun free_name_assm name =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1010
      HOLogic.mk_eq (Free (nickname_of name, type_of name),
35180
c57dba973391 more work on Nitpick's support for nonstandard models + fix in model reconstruction
blanchet
parents: 35179
diff changeset
  1011
                     term_for_name pool scope sel_names rel_table bounds name)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1012
    val freeT_assms = map free_type_assm (filter (is_TFree o fst) card_assigns)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1013
    val model_assms = map free_name_assm free_names
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1014
    val assm = foldr1 s_conj (freeT_assms @ model_assms)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1015
    fun try_out negate =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1016
      let
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1017
        val concl = (negate ? curry (op $) @{const Not})
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35408
diff changeset
  1018
                    (Object_Logic.atomize_term thy prop)
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1019
        val prop = HOLogic.mk_Trueprop (HOLogic.mk_imp (assm, concl))
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1020
                   |> map_types (map_type_tfree
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1021
                                     (fn (s, []) => TFree (s, HOLogic.typeS)
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1022
                                       | x => TFree x))
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1023
       val _ = if debug then
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1024
                 priority ((if negate then "Genuineness" else "Spuriousness") ^
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1025
                           " goal: " ^ Syntax.string_of_term ctxt prop ^ ".")
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1026
               else
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1027
                 ()
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
  1028
        val goal = prop |> cterm_of thy |> Goal.init
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1029
      in
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1030
        (goal |> SINGLE (DETERM_TIMEOUT auto_timeout
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1031
                                        (auto_tac (clasimpset_of ctxt)))
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1032
              |> the |> Goal.finish ctxt; true)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1033
        handle THM _ => false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1034
             | TimeLimit.TimeOut => false
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1035
      end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1036
  in
33705
947184dc75c9 removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents: 33580
diff changeset
  1037
    if try_out false then SOME true
947184dc75c9 removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents: 33580
diff changeset
  1038
    else if try_out true then SOME false
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1039
    else NONE
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1040
  end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1041
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
  1042
end;