src/HOL/Library/refute.ML
author blanchet
Sun, 04 May 2014 18:57:45 +0200
changeset 56851 35ff4ede3409
parent 56846 9df717fef2bb
child 56853 a265e41cc33b
permissions -rw-r--r--
renamed 'dpll_p' to 'cdclite', to avoid confusion with the old 'dpll' and to reflect the idea that the new prover implements some ideas from CDCL not in DPLL -- this follows its author's, Sascha B.'s, wish
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50530
6266e44b3396 updated some headers;
wenzelm
parents: 49985
diff changeset
     1
(*  Title:      HOL/Library/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
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     7
signature REFUTE =
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     8
sig
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     9
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    10
  exception REFUTE of string * string
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    11
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    12
(* ------------------------------------------------------------------------- *)
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
    13
(* 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
    14
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    15
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    16
  type params
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    17
  type interpretation
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    18
  type model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    19
  type arguments
14456
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
  exception MAXVARS_EXCEEDED
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    22
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    23
  val add_interpreter : string -> (Proof.context -> model -> arguments -> term ->
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    24
    (interpretation * model * arguments) option) -> theory -> theory
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    25
  val add_printer : string -> (Proof.context -> model -> typ ->
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    26
    interpretation -> (int -> bool) -> term option) -> theory -> theory
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    27
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    28
  val interpret : Proof.context -> 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)
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
    30
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    31
  val print : Proof.context -> model -> typ -> interpretation -> (int -> bool) -> term
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    32
  val print_model : Proof.context -> model -> (int -> bool) -> string
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    33
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    34
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    35
(* Interface                                                                 *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    36
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    37
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    38
  val set_default_param  : (string * string) -> theory -> theory
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    39
  val get_default_param  : Proof.context -> string -> string option
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    40
  val get_default_params : Proof.context -> (string * string) list
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    41
  val actual_params      : Proof.context -> (string * string) list -> params
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    42
45387
ccffb3f9f42b return outcome code, so that it can be picked up by Mutabelle
blanchet
parents: 44121
diff changeset
    43
  val find_model :
ccffb3f9f42b return outcome code, so that it can be picked up by Mutabelle
blanchet
parents: 44121
diff changeset
    44
    Proof.context -> params -> term list -> term -> bool -> string
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    45
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    46
  (* 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
    47
  val satisfy_term :
45387
ccffb3f9f42b return outcome code, so that it can be picked up by Mutabelle
blanchet
parents: 44121
diff changeset
    48
    Proof.context -> (string * string) list -> term list -> term -> string
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    49
  (* 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
    50
  val refute_term :
45387
ccffb3f9f42b return outcome code, so that it can be picked up by Mutabelle
blanchet
parents: 44121
diff changeset
    51
    Proof.context -> (string * string) list -> term list -> term -> string
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    52
  val refute_goal :
45387
ccffb3f9f42b return outcome code, so that it can be picked up by Mutabelle
blanchet
parents: 44121
diff changeset
    53
    Proof.context -> (string * string) list -> thm -> int -> string
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    54
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    55
  val setup : theory -> theory
22092
ab3dfcef6489 reformatted to 80 chars/line
webertj
parents: 22055
diff changeset
    56
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    57
(* ------------------------------------------------------------------------- *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    58
(* 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
    59
(* ------------------------------------------------------------------------- *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    60
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    61
  val get_classdef : theory -> string -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    62
  val norm_rhs : term -> term
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    63
  val get_def : theory -> string * typ -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    64
  val get_typedef : theory -> typ -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    65
  val is_IDT_constructor : theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    66
  val is_IDT_recursor : theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    67
  val is_const_of_class: theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    68
  val string_of_typ : typ -> string
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    69
end;
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    70
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    71
structure Refute : REFUTE =
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    72
struct
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    73
41471
54a58904a598 modernized structure Prop_Logic;
wenzelm
parents: 40720
diff changeset
    74
open Prop_Logic;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    75
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    76
(* We use 'REFUTE' only for internal error conditions that should    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    77
(* never occur in the first place (i.e. errors caused by bugs in our *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    78
(* code).  Otherwise (e.g. to indicate invalid input data) we use    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    79
(* 'error'.                                                          *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    80
exception REFUTE of string * string;  (* ("in function", "cause") *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    81
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    82
(* should be raised by an interpreter when more variables would be *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    83
(* required than allowed by 'maxvars'                              *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    84
exception MAXVARS_EXCEEDED;
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    85
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    86
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    87
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    88
(* TREES                                                                     *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    89
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    90
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    91
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    92
(* tree: implements an arbitrarily (but finitely) branching tree as a list   *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    93
(*       of (lists of ...) elements                                          *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    94
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    95
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    96
datatype 'a tree =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    97
    Leaf of 'a
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    98
  | Node of ('a tree) list;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    99
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   100
fun tree_map f tr =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   101
  case tr of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   102
    Leaf x  => Leaf (f x)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   103
  | Node xs => Node (map (tree_map f) xs);
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   104
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   105
fun tree_pair (t1, t2) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   106
  case t1 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   107
    Leaf x =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   108
      (case t2 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   109
          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
   110
        | 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
   111
            "trees are of different height (second tree is higher)"))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   112
  | Node xs =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   113
      (case t2 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   114
          (* '~~' 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
   115
          (* 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
   116
          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
   117
        | 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
   118
            "trees are of different height (first tree is higher)"));
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   119
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   120
(* ------------------------------------------------------------------------- *)
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
   121
(* 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
   122
(*         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
   123
(*                                                                           *)
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
   124
(* 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
   125
(* "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
   126
(*                                                                           *)
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
   127
(* 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
   128
(*                                                                           *)
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
   129
(* "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
   130
(*                       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
   131
(* "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
   132
(* "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
   133
(* "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
   134
(*                       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
   135
(*                       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
   136
(* "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
   137
(* "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
   138
(* "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
   139
(*                       not considered.                                     *)
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
   140
(* "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
   141
(*                       "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
   142
(* ------------------------------------------------------------------------- *)
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
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   144
type params =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   145
  {
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   146
    sizes    : (string * int) list,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   147
    minsize  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   148
    maxsize  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   149
    maxvars  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   150
    maxtime  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   151
    satsolver: string,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   152
    no_assms : bool,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   153
    expect   : string
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   154
  };
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
   155
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
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   157
(* 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
   158
(*                 'interpretation'                                          *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   159
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   160
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   161
type interpretation =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   162
  prop_formula list tree;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   163
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   164
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   165
(* 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
   166
(*        terms                                                              *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   167
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   168
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   169
type model =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   170
  (typ * int) list * (term * interpretation) list;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   171
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   172
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   173
(* arguments: additional arguments required during interpretation of terms   *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   174
(* ------------------------------------------------------------------------- *)
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
   175
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   176
type arguments =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   177
  {
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   178
    (* just passed unchanged from 'params': *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   179
    maxvars   : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   180
    (* whether to use 'make_equality' or 'make_def_equality': *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   181
    def_eq    : bool,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   182
    (* the following may change during the translation: *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   183
    next_idx  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   184
    bounds    : interpretation list,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   185
    wellformed: prop_formula
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   186
  };
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   187
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   188
structure Data = Theory_Data
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   189
(
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   190
  type T =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   191
    {interpreters: (string * (Proof.context -> model -> arguments -> term ->
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   192
      (interpretation * model * arguments) option)) list,
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   193
     printers: (string * (Proof.context -> model -> typ -> interpretation ->
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   194
      (int -> bool) -> term option)) list,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   195
     parameters: string Symtab.table};
46960
f19e5837ad69 refute_params are given in *this* theory;
wenzelm
parents: 46949
diff changeset
   196
  val empty = {interpreters = [], printers = [], parameters = Symtab.empty};
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   197
  val extend = I;
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   198
  fun merge
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   199
    ({interpreters = in1, printers = pr1, parameters = pa1},
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   200
     {interpreters = in2, printers = pr2, parameters = pa2}) : T =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   201
    {interpreters = AList.merge (op =) (K true) (in1, in2),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   202
     printers = AList.merge (op =) (K true) (pr1, pr2),
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 41471
diff changeset
   203
     parameters = Symtab.merge (op =) (pa1, pa2)};
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   204
);
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   205
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42137
diff changeset
   206
val get_data = Data.get o Proof_Context.theory_of;
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   207
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   208
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   209
(* ------------------------------------------------------------------------- *)
15334
d5a92997dc1b exception CANNOT_INTERPRET removed (not needed anymore since the stlc_interpreter can interpret any term)
webertj
parents: 15333
diff changeset
   210
(* 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
   211
(*            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
   212
(*            track of the interpretation of subterms                        *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   213
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   214
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   215
fun interpret ctxt model args t =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   216
  case get_first (fn (_, f) => f ctxt model args t)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   217
      (#interpreters (get_data ctxt)) of
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   218
    NONE => raise REFUTE ("interpret",
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   219
      "no interpreter for term " ^ quote (Syntax.string_of_term ctxt t))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   220
  | SOME x => x;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   221
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   222
(* ------------------------------------------------------------------------- *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   223
(* 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
   224
(*        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
   225
(* ------------------------------------------------------------------------- *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   226
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   227
fun print ctxt model T intr assignment =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   228
  case get_first (fn (_, f) => f ctxt model T intr assignment)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   229
      (#printers (get_data ctxt)) of
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   230
    NONE => raise REFUTE ("print",
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   231
      "no printer for type " ^ quote (Syntax.string_of_typ ctxt T))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   232
  | SOME x => x;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   233
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   234
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   235
(* 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
   236
(*              (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
   237
(*              printers                                                     *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   238
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   239
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   240
fun print_model ctxt model assignment =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   241
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   242
    val (typs, terms) = model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   243
    val typs_msg =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   244
      if null typs then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   245
        "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
   246
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   247
        "Size of types: " ^ commas (map (fn (T, i) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   248
          Syntax.string_of_typ ctxt 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
   249
    val show_consts_msg =
39050
600de0485859 turned show_consts into proper configuration option;
wenzelm
parents: 39049
diff changeset
   250
      if not (Config.get ctxt show_consts) andalso Library.exists (is_Const o fst) terms then
600de0485859 turned show_consts into proper configuration option;
wenzelm
parents: 39049
diff changeset
   251
        "enable \"show_consts\" to show the interpretation of constants\n"
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   252
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   253
        ""
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   254
    val terms_msg =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   255
      if null terms then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   256
        "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
   257
      else
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
   258
        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
   259
          (* print constants only if 'show_consts' is true *)
39050
600de0485859 turned show_consts into proper configuration option;
wenzelm
parents: 39049
diff changeset
   260
          if Config.get ctxt show_consts orelse not (is_Const t) then
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   261
            SOME (Syntax.string_of_term ctxt t ^ ": " ^
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   262
              Syntax.string_of_term ctxt
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   263
                (print ctxt 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
   264
          else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   265
            NONE) terms) ^ "\n"
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   266
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   267
    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
   268
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   269
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   270
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   271
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   272
(* PARAMETER MANAGEMENT                                                      *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   273
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   274
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   275
fun add_interpreter name f = Data.map (fn {interpreters, printers, parameters} =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   276
  case AList.lookup (op =) interpreters name of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   277
    NONE => {interpreters = (name, f) :: interpreters,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   278
      printers = printers, parameters = parameters}
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   279
  | SOME _ => error ("Interpreter " ^ name ^ " already declared"));
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   280
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   281
fun add_printer name f = Data.map (fn {interpreters, printers, parameters} =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   282
  case AList.lookup (op =) printers name of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   283
    NONE => {interpreters = interpreters,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   284
      printers = (name, f) :: printers, parameters = parameters}
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   285
  | SOME _ => error ("Printer " ^ name ^ " already declared"));
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   286
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   287
(* ------------------------------------------------------------------------- *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   288
(* set_default_param: stores the '(name, value)' pair in Data's              *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   289
(*                    parameter table                                        *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   290
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   291
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   292
fun set_default_param (name, value) = Data.map
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   293
  (fn {interpreters, printers, parameters} =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   294
    {interpreters = interpreters, printers = printers,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   295
      parameters = Symtab.update (name, value) parameters});
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   296
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   297
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   298
(* get_default_param: retrieves the value associated with 'name' from        *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   299
(*                    Data's parameter table                                 *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   300
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   301
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   302
val get_default_param = Symtab.lookup o #parameters o get_data;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   303
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   304
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   305
(* get_default_params: returns a list of all '(name, value)' pairs that are  *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   306
(*                     stored in Data's parameter table                      *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   307
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   308
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   309
val get_default_params = Symtab.dest o #parameters o get_data;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   310
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   311
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   312
(* actual_params: takes a (possibly empty) list 'params' of parameters that  *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   313
(*      override the default parameters currently specified, 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
   314
(*      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
   315
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   316
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   317
fun actual_params ctxt override =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   318
  let
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   319
    fun read_bool (parms, name) =
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   320
      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
   321
        SOME "true" => true
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   322
      | SOME "false" => false
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   323
      | SOME s => error ("parameter " ^ quote name ^
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   324
          " (value is " ^ quote s ^ ") must be \"true\" or \"false\"")
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   325
      | NONE   => error ("parameter " ^ quote name ^
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   326
          " 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
   327
    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
   328
      case AList.lookup (op =) parms name of
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   329
        SOME s =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   330
          (case Int.fromString s of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   331
            SOME i => i
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   332
          | NONE   => error ("parameter " ^ quote name ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   333
            " (value is " ^ quote s ^ ") must be an integer value"))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   334
      | NONE => error ("parameter " ^ quote name ^
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   335
          " 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
   336
    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
   337
      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
   338
        SOME s => s
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   339
      | NONE => error ("parameter " ^ quote name ^
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   340
        " 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
   341
    (* 'override' first, defaults last: *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   342
    val allparams = override @ get_default_params ctxt
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   343
    val minsize = read_int (allparams, "minsize")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   344
    val maxsize = read_int (allparams, "maxsize")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   345
    val maxvars = read_int (allparams, "maxvars")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   346
    val maxtime = read_int (allparams, "maxtime")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   347
    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
   348
    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
   349
    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
   350
    (* 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
   351
    (* 'sizes'                                                            *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   352
    (* 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
   353
    (*       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
   354
    (* (string * int) list *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   355
    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
   356
      (fn (name, value) => Option.map (pair name) (Int.fromString value))
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33246
diff changeset
   357
      (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
   358
        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
   359
        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
   360
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   361
    {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
   362
      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
   363
  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
   364
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
   365
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
   366
(* ------------------------------------------------------------------------- *)
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
   367
(* 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
   368
(* ------------------------------------------------------------------------- *)
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
   369
55891
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   370
fun typ_of_dtyp _ typ_assoc (Datatype.DtTFree a) =
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   371
    the (AList.lookup (op =) typ_assoc (Datatype.DtTFree a))
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   372
  | typ_of_dtyp descr typ_assoc (Datatype.DtType (s, Us)) =
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   373
    Type (s, map (typ_of_dtyp descr typ_assoc) Us)
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   374
  | typ_of_dtyp descr typ_assoc (Datatype.DtRec i) =
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   375
    let val (s, ds, _) = the (AList.lookup (op =) descr i) in
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   376
      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   377
    end
d1a9b07783ab support 'datatype_new'-defined datatypes in Nitpick + better support for 'codatatype's
blanchet
parents: 55507
diff changeset
   378
54757
4960647932ec use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents: 54556
diff changeset
   379
val close_form = ATP_Util.close_form
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents: 42680
diff changeset
   380
val monomorphic_term = ATP_Util.monomorphic_term
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents: 42680
diff changeset
   381
val specialize_type = ATP_Util.specialize_type
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   382
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   383
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   384
(* 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
   385
(*                    denotes membership to an axiomatic type class          *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   386
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   387
46096
a00685a18e55 tuned Refute
blanchet
parents: 45896
diff changeset
   388
fun is_const_of_class thy (s, _) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   389
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   390
    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
   391
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   392
    (* 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
   393
    (* or if we should also check the type 'T'.                   *)
36692
54b64d4ad524 farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents: 36555
diff changeset
   394
    member (op =) class_const_names s
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   395
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   396
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   397
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   398
(* 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
   399
(*                     of an inductive datatype in 'thy'                     *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   400
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   401
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   402
fun is_IDT_constructor thy (s, T) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   403
  (case body_type T of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   404
    Type (s', _) =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   405
      (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
   406
        SOME constrs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   407
          List.exists (fn (cname, cty) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   408
            cname = s andalso Sign.typ_instance thy (T, cty)) constrs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   409
      | NONE => false)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   410
  | _  => false);
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   411
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   412
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   413
(* 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
   414
(*                  operator of an inductive datatype in 'thy'               *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   415
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   416
46096
a00685a18e55 tuned Refute
blanchet
parents: 45896
diff changeset
   417
fun is_IDT_recursor thy (s, _) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   418
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   419
    val rec_names = Symtab.fold (append o #rec_names o snd)
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   420
      (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
   421
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   422
    (* 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
   423
    (* or if we should also check the type 'T'.                   *)
36692
54b64d4ad524 farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents: 36555
diff changeset
   424
    member (op =) rec_names s
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   425
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   426
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   427
(* ------------------------------------------------------------------------- *)
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
   428
(* 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
   429
(* ------------------------------------------------------------------------- *)
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
   430
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   431
fun norm_rhs eqn =
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
   432
  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
   433
    fun lambda (v as Var ((x, _), T)) t = Abs (x, T, abstract_over (v, t))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   434
      | lambda v t = raise TERM ("lambda", [v, t])
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
   435
    val (lhs, rhs) = Logic.dest_equals eqn
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   436
    val (_, args) = Term.strip_comb lhs
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
   437
  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
   438
    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
   439
  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
   440
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
   441
(* ------------------------------------------------------------------------- *)
37405
7c49988afd0e avoid references to old constdefs
haftmann
parents: 37391
diff changeset
   442
(* get_def: looks up the definition of a constant                            *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   443
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   444
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   445
fun get_def thy (s, T) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   446
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   447
    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
   448
      | get_def_ax ((axname, ax) :: axioms) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   449
          (let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   450
            val (lhs, _) = Logic.dest_equals ax  (* equations only *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   451
            val c        = Term.head_of lhs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   452
            val (s', T') = Term.dest_Const c
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   453
          in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   454
            if s=s' then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   455
              let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   456
                val typeSubs = Sign.typ_match thy (T', T) Vartab.empty
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   457
                val ax'      = monomorphic_term typeSubs ax
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   458
                val rhs      = norm_rhs ax'
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   459
              in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   460
                SOME (axname, rhs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   461
              end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   462
            else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   463
              get_def_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   464
          end handle ERROR _         => get_def_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   465
                   | TERM _          => get_def_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   466
                   | Type.TYPE_MATCH => get_def_ax axioms)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   467
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   468
    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
   469
  end;
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
(* 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
   473
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   474
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   475
fun get_typedef thy T =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   476
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   477
    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
   478
      | get_typedef_ax ((axname, ax) :: axioms) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   479
          (let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   480
            fun type_of_type_definition (Const (s', T')) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   481
                  if s'= @{const_name type_definition} then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   482
                    SOME T'
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   483
                  else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   484
                    NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   485
              | type_of_type_definition (Free _) = NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   486
              | type_of_type_definition (Var _) = NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   487
              | type_of_type_definition (Bound _) = NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   488
              | type_of_type_definition (Abs (_, _, body)) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   489
                  type_of_type_definition body
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   490
              | type_of_type_definition (t1 $ t2) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   491
                  (case type_of_type_definition t1 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   492
                    SOME x => SOME x
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   493
                  | NONE => type_of_type_definition t2)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   494
          in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   495
            case type_of_type_definition ax of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   496
              SOME T' =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   497
                let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   498
                  val T'' = domain_type (domain_type T')
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   499
                  val typeSubs = Sign.typ_match thy (T'', T) Vartab.empty
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   500
                in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   501
                  SOME (axname, monomorphic_term typeSubs ax)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   502
                end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   503
            | NONE => get_typedef_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   504
          end handle ERROR _         => get_typedef_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   505
                   | TERM _          => get_typedef_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   506
                   | Type.TYPE_MATCH => get_typedef_ax axioms)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   507
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   508
    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
   509
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   510
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   511
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   512
(* 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
   513
(*               created by the "axclass" command                            *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   514
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   515
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   516
fun 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
   517
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   518
    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
   519
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   520
    Option.map (pair axname)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   521
      (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
   522
  end;
21985
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
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   525
(* 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
   526
(*              normalizes the result term; certain constants are not        *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   527
(*              unfolded (cf. 'collect_axioms' and the various interpreters  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   528
(*              below): if the interpretation respects a definition anyway,  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   529
(*              that definition does not need to be unfolded                 *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   530
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   531
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   532
(* Note: we could intertwine unfolding of constants and beta-(eta-)       *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   533
(*       normalization; this would save some unfolding for terms where    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   534
(*       constants are eliminated by beta-reduction (e.g. 'K c1 c2').  On *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   535
(*       the other hand, this would cause additional work for terms where *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   536
(*       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
   537
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   538
fun unfold_defs thy t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   539
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   540
    fun unfold_loop t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   541
      case t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   542
      (* Pure *)
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56243
diff changeset
   543
        Const (@{const_name Pure.all}, _) => t
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56243
diff changeset
   544
      | Const (@{const_name Pure.eq}, _) => t
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56243
diff changeset
   545
      | Const (@{const_name Pure.imp}, _) => t
56243
2e10a36b8d46 more qualified names;
wenzelm
parents: 56147
diff changeset
   546
      | Const (@{const_name Pure.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
   547
      (* HOL *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   548
      | Const (@{const_name Trueprop}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   549
      | 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
   550
      | (* 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
   551
        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
   552
      | (* 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
   553
        Const (@{const_name False}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   554
      | Const (@{const_name undefined}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   555
      | 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
   556
      | 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
   557
      | Const (@{const_name All}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   558
      | Const (@{const_name Ex}, _) => t
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38795
diff changeset
   559
      | Const (@{const_name HOL.eq}, _) => t
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   560
      | Const (@{const_name HOL.conj}, _) => t
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   561
      | Const (@{const_name HOL.disj}, _) => t
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38553
diff changeset
   562
      | Const (@{const_name HOL.implies}, _) => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   563
      (* sets *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   564
      | Const (@{const_name Collect}, _) => t
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37603
diff changeset
   565
      | Const (@{const_name Set.member}, _) => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   566
      (* other optimizations *)
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   567
      | 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
   568
      | Const (@{const_name Finite_Set.finite}, _) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   569
      | Const (@{const_name Orderings.less}, Type ("fun", [@{typ nat},
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   570
          Type ("fun", [@{typ nat}, @{typ bool}])])) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   571
      | Const (@{const_name Groups.plus}, Type ("fun", [@{typ nat},
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   572
          Type ("fun", [@{typ nat}, @{typ nat}])])) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   573
      | Const (@{const_name Groups.minus}, Type ("fun", [@{typ nat},
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   574
          Type ("fun", [@{typ nat}, @{typ nat}])])) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   575
      | Const (@{const_name Groups.times}, Type ("fun", [@{typ nat},
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   576
          Type ("fun", [@{typ nat}, @{typ nat}])])) => t
56252
b72e0a9d62b9 more antiquotations;
wenzelm
parents: 56245
diff changeset
   577
      | Const (@{const_name append}, _) => t
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
   578
(* UNSOUND
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   579
      | Const (@{const_name lfp}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   580
      | Const (@{const_name gfp}, _) => t
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
   581
*)
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   582
      | 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
   583
      | 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
   584
      (* simply-typed lambda calculus *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   585
      | Const (s, T) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   586
          (if is_IDT_constructor thy (s, T)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   587
            orelse is_IDT_recursor thy (s, T) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   588
            t  (* do not unfold IDT constructors/recursors *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   589
          (* unfold the constant if there is a defining equation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   590
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   591
            case get_def thy (s, T) of
46096
a00685a18e55 tuned Refute
blanchet
parents: 45896
diff changeset
   592
              SOME ((*axname*) _, rhs) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   593
              (* Note: if the term to be unfolded (i.e. 'Const (s, T)')  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   594
              (* occurs on the right-hand side of the equation, i.e. in  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   595
              (* 'rhs', we must not use this equation to unfold, because *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   596
              (* that would loop.  Here would be the right place to      *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   597
              (* check this.  However, getting this really right seems   *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   598
              (* difficult because the user may state arbitrary axioms,  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   599
              (* which could interact with overloading to create loops.  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   600
              ((*tracing (" unfolding: " ^ axname);*)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   601
               unfold_loop rhs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   602
            | NONE => t)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   603
      | Free _ => t
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   604
      | Var _ => t
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   605
      | Bound _ => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   606
      | Abs (s, T, body) => Abs (s, T, unfold_loop body)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   607
      | t1 $ t2 => (unfold_loop t1) $ (unfold_loop t2)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   608
    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
   609
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   610
    result
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   611
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   612
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   613
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   614
(* collect_axioms: collects (monomorphic, universally quantified, unfolded   *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   615
(*                 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
   616
(* ------------------------------------------------------------------------- *)
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
   617
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   618
(* Note: to make the collection of axioms more easily extensible, this    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   619
(*       function could be based on user-supplied "axiom collectors",     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   620
(*       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
   621
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   622
(* Note: currently we use "inverse" functions to the definitional         *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   623
(*       mechanisms provided by Isabelle/HOL, e.g. for "axclass",         *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   624
(*       "typedef", "definition".  A more general approach could consider *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   625
(*       *every* axiom of the theory and collect it if it has a constant/ *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   626
(*       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
   627
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   628
(* Which axioms are "relevant" for a particular term/type goes hand in    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   629
(* hand with the interpretation of that term/type by its interpreter (see *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   630
(* way below): if the interpretation respects an axiom anyway, the axiom  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   631
(* 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
   632
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   633
(* To avoid collecting the same axiom multiple times, we use an           *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   634
(* 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
   635
56256
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   636
local
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   637
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   638
fun get_axiom thy xname =
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   639
  Name_Space.check (Context.Theory thy) (Theory.axiom_table thy) (xname, Position.none);
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   640
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   641
val the_eq_trivial = get_axiom @{theory HOL} "the_eq_trivial";
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   642
val someI = get_axiom @{theory Hilbert_Choice} "someI";
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   643
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   644
in
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   645
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   646
fun collect_axioms ctxt t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   647
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42137
diff changeset
   648
    val thy = Proof_Context.theory_of ctxt
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
   649
    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
   650
    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
   651
    fun collect_this_axiom (axname, ax) axs =
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   652
      let
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   653
        val ax' = unfold_defs thy ax
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   654
      in
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   655
        if member (op aconv) axs ax' then axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   656
        else (tracing axname; collect_term_axioms ax' (ax' :: axs))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   657
      end
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   658
    and collect_sort_axioms T axs =
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   659
      let
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   660
        val sort =
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   661
          (case T of
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   662
            TFree (_, sort) => sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   663
          | TVar (_, sort)  => sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   664
          | _ => raise REFUTE ("collect_axioms",
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   665
              "type " ^ Syntax.string_of_typ ctxt T ^ " is not a variable"))
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   666
        (* obtain axioms for all superclasses *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   667
        val superclasses = sort @ maps (Sign.super_classes thy) sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   668
        (* merely an optimization, because 'collect_this_axiom' disallows *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   669
        (* duplicate axioms anyway:                                       *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   670
        val superclasses = distinct (op =) superclasses
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   671
        val class_axioms = maps (fn class => map (fn ax =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   672
          ("<" ^ class ^ ">", Thm.prop_of ax))
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 51557
diff changeset
   673
          (#axioms (Axclass.get_info thy class) handle ERROR _ => []))
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   674
          superclasses
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   675
        (* replace the (at most one) schematic type variable in each axiom *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   676
        (* by the actual type 'T'                                          *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   677
        val monomorphic_class_axioms = map (fn (axname, ax) =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   678
          (case Term.add_tvars ax [] of
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   679
            [] => (axname, ax)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   680
          | [(idx, S)] => (axname, monomorphic_term (Vartab.make [(idx, (S, T))]) ax)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   681
          | _ =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   682
            raise REFUTE ("collect_axioms", "class axiom " ^ axname ^ " (" ^
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   683
              Syntax.string_of_term ctxt ax ^
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   684
              ") contains more than one type variable")))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   685
          class_axioms
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   686
      in
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   687
        fold collect_this_axiom monomorphic_class_axioms axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   688
      end
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   689
    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
   690
      case T of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   691
      (* simple types *)
56252
b72e0a9d62b9 more antiquotations;
wenzelm
parents: 56245
diff changeset
   692
        Type (@{type_name prop}, []) => axs
b72e0a9d62b9 more antiquotations;
wenzelm
parents: 56245
diff changeset
   693
      | Type (@{type_name fun}, [T1, T2]) => collect_type_axioms T2 (collect_type_axioms T1 axs)
46098
ce939621a1fe handle "set" correctly in Refute -- inspired by old code from Isabelle2007
blanchet
parents: 46096
diff changeset
   694
      | Type (@{type_name set}, [T1]) => 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
   695
      (* axiomatic type classes *)
56252
b72e0a9d62b9 more antiquotations;
wenzelm
parents: 56245
diff changeset
   696
      | Type (@{type_name itself}, [T1]) => collect_type_axioms T1 axs
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   697
      | Type (s, Ts) =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   698
        (case Datatype.get_info thy s of
46096
a00685a18e55 tuned Refute
blanchet
parents: 45896
diff changeset
   699
          SOME _ =>  (* inductive datatype *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   700
            (* only collect relevant type axioms for the argument types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   701
            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
   702
        | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   703
          (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
   704
            SOME (axname, ax) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   705
              collect_this_axiom (axname, ax) axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   706
          | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   707
            (* 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
   708
            (* at least collect relevant type axioms for the argument types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   709
            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
   710
      (* axiomatic type classes *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   711
      | TFree _ => collect_sort_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   712
      (* axiomatic type classes *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   713
      | TVar _ => collect_sort_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   714
    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
   715
      case t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   716
      (* Pure *)
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56243
diff changeset
   717
        Const (@{const_name Pure.all}, _) => axs
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56243
diff changeset
   718
      | Const (@{const_name Pure.eq}, _) => axs
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56243
diff changeset
   719
      | Const (@{const_name Pure.imp}, _) => axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   720
      (* axiomatic type classes *)
56243
2e10a36b8d46 more qualified names;
wenzelm
parents: 56147
diff changeset
   721
      | Const (@{const_name Pure.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
   722
      (* HOL *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   723
      | Const (@{const_name Trueprop}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   724
      | 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
   725
      (* 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
   726
      | 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
   727
      (* 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
   728
      | Const (@{const_name False}, _) => axs
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   729
      | 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
   730
      | Const (@{const_name The}, T) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   731
          let
56256
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   732
            val ax = specialize_type thy (@{const_name The}, T) (#2 the_eq_trivial)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   733
          in
56256
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   734
            collect_this_axiom (#1 the_eq_trivial, ax) axs
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   735
          end
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   736
      | Const (@{const_name Hilbert_Choice.Eps}, T) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   737
          let
56256
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   738
            val ax = specialize_type thy (@{const_name Hilbert_Choice.Eps}, T) (#2 someI)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   739
          in
56256
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   740
            collect_this_axiom (#1 someI, ax) axs
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   741
          end
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   742
      | Const (@{const_name All}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   743
      | Const (@{const_name Ex}, T) => collect_type_axioms T axs
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38795
diff changeset
   744
      | Const (@{const_name HOL.eq}, T) => collect_type_axioms T axs
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   745
      | Const (@{const_name HOL.conj}, _) => axs
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   746
      | Const (@{const_name HOL.disj}, _) => axs
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38553
diff changeset
   747
      | Const (@{const_name HOL.implies}, _) => axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   748
      (* sets *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   749
      | Const (@{const_name Collect}, T) => collect_type_axioms T axs
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37603
diff changeset
   750
      | Const (@{const_name Set.member}, 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
   751
      (* other optimizations *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   752
      | 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
   753
      | Const (@{const_name Finite_Set.finite}, T) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   754
        collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   755
      | Const (@{const_name Orderings.less}, T as Type ("fun", [@{typ nat},
38553
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 37677
diff changeset
   756
        Type ("fun", [@{typ nat}, @{typ bool}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   757
          collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   758
      | Const (@{const_name Groups.plus}, T as Type ("fun", [@{typ nat},
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   759
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   760
          collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   761
      | Const (@{const_name Groups.minus}, T as Type ("fun", [@{typ nat},
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   762
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   763
          collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   764
      | Const (@{const_name Groups.times}, T as Type ("fun", [@{typ nat},
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   765
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   766
          collect_type_axioms T axs
56252
b72e0a9d62b9 more antiquotations;
wenzelm
parents: 56245
diff changeset
   767
      | Const (@{const_name append}, T) => collect_type_axioms T axs
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
   768
(* UNSOUND
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   769
      | Const (@{const_name lfp}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   770
      | Const (@{const_name gfp}, T) => collect_type_axioms T axs
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
   771
*)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   772
      | Const (@{const_name fst}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   773
      | 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
   774
      (* simply-typed lambda calculus *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   775
      | Const (s, T) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   776
          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
   777
            (* 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
   778
            (* and the class definition                               *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   779
            let
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   780
              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
   781
              val of_class = Logic.mk_of_class (TVar (("'a", 0), [class]), class)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   782
              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
   783
                (* 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
   784
                handle Type.TYPE_MATCH => NONE
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   785
              val ax_1 = Option.map (fn ax => (Syntax.string_of_term ctxt ax, ax)) ax_in
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   786
              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
   787
            in
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   788
              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
   789
            end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   790
          else if is_IDT_constructor thy (s, T)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   791
            orelse is_IDT_recursor thy (s, T)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   792
          then
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   793
            (* only collect relevant type axioms *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   794
            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
   795
          else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   796
            (* 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
   797
            (* 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
   798
            (* 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
   799
            (* only collect relevant type axioms *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   800
            collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   801
      | Free (_, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   802
      | Var (_, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   803
      | Bound _ => axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   804
      | Abs (_, T, body) => collect_term_axioms body (collect_type_axioms T axs)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   805
      | t1 $ t2 => collect_term_axioms t2 (collect_term_axioms t1 axs)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   806
    val result = map close_form (collect_term_axioms t [])
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
   807
    val _ = tracing " ...done."
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   808
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   809
    result
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   810
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   811
56256
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   812
end;
1e01c159e7d9 more antiquotations;
wenzelm
parents: 56252
diff changeset
   813
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   814
(* ------------------------------------------------------------------------- *)
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
   815
(* 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
   816
(*               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
   817
(*               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
   818
(*               'propT'.  For IDTs, also the argument types of constructors *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   819
(*               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
   820
(* ------------------------------------------------------------------------- *)
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
   821
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   822
fun ground_types ctxt t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   823
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42137
diff changeset
   824
    val thy = Proof_Context.theory_of ctxt
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   825
    fun collect_types T acc =
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   826
      (case T of
56252
b72e0a9d62b9 more antiquotations;
wenzelm
parents: 56245
diff changeset
   827
        Type (@{type_name fun}, [T1, T2]) => collect_types T1 (collect_types T2 acc)
b72e0a9d62b9 more antiquotations;
wenzelm
parents: 56245
diff changeset
   828
      | Type (@{type_name prop}, []) => acc
46098
ce939621a1fe handle "set" correctly in Refute -- inspired by old code from Isabelle2007
blanchet
parents: 46096
diff changeset
   829
      | Type (@{type_name set}, [T1]) => collect_types T1 acc
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   830
      | Type (s, Ts) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   831
          (case Datatype.get_info thy s of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   832
            SOME info =>  (* inductive datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   833
              let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   834
                val index = #index info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   835
                val descr = #descr info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   836
                val (_, typs, _) = the (AList.lookup (op =) descr index)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   837
                val typ_assoc = typs ~~ Ts
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   838
                (* sanity check: every element in 'dtyps' must be a *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   839
                (* 'DtTFree'                                        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   840
                val _ = if Library.exists (fn d =>
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45741
diff changeset
   841
                  case d of Datatype.DtTFree _ => false | _ => true) typs then
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   842
                  raise REFUTE ("ground_types", "datatype argument (for type "
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   843
                    ^ Syntax.string_of_typ ctxt T ^ ") is not a variable")
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   844
                else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   845
                (* required for mutually recursive datatypes; those need to   *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   846
                (* be added even if they are an instance of an otherwise non- *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   847
                (* recursive datatype                                         *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   848
                fun collect_dtyp d acc =
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   849
                  let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   850
                    val dT = typ_of_dtyp descr typ_assoc d
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   851
                  in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   852
                    case d of
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45741
diff changeset
   853
                      Datatype.DtTFree _ =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   854
                      collect_types dT acc
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45741
diff changeset
   855
                    | Datatype.DtType (_, ds) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   856
                      collect_types dT (fold_rev collect_dtyp ds acc)
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45741
diff changeset
   857
                    | Datatype.DtRec i =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   858
                      if member (op =) acc dT then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   859
                        acc  (* prevent infinite recursion *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   860
                      else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   861
                        let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   862
                          val (_, dtyps, dconstrs) = the (AList.lookup (op =) descr i)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   863
                          (* if the current type is a recursive IDT (i.e. a depth *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   864
                          (* is required), add it to 'acc'                        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   865
                          val acc_dT = if Library.exists (fn (_, ds) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   866
                            Library.exists Datatype_Aux.is_rec_type ds) dconstrs then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   867
                              insert (op =) dT acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   868
                            else acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   869
                          (* collect argument types *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   870
                          val acc_dtyps = fold_rev collect_dtyp dtyps acc_dT
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   871
                          (* collect constructor types *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   872
                          val acc_dconstrs = fold_rev collect_dtyp (maps snd dconstrs) acc_dtyps
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   873
                        in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   874
                          acc_dconstrs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   875
                        end
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   876
                  end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   877
              in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   878
                (* argument types 'Ts' could be added here, but they are also *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   879
                (* added by 'collect_dtyp' automatically                      *)
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45741
diff changeset
   880
                collect_dtyp (Datatype.DtRec index) acc
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   881
              end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   882
          | NONE =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   883
            (* not an inductive datatype, e.g. defined via "typedef" or *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   884
            (* "typedecl"                                               *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   885
            insert (op =) T (fold collect_types Ts acc))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   886
      | TFree _ => insert (op =) T acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   887
      | 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
   888
  in
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   889
    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
   890
  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
   891
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
   892
(* ------------------------------------------------------------------------- *)
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
   893
(* 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
   894
(*                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
   895
(*                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
   896
(*                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
   897
(* ------------------------------------------------------------------------- *)
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
   898
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   899
fun string_of_typ (Type (s, _))     = s
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   900
  | string_of_typ (TFree (s, _))    = s
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   901
  | 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
   902
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
   903
(* ------------------------------------------------------------------------- *)
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
   904
(* 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
   905
(*                 '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
   906
(*                 '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
   907
(* ------------------------------------------------------------------------- *)
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
   908
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   909
fun first_universe xs sizes minsize =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   910
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   911
    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
   912
      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
   913
        SOME n => n
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   914
      | NONE => minsize
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   915
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   916
    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
   917
  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
   918
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
   919
(* ------------------------------------------------------------------------- *)
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
   920
(* 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
   921
(*                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
   922
(*                '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
   923
(*                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
   924
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   925
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   926
fun next_universe xs sizes minsize maxsize =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   927
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   928
    (* 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
   929
    (* 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
   930
    fun make_first _ 0 sum =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   931
          if sum = 0 then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   932
            SOME []
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   933
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   934
            NONE
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   935
      | make_first max len sum =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   936
          if sum <= max orelse max < 0 then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   937
            Option.map (fn xs' => sum :: xs') (make_first max (len-1) 0)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   938
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   939
            Option.map (fn xs' => max :: xs') (make_first max (len-1) (sum-max))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   940
    (* 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
   941
    (* all list elements x (unless 'max'<0)                                *)
46096
a00685a18e55 tuned Refute
blanchet
parents: 45896
diff changeset
   942
    fun next _ _ _ [] =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   943
          NONE
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   944
      | next max len sum [x] =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   945
          (* we've reached the last list element, so there's no shift possible *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   946
          make_first max (len+1) (sum+x+1)  (* increment 'sum' by 1 *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   947
      | next max len sum (x1::x2::xs) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   948
          if x1>0 andalso (x2<max orelse max<0) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   949
            (* we can shift *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   950
            SOME (the (make_first max (len+1) (sum+x1-1)) @ (x2+1) :: xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   951
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   952
            (* continue search *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   953
            next max (len+1) (sum+x1) (x2::xs)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   954
    (* only consider those types for which the size is not fixed *)
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33246
diff changeset
   955
    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
   956
    (* 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
   957
    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
   958
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   959
    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
   960
      SOME diffs' =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   961
        (* merge with those types for which the size is fixed *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   962
        SOME (fst (fold_map (fn (T, _) => fn ds =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   963
          case AList.lookup (op =) sizes (string_of_typ T) of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   964
          (* return the fixed size *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   965
            SOME n => ((T, n), ds)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   966
          (* consume the head of 'ds', add 'minsize' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   967
          | NONE   => ((T, minsize + hd ds), tl ds))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   968
          xs diffs'))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   969
    | NONE => NONE
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   970
  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
   971
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
   972
(* ------------------------------------------------------------------------- *)
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
   973
(* 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
   974
(*         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
   975
(* ------------------------------------------------------------------------- *)
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
   976
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   977
fun toTrue (Leaf [fm, _]) = fm
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   978
  | toTrue _ = 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
   979
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
   980
(* ------------------------------------------------------------------------- *)
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
   981
(* 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
   982
(*          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
   983
(*          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
   984
(* ------------------------------------------------------------------------- *)
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
   985
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   986
fun toFalse (Leaf [_, fm]) = fm
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   987
  | toFalse _ = 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
   988
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
   989
(* ------------------------------------------------------------------------- *)
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
   990
(* 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
   991
(*             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
   992
(*             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
   993
(* {...}     : 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
   994
(* 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
   995
(* 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
   996
(* 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
   997
(* ------------------------------------------------------------------------- *)
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
   998
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   999
fun find_model ctxt
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1000
    {sizes, minsize, maxsize, maxvars, maxtime, satsolver, no_assms, expect}
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1001
    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
  1002
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42137
diff changeset
  1003
    val thy = Proof_Context.theory_of ctxt
33054
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1004
    fun check_expect outcome_code =
45387
ccffb3f9f42b return outcome code, so that it can be picked up by Mutabelle
blanchet
parents: 44121
diff changeset
  1005
      if expect = "" orelse outcome_code = expect then outcome_code
33054
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1006
      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
  1007
    fun wrapper () =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1008
      let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1009
        val timer = Timer.startRealTimer ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1010
        val t =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1011
          if no_assms then t
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1012
          else if negate then Logic.list_implies (assm_ts, t)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1013
          else Logic.mk_conjunction_list (t :: assm_ts)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1014
        val u = unfold_defs thy t
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1015
        val _ = tracing ("Unfolded term: " ^ Syntax.string_of_term ctxt u)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1016
        val axioms = collect_axioms ctxt u
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1017
        val types = fold (union (op =) o ground_types ctxt) (u :: axioms) []
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1018
        val _ = tracing ("Ground types: "
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1019
          ^ (if null types then "none."
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1020
             else commas (map (Syntax.string_of_typ ctxt) types)))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1021
        (* we can only consider fragments of recursive IDTs, so we issue a  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1022
        (* warning if the formula contains a recursive IDT                  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1023
        (* TODO: no warning needed for /positive/ occurrences of IDTs       *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1024
        val maybe_spurious = Library.exists (fn
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1025
            Type (s, _) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1026
              (case Datatype.get_info thy s of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1027
                SOME info =>  (* inductive datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1028
                  let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1029
                    val index           = #index info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1030
                    val descr           = #descr info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1031
                    val (_, _, constrs) = the (AList.lookup (op =) descr index)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1032
                  in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1033
                    (* recursive datatype? *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1034
                    Library.exists (fn (_, ds) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1035
                      Library.exists Datatype_Aux.is_rec_type ds) constrs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1036
                  end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1037
              | NONE => false)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1038
          | _ => false) types
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1039
        val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1040
          if maybe_spurious then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1041
            warning ("Term contains a recursive datatype; "
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1042
              ^ "countermodel(s) may be spurious!")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1043
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1044
            ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1045
        fun find_model_loop universe =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1046
          let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1047
            val msecs_spent = Time.toMilliseconds (Timer.checkRealTimer timer)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1048
            val _ = maxtime = 0 orelse msecs_spent < 1000 * maxtime
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1049
                    orelse raise TimeLimit.TimeOut
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1050
            val init_model = (universe, [])
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1051
            val init_args  = {maxvars = maxvars, def_eq = false, next_idx = 1,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1052
              bounds = [], wellformed = True}
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1053
            val _ = tracing ("Translating term (sizes: "
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1054
              ^ commas (map (fn (_, n) => string_of_int n) universe) ^ ") ...")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1055
            (* translate 'u' and all axioms *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1056
            val (intrs, (model, args)) = fold_map (fn t' => fn (m, a) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1057
              let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1058
                val (i, m', a') = interpret ctxt m a t'
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1059
              in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1060
                (* set 'def_eq' to 'true' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1061
                (i, (m', {maxvars = #maxvars a', def_eq = true,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1062
                  next_idx = #next_idx a', bounds = #bounds a',
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1063
                  wellformed = #wellformed a'}))