src/HOL/Tools/refute.ML
author blanchet
Mon, 14 Dec 2009 12:14:12 +0100
changeset 34120 f9920a3ddf50
parent 34017 ef2776c89799
child 34974 18b41bba42b5
permissions -rw-r--r--
added "no_assms" option to Refute, and include structured proof assumptions by default; will do the same for Quickcheck unless there are objections
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     1
(*  Title:      HOL/Tools/refute.ML
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 29004
diff changeset
     2
    Author:     Tjark Weber, TU Muenchen
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     3
14965
7155b319eafa new SAT solver interface
webertj
parents: 14818
diff changeset
     4
Finite model generation for HOL formulas, using a SAT solver.
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     5
*)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     6
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
     7
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
     8
(* Declares the 'REFUTE' signature as well as a structure 'Refute'.          *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
     9
(* Documentation is available in the Isabelle/Isar theory 'HOL/Refute.thy'.  *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    10
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    11
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    12
signature REFUTE =
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    13
sig
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    14
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    15
  exception REFUTE of string * string
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    16
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    17
(* ------------------------------------------------------------------------- *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
    18
(* Model/interpretation related code (translation HOL -> propositional logic *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    19
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    20
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    21
  type params
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    22
  type interpretation
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    23
  type model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    24
  type arguments
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    25
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    26
  exception MAXVARS_EXCEEDED
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    27
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    28
  val add_interpreter : string -> (theory -> model -> arguments -> term ->
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    29
    (interpretation * model * arguments) option) -> theory -> theory
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    30
  val add_printer     : string -> (theory -> model -> typ ->
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    31
    interpretation -> (int -> bool) -> term option) -> theory -> theory
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    32
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    33
  val interpret : theory -> model -> arguments -> term ->
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    34
    (interpretation * model * arguments)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
    35
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    36
  val print       : theory -> model -> typ -> interpretation -> (int -> bool) -> term
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    37
  val print_model : theory -> model -> (int -> bool) -> string
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    38
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    39
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    40
(* Interface                                                                 *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    41
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    42
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    43
  val set_default_param  : (string * string) -> theory -> theory
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    44
  val get_default_param  : theory -> string -> string option
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    45
  val get_default_params : theory -> (string * string) list
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    46
  val actual_params      : theory -> (string * string) list -> params
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    47
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    48
  val find_model : theory -> params -> term list -> term -> bool -> unit
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    49
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    50
  (* tries to find a model for a formula: *)
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    51
  val satisfy_term :
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    52
    theory -> (string * string) list -> term list -> term -> unit
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    53
  (* tries to find a model that refutes a formula: *)
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    54
  val refute_term :
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    55
    theory -> (string * string) list -> term list -> term -> unit
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    56
  val refute_goal :
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    57
    Proof.context -> (string * string) list -> thm -> int -> unit
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    58
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    59
  val setup : theory -> theory
22092
ab3dfcef6489 reformatted to 80 chars/line
webertj
parents: 22055
diff changeset
    60
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    61
(* ------------------------------------------------------------------------- *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    62
(* Additional functions used by Nitpick (to be factored out)                 *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    63
(* ------------------------------------------------------------------------- *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    64
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    65
  val close_form : term -> term
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    66
  val get_classdef : theory -> string -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    67
  val norm_rhs : term -> term
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    68
  val get_def : theory -> string * typ -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    69
  val get_typedef : theory -> typ -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    70
  val is_IDT_constructor : theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    71
  val is_IDT_recursor : theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    72
  val is_const_of_class: theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    73
  val monomorphic_term : Type.tyenv -> term -> term
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    74
  val specialize_type : theory -> (string * typ) -> term -> term
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    75
  val string_of_typ : typ -> string
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    76
  val typ_of_dtyp : Datatype.descr -> (Datatype.dtyp * typ) list -> Datatype.dtyp -> typ
22092
ab3dfcef6489 reformatted to 80 chars/line
webertj
parents: 22055
diff changeset
    77
end;  (* signature REFUTE *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    78
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    79
structure Refute : REFUTE =
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    80
struct
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    81
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    82
  open PropLogic;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    83
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    84
  (* We use 'REFUTE' only for internal error conditions that should    *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    85
  (* never occur in the first place (i.e. errors caused by bugs in our *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    86
  (* code).  Otherwise (e.g. to indicate invalid input data) we use    *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    87
  (* 'error'.                                                          *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    88
  exception REFUTE of string * string;  (* ("in function", "cause") *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    89
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    90
  (* should be raised by an interpreter when more variables would be *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    91
  (* required than allowed by 'maxvars'                              *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    92
  exception MAXVARS_EXCEEDED;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    93
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    94
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    95
(* TREES                                                                     *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    96
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    97
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    98
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    99
(* tree: implements an arbitrarily (but finitely) branching tree as a list   *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   100
(*       of (lists of ...) elements                                          *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   101
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   102
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   103
  datatype 'a tree =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   104
      Leaf of 'a
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   105
    | Node of ('a tree) list;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   106
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   107
  (* ('a -> 'b) -> 'a tree -> 'b tree *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   108
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   109
  fun tree_map f tr =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   110
    case tr of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   111
      Leaf x  => Leaf (f x)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   112
    | Node xs => Node (map (tree_map f) xs);
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   113
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   114
  (* ('a * 'b -> 'a) -> 'a * ('b tree) -> 'a *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   115
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   116
  fun tree_foldl f =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   117
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   118
    fun itl (e, Leaf x)  = f(e,x)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   119
      | itl (e, Node xs) = Library.foldl (tree_foldl f) (e,xs)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   120
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   121
    itl
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   122
  end;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   123
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   124
  (* 'a tree * 'b tree -> ('a * 'b) tree *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   125
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   126
  fun tree_pair (t1, t2) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   127
    case t1 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   128
      Leaf x =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   129
      (case t2 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   130
          Leaf y => Leaf (x,y)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   131
        | Node _ => raise REFUTE ("tree_pair",
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   132
            "trees are of different height (second tree is higher)"))
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   133
    | Node xs =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   134
      (case t2 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   135
          (* '~~' will raise an exception if the number of branches in   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   136
          (* both trees is different at the current node                 *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   137
          Node ys => Node (map tree_pair (xs ~~ ys))
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   138
        | Leaf _  => raise REFUTE ("tree_pair",
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   139
            "trees are of different height (first tree is higher)"));
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   140
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   141
(* ------------------------------------------------------------------------- *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   142
(* params: parameters that control the translation into a propositional      *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   143
(*         formula/model generation                                          *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   144
(*                                                                           *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   145
(* The following parameters are supported (and required (!), except for      *)
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
   146
(* "sizes" and "expect"):                                                    *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   147
(*                                                                           *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   148
(* Name          Type    Description                                         *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   149
(*                                                                           *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   150
(* "sizes"       (string * int) list                                         *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   151
(*                       Size of ground types (e.g. 'a=2), or depth of IDTs. *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   152
(* "minsize"     int     If >0, minimal size of each ground type/IDT depth.  *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   153
(* "maxsize"     int     If >0, maximal size of each ground type/IDT depth.  *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   154
(* "maxvars"     int     If >0, use at most 'maxvars' Boolean variables      *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   155
(*                       when transforming the term into a propositional     *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   156
(*                       formula.                                            *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   157
(* "maxtime"     int     If >0, terminate after at most 'maxtime' seconds.   *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   158
(* "satsolver"   string  SAT solver to be used.                              *)
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   159
(* "no_assms"    bool    If "true", assumptions in structured proofs are     *)
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   160
(*                       not considered.                                     *)
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
   161
(* "expect"      string  Expected result ("genuine", "potential", "none", or *)
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   162
(*                       "unknown").                                         *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   163
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   164
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   165
  type params =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   166
    {
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   167
      sizes    : (string * int) list,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   168
      minsize  : int,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   169
      maxsize  : int,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   170
      maxvars  : int,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   171
      maxtime  : int,
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
   172
      satsolver: string,
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   173
      no_assms : bool,
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
   174
      expect   : string
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   175
    };
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   176
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   177
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   178
(* interpretation: a term's interpretation is given by a variable of type    *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   179
(*                 'interpretation'                                          *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   180
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   181
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   182
  type interpretation =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   183
    prop_formula list tree;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   184
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   185
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   186
(* model: a model specifies the size of types and the interpretation of      *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   187
(*        terms                                                              *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   188
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   189
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   190
  type model =
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
   191
    (typ * int) list * (term * interpretation) list;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   192
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   193
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   194
(* arguments: additional arguments required during interpretation of terms   *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   195
(* ------------------------------------------------------------------------- *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   196
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   197
  type arguments =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   198
    {
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   199
      (* just passed unchanged from 'params': *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   200
      maxvars   : int,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   201
      (* whether to use 'make_equality' or 'make_def_equality': *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   202
      def_eq    : bool,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   203
      (* the following may change during the translation: *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   204
      next_idx  : int,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   205
      bounds    : interpretation list,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   206
      wellformed: prop_formula
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   207
    };
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   208
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   209
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33339
diff changeset
   210
  structure RefuteData = Theory_Data
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22580
diff changeset
   211
  (
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   212
    type T =
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
   213
      {interpreters: (string * (theory -> model -> arguments -> term ->
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   214
        (interpretation * model * arguments) option)) list,
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
   215
       printers: (string * (theory -> model -> typ -> interpretation ->
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
   216
        (int -> bool) -> term option)) list,
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   217
       parameters: string Symtab.table};
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   218
    val empty = {interpreters = [], printers = [], parameters = Symtab.empty};
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   219
    val extend = I;
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33339
diff changeset
   220
    fun merge
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   221
      ({interpreters = in1, printers = pr1, parameters = pa1},
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33339
diff changeset
   222
       {interpreters = in2, printers = pr2, parameters = pa2}) : T =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   223
      {interpreters = AList.merge (op =) (K true) (in1, in2),
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   224
       printers = AList.merge (op =) (K true) (pr1, pr2),
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   225
       parameters = Symtab.merge (op=) (pa1, pa2)};
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22580
diff changeset
   226
  );
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   227
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   228
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   229
(* ------------------------------------------------------------------------- *)
15334
d5a92997dc1b exception CANNOT_INTERPRET removed (not needed anymore since the stlc_interpreter can interpret any term)
webertj
parents: 15333
diff changeset
   230
(* interpret: interprets the term 't' using a suitable interpreter; returns  *)
d5a92997dc1b exception CANNOT_INTERPRET removed (not needed anymore since the stlc_interpreter can interpret any term)
webertj
parents: 15333
diff changeset
   231
(*            the interpretation and a (possibly extended) model that keeps  *)
d5a92997dc1b exception CANNOT_INTERPRET removed (not needed anymore since the stlc_interpreter can interpret any term)
webertj
parents: 15333
diff changeset
   232
(*            track of the interpretation of subterms                        *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   233
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   234
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   235
  (* theory -> model -> arguments -> Term.term ->
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   236
    (interpretation * model * arguments) *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   237
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   238
  fun interpret thy model args t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   239
    case get_first (fn (_, f) => f thy model args t)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   240
      (#interpreters (RefuteData.get thy)) of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   241
      NONE   => raise REFUTE ("interpret",
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26931
diff changeset
   242
        "no interpreter for term " ^ quote (Syntax.string_of_term_global thy t))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   243
    | SOME x => x;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   244
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   245
(* ------------------------------------------------------------------------- *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   246
(* print: converts the interpretation 'intr', which must denote a term of    *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   247
(*        type 'T', into a term using a suitable printer                     *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   248
(* ------------------------------------------------------------------------- *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   249
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   250
  (* theory -> model -> Term.typ -> interpretation -> (int -> bool) ->
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   251
    Term.term *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   252
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   253
  fun print thy model T intr assignment =
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   254
    case get_first (fn (_, f) => f thy model T intr assignment)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   255
      (#printers (RefuteData.get thy)) of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   256
      NONE   => raise REFUTE ("print",
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26931
diff changeset
   257
        "no printer for type " ^ quote (Syntax.string_of_typ_global thy T))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   258
    | SOME x => x;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   259
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   260
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   261
(* print_model: turns the model into a string, using a fixed interpretation  *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   262
(*              (given by an assignment for Boolean variables) and suitable  *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   263
(*              printers                                                     *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   264
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   265
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   266
  (* theory -> model -> (int -> bool) -> string *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   267
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   268
  fun print_model thy model assignment =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   269
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   270
    val (typs, terms) = model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   271
    val typs_msg =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   272
      if null typs then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   273
        "empty universe (no type variables in term)\n"
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   274
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   275
        "Size of types: " ^ commas (map (fn (T, i) =>
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26931
diff changeset
   276
          Syntax.string_of_typ_global thy T ^ ": " ^ string_of_int i) typs) ^ "\n"
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   277
    val show_consts_msg =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   278
      if not (!show_consts) andalso Library.exists (is_Const o fst) terms then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   279
        "set \"show_consts\" to show the interpretation of constants\n"
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   280
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   281
        ""
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   282
    val terms_msg =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   283
      if null terms then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   284
        "empty interpretation (no free variables in term)\n"
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   285
      else
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
   286
        cat_lines (map_filter (fn (t, intr) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   287
          (* print constants only if 'show_consts' is true *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   288
          if (!show_consts) orelse not (is_Const t) then
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26931
diff changeset
   289
            SOME (Syntax.string_of_term_global thy t ^ ": " ^
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26931
diff changeset
   290
              Syntax.string_of_term_global thy
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   291
                (print thy model (Term.type_of t) intr assignment))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   292
          else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   293
            NONE) terms) ^ "\n"
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   294
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   295
    typs_msg ^ show_consts_msg ^ terms_msg
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   296
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   297
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   298
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   299
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   300
(* PARAMETER MANAGEMENT                                                      *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   301
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   302
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   303
  (* string -> (theory -> model -> arguments -> Term.term ->
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   304
    (interpretation * model * arguments) option) -> theory -> theory *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   305
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   306
  fun add_interpreter name f thy =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   307
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   308
    val {interpreters, printers, parameters} = RefuteData.get thy
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   309
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   310
    case AList.lookup (op =) interpreters name of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   311
      NONE   => RefuteData.put {interpreters = (name, f) :: interpreters,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   312
      printers = printers, parameters = parameters} thy
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   313
    | SOME _ => error ("Interpreter " ^ name ^ " already declared")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   314
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   315
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   316
  (* string -> (theory -> model -> Term.typ -> interpretation ->
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   317
    (int -> bool) -> Term.term option) -> theory -> theory *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   318
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   319
  fun add_printer name f thy =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   320
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   321
    val {interpreters, printers, parameters} = RefuteData.get thy
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   322
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   323
    case AList.lookup (op =) printers name of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   324
      NONE   => RefuteData.put {interpreters = interpreters,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   325
      printers = (name, f) :: printers, parameters = parameters} thy
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   326
    | SOME _ => error ("Printer " ^ name ^ " already declared")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   327
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   328
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   329
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   330
(* set_default_param: stores the '(name, value)' pair in RefuteData's        *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   331
(*                    parameter table                                        *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   332
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   333
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   334
  (* (string * string) -> theory -> theory *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   335
29004
a5a91f387791 removed Table.extend, NameSpace.extend_table
haftmann
parents: 28524
diff changeset
   336
  fun set_default_param (name, value) = RefuteData.map 
a5a91f387791 removed Table.extend, NameSpace.extend_table
haftmann
parents: 28524
diff changeset
   337
    (fn {interpreters, printers, parameters} =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   338
      {interpreters = interpreters, printers = printers,
29004
a5a91f387791 removed Table.extend, NameSpace.extend_table
haftmann
parents: 28524
diff changeset
   339
        parameters = Symtab.update (name, value) parameters});
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   340
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   341
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   342
(* get_default_param: retrieves the value associated with 'name' from        *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   343
(*                    RefuteData's parameter table                           *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   344
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   345
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   346
  (* theory -> string -> string option *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   347
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   348
  val get_default_param = Symtab.lookup o #parameters o RefuteData.get;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   349
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   350
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   351
(* get_default_params: returns a list of all '(name, value)' pairs that are  *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   352
(*                     stored in RefuteData's parameter table                *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   353
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   354
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   355
  (* theory -> (string * string) list *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   356
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   357
  val get_default_params = Symtab.dest o #parameters o RefuteData.get;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   358
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   359
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   360
(* actual_params: takes a (possibly empty) list 'params' of parameters that  *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   361
(*      override the default parameters currently specified in 'thy', and    *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   362
(*      returns a record that can be passed to 'find_model'.                 *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   363
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   364
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   365
  (* theory -> (string * string) list -> params *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   366
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   367
  fun actual_params thy override =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   368
  let
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   369
    (* (string * string) list * string -> bool *)
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   370
    fun read_bool (parms, name) =
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   371
      case AList.lookup (op =) parms name of
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   372
        SOME "true" => true
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   373
      | SOME "false" => false
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   374
      | SOME s => error ("parameter " ^ quote name ^
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   375
        " (value is " ^ quote s ^ ") must be \"true\" or \"false\"")
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   376
      | NONE   => error ("parameter " ^ quote name ^
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   377
          " must be assigned a value")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   378
    (* (string * string) list * string -> int *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   379
    fun read_int (parms, name) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   380
      case AList.lookup (op =) parms name of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   381
        SOME s => (case Int.fromString s of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   382
          SOME i => i
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   383
        | NONE   => error ("parameter " ^ quote name ^
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   384
          " (value is " ^ quote s ^ ") must be an integer value"))
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   385
      | NONE   => error ("parameter " ^ quote name ^
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   386
          " must be assigned a value")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   387
    (* (string * string) list * string -> string *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   388
    fun read_string (parms, name) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   389
      case AList.lookup (op =) parms name of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   390
        SOME s => s
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   391
      | NONE   => error ("parameter " ^ quote name ^
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   392
        " must be assigned a value")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   393
    (* 'override' first, defaults last: *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   394
    (* (string * string) list *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   395
    val allparams = override @ (get_default_params thy)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   396
    (* int *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   397
    val minsize   = read_int (allparams, "minsize")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   398
    val maxsize   = read_int (allparams, "maxsize")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   399
    val maxvars   = read_int (allparams, "maxvars")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   400
    val maxtime   = read_int (allparams, "maxtime")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   401
    (* string *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   402
    val satsolver = read_string (allparams, "satsolver")
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   403
    val no_assms = read_bool (allparams, "no_assms")
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
   404
    val expect = the_default "" (AList.lookup (op =) allparams "expect")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   405
    (* all remaining parameters of the form "string=int" are collected in *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   406
    (* 'sizes'                                                            *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   407
    (* TODO: it is currently not possible to specify a size for a type    *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   408
    (*       whose name is one of the other parameters (e.g. 'maxvars')   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   409
    (* (string * int) list *)
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
   410
    val sizes     = map_filter
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   411
      (fn (name, value) => Option.map (pair name) (Int.fromString value))
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33246
diff changeset
   412
      (filter (fn (name, _) => name<>"minsize" andalso name<>"maxsize"
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   413
        andalso name<>"maxvars" andalso name<>"maxtime"
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   414
        andalso name<>"satsolver" andalso name<>"no_assms") allparams)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   415
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   416
    {sizes=sizes, minsize=minsize, maxsize=maxsize, maxvars=maxvars,
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   417
      maxtime=maxtime, satsolver=satsolver, no_assms=no_assms, expect=expect}
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   418
  end;
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   419
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   420
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   421
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   422
(* TRANSLATION HOL -> PROPOSITIONAL LOGIC, BOOLEAN ASSIGNMENT -> MODEL       *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   423
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   424
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   425
  fun typ_of_dtyp descr typ_assoc (Datatype_Aux.DtTFree a) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   426
    (* replace a 'DtTFree' variable by the associated type *)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   427
    the (AList.lookup (op =) typ_assoc (Datatype_Aux.DtTFree a))
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   428
    | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtType (s, ds)) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   429
    Type (s, map (typ_of_dtyp descr typ_assoc) ds)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   430
    | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtRec i) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   431
    let
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
   432
      val (s, ds, _) = the (AList.lookup (op =) descr i)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   433
    in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   434
      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   435
    end;
15335
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
   436
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
   437
(* ------------------------------------------------------------------------- *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   438
(* close_form: universal closure over schematic variables in 't'             *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   439
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   440
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   441
  (* Term.term -> Term.term *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   442
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   443
  fun close_form t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   444
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   445
    (* (Term.indexname * Term.typ) list *)
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 29004
diff changeset
   446
    val vars = sort_wrt (fst o fst) (map dest_Var (OldTerm.term_vars t))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   447
  in
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   448
    fold (fn ((x, i), T) => fn t' =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   449
      Term.all T $ Abs (x, T, abstract_over (Var ((x, i), T), t'))) vars t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   450
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   451
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   452
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   453
(* monomorphic_term: applies a type substitution 'typeSubs' for all type     *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   454
(*                   variables in a term 't'                                 *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   455
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   456
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   457
  (* Type.tyenv -> Term.term -> Term.term *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   458
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   459
  fun monomorphic_term typeSubs t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   460
    map_types (map_type_tvar
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   461
      (fn v =>
26328
b2d6f520172c Type.lookup now curried
haftmann
parents: 25538
diff changeset
   462
        case Type.lookup typeSubs v of
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   463
          NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   464
          (* schematic type variable not instantiated *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   465
          raise REFUTE ("monomorphic_term",
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   466
            "no substitution for type variable " ^ fst (fst v) ^
26957
e3f04fdd994d eliminated theory CPure;
wenzelm
parents: 26939
diff changeset
   467
            " in term " ^ Syntax.string_of_term_global Pure.thy t)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   468
        | SOME typ =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   469
          typ)) t;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   470
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   471
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   472
(* specialize_type: given a constant 's' of type 'T', which is a subterm of  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   473
(*                  't', where 't' has a (possibly) more general type, the   *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   474
(*                  schematic type variables in 't' are instantiated to      *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   475
(*                  match the type 'T' (may raise Type.TYPE_MATCH)           *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   476
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   477
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   478
  (* theory -> (string * Term.typ) -> Term.term -> Term.term *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   479
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   480
  fun specialize_type thy (s, T) t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   481
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   482
    fun find_typeSubs (Const (s', T')) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   483
      if s=s' then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   484
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   485
          handle Type.TYPE_MATCH => NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   486
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   487
        NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   488
      | find_typeSubs (Free _)           = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   489
      | find_typeSubs (Var _)            = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   490
      | find_typeSubs (Bound _)          = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   491
      | find_typeSubs (Abs (_, _, body)) = find_typeSubs body
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   492
      | find_typeSubs (t1 $ t2)          =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   493
      (case find_typeSubs t1 of SOME x => SOME x
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   494
                              | NONE   => find_typeSubs t2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   495
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   496
    case find_typeSubs t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   497
      SOME typeSubs =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   498
      monomorphic_term typeSubs t
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   499
    | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   500
      (* no match found - perhaps due to sort constraints *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   501
      raise Type.TYPE_MATCH
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   502
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   503
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   504
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   505
(* is_const_of_class: returns 'true' iff 'Const (s, T)' is a constant that   *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   506
(*                    denotes membership to an axiomatic type class          *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   507
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   508
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   509
  (* theory -> string * Term.typ -> bool *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   510
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   511
  fun is_const_of_class thy (s, T) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   512
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   513
    val class_const_names = map Logic.const_of_class (Sign.all_classes thy)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   514
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   515
    (* I'm not quite sure if checking the name 's' is sufficient, *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   516
    (* or if we should also check the type 'T'.                   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   517
    s mem_string class_const_names
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   518
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   519
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   520
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   521
(* is_IDT_constructor: returns 'true' iff 'Const (s, T)' is the constructor  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   522
(*                     of an inductive datatype in 'thy'                     *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   523
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   524
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   525
  (* theory -> string * Term.typ -> bool *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   526
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   527
  fun is_IDT_constructor thy (s, T) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   528
    (case body_type T of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   529
      Type (s', _) =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   530
      (case Datatype.get_constrs thy s' of
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   531
        SOME constrs =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   532
        List.exists (fn (cname, cty) =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   533
          cname = s andalso Sign.typ_instance thy (T, cty)) constrs
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   534
      | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   535
        false)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   536
    | _  =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   537
      false);
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   538
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   539
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   540
(* is_IDT_recursor: returns 'true' iff 'Const (s, T)' is the recursion       *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   541
(*                  operator of an inductive datatype in 'thy'               *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   542
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   543
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   544
  (* theory -> string * Term.typ -> bool *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   545
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   546
  fun is_IDT_recursor thy (s, T) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   547
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   548
    val rec_names = Symtab.fold (append o #rec_names o snd)
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   549
      (Datatype.get_all thy) []
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   550
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   551
    (* I'm not quite sure if checking the name 's' is sufficient, *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   552
    (* or if we should also check the type 'T'.                   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   553
    s mem_string rec_names
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   554
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   555
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   556
(* ------------------------------------------------------------------------- *)
30275
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   557
(* norm_rhs: maps  f ?t1 ... ?tn == rhs  to  %t1...tn. rhs                   *)
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   558
(* ------------------------------------------------------------------------- *)
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   559
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   560
  fun norm_rhs eqn =
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   561
  let
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   562
    fun lambda (v as Var ((x, _), T)) t = Abs (x, T, abstract_over (v, t))
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   563
      | lambda v t                      = raise TERM ("lambda", [v, t])
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   564
    val (lhs, rhs) = Logic.dest_equals eqn
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   565
    val (_, args)  = Term.strip_comb lhs
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   566
  in
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   567
    fold lambda (rev args) rhs
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   568
  end
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   569
381ce8d88cb8 Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
blanchet
parents: 30242
diff changeset
   570
(* ------------------------------------------------------------------------- *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   571
(* get_def: looks up the definition of a constant, as created by "constdefs" *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   572
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   573
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   574
  (* theory -> string * Term.typ -> (string * Term.term) option *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   575
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   576
  fun get_def thy (s, T) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   577
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   578
    (* (string * Term.term) list -> (string * Term.term) option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   579
    fun get_def_ax [] = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   580
      | get_def_ax ((axname, ax) :: axioms) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   581
      (let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   582
        val (lhs, _) = Logic.dest_equals ax  (* equations only *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   583
        val c        = Term.head_of lhs
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   584
        val (s', T') = Term.dest_Const c
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   585
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   586
        if s=s' then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   587
          let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   588
            val typeSubs = Sign.typ_match thy (T', T) Vartab.empty
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   589
            val ax'      = monomorphic_term typeSubs ax
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   590
            val rhs      = norm_rhs ax'
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   591
          in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   592
            SOME (axname, rhs)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   593
          end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   594
        else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   595
          get_def_ax axioms
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   596
      end handle ERROR _         => get_def_ax axioms
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   597
               | TERM _          => get_def_ax axioms
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   598
               | Type.TYPE_MATCH => get_def_ax axioms)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   599
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   600
    get_def_ax (Theory.all_axioms_of thy)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   601
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   602
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   603
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   604
(* get_typedef: looks up the definition of a type, as created by "typedef"   *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   605
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   606
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   607
  (* theory -> Term.typ -> (string * Term.term) option *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   608
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   609
  fun get_typedef thy T =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   610
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   611
    (* (string * Term.term) list -> (string * Term.term) option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   612
    fun get_typedef_ax [] = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   613
      | get_typedef_ax ((axname, ax) :: axioms) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   614
      (let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   615
        (* Term.term -> Term.typ option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   616
        fun type_of_type_definition (Const (s', T')) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   617
          if s'="Typedef.type_definition" then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   618
            SOME T'
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   619
          else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   620
            NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   621
          | type_of_type_definition (Free _)           = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   622
          | type_of_type_definition (Var _)            = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   623
          | type_of_type_definition (Bound _)          = NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   624
          | type_of_type_definition (Abs (_, _, body)) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   625
          type_of_type_definition body
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   626
          | type_of_type_definition (t1 $ t2)          =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   627
          (case type_of_type_definition t1 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   628
            SOME x => SOME x
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   629
          | NONE   => type_of_type_definition t2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   630
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   631
        case type_of_type_definition ax of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   632
          SOME T' =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   633
          let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   634
            val T''      = (domain_type o domain_type) T'
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   635
            val typeSubs = Sign.typ_match thy (T'', T) Vartab.empty
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   636
          in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   637
            SOME (axname, monomorphic_term typeSubs ax)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   638
          end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   639
        | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   640
          get_typedef_ax axioms
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   641
      end handle ERROR _         => get_typedef_ax axioms
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   642
               | MATCH           => get_typedef_ax axioms
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   643
               | Type.TYPE_MATCH => get_typedef_ax axioms)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   644
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   645
    get_typedef_ax (Theory.all_axioms_of thy)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   646
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   647
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   648
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   649
(* get_classdef: looks up the defining axiom for an axiomatic type class, as *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   650
(*               created by the "axclass" command                            *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   651
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   652
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   653
  (* theory -> string -> (string * Term.term) option *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   654
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   655
  fun get_classdef thy class =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   656
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   657
    val axname = class ^ "_class_def"
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   658
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   659
    Option.map (pair axname)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   660
      (AList.lookup (op =) (Theory.all_axioms_of thy) axname)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   661
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   662
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   663
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   664
(* unfold_defs: unfolds all defined constants in a term 't', beta-eta        *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   665
(*              normalizes the result term; certain constants are not        *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   666
(*              unfolded (cf. 'collect_axioms' and the various interpreters  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   667
(*              below): if the interpretation respects a definition anyway,  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   668
(*              that definition does not need to be unfolded                 *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   669
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   670
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   671
  (* theory -> Term.term -> Term.term *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   672
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   673
  (* Note: we could intertwine unfolding of constants and beta-(eta-)       *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   674
  (*       normalization; this would save some unfolding for terms where    *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   675
  (*       constants are eliminated by beta-reduction (e.g. 'K c1 c2').  On *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   676
  (*       the other hand, this would cause additional work for terms where *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   677
  (*       constants are duplicated by beta-reduction (e.g. 'S c1 c2 c3').  *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   678
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   679
  fun unfold_defs thy t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   680
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   681
    (* Term.term -> Term.term *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   682
    fun unfold_loop t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   683
      case t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   684
      (* Pure *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   685
        Const (@{const_name all}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   686
      | Const (@{const_name "=="}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   687
      | Const (@{const_name "==>"}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   688
      | Const (@{const_name TYPE}, _) => t  (* axiomatic type classes *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   689
      (* HOL *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   690
      | Const (@{const_name Trueprop}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   691
      | Const (@{const_name Not}, _) => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   692
      | (* redundant, since 'True' is also an IDT constructor *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   693
        Const (@{const_name True}, _) => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   694
      | (* redundant, since 'False' is also an IDT constructor *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   695
        Const (@{const_name False}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   696
      | Const (@{const_name undefined}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   697
      | Const (@{const_name The}, _) => t
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   698
      | Const (@{const_name Hilbert_Choice.Eps}, _) => t
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   699
      | Const (@{const_name All}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   700
      | Const (@{const_name Ex}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   701
      | Const (@{const_name "op ="}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   702
      | Const (@{const_name "op &"}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   703
      | Const (@{const_name "op |"}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   704
      | Const (@{const_name "op -->"}, _) => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   705
      (* sets *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   706
      | Const (@{const_name Collect}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   707
      | Const (@{const_name "op :"}, _) => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   708
      (* other optimizations *)
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   709
      | Const (@{const_name Finite_Set.card}, _) => t
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   710
      | Const (@{const_name Finite_Set.finite}, _) => t
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23029
diff changeset
   711
      | Const (@{const_name HOL.less}, Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   712
        Type ("fun", [Type ("nat", []), Type ("bool", [])])])) => t
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22846
diff changeset
   713
      | Const (@{const_name HOL.plus}, Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   714
        Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22846
diff changeset
   715
      | Const (@{const_name HOL.minus}, Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   716
        Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22846
diff changeset
   717
      | Const (@{const_name HOL.times}, Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   718
        Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   719
      | Const (@{const_name List.append}, _) => t
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   720
      | Const (@{const_name lfp}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   721
      | Const (@{const_name gfp}, _) => t
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   722
      | Const (@{const_name fst}, _) => t
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   723
      | Const (@{const_name snd}, _) => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   724
      (* simply-typed lambda calculus *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   725
      | Const (s, T) =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   726
        (if is_IDT_constructor thy (s, T)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   727
          orelse is_IDT_recursor thy (s, T) then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   728
          t  (* do not unfold IDT constructors/recursors *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   729
        (* unfold the constant if there is a defining equation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   730
        else case get_def thy (s, T) of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   731
          SOME (axname, rhs) =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   732
          (* Note: if the term to be unfolded (i.e. 'Const (s, T)')  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   733
          (* occurs on the right-hand side of the equation, i.e. in  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   734
          (* 'rhs', we must not use this equation to unfold, because *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   735
          (* that would loop.  Here would be the right place to      *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   736
          (* check this.  However, getting this really right seems   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   737
          (* difficult because the user may state arbitrary axioms,  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   738
          (* which could interact with overloading to create loops.  *)
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
   739
          ((*tracing (" unfolding: " ^ axname);*)
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   740
           unfold_loop rhs)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   741
        | NONE => t)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   742
      | Free _           => t
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   743
      | Var _            => t
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   744
      | Bound _          => t
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   745
      | Abs (s, T, body) => Abs (s, T, unfold_loop body)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   746
      | t1 $ t2          => (unfold_loop t1) $ (unfold_loop t2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   747
    val result = Envir.beta_eta_contract (unfold_loop t)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   748
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   749
    result
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   750
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   751
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   752
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   753
(* collect_axioms: collects (monomorphic, universally quantified, unfolded   *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   754
(*                 versions of) all HOL axioms that are relevant w.r.t 't'   *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   755
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   756
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   757
  (* Note: to make the collection of axioms more easily extensible, this    *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   758
  (*       function could be based on user-supplied "axiom collectors",     *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   759
  (*       similar to 'interpret'/interpreters or 'print'/printers          *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   760
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   761
  (* Note: currently we use "inverse" functions to the definitional         *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   762
  (*       mechanisms provided by Isabelle/HOL, e.g. for "axclass",         *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   763
  (*       "typedef", "constdefs".  A more general approach could consider  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   764
  (*       *every* axiom of the theory and collect it if it has a constant/ *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   765
  (*       type/typeclass in common with the term 't'.                      *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   766
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   767
  (* theory -> Term.term -> Term.term list *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   768
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   769
  (* Which axioms are "relevant" for a particular term/type goes hand in    *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   770
  (* hand with the interpretation of that term/type by its interpreter (see *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   771
  (* way below): if the interpretation respects an axiom anyway, the axiom  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   772
  (* does not need to be added as a constraint here.                        *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   773
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   774
  (* To avoid collecting the same axiom multiple times, we use an           *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   775
  (* accumulator 'axs' which contains all axioms collected so far.          *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   776
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   777
  fun collect_axioms thy t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   778
  let
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
   779
    val _ = tracing "Adding axioms..."
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   780
    val axioms = Theory.all_axioms_of thy
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   781
    fun collect_this_axiom (axname, ax) axs =
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   782
      let
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   783
        val ax' = unfold_defs thy ax
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   784
      in
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   785
        if member (op aconv) axs ax' then axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   786
        else (tracing axname; collect_term_axioms ax' (ax' :: axs))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   787
      end
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   788
    and collect_sort_axioms T axs =
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   789
      let
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   790
        val sort =
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   791
          (case T of
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   792
            TFree (_, sort) => sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   793
          | TVar (_, sort)  => sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   794
          | _ => raise REFUTE ("collect_axioms",
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   795
              "type " ^ Syntax.string_of_typ_global thy T ^ " is not a variable"))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   796
        (* obtain axioms for all superclasses *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   797
        val superclasses = sort @ maps (Sign.super_classes thy) sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   798
        (* merely an optimization, because 'collect_this_axiom' disallows *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   799
        (* duplicate axioms anyway:                                       *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   800
        val superclasses = distinct (op =) superclasses
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   801
        val class_axioms = maps (fn class => map (fn ax =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   802
          ("<" ^ class ^ ">", Thm.prop_of ax))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   803
          (#axioms (AxClass.get_info thy class) handle ERROR _ => []))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   804
          superclasses
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   805
        (* replace the (at most one) schematic type variable in each axiom *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   806
        (* by the actual type 'T'                                          *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   807
        val monomorphic_class_axioms = map (fn (axname, ax) =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   808
          (case Term.add_tvars ax [] of
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   809
            [] => (axname, ax)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   810
          | [(idx, S)] => (axname, monomorphic_term (Vartab.make [(idx, (S, T))]) ax)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   811
          | _ =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   812
            raise REFUTE ("collect_axioms", "class axiom " ^ axname ^ " (" ^
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   813
              Syntax.string_of_term_global thy ax ^
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   814
              ") contains more than one type variable")))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   815
          class_axioms
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   816
      in
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   817
        fold collect_this_axiom monomorphic_class_axioms axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   818
      end
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   819
    and collect_type_axioms T axs =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   820
      case T of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   821
      (* simple types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   822
        Type ("prop", []) => axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   823
      | Type ("fun", [T1, T2]) => collect_type_axioms T2 (collect_type_axioms T1 axs)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   824
      (* axiomatic type classes *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   825
      | Type ("itself", [T1]) => collect_type_axioms T1 axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   826
      | Type (s, Ts) =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   827
        (case Datatype.get_info thy s of
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   828
          SOME info =>  (* inductive datatype *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   829
            (* only collect relevant type axioms for the argument types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   830
            fold collect_type_axioms Ts axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   831
        | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   832
          (case get_typedef thy T of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   833
            SOME (axname, ax) =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   834
            collect_this_axiom (axname, ax) axs
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   835
          | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   836
            (* unspecified type, perhaps introduced with "typedecl" *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   837
            (* at least collect relevant type axioms for the argument types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   838
            fold collect_type_axioms Ts axs))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   839
      (* axiomatic type classes *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   840
      | TFree _ => collect_sort_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   841
      (* axiomatic type classes *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   842
      | TVar _ => collect_sort_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   843
    and collect_term_axioms t axs =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   844
      case t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   845
      (* Pure *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   846
        Const (@{const_name all}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   847
      | Const (@{const_name "=="}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   848
      | Const (@{const_name "==>"}, _) => axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   849
      (* axiomatic type classes *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   850
      | Const (@{const_name TYPE}, T) => collect_type_axioms T axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   851
      (* HOL *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   852
      | Const (@{const_name Trueprop}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   853
      | Const (@{const_name Not}, _) => axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   854
      (* redundant, since 'True' is also an IDT constructor *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   855
      | Const (@{const_name True}, _) => axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   856
      (* redundant, since 'False' is also an IDT constructor *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   857
      | Const (@{const_name False}, _) => axs
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   858
      | Const (@{const_name undefined}, T) => collect_type_axioms T axs
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   859
      | Const (@{const_name The}, T) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   860
        let
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   861
          val ax = specialize_type thy (@{const_name The}, T)
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
   862
            (the (AList.lookup (op =) axioms "HOL.the_eq_trivial"))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   863
        in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   864
          collect_this_axiom ("HOL.the_eq_trivial", ax) axs
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   865
        end
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   866
      | Const (@{const_name Hilbert_Choice.Eps}, T) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   867
        let
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   868
          val ax = specialize_type thy (@{const_name Hilbert_Choice.Eps}, T)
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
   869
            (the (AList.lookup (op =) axioms "Hilbert_Choice.someI"))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   870
        in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   871
          collect_this_axiom ("Hilbert_Choice.someI", ax) axs
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   872
        end
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   873
      | Const (@{const_name All}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   874
      | Const (@{const_name Ex}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   875
      | Const (@{const_name "op ="}, T) => collect_type_axioms T axs
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   876
      | Const (@{const_name "op &"}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   877
      | Const (@{const_name "op |"}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   878
      | Const (@{const_name "op -->"}, _) => axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   879
      (* sets *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   880
      | Const (@{const_name Collect}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   881
      | Const (@{const_name "op :"}, T) => collect_type_axioms T axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   882
      (* other optimizations *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   883
      | Const (@{const_name Finite_Set.card}, T) => collect_type_axioms T axs
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   884
      | Const (@{const_name Finite_Set.finite}, T) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   885
        collect_type_axioms T axs
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23029
diff changeset
   886
      | Const (@{const_name HOL.less}, T as Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   887
        Type ("fun", [Type ("nat", []), Type ("bool", [])])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   888
          collect_type_axioms T axs
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22846
diff changeset
   889
      | Const (@{const_name HOL.plus}, T as Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   890
        Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   891
          collect_type_axioms T axs
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22846
diff changeset
   892
      | Const (@{const_name HOL.minus}, T as Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   893
        Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   894
          collect_type_axioms T axs
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22846
diff changeset
   895
      | Const (@{const_name HOL.times}, T as Type ("fun", [Type ("nat", []),
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   896
        Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   897
          collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   898
      | Const (@{const_name List.append}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   899
      | Const (@{const_name lfp}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   900
      | Const (@{const_name gfp}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   901
      | Const (@{const_name fst}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   902
      | Const (@{const_name snd}, T) => collect_type_axioms T axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   903
      (* simply-typed lambda calculus *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   904
      | Const (s, T) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   905
          if is_const_of_class thy (s, T) then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   906
            (* axiomatic type classes: add "OFCLASS(?'a::c, c_class)" *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   907
            (* and the class definition                               *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   908
            let
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   909
              val class = Logic.class_of_const s
31943
5e960a0780a2 renamed inclass/Inclass to of_class/OfClass, in accordance to of_sort;
wenzelm
parents: 31784
diff changeset
   910
              val of_class = Logic.mk_of_class (TVar (("'a", 0), [class]), class)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   911
              val ax_in = SOME (specialize_type thy (s, T) of_class)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   912
                (* type match may fail due to sort constraints *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   913
                handle Type.TYPE_MATCH => NONE
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   914
              val ax_1 = Option.map (fn ax => (Syntax.string_of_term_global thy ax, ax)) ax_in
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   915
              val ax_2 = Option.map (apsnd (specialize_type thy (s, T))) (get_classdef thy class)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   916
            in
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   917
              collect_type_axioms T (fold collect_this_axiom (map_filter I [ax_1, ax_2]) axs)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   918
            end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   919
          else if is_IDT_constructor thy (s, T)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   920
            orelse is_IDT_recursor thy (s, T) then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   921
            (* only collect relevant type axioms *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   922
            collect_type_axioms T axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   923
          else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   924
            (* other constants should have been unfolded, with some *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   925
            (* exceptions: e.g. Abs_xxx/Rep_xxx functions for       *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   926
            (* typedefs, or type-class related constants            *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   927
            (* only collect relevant type axioms *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   928
            collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   929
      | Free (_, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   930
      | Var (_, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   931
      | Bound _ => axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   932
      | Abs (_, T, body) => collect_term_axioms body (collect_type_axioms T axs)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   933
      | t1 $ t2 => collect_term_axioms t2 (collect_term_axioms t1 axs)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   934
    val result = map close_form (collect_term_axioms t [])
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
   935
    val _ = tracing " ...done."
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   936
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   937
    result
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   938
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   939
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   940
(* ------------------------------------------------------------------------- *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   941
(* ground_types: collects all ground types in a term (including argument     *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   942
(*               types of other types), suppressing duplicates.  Does not    *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   943
(*               return function types, set types, non-recursive IDTs, or    *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   944
(*               'propT'.  For IDTs, also the argument types of constructors *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   945
(*               and all mutually recursive IDTs are considered.             *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   946
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   947
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   948
  fun ground_types thy t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   949
  let
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   950
    fun collect_types T acc =
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   951
      (case T of
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   952
        Type ("fun", [T1, T2]) => collect_types T1 (collect_types T2 acc)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   953
      | Type ("prop", [])      => acc
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   954
      | Type (s, Ts)           =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   955
        (case Datatype.get_info thy s of
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   956
          SOME info =>  (* inductive datatype *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   957
          let
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   958
            val index        = #index info
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   959
            val descr        = #descr info
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
   960
            val (_, typs, _) = the (AList.lookup (op =) descr index)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   961
            val typ_assoc    = typs ~~ Ts
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   962
            (* sanity check: every element in 'dtyps' must be a *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   963
            (* 'DtTFree'                                        *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   964
            val _ = if Library.exists (fn d =>
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   965
              case d of Datatype_Aux.DtTFree _ => false | _ => true) typs then
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   966
              raise REFUTE ("ground_types", "datatype argument (for type "
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26931
diff changeset
   967
                ^ Syntax.string_of_typ_global thy T ^ ") is not a variable")
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   968
            else ()
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   969
            (* required for mutually recursive datatypes; those need to   *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   970
            (* be added even if they are an instance of an otherwise non- *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   971
            (* recursive datatype                                         *)
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   972
            fun collect_dtyp d acc =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   973
            let
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   974
              val dT = typ_of_dtyp descr typ_assoc d
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   975
            in
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   976
              case d of
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   977
                Datatype_Aux.DtTFree _ =>
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   978
                collect_types dT acc
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   979
              | Datatype_Aux.DtType (_, ds) =>
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   980
                collect_types dT (fold_rev collect_dtyp ds acc)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   981
              | Datatype_Aux.DtRec i =>
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   982
                if dT mem acc then
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   983
                  acc  (* prevent infinite recursion *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   984
                else
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   985
                  let
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
   986
                    val (_, dtyps, dconstrs) = the (AList.lookup (op =) descr i)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   987
                    (* if the current type is a recursive IDT (i.e. a depth *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   988
                    (* is required), add it to 'acc'                        *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   989
                    val acc_dT = if Library.exists (fn (_, ds) =>
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
   990
                      Library.exists Datatype_Aux.is_rec_type ds) dconstrs then
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   991
                        insert (op =) dT acc
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   992
                      else acc
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   993
                    (* collect argument types *)
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   994
                    val acc_dtyps = fold_rev collect_dtyp dtyps acc_dT
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   995
                    (* collect constructor types *)
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   996
                    val acc_dconstrs = fold_rev collect_dtyp (maps snd dconstrs) acc_dtyps
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   997
                  in
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   998
                    acc_dconstrs
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   999
                  end
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1000
            end
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1001
          in
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1002
            (* argument types 'Ts' could be added here, but they are also *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1003
            (* added by 'collect_dtyp' automatically                      *)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
  1004
            collect_dtyp (Datatype_Aux.DtRec index) acc
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1005
          end
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1006
        | NONE =>
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1007
          (* not an inductive datatype, e.g. defined via "typedef" or *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1008
          (* "typedecl"                                               *)
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
  1009
          insert (op =) T (fold collect_types Ts acc))
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1010
      | TFree _                => insert (op =) T acc
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1011
      | TVar _                 => insert (op =) T acc)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1012
  in
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
  1013
    fold_types collect_types t []
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1014
  end;
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1015
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1016
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1017
(* string_of_typ: (rather naive) conversion from types to strings, used to   *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1018
(*                look up the size of a type in 'sizes'.  Parameterized      *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1019
(*                types with different parameters (e.g. "'a list" vs. "bool  *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1020
(*                list") are identified.                                     *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1021
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1022
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1023
  (* Term.typ -> string *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1024
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1025
  fun string_of_typ (Type (s, _))     = s
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1026
    | string_of_typ (TFree (s, _))    = s
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1027
    | string_of_typ (TVar ((s,_), _)) = s;
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1028
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1029
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1030
(* first_universe: returns the "first" (i.e. smallest) universe by assigning *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1031
(*                 'minsize' to every type for which no size is specified in *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1032
(*                 'sizes'                                                   *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1033
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1034
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1035
  (* Term.typ list -> (string * int) list -> int -> (Term.typ * int) list *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1036
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1037
  fun first_universe xs sizes minsize =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1038
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1039
    fun size_of_typ T =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1040
      case AList.lookup (op =) sizes (string_of_typ T) of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1041
        SOME n => n
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1042
      | NONE => minsize
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1043
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1044
    map (fn T => (T, size_of_typ T)) xs
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1045
  end;
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1046
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1047
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1048
(* next_universe: enumerates all universes (i.e. assignments of sizes to     *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1049
(*                types), where the minimal size of a type is given by       *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1050
(*                'minsize', the maximal size is given by 'maxsize', and a   *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1051
(*                type may have a fixed size given in 'sizes'                *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1052
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1053
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1054
  (* (Term.typ * int) list -> (string * int) list -> int -> int ->
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1055
    (Term.typ * int) list option *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1056
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1057
  fun next_universe xs sizes minsize maxsize =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1058
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1059
    (* creates the "first" list of length 'len', where the sum of all list *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1060
    (* elements is 'sum', and the length of the list is 'len'              *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1061
    (* int -> int -> int -> int list option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1062
    fun make_first _ 0 sum =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1063
      if sum=0 then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1064
        SOME []
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1065
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1066
        NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1067
      | make_first max len sum =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1068
      if sum<=max orelse max<0 then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1069
        Option.map (fn xs' => sum :: xs') (make_first max (len-1) 0)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1070
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1071
        Option.map (fn xs' => max :: xs') (make_first max (len-1) (sum-max))
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1072
    (* enumerates all int lists with a fixed length, where 0<=x<='max' for *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1073
    (* all list elements x (unless 'max'<0)                                *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1074
    (* int -> int -> int -> int list -> int list option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1075
    fun next max len sum [] =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1076
      NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1077
      | next max len sum [x] =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1078
      (* we've reached the last list element, so there's no shift possible *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1079
      make_first max (len+1) (sum+x+1)  (* increment 'sum' by 1 *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1080
      | next max len sum (x1::x2::xs) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1081
      if x1>0 andalso (x2<max orelse max<0) then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1082
        (* we can shift *)
33035
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 33002
diff changeset
  1083
        SOME (the (make_first max (len+1) (sum+x1-1)) @ (x2+1) :: xs)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1084
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1085
        (* continue search *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1086
        next max (len+1) (sum+x1) (x2::xs)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1087
    (* only consider those types for which the size is not fixed *)
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33246
diff changeset
  1088
    val mutables = filter_out (AList.defined (op =) sizes o string_of_typ o fst) xs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1089
    (* subtract 'minsize' from every size (will be added again at the end) *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1090
    val diffs = map (fn (_, n) => n-minsize) mutables
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1091
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1092
    case next (maxsize-minsize) 0 0 diffs of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1093
      SOME diffs' =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1094
      (* merge with those types for which the size is fixed *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1095
      SOME (fst (fold_map (fn (T, _) => fn ds =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1096
        case AList.lookup (op =) sizes (string_of_typ T) of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1097
        (* return the fixed size *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1098
          SOME n => ((T, n), ds)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1099
        (* consume the head of 'ds', add 'minsize' *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1100
        | NONE   => ((T, minsize + hd ds), tl ds))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1101
        xs diffs'))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1102
    | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1103
      NONE
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1104
  end;
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1105
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1106
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1107
(* toTrue: converts the interpretation of a Boolean value to a propositional *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1108
(*         formula that is true iff the interpretation denotes "true"        *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1109
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1110
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1111
  (* interpretation -> prop_formula *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1112
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1113
  fun toTrue (Leaf [fm, _]) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1114
    fm
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1115
    | toTrue _              =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1116
    raise REFUTE ("toTrue", "interpretation does not denote a Boolean value");
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1117
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1118
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1119
(* toFalse: converts the interpretation of a Boolean value to a              *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1120
(*          propositional formula that is true iff the interpretation        *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1121
(*          denotes "false"                                                  *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1122
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1123
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1124
  (* interpretation -> prop_formula *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1125
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1126
  fun toFalse (Leaf [_, fm]) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1127
    fm
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1128
    | toFalse _              =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1129
    raise REFUTE ("toFalse", "interpretation does not denote a Boolean value");
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1130
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1131
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1132
(* find_model: repeatedly calls 'interpret' with appropriate parameters,     *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1133
(*             applies a SAT solver, and (in case a model is found) displays *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1134
(*             the model to the user by calling 'print_model'                *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1135
(* thy       : the current theory                                            *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1136
(* {...}     : parameters that control the translation/model generation      *)
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1137
(* assm_ts   : assumptions to be considered unless "no_assms" is specified   *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1138
(* t         : term to be translated into a propositional formula            *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1139
(* negate    : if true, find a model that makes 't' false (rather than true) *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1140
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1141
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1142
  (* theory -> params -> Term.term -> bool -> unit *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1143
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
  1144
  fun find_model thy {sizes, minsize, maxsize, maxvars, maxtime, satsolver,
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1145
    no_assms, expect} assm_ts t negate =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1146
  let
33054
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1147
    (* string -> unit *)
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1148
    fun check_expect outcome_code =
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1149
      if expect = "" orelse outcome_code = expect then ()
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1150
      else error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1151
    (* unit -> unit *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1152
    fun wrapper () =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1153
    let
30349
110826d1e5ff Added a second timeout mechanism to Refute.
blanchet
parents: 30347
diff changeset
  1154
      val timer  = Timer.startRealTimer ()
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1155
      val t = if no_assms then t
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1156
              else if negate then Logic.list_implies (assm_ts, t)
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1157
              else Logic.mk_conjunction_list (t :: assm_ts)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1158
      val u      = unfold_defs thy t
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
  1159
      val _      = tracing ("Unfolded term: " ^ Syntax.string_of_term_global thy u)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1160
      val axioms = collect_axioms thy u
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1161
      (* Term.typ list *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1162
      val types = fold (union (op =) o ground_types thy) (u :: axioms) []
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
  1163
      val _     = tracing ("Ground types: "
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1164
        ^ (if null types then "none."
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26931
diff changeset
  1165
           else commas (map (Syntax.string_of_typ_global thy) types)))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1166
      (* we can only consider fragments of recursive IDTs, so we issue a  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1167
      (* warning if the formula contains a recursive IDT                  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1168
      (* TODO: no warning needed for /positive/ occurrences of IDTs       *)
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
  1169
      val maybe_spurious = Library.exists (fn
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1170
          Type (s, _) =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
  1171
          (case Datatype.get_info thy s of
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1172
            SOME info =>  (* inductive datatype *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1173
            let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1174
              val index           = #index info
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1175
              val descr           = #descr info
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
  1176
              val (_, _, constrs) = the (AList.lookup (op =) descr index)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1177
            in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1178
              (* recursive datatype? *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1179
              Library.exists (fn (_, ds) =>
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33955
diff changeset
  1180
                Library.exists Datatype_Aux.is_rec_type ds) constrs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1181
            end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1182
          | NONE => false)
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
  1183
        | _ => false) types
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
  1184
      val _ = if maybe_spurious then
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1185
          warning ("Term contains a recursive datatype; "
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1186
            ^ "countermodel(s) may be spurious!")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1187
        else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1188
          ()
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
  1189
      (* (Term.typ * int) list -> string *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1190
      fun find_model_loop universe =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1191
      let
30349
110826d1e5ff Added a second timeout mechanism to Refute.
blanchet
parents: 30347
diff changeset
  1192
        val msecs_spent = Time.toMilliseconds (Timer.checkRealTimer timer)
110826d1e5ff Added a second timeout mechanism to Refute.
blanchet
parents: 30347
diff changeset
  1193
        val _ = maxtime = 0 orelse msecs_spent < 1000 * maxtime
110826d1e5ff Added a second timeout mechanism to Refute.
blanchet
parents: 30347
diff changeset
  1194
                orelse raise TimeLimit.TimeOut
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1195
        val init_model = (universe, [])
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1196
        val init_args  = {maxvars = maxvars, def_eq = false, next_idx = 1,
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1197
          bounds = [], wellformed = True}
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
  1198
        val _ = tracing ("Translating term (sizes: "
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1199
          ^ commas (map (fn (_, n) => string_of_int n) universe) ^ ") ...")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1200
        (* translate 'u' and all axioms *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1201
        val (intrs, (model, args)) = fold_map (fn t' => fn (m, a) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1202
          let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1203
            val (i, m', a') = interpret thy m a t'
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1204
          in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1205
            (* set 'def_eq' to 'true' *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1206
            (i, (m', {maxvars = #maxvars a', def_eq = true,
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1207
              next_idx = #next_idx a', bounds = #bounds a',
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1208
              wellformed = #wellformed a'}))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1209
          end) (u :: axioms) (init_model, init_args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1210
        (* make 'u' either true or false, and make all axioms true, and *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1211
        (* add the well-formedness side condition                       *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1212
        val fm_u  = (if negate then toFalse else toTrue) (hd intrs)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1213
        val fm_ax = PropLogic.all (map toTrue (tl intrs))
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1214
        val fm    = PropLogic.all [#wellformed args, fm_ax, fm_u]
34017
ef2776c89799 better error message in Refute when specifying a non-existing SAT solver
blanchet
parents: 33968
diff changeset
  1215
        val solver =
ef2776c89799 better error message in Refute when specifying a non-existing SAT solver
blanchet
parents: 33968
diff changeset
  1216
          SatSolver.invoke_solver satsolver
ef2776c89799 better error message in Refute when specifying a non-existing SAT solver
blanchet
parents: 33968
diff changeset
  1217
          handle Option.Option =>
ef2776c89799 better error message in Refute when specifying a non-existing SAT solver
blanchet
parents: 33968
diff changeset