src/HOL/Tools/refute.ML
author wenzelm
Mon, 25 Oct 2010 21:06:56 +0200
changeset 40132 7ee65dbffa31
parent 39811 0659e84bdc5f
child 40627 becf5d5187cc
permissions -rw-r--r--
renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     1
(*  Title:      HOL/Tools/refute.ML
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 29004
diff changeset
     2
    Author:     Tjark Weber, TU Muenchen
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     3
14965
7155b319eafa new SAT solver interface
webertj
parents: 14818
diff changeset
     4
Finite model generation for HOL formulas, using a SAT solver.
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     5
*)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
     6
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
     7
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
     8
(* Declares the 'REFUTE' signature as well as a structure 'Refute'.          *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
     9
(* Documentation is available in the Isabelle/Isar theory 'HOL/Refute.thy'.  *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    10
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    11
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    12
signature REFUTE =
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    13
sig
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    14
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    15
  exception REFUTE of string * string
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    16
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    17
(* ------------------------------------------------------------------------- *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
    18
(* Model/interpretation related code (translation HOL -> propositional logic *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    19
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    20
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    21
  type params
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    22
  type interpretation
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    23
  type model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    24
  type arguments
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    25
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    26
  exception MAXVARS_EXCEEDED
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    27
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    28
  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
    29
    (interpretation * model * arguments) option) -> theory -> theory
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    30
  val add_printer : string -> (Proof.context -> model -> typ ->
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    31
    interpretation -> (int -> bool) -> term option) -> theory -> theory
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    32
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    33
  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
    34
    (interpretation * model * arguments)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
    35
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    36
  val print : Proof.context -> model -> typ -> interpretation -> (int -> bool) -> term
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    37
  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
    38
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    39
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    40
(* Interface                                                                 *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    41
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    42
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    43
  val set_default_param  : (string * string) -> theory -> theory
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    44
  val get_default_param  : Proof.context -> string -> string option
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    45
  val get_default_params : Proof.context -> (string * string) list
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    46
  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
    47
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    48
  val find_model : Proof.context -> params -> term list -> term -> bool -> unit
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    49
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    50
  (* tries to find a model for a formula: *)
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    51
  val satisfy_term :
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    52
    Proof.context -> (string * string) list -> term list -> term -> unit
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    53
  (* tries to find a model that refutes a formula: *)
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    54
  val refute_term :
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
    55
    Proof.context -> (string * string) list -> term list -> term -> unit
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    56
  val refute_goal :
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
    57
    Proof.context -> (string * string) list -> thm -> int -> unit
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    58
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
    59
  val setup : theory -> theory
22092
ab3dfcef6489 reformatted to 80 chars/line
webertj
parents: 22055
diff changeset
    60
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    61
(* ------------------------------------------------------------------------- *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    62
(* Additional functions used by Nitpick (to be factored out)                 *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    63
(* ------------------------------------------------------------------------- *)
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
    64
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    65
  val close_form : term -> term
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    66
  val get_classdef : theory -> string -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    67
  val norm_rhs : term -> term
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    68
  val get_def : theory -> string * typ -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    69
  val get_typedef : theory -> typ -> (string * term) option
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    70
  val is_IDT_constructor : theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    71
  val is_IDT_recursor : theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    72
  val is_const_of_class: theory -> string * typ -> bool
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    73
  val string_of_typ : typ -> string
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33063
diff changeset
    74
  val typ_of_dtyp : Datatype.descr -> (Datatype.dtyp * typ) list -> Datatype.dtyp -> typ
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    75
end;
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    76
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    77
structure Refute : REFUTE =
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    78
struct
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    79
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    80
open PropLogic;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
    81
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    82
(* 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
    83
(* 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
    84
(* 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
    85
(* 'error'.                                                          *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    86
exception REFUTE of string * string;  (* ("in function", "cause") *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    87
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    88
(* 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
    89
(* required than allowed by 'maxvars'                              *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    90
exception MAXVARS_EXCEEDED;
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
    91
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    92
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    93
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    94
(* TREES                                                                     *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    95
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    96
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    97
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    98
(* tree: implements an arbitrarily (but finitely) branching tree as a list   *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
    99
(*       of (lists of ...) elements                                          *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   100
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   101
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   102
datatype 'a tree =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   103
    Leaf of 'a
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   104
  | Node of ('a tree) list;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   105
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   106
(* ('a -> 'b) -> 'a tree -> 'b tree *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   107
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   108
fun tree_map f tr =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   109
  case tr of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   110
    Leaf x  => Leaf (f x)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   111
  | Node xs => Node (map (tree_map f) xs);
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   112
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   113
(* ('a * 'b -> 'a) -> 'a * ('b tree) -> 'a *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   114
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   115
fun tree_foldl f =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   116
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   117
    fun itl (e, Leaf x)  = f(e,x)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   118
      | itl (e, Node xs) = Library.foldl (tree_foldl f) (e,xs)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   119
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   120
    itl
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   121
  end;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   122
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   123
(* 'a tree * 'b tree -> ('a * 'b) tree *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   124
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   125
fun tree_pair (t1, t2) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   126
  case t1 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   127
    Leaf x =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   128
      (case t2 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   129
          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
   130
        | 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
   131
            "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
   132
  | Node xs =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   133
      (case t2 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   134
          (* '~~' 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
   135
          (* 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
   136
          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
   137
        | 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
   138
            "trees are of different height (first tree is higher)"));
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   139
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   140
(* ------------------------------------------------------------------------- *)
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
   141
(* 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
   142
(*         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
   143
(*                                                                           *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   144
(* 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
   145
(* "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
   146
(*                                                                           *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   147
(* 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
   148
(*                                                                           *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   149
(* "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
   150
(*                       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
   151
(* "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
   152
(* "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
   153
(* "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
   154
(*                       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
   155
(*                       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
   156
(* "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
   157
(* "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
   158
(* "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
   159
(*                       not considered.                                     *)
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
   160
(* "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
   161
(*                       "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
   162
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   163
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   164
type params =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   165
  {
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   166
    sizes    : (string * int) list,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   167
    minsize  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   168
    maxsize  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   169
    maxvars  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   170
    maxtime  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   171
    satsolver: string,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   172
    no_assms : bool,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   173
    expect   : string
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
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
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   176
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   177
(* 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
   178
(*                 'interpretation'                                          *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   179
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   180
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   181
type interpretation =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   182
  prop_formula list tree;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   183
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   184
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   185
(* 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
   186
(*        terms                                                              *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   187
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   188
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   189
type model =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   190
  (typ * int) list * (term * interpretation) list;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   191
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   192
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   193
(* arguments: additional arguments required during interpretation of terms   *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   194
(* ------------------------------------------------------------------------- *)
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
   195
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   196
type arguments =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   197
  {
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   198
    (* just passed unchanged from 'params': *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   199
    maxvars   : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   200
    (* 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
   201
    def_eq    : bool,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   202
    (* 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
   203
    next_idx  : int,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   204
    bounds    : interpretation list,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   205
    wellformed: prop_formula
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   206
  };
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   207
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   208
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   209
structure Data = Theory_Data
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   210
(
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   211
  type T =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   212
    {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
   213
      (interpretation * model * arguments) option)) list,
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   214
     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
   215
      (int -> bool) -> term option)) list,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   216
     parameters: string Symtab.table};
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   217
  val empty = {interpreters = [], printers = [], parameters = Symtab.empty};
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   218
  val extend = I;
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   219
  fun merge
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   220
    ({interpreters = in1, printers = pr1, parameters = pa1},
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   221
     {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
   222
    {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
   223
     printers = AList.merge (op =) (K true) (pr1, pr2),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   224
     parameters = Symtab.merge (op=) (pa1, pa2)};
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   225
);
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   226
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   227
val get_data = Data.get o ProofContext.theory_of;
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   228
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   229
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   230
(* ------------------------------------------------------------------------- *)
15334
d5a92997dc1b exception CANNOT_INTERPRET removed (not needed anymore since the stlc_interpreter can interpret any term)
webertj
parents: 15333
diff changeset
   231
(* 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
   232
(*            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
   233
(*            track of the interpretation of subterms                        *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   234
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   235
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   236
fun interpret ctxt model args t =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   237
  case get_first (fn (_, f) => f ctxt model args t)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   238
      (#interpreters (get_data ctxt)) of
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   239
    NONE => raise REFUTE ("interpret",
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   240
      "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
   241
  | SOME x => x;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   242
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   243
(* ------------------------------------------------------------------------- *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   244
(* 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
   245
(*        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
   246
(* ------------------------------------------------------------------------- *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   247
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   248
fun print ctxt model T intr assignment =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   249
  case get_first (fn (_, f) => f ctxt model T intr assignment)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   250
      (#printers (get_data ctxt)) of
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   251
    NONE => raise REFUTE ("print",
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   252
      "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
   253
  | SOME x => x;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   254
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   255
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   256
(* 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
   257
(*              (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
   258
(*              printers                                                     *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   259
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   260
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   261
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
   262
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   263
    val (typs, terms) = model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   264
    val typs_msg =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   265
      if null typs then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   266
        "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
   267
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   268
        "Size of types: " ^ commas (map (fn (T, i) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   269
          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
   270
    val show_consts_msg =
39050
600de0485859 turned show_consts into proper configuration option;
wenzelm
parents: 39049
diff changeset
   271
      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
   272
        "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
   273
      else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   274
        ""
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   275
    val terms_msg =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   276
      if null terms then
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   277
        "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
   278
      else
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
   279
        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
   280
          (* print constants only if 'show_consts' is true *)
39050
600de0485859 turned show_consts into proper configuration option;
wenzelm
parents: 39049
diff changeset
   281
          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
   282
            SOME (Syntax.string_of_term ctxt t ^ ": " ^
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   283
              Syntax.string_of_term ctxt
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   284
                (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
   285
          else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   286
            NONE) terms) ^ "\n"
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   287
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   288
    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
   289
  end;
14456
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
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   292
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   293
(* PARAMETER MANAGEMENT                                                      *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   294
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   295
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   296
fun add_interpreter name f = Data.map (fn {interpreters, printers, parameters} =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   297
  case AList.lookup (op =) interpreters name of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   298
    NONE => {interpreters = (name, f) :: interpreters,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   299
      printers = printers, parameters = parameters}
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   300
  | SOME _ => error ("Interpreter " ^ name ^ " already declared"));
14456
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
fun add_printer name f = Data.map (fn {interpreters, printers, parameters} =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   303
  case AList.lookup (op =) printers name of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   304
    NONE => {interpreters = interpreters,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   305
      printers = (name, f) :: printers, parameters = parameters}
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   306
  | SOME _ => error ("Printer " ^ name ^ " already declared"));
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
(* 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
   310
(*                    parameter table                                        *)
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
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   313
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
   314
  (fn {interpreters, printers, parameters} =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   315
    {interpreters = interpreters, printers = printers,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   316
      parameters = Symtab.update (name, value) parameters});
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   317
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
   318
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   319
(* get_default_param: retrieves the value associated with 'name' from        *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   320
(*                    Data's parameter table                                 *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   321
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   322
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   323
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
   324
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   325
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   326
(* 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
   327
(*                     stored in Data's parameter table                      *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   328
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   329
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   330
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
   331
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   332
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   333
(* 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
   334
(*      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
   335
(*      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
   336
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   337
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   338
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
   339
  let
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   340
    (* (string * string) list * string -> bool *)
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   341
    fun read_bool (parms, name) =
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   342
      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
   343
        SOME "true" => true
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   344
      | SOME "false" => false
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   345
      | 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
   346
          " (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
   347
      | NONE   => error ("parameter " ^ quote name ^
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
   348
          " 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
   349
    (* (string * string) list * string -> int *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   350
    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
   351
      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
   352
        SOME s =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   353
          (case Int.fromString s of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   354
            SOME i => i
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   355
          | NONE   => error ("parameter " ^ quote name ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   356
            " (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
   357
      | 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
   358
          " 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
   359
    (* (string * string) list * string -> string *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   360
    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
   361
      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
   362
        SOME s => s
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   363
      | 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
   364
        " 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
   365
    (* 'override' first, defaults last: *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   366
    (* (string * string) list *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   367
    val allparams = override @ get_default_params ctxt
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   368
    (* int *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   369
    val minsize = read_int (allparams, "minsize")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   370
    val maxsize = read_int (allparams, "maxsize")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   371
    val maxvars = read_int (allparams, "maxvars")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   372
    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
   373
    (* string *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   374
    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
   375
    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
   376
    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
   377
    (* 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
   378
    (* 'sizes'                                                            *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   379
    (* 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
   380
    (*       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
   381
    (* (string * int) list *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   382
    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
   383
      (fn (name, value) => Option.map (pair name) (Int.fromString value))
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33246
diff changeset
   384
      (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
   385
        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
   386
        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
   387
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   388
    {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
   389
      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
   390
  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
   391
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
   392
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
   393
(* ------------------------------------------------------------------------- *)
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
   394
(* 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
   395
(* ------------------------------------------------------------------------- *)
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
   396
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   397
fun typ_of_dtyp descr typ_assoc (Datatype_Aux.DtTFree a) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   398
      (* replace a 'DtTFree' variable by the associated type *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   399
      the (AList.lookup (op =) typ_assoc (Datatype_Aux.DtTFree a))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   400
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtType (s, ds)) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   401
      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   402
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtRec i) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   403
      let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   404
        val (s, ds, _) = the (AList.lookup (op =) descr i)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   405
      in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   406
        Type (s, map (typ_of_dtyp descr typ_assoc) ds)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   407
      end;
15335
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
   408
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
   409
(* ------------------------------------------------------------------------- *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   410
(* close_form: universal closure over schematic variables in 't'             *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   411
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   412
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   413
(* Term.term -> Term.term *)
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   414
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   415
fun close_form t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   416
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   417
    (* (Term.indexname * Term.typ) list *)
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 29004
diff changeset
   418
    val vars = sort_wrt (fst o fst) (map dest_Var (OldTerm.term_vars t))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   419
  in
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   420
    fold (fn ((x, i), T) => fn t' =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   421
      Term.all T $ Abs (x, T, abstract_over (Var ((x, i), T), t'))) vars t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   422
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   423
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36374
diff changeset
   424
val monomorphic_term = Sledgehammer_Util.monomorphic_term
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36374
diff changeset
   425
val specialize_type = Sledgehammer_Util.specialize_type
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
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   428
(* 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
   429
(*                    denotes membership to an axiomatic type class          *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   430
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   431
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   432
fun is_const_of_class thy (s, T) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   433
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   434
    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
   435
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   436
    (* 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
   437
    (* 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
   438
    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
   439
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   440
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   441
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   442
(* 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
   443
(*                     of an inductive datatype in 'thy'                     *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   444
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   445
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   446
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
   447
  (case body_type T of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   448
    Type (s', _) =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   449
      (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
   450
        SOME constrs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   451
          List.exists (fn (cname, cty) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   452
            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
   453
      | NONE => false)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   454
  | _  => false);
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   455
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   456
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   457
(* 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
   458
(*                  operator of an inductive datatype in 'thy'               *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   459
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   460
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   461
fun is_IDT_recursor thy (s, T) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   462
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   463
    val rec_names = Symtab.fold (append o #rec_names o snd)
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   464
      (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
   465
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   466
    (* 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
   467
    (* 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
   468
    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
   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
(* ------------------------------------------------------------------------- *)
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
   472
(* 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
   473
(* ------------------------------------------------------------------------- *)
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
   474
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   475
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
   476
  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
   477
    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
   478
      | 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
   479
    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
   480
    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
   481
  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
   482
    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
   483
  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
   484
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
   485
(* ------------------------------------------------------------------------- *)
37405
7c49988afd0e avoid references to old constdefs
haftmann
parents: 37391
diff changeset
   486
(* 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
   487
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   488
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   489
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
   490
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   491
    (* (string * Term.term) list -> (string * Term.term) option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   492
    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
   493
      | 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
   494
          (let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   495
            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
   496
            val c        = Term.head_of lhs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   497
            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
   498
          in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   499
            if s=s' then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   500
              let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   501
                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
   502
                val ax'      = monomorphic_term typeSubs ax
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   503
                val rhs      = norm_rhs ax'
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   504
              in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   505
                SOME (axname, rhs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   506
              end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   507
            else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   508
              get_def_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   509
          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
   510
                   | TERM _          => get_def_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   511
                   | 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
   512
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   513
    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
   514
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   515
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   516
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   517
(* 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
   518
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   519
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   520
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
   521
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   522
    (* (string * Term.term) list -> (string * Term.term) option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   523
    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
   524
      | 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
   525
          (let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   526
            (* Term.term -> Term.typ option *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   527
            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
   528
                  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
   529
                    SOME T'
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   530
                  else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   531
                    NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   532
              | type_of_type_definition (Free _) = NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   533
              | type_of_type_definition (Var _) = NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   534
              | type_of_type_definition (Bound _) = NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   535
              | type_of_type_definition (Abs (_, _, body)) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   536
                  type_of_type_definition body
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   537
              | type_of_type_definition (t1 $ t2) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   538
                  (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
   539
                    SOME x => SOME x
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   540
                  | 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
   541
          in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   542
            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
   543
              SOME T' =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   544
                let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   545
                  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
   546
                  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
   547
                in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   548
                  SOME (axname, monomorphic_term typeSubs ax)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   549
                end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   550
            | NONE => get_typedef_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   551
          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
   552
                   | TERM _          => get_typedef_ax axioms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   553
                   | 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
   554
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   555
    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
   556
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   557
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   558
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   559
(* 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
   560
(*               created by the "axclass" command                            *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   561
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   562
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   563
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
   564
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   565
    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
   566
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   567
    Option.map (pair axname)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   568
      (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
   569
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   570
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   571
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   572
(* 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
   573
(*              normalizes the result term; certain constants are not        *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   574
(*              unfolded (cf. 'collect_axioms' and the various interpreters  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   575
(*              below): if the interpretation respects a definition anyway,  *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   576
(*              that definition does not need to be unfolded                 *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   577
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   578
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   579
(* 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
   580
(*       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
   581
(*       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
   582
(*       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
   583
(*       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
   584
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   585
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
   586
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   587
    (* Term.term -> Term.term *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   588
    fun unfold_loop t =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   589
      case t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   590
      (* Pure *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   591
        Const (@{const_name all}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   592
      | Const (@{const_name "=="}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   593
      | Const (@{const_name "==>"}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   594
      | Const (@{const_name TYPE}, _) => t  (* axiomatic type classes *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   595
      (* HOL *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   596
      | Const (@{const_name Trueprop}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   597
      | 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
   598
      | (* 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
   599
        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
   600
      | (* 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
   601
        Const (@{const_name False}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   602
      | Const (@{const_name undefined}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   603
      | 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
   604
      | 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
   605
      | Const (@{const_name All}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   606
      | Const (@{const_name Ex}, _) => t
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38795
diff changeset
   607
      | 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
   608
      | 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
   609
      | Const (@{const_name HOL.disj}, _) => t
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38553
diff changeset
   610
      | 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
   611
      (* sets *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   612
      | Const (@{const_name Collect}, _) => t
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37603
diff changeset
   613
      | 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
   614
      (* other optimizations *)
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   615
      | 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
   616
      | Const (@{const_name Finite_Set.finite}, _) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   617
      | 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
   618
          Type ("fun", [@{typ nat}, @{typ bool}])])) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   619
      | 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
   620
          Type ("fun", [@{typ nat}, @{typ nat}])])) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   621
      | 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
   622
          Type ("fun", [@{typ nat}, @{typ nat}])])) => t
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   623
      | 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
   624
          Type ("fun", [@{typ nat}, @{typ nat}])])) => t
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   625
      | Const (@{const_name List.append}, _) => t
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
   626
(* UNSOUND
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   627
      | Const (@{const_name lfp}, _) => t
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   628
      | Const (@{const_name gfp}, _) => t
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
   629
*)
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   630
      | 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
   631
      | 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
   632
      (* simply-typed lambda calculus *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   633
      | Const (s, T) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   634
          (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
   635
            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
   636
            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
   637
          (* 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
   638
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   639
            case get_def thy (s, T) of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   640
              SOME (axname, rhs) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   641
              (* 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
   642
              (* 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
   643
              (* '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
   644
              (* 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
   645
              (* 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
   646
              (* 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
   647
              (* 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
   648
              ((*tracing (" unfolding: " ^ axname);*)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   649
               unfold_loop rhs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   650
            | NONE => t)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   651
      | Free _ => t
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   652
      | Var _ => t
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   653
      | Bound _ => t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   654
      | 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
   655
      | 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
   656
    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
   657
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   658
    result
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   659
  end;
21985
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   660
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   661
(* ------------------------------------------------------------------------- *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   662
(* collect_axioms: collects (monomorphic, universally quantified, unfolded   *)
acfb13e8819e constants are unfolded, universal quantifiers are stripped, some minor changes
webertj
parents: 21931
diff changeset
   663
(*                 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
   664
(* ------------------------------------------------------------------------- *)
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
   665
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   666
(* 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
   667
(*       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
   668
(*       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
   669
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   670
(* 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
   671
(*       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
   672
(*       "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
   673
(*       *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
   674
(*       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
   675
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   676
(* 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
   677
(* 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
   678
(* 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
   679
(* 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
   680
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   681
(* 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
   682
(* 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
   683
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   684
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
   685
  let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   686
    val thy = ProofContext.theory_of ctxt
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
   687
    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
   688
    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
   689
    fun collect_this_axiom (axname, ax) axs =
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   690
      let
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   691
        val ax' = unfold_defs thy ax
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   692
      in
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   693
        if member (op aconv) axs ax' then axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   694
        else (tracing axname; collect_term_axioms ax' (ax' :: axs))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   695
      end
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   696
    and collect_sort_axioms T axs =
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   697
      let
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   698
        val sort =
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   699
          (case T of
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   700
            TFree (_, sort) => sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   701
          | TVar (_, sort)  => sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   702
          | _ => raise REFUTE ("collect_axioms",
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   703
              "type " ^ Syntax.string_of_typ ctxt T ^ " is not a variable"))
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   704
        (* obtain axioms for all superclasses *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   705
        val superclasses = sort @ maps (Sign.super_classes thy) sort
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   706
        (* merely an optimization, because 'collect_this_axiom' disallows *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   707
        (* duplicate axioms anyway:                                       *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   708
        val superclasses = distinct (op =) superclasses
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   709
        val class_axioms = maps (fn class => map (fn ax =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   710
          ("<" ^ class ^ ">", Thm.prop_of ax))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   711
          (#axioms (AxClass.get_info thy class) handle ERROR _ => []))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   712
          superclasses
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   713
        (* replace the (at most one) schematic type variable in each axiom *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   714
        (* by the actual type 'T'                                          *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   715
        val monomorphic_class_axioms = map (fn (axname, ax) =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   716
          (case Term.add_tvars ax [] of
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   717
            [] => (axname, ax)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   718
          | [(idx, S)] => (axname, monomorphic_term (Vartab.make [(idx, (S, T))]) ax)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   719
          | _ =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   720
            raise REFUTE ("collect_axioms", "class axiom " ^ axname ^ " (" ^
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   721
              Syntax.string_of_term ctxt ax ^
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   722
              ") contains more than one type variable")))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   723
          class_axioms
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   724
      in
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   725
        fold collect_this_axiom monomorphic_class_axioms axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   726
      end
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   727
    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
   728
      case T of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   729
      (* simple types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   730
        Type ("prop", []) => axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   731
      | Type ("fun", [T1, T2]) => collect_type_axioms T2 (collect_type_axioms T1 axs)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   732
      (* axiomatic type classes *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   733
      | Type ("itself", [T1]) => collect_type_axioms T1 axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   734
      | Type (s, Ts) =>
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31737
diff changeset
   735
        (case Datatype.get_info thy s of
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   736
          SOME info =>  (* inductive datatype *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   737
            (* only collect relevant type axioms for the argument types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   738
            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
   739
        | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   740
          (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
   741
            SOME (axname, ax) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   742
              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
   743
          | NONE =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   744
            (* 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
   745
            (* at least collect relevant type axioms for the argument types *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   746
            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
   747
      (* axiomatic type classes *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   748
      | TFree _ => collect_sort_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   749
      (* axiomatic type classes *)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   750
      | TVar _ => collect_sort_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   751
    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
   752
      case t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   753
      (* Pure *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   754
        Const (@{const_name all}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   755
      | Const (@{const_name "=="}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   756
      | Const (@{const_name "==>"}, _) => axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   757
      (* axiomatic type classes *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   758
      | Const (@{const_name TYPE}, T) => collect_type_axioms T axs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   759
      (* HOL *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   760
      | Const (@{const_name Trueprop}, _) => axs
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   761
      | 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
   762
      (* 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
   763
      | 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
   764
      (* 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
   765
      | Const (@{const_name False}, _) => axs
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   766
      | 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
   767
      | Const (@{const_name The}, T) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   768
          let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   769
            val ax = specialize_type thy (@{const_name The}, T)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   770
              (the (AList.lookup (op =) axioms "HOL.the_eq_trivial"))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   771
          in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   772
            collect_this_axiom ("HOL.the_eq_trivial", ax) axs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   773
          end
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
   774
      | 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
   775
          let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   776
            val ax = specialize_type thy (@{const_name Hilbert_Choice.Eps}, T)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   777
              (the (AList.lookup (op =) axioms "Hilbert_Choice.someI"))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   778
          in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   779
            collect_this_axiom ("Hilbert_Choice.someI", ax) axs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   780
          end
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   781
      | Const (@{const_name All}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   782
      | 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
   783
      | 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
   784
      | 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
   785
      | Const (@{const_name HOL.disj}, _) => axs
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38553
diff changeset
   786
      | 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
   787
      (* sets *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   788
      | Const (@{const_name Collect}, T) => collect_type_axioms T axs
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37603
diff changeset
   789
      | 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
   790
      (* other optimizations *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   791
      | 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
   792
      | Const (@{const_name Finite_Set.finite}, T) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   793
        collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   794
      | 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
   795
        Type ("fun", [@{typ nat}, @{typ bool}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   796
          collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   797
      | Const (@{const_name Groups.plus}, T as Type ("fun", [@{typ nat},
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   798
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   799
          collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   800
      | Const (@{const_name Groups.minus}, T as Type ("fun", [@{typ nat},
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   801
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   802
          collect_type_axioms T axs
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   803
      | Const (@{const_name Groups.times}, T as Type ("fun", [@{typ nat},
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
   804
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   805
          collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   806
      | Const (@{const_name List.append}, T) => collect_type_axioms T axs
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
   807
(* UNSOUND
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   808
      | Const (@{const_name lfp}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   809
      | 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
   810
*)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   811
      | Const (@{const_name fst}, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   812
      | 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
   813
      (* simply-typed lambda calculus *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
   814
      | Const (s, T) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   815
          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
   816
            (* 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
   817
            (* and the class definition                               *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   818
            let
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   819
              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
   820
              val of_class = Logic.mk_of_class (TVar (("'a", 0), [class]), class)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   821
              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
   822
                (* 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
   823
                handle Type.TYPE_MATCH => NONE
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   824
              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
   825
              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
   826
            in
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   827
              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
   828
            end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   829
          else if is_IDT_constructor thy (s, T)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   830
            orelse is_IDT_recursor thy (s, T)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   831
          then
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   832
            (* only collect relevant type axioms *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   833
            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
   834
          else
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   835
            (* 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
   836
            (* 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
   837
            (* 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
   838
            (* only collect relevant type axioms *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   839
            collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   840
      | Free (_, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   841
      | Var (_, T) => collect_type_axioms T axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   842
      | Bound _ => axs
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   843
      | Abs (_, T, body) => collect_term_axioms body (collect_type_axioms T axs)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   844
      | t1 $ t2 => collect_term_axioms t2 (collect_term_axioms t1 axs)
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   845
    val result = map close_form (collect_term_axioms t [])
32950
5d5e123443b3 normalized aliases of Output operations;
wenzelm
parents: 32857
diff changeset
   846
    val _ = tracing " ...done."
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   847
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   848
    result
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   849
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   850
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   851
(* ------------------------------------------------------------------------- *)
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
   852
(* 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
   853
(*               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
   854
(*               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
   855
(*               'propT'.  For IDTs, also the argument types of constructors *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   856
(*               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
   857
(* ------------------------------------------------------------------------- *)
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
   858
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   859
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
   860
  let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   861
    val thy = ProofContext.theory_of ctxt
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   862
    fun collect_types T acc =
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   863
      (case T of
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   864
        Type ("fun", [T1, T2]) => collect_types T1 (collect_types T2 acc)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   865
      | Type ("prop", []) => acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   866
      | Type (s, Ts) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   867
          (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
   868
            SOME info =>  (* inductive datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   869
              let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   870
                val index = #index info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   871
                val descr = #descr info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   872
                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
   873
                val typ_assoc = typs ~~ Ts
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   874
                (* 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
   875
                (* 'DtTFree'                                        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   876
                val _ = if Library.exists (fn d =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   877
                  case d of Datatype_Aux.DtTFree _ => false | _ => true) typs then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   878
                  raise REFUTE ("ground_types", "datatype argument (for type "
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
   879
                    ^ 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
   880
                else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   881
                (* 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
   882
                (* 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
   883
                (* recursive datatype                                         *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   884
                fun collect_dtyp d acc =
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   885
                  let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   886
                    val dT = typ_of_dtyp descr typ_assoc d
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   887
                  in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   888
                    case d of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   889
                      Datatype_Aux.DtTFree _ =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   890
                      collect_types dT acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   891
                    | Datatype_Aux.DtType (_, ds) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   892
                      collect_types dT (fold_rev collect_dtyp ds acc)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   893
                    | Datatype_Aux.DtRec i =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   894
                      if member (op =) acc dT then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   895
                        acc  (* prevent infinite recursion *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   896
                      else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   897
                        let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   898
                          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
   899
                          (* 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
   900
                          (* is required), add it to 'acc'                        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   901
                          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
   902
                            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
   903
                              insert (op =) dT acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   904
                            else acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   905
                          (* collect argument types *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   906
                          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
   907
                          (* collect constructor types *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   908
                          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
   909
                        in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   910
                          acc_dconstrs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   911
                        end
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
   912
                  end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   913
              in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   914
                (* 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
   915
                (* added by 'collect_dtyp' automatically                      *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   916
                collect_dtyp (Datatype_Aux.DtRec index) acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   917
              end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   918
          | NONE =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   919
            (* 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
   920
            (* "typedecl"                                               *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   921
            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
   922
      | TFree _ => insert (op =) T acc
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   923
      | 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
   924
  in
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
   925
    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
   926
  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
   927
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
   928
(* ------------------------------------------------------------------------- *)
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
   929
(* 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
   930
(*                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
   931
(*                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
   932
(*                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
   933
(* ------------------------------------------------------------------------- *)
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
   934
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   935
(* Term.typ -> string *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   936
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   937
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
   938
  | string_of_typ (TFree (s, _))    = s
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   939
  | 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
   940
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   941
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   942
(* 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
   943
(*                 '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
   944
(*                 '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
   945
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   946
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   947
(* Term.typ list -> (string * int) list -> int -> (Term.typ * int) list *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
   948
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   949
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
   950
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   951
    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
   952
      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
   953
        SOME n => n
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
   954
      | NONE => minsize
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   955
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   956
    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
   957
  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
   958
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
   959
(* ------------------------------------------------------------------------- *)
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
   960
(* 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
   961
(*                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
   962
(*                '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
   963
(*                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
   964
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   965
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   966
(* (Term.typ * int) list -> (string * int) list -> int -> int ->
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   967
  (Term.typ * int) list option *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
   968
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   969
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
   970
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   971
    (* 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
   972
    (* 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
   973
    (* int -> int -> int -> int list option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   974
    fun make_first _ 0 sum =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   975
          if sum = 0 then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   976
            SOME []
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   977
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   978
            NONE
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   979
      | make_first max len sum =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   980
          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
   981
            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
   982
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   983
            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
   984
    (* 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
   985
    (* all list elements x (unless 'max'<0)                                *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   986
    (* int -> int -> int -> int list -> int list option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   987
    fun next max len sum [] =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   988
          NONE
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
   989
      | next max len sum [x] =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   990
          (* 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
   991
          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
   992
      | 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
   993
          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
   994
            (* we can shift *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   995
            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
   996
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   997
            (* continue search *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
   998
            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
   999
    (* only consider those types for which the size is not fixed *)
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33246
diff changeset
  1000
    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
  1001
    (* 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
  1002
    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
  1003
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1004
    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
  1005
      SOME diffs' =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1006
        (* 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
  1007
        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
  1008
          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
  1009
          (* return the fixed size *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1010
            SOME n => ((T, n), ds)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1011
          (* 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
  1012
          | 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
  1013
          xs diffs'))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1014
    | NONE => NONE
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1015
  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
  1016
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1017
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1018
(* 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
  1019
(*         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
  1020
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1021
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1022
(* interpretation -> prop_formula *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1023
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1024
fun toTrue (Leaf [fm, _]) = fm
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1025
  | 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
  1026
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
  1027
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1028
(* 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
  1029
(*          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
  1030
(*          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
  1031
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1032
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1033
(* interpretation -> prop_formula *)
14807
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1034
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1035
fun toFalse (Leaf [_, fm]) = fm
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1036
  | 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
  1037
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
  1038
(* ------------------------------------------------------------------------- *)
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
  1039
(* 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
  1040
(*             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
  1041
(*             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
  1042
(* {...}     : 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
  1043
(* 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
  1044
(* 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
  1045
(* 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
  1046
(* ------------------------------------------------------------------------- *)
e8ccb13d7774 major code change: refute can now handle any Isabelle term, adds certain axioms automatically, and can handle inductive datatypes (but not yet recursion over them)
webertj
parents: 14604
diff changeset
  1047
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1048
fun find_model ctxt
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1049
    {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
  1050
    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
  1051
  let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1052
    val thy = ProofContext.theory_of ctxt
33054
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1053
    (* string -> unit *)
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1054
    fun check_expect outcome_code =
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1055
      if expect = "" orelse outcome_code = expect then ()
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1056
      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
  1057
    (* unit -> unit *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1058
    fun wrapper () =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1059
      let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1060
        val timer = Timer.startRealTimer ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1061
        val t =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1062
          if no_assms then t
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1063
          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
  1064
          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
  1065
        val u = unfold_defs thy t
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1066
        val _ = tracing ("Unfolded term: " ^ Syntax.string_of_term ctxt u)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1067
        val axioms = collect_axioms ctxt u
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1068
        (* Term.typ list *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1069
        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
  1070
        val _ = tracing ("Ground types: "
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1071
          ^ (if null types then "none."
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1072
             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
  1073
        (* 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
  1074
        (* 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
  1075
        (* 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
  1076
        val maybe_spurious = Library.exists (fn
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1077
            Type (s, _) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1078
              (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
  1079
                SOME info =>  (* inductive datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1080
                  let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1081
                    val index           = #index info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1082
                    val descr           = #descr info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1083
                    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
  1084
                  in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1085
                    (* recursive datatype? *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1086
                    Library.exists (fn (_, ds) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1087
                      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
  1088
                  end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1089
              | NONE => false)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1090
          | _ => false) types
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1091
        val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1092
          if maybe_spurious then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1093
            warning ("Term contains a recursive datatype; "
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1094
              ^ "countermodel(s) may be spurious!")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1095
          else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1096
            ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1097
        (* (Term.typ * int) list -> string *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1098
        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
  1099
          let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1100
            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
  1101
            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
  1102
                    orelse raise TimeLimit.TimeOut
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1103
            val init_model = (universe, [])
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1104
            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
  1105
              bounds = [], wellformed = True}
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1106
            val _ = tracing ("Translating term (sizes: "
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1107
              ^ 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
  1108
            (* translate 'u' and all axioms *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1109
            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
  1110
              let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1111
                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
  1112
              in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1113
                (* set 'def_eq' to 'true' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1114
                (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
  1115
                  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
  1116
                  wellformed = #wellformed a'}))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1117
              end) (u :: axioms) (init_model, init_args)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1118
            (* make 'u' either true or false, and make all axioms true, and *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1119
            (* add the well-formedness side condition                       *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1120
            val fm_u = (if negate then toFalse else toTrue) (hd intrs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1121
            val fm_ax = PropLogic.all (map toTrue (tl intrs))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1122
            val fm = PropLogic.all [#wellformed args, fm_ax, fm_u]
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1123
            val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1124
              (if satsolver = "dpll" orelse satsolver = "enumerate" then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1125
                warning ("Using SAT solver " ^ quote satsolver ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1126
                         "; for better performance, consider installing an \
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1127
                         \external solver.")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1128
               else ());
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1129
            val solver =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1130
              SatSolver.invoke_solver satsolver
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1131
              handle Option.Option =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1132
                     error ("Unknown SAT solver: " ^ quote satsolver ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1133
                            ". Available solvers: " ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1134
                            commas (map (quote o fst) (!SatSolver.solvers)) ^ ".")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1135
          in
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1136
            Output.urgent_message "Invoking SAT solver...";
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1137
            (case solver fm of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1138
              SatSolver.SATISFIABLE assignment =>
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1139
                (Output.urgent_message ("*** Model found: ***\n" ^ print_model ctxt model
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1140
                  (fn i => case assignment i of SOME b => b | NONE => true));
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1141
                 if maybe_spurious then "potential" else "genuine")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1142
            | SatSolver.UNSATISFIABLE _ =>
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1143
                (Output.urgent_message "No model exists.";
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1144
                case next_universe universe sizes minsize maxsize of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1145
                  SOME universe' => find_model_loop universe'
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1146
                | NONE => (Output.urgent_message
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1147
                    "Search terminated, no larger universe within the given limits.";
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1148
                    "none"))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1149
            | SatSolver.UNKNOWN =>
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1150
                (Output.urgent_message "No model found.";
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1151
                case next_universe universe sizes minsize maxsize of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1152
                  SOME universe' => find_model_loop universe'
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1153
                | NONE => (Output.urgent_message
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1154
                  "Search terminated, no larger universe within the given limits.";
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1155
                  "unknown"))) handle SatSolver.NOT_CONFIGURED =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1156
              (error ("SAT solver " ^ quote satsolver ^ " is not configured.");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1157
               "unknown")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1158
          end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1159
          handle MAXVARS_EXCEEDED =>
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1160
            (Output.urgent_message ("Search terminated, number of Boolean variables ("
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1161
              ^ string_of_int maxvars ^ " allowed) exceeded.");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1162
              "unknown")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1163
30314
853778f6ef7d Added "expect" option to Refute, like in Nitpick, that allows to write regression tests.
blanchet
parents: 30312
diff changeset
  1164
        val outcome_code = find_model_loop (first_universe types sizes minsize)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1165
      in
33054
dd1192a96968 fixed the "expect" mechanism of Refute in the face of timeouts
blanchet
parents: 32952
diff changeset
  1166
        check_expect outcome_code
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1167
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1168
  in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1169
    (* some parameter sanity checks *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1170
    minsize>=1 orelse
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1171
      error ("\"minsize\" is " ^ string_of_int minsize ^ ", must be at least 1");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1172
    maxsize>=1 orelse
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1173
      error ("\"maxsize\" is " ^ string_of_int maxsize ^ ", must be at least 1");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1174
    maxsize>=minsize orelse
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1175
      error ("\"maxsize\" (=" ^ string_of_int maxsize ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1176
      ") is less than \"minsize\" (=" ^ string_of_int minsize ^ ").");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1177
    maxvars>=0 orelse
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1178
      error ("\"maxvars\" is " ^ string_of_int maxvars ^ ", must be at least 0");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1179
    maxtime>=0 orelse
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1180
      error ("\"maxtime\" is " ^ string_of_int maxtime ^ ", must be at least 0");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1181
    (* enter loop with or without time limit *)
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1182
    Output.urgent_message ("Trying to find a model that "
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1183
      ^ (if negate then "refutes" else "satisfies") ^ ": "
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1184
      ^ 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
  1185
    if maxtime > 0 then (
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1186
      TimeLimit.timeLimit (Time.fromSeconds maxtime)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1187
        wrapper ()
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1188
      handle TimeLimit.TimeOut =>
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1189
        (Output.urgent_message ("Search terminated, time limit (" ^
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1190
            string_of_int maxtime
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1191
            ^ (if maxtime=1 then " second" else " seconds") ^ ") exceeded.");
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1192
         check_expect "unknown")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1193
    ) else wrapper ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1194
  end;
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1195
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1196
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1197
(* ------------------------------------------------------------------------- *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1198
(* INTERFACE, PART 2: FINDING A MODEL                                        *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1199
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1200
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1201
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1202
(* satisfy_term: calls 'find_model' to find a model that satisfies 't'       *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1203
(* params      : list of '(name, value)' pairs used to override default      *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1204
(*               parameters                                                  *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1205
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1206
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1207
fun satisfy_term ctxt params assm_ts t =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1208
  find_model ctxt (actual_params ctxt params) assm_ts t false;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1209
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1210
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1211
(* refute_term: calls 'find_model' to find a model that refutes 't'          *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1212
(* params     : list of '(name, value)' pairs used to override default       *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  1213
(*              parameters                                                   *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1214
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1215
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1216
fun refute_term ctxt params assm_ts t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1217
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1218
    (* disallow schematic type variables, since we cannot properly negate  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1219
    (* terms containing them (their logical meaning is that there EXISTS a *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1220
    (* type s.t. ...; to refute such a formula, we would have to show that *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1221
    (* for ALL types, not ...)                                             *)
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29265
diff changeset
  1222
    val _ = null (Term.add_tvars t []) orelse
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1223
      error "Term to be refuted contains schematic type variables"
21556
e0ffb2d13f9f outermost universal quantifiers are stripped
webertj
parents: 21267
diff changeset
  1224
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1225
    (* existential closure over schematic variables *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1226
    (* (Term.indexname * Term.typ) list *)
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 29004
diff changeset
  1227
    val vars = sort_wrt (fst o fst) (map dest_Var (OldTerm.term_vars t))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1228
    (* Term.term *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1229
    val ex_closure = fold (fn ((x, i), T) => fn t' =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1230
      HOLogic.exists_const T $
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  1231
        Abs (x, T, abstract_over (Var ((x, i), T), t'))) vars t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1232
    (* Note: If 't' is of type 'propT' (rather than 'boolT'), applying   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1233
    (* 'HOLogic.exists_const' is not type-correct.  However, this is not *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1234
    (* really a problem as long as 'find_model' still interprets the     *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1235
    (* resulting term correctly, without checking its type.              *)
21556
e0ffb2d13f9f outermost universal quantifiers are stripped
webertj
parents: 21267
diff changeset
  1236
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1237
    (* replace outermost universally quantified variables by Free's:     *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1238
    (* refuting a term with Free's is generally faster than refuting a   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1239
    (* term with (nested) quantifiers, because quantifiers are expanded, *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1240
    (* while the SAT solver searches for an interpretation for Free's.   *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1241
    (* Also we get more information back that way, namely an             *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1242
    (* interpretation which includes values for the (formerly)           *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1243
    (* quantified variables.                                             *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1244
    (* maps  !!x1...xn. !xk...xm. t   to   t  *)
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
  1245
    fun strip_all_body (Const (@{const_name all}, _) $ Abs (_, _, t)) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1246
          strip_all_body t
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
  1247
      | strip_all_body (Const (@{const_name Trueprop}, _) $ t) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1248
          strip_all_body t
29820
07f53494cf20 Rearrange Refute/SAT theory dependencies so as to use even more antiquotations in refute.ML +
blanchet
parents: 29802
diff changeset
  1249
      | strip_all_body (Const (@{const_name All}, _) $ Abs (_, _, t)) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1250
          strip_all_body t
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
  1251
      | strip_all_body t = t
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1252
    (* maps  !!x1...xn. !xk...xm. t   to   [x1, ..., xn, xk, ..., xm]  *)
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
  1253
    fun strip_all_vars (Const (@{const_name all}, _) $ Abs (a, T, t)) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1254
          (a, T) :: strip_all_vars t
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
  1255
      | strip_all_vars (Const (@{const_name Trueprop}, _) $ t) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1256
          strip_all_vars t
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
  1257
      | strip_all_vars (Const (@{const_name All}, _) $ Abs (a, T, t)) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1258
          (a, T) :: strip_all_vars t
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1259
      | strip_all_vars t = [] : (string * typ) list
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1260
    val strip_t = strip_all_body ex_closure
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1261
    val frees = Term.rename_wrt_term strip_t (strip_all_vars ex_closure)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1262
    val subst_t = Term.subst_bounds (map Free frees, strip_t)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1263
  in
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1264
    find_model ctxt (actual_params ctxt params) assm_ts subst_t true
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1265
  end;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1266
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1267
(* ------------------------------------------------------------------------- *)
32857
394d37f19e0a Refute.refute_goal: canonical goal addresses from 1 (renamed from refute_subgoal to clarify change in semantics);
wenzelm
parents: 31986
diff changeset
  1268
(* refute_goal                                                               *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1269
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1270
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1271
fun refute_goal ctxt params th i =
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1272
  let
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1273
    val t = th |> prop_of
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1274
  in
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1275
    if Logic.count_prems t = 0 then
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39811
diff changeset
  1276
      Output.urgent_message "No subgoal!"
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1277
    else
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1278
      let
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1279
        val assms = map term_of (Assumption.all_assms_of ctxt)
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1280
        val (t, frees) = Logic.goal_params t i
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1281
      in
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1282
        refute_term ctxt params assms (subst_bounds (frees, t))
34120
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1283
      end
f9920a3ddf50 added "no_assms" option to Refute, and include structured proof assumptions by default;
blanchet
parents: 34017
diff changeset
  1284
  end
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1285
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1286
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1287
(* ------------------------------------------------------------------------- *)
15292
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1288
(* INTERPRETERS: Auxiliary Functions                                         *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1289
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1290
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1291
(* ------------------------------------------------------------------------- *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1292
(* make_constants: returns all interpretations for type 'T' that consist of  *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1293
(*                 unit vectors with 'True'/'False' only (no Boolean         *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1294
(*                 variables)                                                *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1295
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  1296
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1297
fun make_constants ctxt model T =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1298
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1299
    (* returns a list with all unit vectors of length n *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1300
    (* int -> interpretation list *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1301
    fun unit_vectors n =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1302
      let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1303
        (* returns the k-th unit vector of length n *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1304
        (* int * int -> interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1305
        fun unit_vector (k, n) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1306
          Leaf ((replicate (k-1) False) @ (True :: (replicate (n-k) False)))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1307
        (* int -> interpretation list *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1308
        fun unit_vectors_loop k =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1309
          if k>n then [] else unit_vector (k,n) :: unit_vectors_loop (k+1)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1310
      in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1311
        unit_vectors_loop 1
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1312
      end
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1313
    (* returns a list of lists, each one consisting of n (possibly *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1314
    (* identical) elements from 'xs'                               *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1315
    (* int -> 'a list -> 'a list list *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1316
    fun pick_all 1 xs = map single xs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1317
      | pick_all n xs =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1318
          let val rec_pick = pick_all (n - 1) xs in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1319
            maps (fn x => map (cons x) rec_pick) xs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1320
          end
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1321
    (* returns all constant interpretations that have the same tree *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1322
    (* structure as the interpretation argument                     *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1323
    (* interpretation -> interpretation list *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1324
    fun make_constants_intr (Leaf xs) = unit_vectors (length xs)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1325
      | make_constants_intr (Node xs) = map Node (pick_all (length xs)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1326
          (make_constants_intr (hd xs)))
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1327
    (* obtain the interpretation for a variable of type 'T' *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1328
    val (i, _, _) = interpret ctxt model {maxvars=0, def_eq=false, next_idx=1,
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1329
      bounds=[], wellformed=True} (Free ("dummy", T))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1330
  in
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1331
    make_constants_intr i
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1332
  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
  1333
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
  1334
(* ------------------------------------------------------------------------- *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1335
(* size_of_type: returns the number of elements in a type 'T' (i.e. 'length  *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1336
(*               (make_constants T)', but implemented more efficiently)      *)
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
  1337
(* ------------------------------------------------------------------------- *)
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
  1338
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1339
(* returns 0 for an empty ground type or a function type with empty      *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1340
(* codomain, but fails for a function type with empty domain --          *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1341
(* admissibility of datatype constructor argument types (see "Inductive  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1342
(* datatypes in HOL - lessons learned ...", S. Berghofer, M. Wenzel,     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1343
(* TPHOLs 99) ensures that recursive, possibly empty, datatype fragments *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1344
(* never occur as the domain of a function type that is the type of a    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1345
(* constructor argument                                                  *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1346
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1347
fun size_of_type ctxt model T =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1348
  let
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1349
    (* returns the number of elements that have the same tree structure as a *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1350
    (* given interpretation                                                  *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1351
    fun size_of_intr (Leaf xs) = length xs
39047
cdff476ba39e use existing Integer.pow, despite its slightly odd argument order;
wenzelm
parents: 39046
diff changeset
  1352
      | size_of_intr (Node xs) = Integer.pow (length xs) (size_of_intr (hd xs))
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1353
    (* obtain the interpretation for a variable of type 'T' *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1354
    val (i, _, _) = interpret ctxt model {maxvars=0, def_eq=false, next_idx=1,
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1355
      bounds=[], wellformed=True} (Free ("dummy", T))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1356
  in
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1357
    size_of_intr i
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1358
  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
  1359
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
  1360
(* ------------------------------------------------------------------------- *)
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
  1361
(* TT/FF: interpretations that denote "true" or "false", respectively        *)
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
  1362
(* ------------------------------------------------------------------------- *)
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
  1363
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1364
(* 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
  1365
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1366
val TT = Leaf [True, False];
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
  1367
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1368
val FF = Leaf [False, True];
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
  1369
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
  1370
(* ------------------------------------------------------------------------- *)
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
  1371
(* make_equality: returns an interpretation that denotes (extensional)       *)
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
  1372
(*                equality of two interpretations                            *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1373
(* - two interpretations are 'equal' iff they are both defined and denote    *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1374
(*   the same value                                                          *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1375
(* - two interpretations are 'not_equal' iff they are both defined at least  *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1376
(*   partially, and a defined part denotes different values                  *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1377
(* - a completely undefined interpretation is neither 'equal' nor            *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1378
(*   'not_equal' to another 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
  1379
(* ------------------------------------------------------------------------- *)
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
  1380
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1381
(* We could in principle represent '=' on a type T by a particular        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1382
(* interpretation.  However, the size of that interpretation is quadratic *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1383
(* in the size of T.  Therefore comparing the interpretations 'i1' and    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1384
(* 'i2' directly is more efficient than constructing the interpretation   *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1385
(* for equality on T first, and "applying" this interpretation to 'i1'    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1386
(* and 'i2' in the usual way (cf. 'interpretation_apply') then.           *)
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
  1387
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1388
(* interpretation * interpretation -> 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
  1389
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1390
fun make_equality (i1, i2) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1391
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1392
    (* interpretation * interpretation -> prop_formula *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1393
    fun equal (i1, i2) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1394
      (case i1 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1395
        Leaf xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1396
          (case i2 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1397
            Leaf ys => PropLogic.dot_product (xs, ys)  (* defined and equal *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1398
          | Node _  => raise REFUTE ("make_equality",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1399
            "second interpretation is higher"))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1400
      | Node xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1401
          (case i2 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1402
            Leaf _  => raise REFUTE ("make_equality",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1403
            "first interpretation is higher")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1404
          | Node ys => PropLogic.all (map equal (xs ~~ ys))))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1405
    (* interpretation * interpretation -> prop_formula *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1406
    fun not_equal (i1, i2) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1407
      (case i1 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1408
        Leaf xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1409
          (case i2 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1410
            (* defined and not equal *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1411
            Leaf ys => PropLogic.all ((PropLogic.exists xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1412
            :: (PropLogic.exists ys)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1413
            :: (map (fn (x,y) => SOr (SNot x, SNot y)) (xs ~~ ys)))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1414
          | Node _  => raise REFUTE ("make_equality",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1415
            "second interpretation is higher"))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1416
      | Node xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1417
          (case i2 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1418
            Leaf _  => raise REFUTE ("make_equality",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1419
            "first interpretation is higher")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1420
          | Node ys => PropLogic.exists (map not_equal (xs ~~ ys))))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1421
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1422
    (* a value may be undefined; therefore 'not_equal' is not just the *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1423
    (* negation of 'equal'                                             *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1424
    Leaf [equal (i1, i2), not_equal (i1, i2)]
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1425
  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
  1426
15292
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1427
(* ------------------------------------------------------------------------- *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1428
(* make_def_equality: returns an interpretation that denotes (extensional)   *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1429
(*                    equality of two interpretations                        *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1430
(* This function treats undefined/partially defined interpretations          *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1431
(* different from 'make_equality': two undefined interpretations are         *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1432
(* considered equal, while a defined interpretation is considered not equal  *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1433
(* to an undefined interpretation.                                           *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1434
(* ------------------------------------------------------------------------- *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1435
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1436
(* interpretation * interpretation -> interpretation *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1437
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1438
fun make_def_equality (i1, i2) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1439
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1440
    (* interpretation * interpretation -> prop_formula *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1441
    fun equal (i1, i2) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1442
      (case i1 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1443
        Leaf xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1444
          (case i2 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1445
            (* defined and equal, or both undefined *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1446
            Leaf ys => SOr (PropLogic.dot_product (xs, ys),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1447
            SAnd (PropLogic.all (map SNot xs), PropLogic.all (map SNot ys)))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1448
          | Node _  => raise REFUTE ("make_def_equality",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1449
            "second interpretation is higher"))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1450
      | Node xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1451
          (case i2 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1452
            Leaf _  => raise REFUTE ("make_def_equality",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1453
            "first interpretation is higher")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1454
          | Node ys => PropLogic.all (map equal (xs ~~ ys))))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1455
    (* interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1456
    val eq = equal (i1, i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1457
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1458
    Leaf [eq, SNot eq]
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1459
  end;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1460
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1461
(* ------------------------------------------------------------------------- *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1462
(* interpretation_apply: returns an interpretation that denotes the result   *)
22092
ab3dfcef6489 reformatted to 80 chars/line
webertj
parents: 22055
diff changeset
  1463
(*                       of applying the function denoted by 'i1' to the     *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1464
(*                       argument denoted by 'i2'                            *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1465
(* ------------------------------------------------------------------------- *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1466
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1467
(* interpretation * interpretation -> interpretation *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1468
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1469
fun interpretation_apply (i1, i2) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1470
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1471
    (* interpretation * interpretation -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1472
    fun interpretation_disjunction (tr1,tr2) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1473
      tree_map (fn (xs,ys) => map (fn (x,y) => SOr(x,y)) (xs ~~ ys))
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1474
        (tree_pair (tr1,tr2))
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1475
    (* prop_formula * interpretation -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1476
    fun prop_formula_times_interpretation (fm,tr) =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1477
      tree_map (map (fn x => SAnd (fm,x))) tr
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1478
    (* prop_formula list * interpretation list -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1479
    fun prop_formula_list_dot_product_interpretation_list ([fm],[tr]) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1480
          prop_formula_times_interpretation (fm,tr)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1481
      | prop_formula_list_dot_product_interpretation_list (fm::fms,tr::trees) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1482
          interpretation_disjunction (prop_formula_times_interpretation (fm,tr),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1483
            prop_formula_list_dot_product_interpretation_list (fms,trees))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1484
      | prop_formula_list_dot_product_interpretation_list (_,_) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1485
          raise REFUTE ("interpretation_apply", "empty list (in dot product)")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1486
    (* concatenates 'x' with every list in 'xss', returning a new list of *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1487
    (* lists                                                              *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1488
    (* 'a -> 'a list list -> 'a list list *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1489
    fun cons_list x xss = map (cons x) xss
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1490
    (* returns a list of lists, each one consisting of one element from each *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1491
    (* element of 'xss'                                                      *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1492
    (* 'a list list -> 'a list list *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1493
    fun pick_all [xs] = map single xs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1494
      | pick_all (xs::xss) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1495
          let val rec_pick = pick_all xss in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1496
            maps (fn x => map (cons x) rec_pick) xs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1497
          end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1498
      | pick_all _ = raise REFUTE ("interpretation_apply", "empty list (in pick_all)")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1499
    (* interpretation -> prop_formula list *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1500
    fun interpretation_to_prop_formula_list (Leaf xs) = xs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1501
      | interpretation_to_prop_formula_list (Node trees) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1502
          map PropLogic.all (pick_all
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1503
            (map interpretation_to_prop_formula_list trees))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1504
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1505
    case i1 of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1506
      Leaf _ =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1507
        raise REFUTE ("interpretation_apply", "first interpretation is a leaf")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1508
    | Node xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1509
        prop_formula_list_dot_product_interpretation_list
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1510
          (interpretation_to_prop_formula_list i2, xs)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1511
  end;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1512
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1513
(* ------------------------------------------------------------------------- *)
15292
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1514
(* eta_expand: eta-expands a term 't' by adding 'i' lambda abstractions      *)
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1515
(* ------------------------------------------------------------------------- *)
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1516
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1517
(* Term.term -> int -> Term.term *)
15292
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1518
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1519
fun eta_expand t i =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1520
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1521
    val Ts = Term.binder_types (Term.fastype_of t)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1522
    val t' = Term.incr_boundvars i t
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1523
  in
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
  1524
    fold_rev (fn T => fn term => Abs ("<eta_expand>", T, term))
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
  1525
      (List.take (Ts, i))
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
  1526
      (Term.list_comb (t', map Bound (i-1 downto 0)))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1527
  end;
15292
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1528
15335
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
  1529
(* ------------------------------------------------------------------------- *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1530
(* size_of_dtyp: the size of (an initial fragment of) an inductive data type *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1531
(*               is the sum (over its constructors) of the product (over     *)
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1532
(*               their arguments) of the size of the argument types          *)
15335
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
  1533
(* ------------------------------------------------------------------------- *)
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
  1534
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1535
fun size_of_dtyp ctxt typ_sizes descr typ_assoc constructors =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1536
  Integer.sum (map (fn (_, dtyps) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1537
    Integer.prod (map (size_of_type ctxt (typ_sizes, []) o
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1538
      (typ_of_dtyp descr typ_assoc)) dtyps))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1539
        constructors);
15335
f81e6e24351f minor code refactoring (typ_of_dtyp, size_of_dtyp)
webertj
parents: 15334
diff changeset
  1540
15292
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1541
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1542
(* ------------------------------------------------------------------------- *)
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1543
(* INTERPRETERS: Actual Interpreters                                         *)
09e218879265 minor changes (comments/code refactoring)
webertj
parents: 15283
diff changeset
  1544
(* ------------------------------------------------------------------------- *)
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
  1545
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1546
(* simply typed lambda calculus: Isabelle's basic term syntax, with type *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1547
(* variables, function types, and propT                                  *)
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
  1548
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1549
fun stlc_interpreter ctxt model args t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1550
  let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1551
    val thy = ProofContext.theory_of ctxt
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1552
    val (typs, terms) = model
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1553
    val {maxvars, def_eq, next_idx, bounds, wellformed} = args
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1554
    (* Term.typ -> (interpretation * model * arguments) option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1555
    fun interpret_groundterm T =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1556
      let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1557
        (* unit -> (interpretation * model * arguments) option *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1558
        fun interpret_groundtype () =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1559
          let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1560
            (* the model must specify a size for ground types *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1561
            val size =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1562
              if T = Term.propT then 2
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1563
              else the (AList.lookup (op =) typs T)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1564
            val next = next_idx + size
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1565
            (* check if 'maxvars' is large enough *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1566
            val _ = (if next - 1 > maxvars andalso maxvars > 0 then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1567
              raise MAXVARS_EXCEEDED else ())
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1568
            (* prop_formula list *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1569
            val fms  = map BoolVar (next_idx upto (next_idx + size - 1))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1570
            (* interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1571
            val intr = Leaf fms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1572
            (* prop_formula list -> prop_formula *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1573
            fun one_of_two_false [] = True
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1574
              | one_of_two_false (x::xs) = SAnd (PropLogic.all (map (fn x' =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1575
                  SOr (SNot x, SNot x')) xs), one_of_two_false xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1576
            (* prop_formula *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1577
            val wf = one_of_two_false fms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1578
          in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1579
            (* extend the model, increase 'next_idx', add well-formedness *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1580
            (* condition                                                  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1581
            SOME (intr, (typs, (t, intr)::terms), {maxvars = maxvars,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1582
              def_eq = def_eq, next_idx = next, bounds = bounds,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1583
              wellformed = SAnd (wellformed, wf)})
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1584
          end
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1585
      in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1586
        case T of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1587
          Type ("fun", [T1, T2]) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1588
            let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1589
              (* we create 'size_of_type ... T1' different copies of the        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1590
              (* interpretation for 'T2', which are then combined into a single *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1591
              (* new interpretation                                             *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1592
              (* make fresh copies, with different variable indices *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1593
              (* 'idx': next variable index                         *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1594
              (* 'n'  : number of copies                            *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1595
              (* int -> int -> (int * interpretation list * prop_formula *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1596
              fun make_copies idx 0 = (idx, [], True)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1597
                | make_copies idx n =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1598
                    let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1599
                      val (copy, _, new_args) = interpret ctxt (typs, [])
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1600
                        {maxvars = maxvars, def_eq = false, next_idx = idx,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1601
                        bounds = [], wellformed = True} (Free ("dummy", T2))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1602
                      val (idx', copies, wf') = make_copies (#next_idx new_args) (n-1)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1603
                    in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1604
                      (idx', copy :: copies, SAnd (#wellformed new_args, wf'))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1605
                    end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1606
              val (next, copies, wf) = make_copies next_idx
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1607
                (size_of_type ctxt model T1)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1608
              (* combine copies into a single interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1609
              val intr = Node copies
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1610
            in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1611
              (* extend the model, increase 'next_idx', add well-formedness *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1612
              (* condition                                                  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1613
              SOME (intr, (typs, (t, intr)::terms), {maxvars = maxvars,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1614
                def_eq = def_eq, next_idx = next, bounds = bounds,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1615
                wellformed = SAnd (wellformed, wf)})
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1616
            end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1617
        | Type _  => interpret_groundtype ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1618
        | TFree _ => interpret_groundtype ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1619
        | TVar  _ => interpret_groundtype ()
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1620
      end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1621
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1622
    case AList.lookup (op =) terms t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1623
      SOME intr =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1624
        (* return an existing interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1625
        SOME (intr, model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1626
    | NONE =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1627
        (case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1628
          Const (_, T) => interpret_groundterm T
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1629
        | Free (_, T) => interpret_groundterm T
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1630
        | Var (_, T) => interpret_groundterm T
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1631
        | Bound i => SOME (List.nth (#bounds args, i), model, args)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1632
        | Abs (x, T, body) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1633
            let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1634
              (* create all constants of type 'T' *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1635
              val constants = make_constants ctxt model T
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1636
              (* interpret the 'body' separately for each constant *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1637
              val (bodies, (model', args')) = fold_map
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1638
                (fn c => fn (m, a) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1639
                  let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1640
                    (* add 'c' to 'bounds' *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1641
                    val (i', m', a') = interpret ctxt m {maxvars = #maxvars a,
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1642
                      def_eq = #def_eq a, next_idx = #next_idx a,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1643
                      bounds = (c :: #bounds a), wellformed = #wellformed a} body
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1644
                  in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1645
                    (* keep the new model m' and 'next_idx' and 'wellformed', *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1646
                    (* but use old 'bounds'                                   *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1647
                    (i', (m', {maxvars = maxvars, def_eq = def_eq,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1648
                      next_idx = #next_idx a', bounds = bounds,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1649
                      wellformed = #wellformed a'}))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1650
                  end)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1651
                constants (model, args)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1652
            in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1653
              SOME (Node bodies, model', args')
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1654
            end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1655
        | t1 $ t2 =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1656
            let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1657
              (* interpret 't1' and 't2' separately *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1658
              val (intr1, model1, args1) = interpret ctxt model args t1
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1659
              val (intr2, model2, args2) = interpret ctxt model1 args1 t2
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1660
            in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1661
              SOME (interpretation_apply (intr1, intr2), model2, args2)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1662
            end)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1663
  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
  1664
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1665
fun Pure_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1666
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1667
    Const (@{const_name all}, _) $ t1 =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1668
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1669
        val (i, m, a) = interpret ctxt model args t1
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1670
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1671
        case i of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1672
          Node xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1673
            (* 3-valued logic *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1674
            let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1675
              val fmTrue  = PropLogic.all (map toTrue xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1676
              val fmFalse = PropLogic.exists (map toFalse xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1677
            in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1678
              SOME (Leaf [fmTrue, fmFalse], m, a)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1679
            end
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1680
        | _ =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1681
          raise REFUTE ("Pure_interpreter",
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1682
            "\"all\" is followed by a non-function")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1683
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1684
  | Const (@{const_name all}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1685
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1686
  | Const (@{const_name "=="}, _) $ t1 $ t2 =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1687
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1688
        val (i1, m1, a1) = interpret ctxt model args t1
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1689
        val (i2, m2, a2) = interpret ctxt m1 a1 t2
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1690
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1691
        (* we use either 'make_def_equality' or 'make_equality' *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1692
        SOME ((if #def_eq args then make_def_equality else make_equality)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1693
          (i1, i2), m2, a2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1694
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1695
  | Const (@{const_name "=="}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1696
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1697
  | Const (@{const_name "=="}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1698
      SOME (interpret ctxt model args (eta_expand t 2))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1699
  | Const (@{const_name "==>"}, _) $ t1 $ t2 =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1700
      (* 3-valued logic *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1701
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1702
        val (i1, m1, a1) = interpret ctxt model args t1
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1703
        val (i2, m2, a2) = interpret ctxt m1 a1 t2
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1704
        val fmTrue       = PropLogic.SOr (toFalse i1, toTrue i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1705
        val fmFalse      = PropLogic.SAnd (toTrue i1, toFalse i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1706
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1707
        SOME (Leaf [fmTrue, fmFalse], m2, a2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1708
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1709
  | Const (@{const_name "==>"}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1710
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1711
  | Const (@{const_name "==>"}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1712
      SOME (interpret ctxt model args (eta_expand t 2))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1713
  | _ => NONE;
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
  1714
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1715
fun HOLogic_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1716
(* Providing interpretations directly is more efficient than unfolding the *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1717
(* logical constants.  In HOL however, logical constants can themselves be *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1718
(* arguments.  They are then translated using eta-expansion.               *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1719
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1720
    Const (@{const_name Trueprop}, _) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1721
      SOME (Node [TT, FF], model, args)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1722
  | Const (@{const_name Not}, _) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1723
      SOME (Node [FF, TT], model, args)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1724
  (* redundant, since 'True' is also an IDT constructor *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1725
  | Const (@{const_name True}, _) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1726
      SOME (TT, model, args)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1727
  (* redundant, since 'False' is also an IDT constructor *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1728
  | Const (@{const_name False}, _) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1729
      SOME (FF, model, args)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1730
  | Const (@{const_name All}, _) $ t1 =>  (* similar to "all" (Pure) *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1731
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1732
        val (i, m, a) = interpret ctxt model args t1
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1733
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1734
        case i of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1735
          Node xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1736
            (* 3-valued logic *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1737
            let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1738
              val fmTrue  = PropLogic.all (map toTrue xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1739
              val fmFalse = PropLogic.exists (map toFalse xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1740
            in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1741
              SOME (Leaf [fmTrue, fmFalse], m, a)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1742
            end
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1743
        | _ =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1744
          raise REFUTE ("HOLogic_interpreter",
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1745
            "\"All\" is followed by a non-function")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1746
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1747
  | Const (@{const_name All}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1748
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1749
  | Const (@{const_name Ex}, _) $ t1 =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1750
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1751
        val (i, m, a) = interpret ctxt model args t1
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1752
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1753
        case i of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1754
          Node xs =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1755
            (* 3-valued logic *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1756
            let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1757
              val fmTrue  = PropLogic.exists (map toTrue xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1758
              val fmFalse = PropLogic.all (map toFalse xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1759
            in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1760
              SOME (Leaf [fmTrue, fmFalse], m, a)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1761
            end
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1762
        | _ =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1763
          raise REFUTE ("HOLogic_interpreter",
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1764
            "\"Ex\" is followed by a non-function")
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1765
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1766
  | Const (@{const_name Ex}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1767
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1768
  | Const (@{const_name HOL.eq}, _) $ t1 $ t2 =>  (* similar to "==" (Pure) *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1769
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1770
        val (i1, m1, a1) = interpret ctxt model args t1
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1771
        val (i2, m2, a2) = interpret ctxt m1 a1 t2
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1772
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1773
        SOME (make_equality (i1, i2), m2, a2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1774
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1775
  | Const (@{const_name HOL.eq}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1776
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1777
  | Const (@{const_name HOL.eq}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1778
      SOME (interpret ctxt model args (eta_expand t 2))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1779
  | Const (@{const_name HOL.conj}, _) $ t1 $ t2 =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1780
      (* 3-valued logic *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1781
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1782
        val (i1, m1, a1) = interpret ctxt model args t1
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1783
        val (i2, m2, a2) = interpret ctxt m1 a1 t2
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1784
        val fmTrue       = PropLogic.SAnd (toTrue i1, toTrue i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1785
        val fmFalse      = PropLogic.SOr (toFalse i1, toFalse i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1786
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1787
        SOME (Leaf [fmTrue, fmFalse], m2, a2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1788
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1789
  | Const (@{const_name HOL.conj}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1790
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1791
  | Const (@{const_name HOL.conj}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1792
      SOME (interpret ctxt model args (eta_expand t 2))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1793
      (* this would make "undef" propagate, even for formulae like *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1794
      (* "False & undef":                                          *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1795
      (* SOME (Node [Node [TT, FF], Node [FF, FF]], model, args) *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1796
  | Const (@{const_name HOL.disj}, _) $ t1 $ t2 =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1797
      (* 3-valued logic *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1798
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1799
        val (i1, m1, a1) = interpret ctxt model args t1
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1800
        val (i2, m2, a2) = interpret ctxt m1 a1 t2
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1801
        val fmTrue       = PropLogic.SOr (toTrue i1, toTrue i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1802
        val fmFalse      = PropLogic.SAnd (toFalse i1, toFalse i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1803
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1804
        SOME (Leaf [fmTrue, fmFalse], m2, a2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1805
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1806
  | Const (@{const_name HOL.disj}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1807
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1808
  | Const (@{const_name HOL.disj}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1809
      SOME (interpret ctxt model args (eta_expand t 2))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1810
      (* this would make "undef" propagate, even for formulae like *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1811
      (* "True | undef":                                           *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1812
      (* SOME (Node [Node [TT, TT], Node [TT, FF]], model, args) *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1813
  | Const (@{const_name HOL.implies}, _) $ t1 $ t2 =>  (* similar to "==>" (Pure) *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1814
      (* 3-valued logic *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1815
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1816
        val (i1, m1, a1) = interpret ctxt model args t1
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1817
        val (i2, m2, a2) = interpret ctxt m1 a1 t2
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1818
        val fmTrue       = PropLogic.SOr (toFalse i1, toTrue i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1819
        val fmFalse      = PropLogic.SAnd (toTrue i1, toFalse i2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1820
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1821
        SOME (Leaf [fmTrue, fmFalse], m2, a2)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1822
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1823
  | Const (@{const_name HOL.implies}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1824
      SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1825
  | Const (@{const_name HOL.implies}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1826
      SOME (interpret ctxt model args (eta_expand t 2))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1827
      (* this would make "undef" propagate, even for formulae like *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1828
      (* "False --> undef":                                        *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1829
      (* SOME (Node [Node [TT, FF], Node [TT, TT]], model, args) *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1830
  | _ => NONE;
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
  1831
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1832
(* interprets variables and constants whose type is an IDT (this is        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1833
(* relatively easy and merely requires us to compute the size of the IDT); *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1834
(* constructors of IDTs however are properly interpreted by                *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1835
(* 'IDT_constructor_interpreter'                                           *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1836
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1837
fun IDT_interpreter ctxt model args t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1838
  let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1839
    val thy = ProofContext.theory_of ctxt
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1840
    val (typs, terms) = model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1841
    (* Term.typ -> (interpretation * model * arguments) option *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1842
    fun interpret_term (Type (s, Ts)) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1843
          (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
  1844
            SOME info =>  (* inductive datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1845
              let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1846
                (* int option -- only recursive IDTs have an associated depth *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1847
                val depth = AList.lookup (op =) typs (Type (s, Ts))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1848
                (* sanity check: depth must be at least 0 *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1849
                val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1850
                  (case depth of SOME n =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1851
                    if n < 0 then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1852
                      raise REFUTE ("IDT_interpreter", "negative depth")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1853
                    else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1854
                  | _ => ())
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1855
              in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1856
                (* termination condition to avoid infinite recursion *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1857
                if depth = (SOME 0) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1858
                  (* return a leaf of size 0 *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1859
                  SOME (Leaf [], model, args)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1860
                else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1861
                  let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1862
                    val index               = #index info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1863
                    val descr               = #descr info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1864
                    val (_, dtyps, 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
  1865
                    val typ_assoc           = dtyps ~~ Ts
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1866
                    (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1867
                    val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1868
                      if Library.exists (fn d =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1869
                        case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1870
                      then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1871
                        raise REFUTE ("IDT_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1872
                          "datatype argument (for type "
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1873
                          ^ Syntax.string_of_typ ctxt (Type (s, Ts))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1874
                          ^ ") is not a variable")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1875
                      else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1876
                    (* if the model specifies a depth for the current type, *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1877
                    (* decrement it to avoid infinite recursion             *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1878
                    val typs' = case depth of NONE => typs | SOME n =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1879
                      AList.update (op =) (Type (s, Ts), n-1) typs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1880
                    (* recursively compute the size of the datatype *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1881
                    val size     = size_of_dtyp ctxt typs' descr typ_assoc constrs
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1882
                    val next_idx = #next_idx args
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1883
                    val next     = next_idx+size
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1884
                    (* check if 'maxvars' is large enough *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1885
                    val _        = (if next-1 > #maxvars args andalso
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1886
                      #maxvars args > 0 then raise MAXVARS_EXCEEDED else ())
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1887
                    (* prop_formula list *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1888
                    val fms      = map BoolVar (next_idx upto (next_idx+size-1))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1889
                    (* interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1890
                    val intr     = Leaf fms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1891
                    (* prop_formula list -> prop_formula *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1892
                    fun one_of_two_false [] = True
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1893
                      | one_of_two_false (x::xs) = SAnd (PropLogic.all (map (fn x' =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1894
                          SOr (SNot x, SNot x')) xs), one_of_two_false xs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1895
                    (* prop_formula *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1896
                    val wf = one_of_two_false fms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1897
                  in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1898
                    (* extend the model, increase 'next_idx', add well-formedness *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1899
                    (* condition                                                  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1900
                    SOME (intr, (typs, (t, intr)::terms), {maxvars = #maxvars args,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1901
                      def_eq = #def_eq args, next_idx = next, bounds = #bounds args,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1902
                      wellformed = SAnd (#wellformed args, wf)})
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1903
                  end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1904
              end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1905
          | NONE =>  (* not an inductive datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1906
              NONE)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1907
      | interpret_term _ =  (* a (free or schematic) type variable *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1908
          NONE
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1909
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1910
    case AList.lookup (op =) terms t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1911
      SOME intr =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1912
        (* return an existing interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1913
        SOME (intr, model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1914
    | NONE =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1915
        (case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1916
          Free (_, T) => interpret_term T
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1917
        | Var (_, T) => interpret_term T
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1918
        | Const (_, T) => interpret_term T
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1919
        | _ => NONE)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1920
  end;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  1921
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1922
(* This function imposes an order on the elements of a datatype fragment  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1923
(* as follows: C_i x_1 ... x_n < C_j y_1 ... y_m iff i < j or             *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1924
(* (x_1, ..., x_n) < (y_1, ..., y_m).  With this order, a constructor is  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1925
(* a function C_i that maps some argument indices x_1, ..., x_n to the    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1926
(* datatype element given by index C_i x_1 ... x_n.  The idea remains the *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1927
(* same for recursive datatypes, although the computation of indices gets *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1928
(* a little tricky.                                                       *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1929
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1930
fun IDT_constructor_interpreter ctxt model args t =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  1931
  let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1932
    val thy = ProofContext.theory_of ctxt
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1933
    (* returns a list of canonical representations for terms of the type 'T' *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1934
    (* It would be nice if we could just use 'print' for this, but 'print'   *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1935
    (* for IDTs calls 'IDT_constructor_interpreter' again, and this could    *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1936
    (* lead to infinite recursion when we have (mutually) recursive IDTs.    *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1937
    (* (Term.typ * int) list -> Term.typ -> Term.term list *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1938
    fun canonical_terms typs T =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1939
          (case T of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1940
            Type ("fun", [T1, T2]) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1941
            (* 'T2' might contain a recursive IDT, so we cannot use 'print' (at *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1942
            (* least not for 'T2'                                               *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1943
            let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1944
              (* returns a list of lists, each one consisting of n (possibly *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1945
              (* identical) elements from 'xs'                               *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1946
              (* int -> 'a list -> 'a list list *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1947
              fun pick_all 1 xs = map single xs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1948
                | pick_all n xs =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1949
                    let val rec_pick = pick_all (n-1) xs in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1950
                      maps (fn x => map (cons x) rec_pick) xs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1951
                    end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1952
              (* ["x1", ..., "xn"] *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1953
              val terms1 = canonical_terms typs T1
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1954
              (* ["y1", ..., "ym"] *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1955
              val terms2 = canonical_terms typs T2
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1956
              (* [[("x1", "y1"), ..., ("xn", "y1")], ..., *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1957
              (*   [("x1", "ym"), ..., ("xn", "ym")]]     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1958
              val functions = map (curry (op ~~) terms1)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1959
                (pick_all (length terms1) terms2)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1960
              (* [["(x1, y1)", ..., "(xn, y1)"], ..., *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1961
              (*   ["(x1, ym)", ..., "(xn, ym)"]]     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1962
              val pairss = map (map HOLogic.mk_prod) functions
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1963
              (* Term.typ *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1964
              val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1965
              val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1966
              (* Term.term *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1967
              val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1968
              val HOLogic_insert    =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1969
                Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1970
            in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1971
              (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1972
              map (fn ps => fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) ps
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1973
                HOLogic_empty_set) pairss
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1974
            end
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1975
      | Type (s, Ts) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1976
          (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
  1977
            SOME info =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1978
              (case AList.lookup (op =) typs T of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1979
                SOME 0 =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1980
                  (* termination condition to avoid infinite recursion *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1981
                  []  (* at depth 0, every IDT is empty *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1982
              | _ =>
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  1983
                let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1984
                  val index = #index info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1985
                  val descr = #descr info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1986
                  val (_, dtyps, 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
  1987
                  val typ_assoc = dtyps ~~ Ts
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1988
                  (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1989
                  val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1990
                    if Library.exists (fn d =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1991
                      case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1992
                    then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1993
                      raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1994
                        "datatype argument (for type "
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  1995
                        ^ 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
  1996
                        ^ ") is not a variable")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1997
                    else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1998
                  (* decrement depth for the IDT 'T' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  1999
                  val typs' =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2000
                    (case AList.lookup (op =) typs T of NONE => typs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2001
                    | SOME n => AList.update (op =) (T, n-1) typs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2002
                  fun constructor_terms terms [] = terms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2003
                    | constructor_terms terms (d::ds) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2004
                        let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2005
                          val dT = typ_of_dtyp descr typ_assoc d
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2006
                          val d_terms = canonical_terms typs' dT
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2007
                        in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2008
                          (* C_i x_1 ... x_n < C_i y_1 ... y_n if *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2009
                          (* (x_1, ..., x_n) < (y_1, ..., y_n)    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2010
                          constructor_terms
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2011
                            (map_product (curry op $) terms d_terms) ds
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2012
                        end
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2013
                in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2014
                  (* C_i ... < C_j ... if i < j *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2015
                  maps (fn (cname, ctyps) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2016
                    let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2017
                      val cTerm = Const (cname,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2018
                        map (typ_of_dtyp descr typ_assoc) ctyps ---> T)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2019
                    in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2020
                      constructor_terms [cTerm] ctyps
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2021
                    end) constrs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2022
                end)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2023
          | NONE =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2024
              (* not an inductive datatype; in this case the argument types in *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2025
              (* 'Ts' may not be IDTs either, so 'print' should be safe        *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2026
              map (fn intr => print ctxt (typs, []) T intr (K false))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2027
                (make_constants ctxt (typs, []) T))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2028
      | _ =>  (* TFree ..., TVar ... *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2029
          map (fn intr => print ctxt (typs, []) T intr (K false))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2030
            (make_constants ctxt (typs, []) T))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2031
    val (typs, terms) = model
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2032
  in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2033
    case AList.lookup (op =) terms t of
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2034
      SOME intr =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2035
        (* return an existing interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2036
        SOME (intr, model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2037
    | NONE =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2038
        (case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2039
          Const (s, T) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2040
            (case body_type T of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2041
              Type (s', Ts') =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2042
                (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
  2043
                  SOME info =>  (* body type is an inductive datatype *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2044
                    let
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2045
                      val index               = #index info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2046
                      val descr               = #descr info
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2047
                      val (_, dtyps, 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
  2048
                      val typ_assoc           = dtyps ~~ Ts'
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2049
                      (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2050
                      val _ = if Library.exists (fn d =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2051
                          case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2052
                        then
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2053
                          raise REFUTE ("IDT_constructor_interpreter",
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2054
                            "datatype argument (for type "
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2055
                            ^ Syntax.string_of_typ ctxt (Type (s', Ts'))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2056
                            ^ ") is not a variable")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2057
                        else ()
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2058
                      (* split the constructors into those occuring before/after *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2059
                      (* 'Const (s, T)'                                          *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2060
                      val (constrs1, constrs2) = take_prefix (fn (cname, ctypes) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2061
                        not (cname = s andalso Sign.typ_instance thy (T,
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2062
                          map (typ_of_dtyp descr typ_assoc) ctypes
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2063
                            ---> Type (s', Ts')))) constrs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2064
                    in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2065
                      case constrs2 of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2066
                        [] =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2067
                          (* 'Const (s, T)' is not a constructor of this datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2068
                          NONE
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2069
                      | (_, ctypes)::cs =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2070
                          let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2071
                            (* int option -- only /recursive/ IDTs have an associated *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2072
                            (*               depth                                    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2073
                            val depth = AList.lookup (op =) typs (Type (s', Ts'))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2074
                            (* this should never happen: at depth 0, this IDT fragment *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2075
                            (* is definitely empty, and in this case we don't need to  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2076
                            (* interpret its constructors                              *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2077
                            val _ = (case depth of SOME 0 =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2078
                                raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2079
                                  "depth is 0")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2080
                              | _ => ())
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2081
                            val typs' = (case depth of NONE => typs | SOME n =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2082
                              AList.update (op =) (Type (s', Ts'), n-1) typs)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2083
                            (* elements of the datatype come before elements generated *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2084
                            (* by 'Const (s, T)' iff they are generated by a           *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2085
                            (* constructor in constrs1                                 *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2086
                            val offset = size_of_dtyp ctxt typs' descr typ_assoc constrs1
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2087
                            (* compute the total (current) size of the datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2088
                            val total = offset +
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2089
                              size_of_dtyp ctxt typs' descr typ_assoc constrs2
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2090
                            (* sanity check *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2091
                            val _ = if total <> size_of_type ctxt (typs, [])
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2092
                              (Type (s', Ts')) then
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2093
                                raise REFUTE ("IDT_constructor_interpreter",
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2094
                                  "total is not equal to current size")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2095
                              else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2096
                            (* returns an interpretation where everything is mapped to *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2097
                            (* an "undefined" element of the datatype                  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2098
                            fun make_undef [] = Leaf (replicate total False)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2099
                              | make_undef (d::ds) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2100
                                  let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2101
                                    (* compute the current size of the type 'd' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2102
                                    val dT   = typ_of_dtyp descr typ_assoc d
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2103
                                    val size = size_of_type ctxt (typs, []) dT
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2104
                                  in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2105
                                    Node (replicate size (make_undef ds))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2106
                                  end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2107
                            (* returns the interpretation for a constructor *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2108
                            fun make_constr [] offset =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2109
                                  if offset < total then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2110
                                    (Leaf (replicate offset False @ True ::
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2111
                                      (replicate (total - offset - 1) False)), offset + 1)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2112
                                  else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2113
                                    raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2114
                                      "offset >= total")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2115
                              | make_constr (d::ds) offset =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2116
                                  let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2117
                                    (* Term.typ *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2118
                                    val dT = typ_of_dtyp descr typ_assoc d
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2119
                                    (* compute canonical term representations for all   *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2120
                                    (* elements of the type 'd' (with the reduced depth *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2121
                                    (* for the IDT)                                     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2122
                                    val terms' = canonical_terms typs' dT
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2123
                                    (* sanity check *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2124
                                    val _ =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2125
                                      if length terms' <> size_of_type ctxt (typs', []) dT
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2126
                                      then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2127
                                        raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2128
                                          "length of terms' is not equal to old size")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2129
                                      else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2130
                                    (* compute canonical term representations for all   *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2131
                                    (* elements of the type 'd' (with the current depth *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2132
                                    (* for the IDT)                                     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2133
                                    val terms = canonical_terms typs dT
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2134
                                    (* sanity check *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2135
                                    val _ =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2136
                                      if length terms <> size_of_type ctxt (typs, []) dT
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2137
                                      then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2138
                                        raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2139
                                          "length of terms is not equal to current size")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2140
                                      else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2141
                                    (* sanity check *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2142
                                    val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2143
                                      if length terms < length terms' then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2144
                                        raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2145
                                          "current size is less than old size")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2146
                                      else ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2147
                                    (* sanity check: every element of terms' must also be *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2148
                                    (*               present in terms                     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2149
                                    val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2150
                                      if forall (member (op =) terms) terms' then ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2151
                                      else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2152
                                        raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2153
                                          "element has disappeared")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2154
                                    (* sanity check: the order on elements of terms' is    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2155
                                    (*               the same in terms, for those elements *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2156
                                    val _ =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2157
                                      let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2158
                                        fun search (x::xs) (y::ys) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2159
                                              if x = y then search xs ys else search (x::xs) ys
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2160
                                          | search (x::xs) [] =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2161
                                              raise REFUTE ("IDT_constructor_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2162
                                                "element order not preserved")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2163
                                          | search [] _ = ()
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2164
                                      in search terms' terms end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2165
                                    (* int * interpretation list *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2166
                                    val (intrs, new_offset) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2167
                                      fold_map (fn t_elem => fn off =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2168
                                        (* if 't_elem' existed at the previous depth,    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2169
                                        (* proceed recursively, otherwise map the entire *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2170
                                        (* subtree to "undefined"                        *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2171
                                        if member (op =) terms' t_elem then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2172
                                          make_constr ds off
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2173
                                        else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2174
                                          (make_undef ds, off))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2175
                                      terms offset
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2176
                                  in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2177
                                    (Node intrs, new_offset)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2178
                                  end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2179
                          in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2180
                            SOME (fst (make_constr ctypes offset), model, args)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2181
                          end
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2182
                    end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2183
                | NONE =>  (* body type is not an inductive datatype *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2184
                    NONE)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2185
            | _ =>  (* body type is a (free or schematic) type variable *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2186
              NONE)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2187
        | _ =>  (* term is not a constant *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2188
          NONE)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2189
  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
  2190
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2191
(* Difficult code ahead.  Make sure you understand the                *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2192
(* 'IDT_constructor_interpreter' and the order in which it enumerates *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2193
(* elements of an IDT before you try to understand this function.     *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2194
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2195
fun IDT_recursion_interpreter ctxt model args t =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2196
  let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2197
    val thy = ProofContext.theory_of ctxt
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2198
  in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2199
    (* careful: here we descend arbitrarily deep into 't', possibly before *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2200
    (* any other interpreter for atomic terms has had a chance to look at  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2201
    (* 't'                                                                 *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2202
    case strip_comb t of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2203
      (Const (s, T), params) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2204
        (* iterate over all datatypes in 'thy' *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2205
        Symtab.fold (fn (_, info) => fn result =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2206
          case result of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2207
            SOME _ =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2208
              result  (* just keep 'result' *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2209
          | NONE =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2210
              if member (op =) (#rec_names info) s then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2211
                (* we do have a recursion operator of one of the (mutually *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2212
                (* recursive) datatypes given by 'info'                    *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2213
                let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2214
                  (* number of all constructors, including those of different  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2215
                  (* (mutually recursive) datatypes within the same descriptor *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2216
                  val mconstrs_count =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2217
                    Integer.sum (map (fn (_, (_, _, cs)) => length cs) (#descr info))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2218
                in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2219
                  if mconstrs_count < length params then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2220
                    (* too many actual parameters; for now we'll use the *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2221
                    (* 'stlc_interpreter' to strip off one application   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2222
                    NONE
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2223
                  else if mconstrs_count > length params then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2224
                    (* too few actual parameters; we use eta expansion          *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2225
                    (* Note that the resulting expansion of lambda abstractions *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2226
                    (* by the 'stlc_interpreter' may be rather slow (depending  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2227
                    (* on the argument types and the size of the IDT, of        *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2228
                    (* course).                                                 *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2229
                    SOME (interpret ctxt model args (eta_expand t
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2230
                      (mconstrs_count - length params)))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2231
                  else  (* mconstrs_count = length params *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2232
                    let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2233
                      (* interpret each parameter separately *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2234
                      val (p_intrs, (model', args')) = fold_map (fn p => fn (m, a) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2235
                        let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2236
                          val (i, m', a') = interpret ctxt m a p
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2237
                        in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2238
                          (i, (m', a'))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2239
                        end) params (model, args)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2240
                      val (typs, _) = model'
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2241
                      (* 'index' is /not/ necessarily the index of the IDT that *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2242
                      (* the recursion operator is associated with, but merely  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2243
                      (* the index of some mutually recursive IDT               *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2244
                      val index         = #index info
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2245
                      val descr         = #descr info
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2246
                      val (_, dtyps, _) = the (AList.lookup (op =) descr index)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2247
                      (* sanity check: we assume that the order of constructors *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2248
                      (*               in 'descr' is the same as the order of   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2249
                      (*               corresponding parameters, otherwise the  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2250
                      (*               association code below won't match the   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2251
                      (*               right constructors/parameters; we also   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2252
                      (*               assume that the order of recursion       *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2253
                      (*               operators in '#rec_names info' is the    *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2254
                      (*               same as the order of corresponding       *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2255
                      (*               datatypes in 'descr'                     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2256
                      val _ = if map fst descr <> (0 upto (length descr - 1)) then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2257
                          raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2258
                            "order of constructors and corresponding parameters/" ^
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2259
                              "recursion operators and corresponding datatypes " ^
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2260
                              "different?")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2261
                        else ()
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2262
                      (* sanity check: every element in 'dtyps' must be a *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2263
                      (*               'DtTFree'                          *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2264
                      val _ =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2265
                        if Library.exists (fn d =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2266
                          case d of Datatype_Aux.DtTFree _ => false
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2267
                                  | _ => true) dtyps
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2268
                        then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2269
                          raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2270
                            "datatype argument is not a variable")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2271
                        else ()
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2272
                      (* the type of a recursion operator is *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2273
                      (* [T1, ..., Tn, IDT] ---> Tresult     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2274
                      val IDT = List.nth (binder_types T, mconstrs_count)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2275
                      (* by our assumption on the order of recursion operators *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2276
                      (* and datatypes, this is the index of the datatype      *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2277
                      (* corresponding to the given recursion operator         *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2278
                      val idt_index = find_index (fn s' => s' = s) (#rec_names info)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2279
                      (* mutually recursive types must have the same type   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2280
                      (* parameters, unless the mutual recursion comes from *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2281
                      (* indirect recursion                                 *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2282
                      fun rec_typ_assoc acc [] = acc
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2283
                        | rec_typ_assoc acc ((d, T)::xs) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2284
                            (case AList.lookup op= acc d of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2285
                              NONE =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2286
                                (case d of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2287
                                  Datatype_Aux.DtTFree _ =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2288
                                  (* add the association, proceed *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2289
                                  rec_typ_assoc ((d, T)::acc) xs
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2290
                                | Datatype_Aux.DtType (s, ds) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2291
                                    let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2292
                                      val (s', Ts) = dest_Type T
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2293
                                    in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2294
                                      if s=s' then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2295
                                        rec_typ_assoc ((d, T)::acc) ((ds ~~ Ts) @ xs)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2296
                                      else
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2297
                                        raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2298
                                          "DtType/Type mismatch")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2299
                                    end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2300
                                | Datatype_Aux.DtRec i =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2301
                                    let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2302
                                      val (_, ds, _) = the (AList.lookup (op =) descr i)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2303
                                      val (_, Ts)    = dest_Type T
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2304
                                    in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2305
                                      rec_typ_assoc ((d, T)::acc) ((ds ~~ Ts) @ xs)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2306
                                    end)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2307
                            | SOME T' =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2308
                                if T=T' then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2309
                                  (* ignore the association since it's already *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2310
                                  (* present, proceed                          *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2311
                                  rec_typ_assoc acc xs
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2312
                                else
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2313
                                  raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2314
                                    "different type associations for the same dtyp"))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2315
                      val typ_assoc = filter
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2316
                        (fn (Datatype_Aux.DtTFree _, _) => true | (_, _) => false)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2317
                        (rec_typ_assoc []
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2318
                          (#2 (the (AList.lookup (op =) descr idt_index)) ~~ (snd o dest_Type) IDT))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2319
                      (* sanity check: typ_assoc must associate types to the   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2320
                      (*               elements of 'dtyps' (and only to those) *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2321
                      val _ =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2322
                        if not (eq_set (op =) (dtyps, map fst typ_assoc))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2323
                        then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2324
                          raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2325
                            "type association has extra/missing elements")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2326
                        else ()
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2327
                      (* interpret each constructor in the descriptor (including *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2328
                      (* those of mutually recursive datatypes)                  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2329
                      (* (int * interpretation list) list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2330
                      val mc_intrs = map (fn (idx, (_, _, cs)) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2331
                        let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2332
                          val c_return_typ = typ_of_dtyp descr typ_assoc
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2333
                            (Datatype_Aux.DtRec idx)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2334
                        in
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2335
                          (idx, map (fn (cname, cargs) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2336
                            (#1 o interpret ctxt (typs, []) {maxvars=0,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2337
                              def_eq=false, next_idx=1, bounds=[],
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2338
                              wellformed=True}) (Const (cname, map (typ_of_dtyp
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2339
                              descr typ_assoc) cargs ---> c_return_typ))) cs)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2340
                        end) descr
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2341
                      (* associate constructors with corresponding parameters *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2342
                      (* (int * (interpretation * interpretation) list) list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2343
                      val (mc_p_intrs, p_intrs') = fold_map
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2344
                        (fn (idx, c_intrs) => fn p_intrs' =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2345
                          let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2346
                            val len = length c_intrs
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2347
                          in
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2348
                            ((idx, c_intrs ~~ List.take (p_intrs', len)),
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2349
                              List.drop (p_intrs', len))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2350
                          end) mc_intrs p_intrs
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2351
                      (* sanity check: no 'p_intr' may be left afterwards *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2352
                      val _ =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2353
                        if p_intrs' <> [] then
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2354
                          raise REFUTE ("IDT_recursion_interpreter",
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2355
                            "more parameter than constructor interpretations")
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2356
                        else ()
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2357
                      (* The recursion operator, applied to 'mconstrs_count'     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2358
                      (* arguments, is a function that maps every element of the *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2359
                      (* inductive datatype to an element of some result type.   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2360
                      (* Recursion operators for mutually recursive IDTs are     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2361
                      (* translated simultaneously.                              *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2362
                      (* Since the order on datatype elements is given by an     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2363
                      (* order on constructors (and then by the order on         *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2364
                      (* argument tuples), we can simply copy corresponding      *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2365
                      (* subtrees from 'p_intrs', in the order in which they are *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2366
                      (* given.                                                  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2367
                      (* interpretation * interpretation -> interpretation list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2368
                      fun ci_pi (Leaf xs, pi) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2369
                            (* if the constructor does not match the arguments to a *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2370
                            (* defined element of the IDT, the corresponding value  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2371
                            (* of the parameter must be ignored                     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2372
                            if List.exists (equal True) xs then [pi] else []
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2373
                        | ci_pi (Node xs, Node ys) = maps ci_pi (xs ~~ ys)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2374
                        | ci_pi (Node _, Leaf _) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2375
                            raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2376
                              "constructor takes more arguments than the " ^
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2377
                                "associated parameter")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2378
                      (* (int * interpretation list) list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2379
                      val rec_operators = map (fn (idx, c_p_intrs) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2380
                        (idx, maps ci_pi c_p_intrs)) mc_p_intrs
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2381
                      (* sanity check: every recursion operator must provide as  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2382
                      (*               many values as the corresponding datatype *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2383
                      (*               has elements                              *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2384
                      val _ = map (fn (idx, intrs) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2385
                        let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2386
                          val T = typ_of_dtyp descr typ_assoc
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2387
                            (Datatype_Aux.DtRec idx)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2388
                        in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2389
                          if length intrs <> size_of_type ctxt (typs, []) T then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2390
                            raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2391
                              "wrong number of interpretations for rec. operator")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2392
                          else ()
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2393
                        end) rec_operators
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2394
                      (* For non-recursive datatypes, we are pretty much done at *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2395
                      (* this point.  For recursive datatypes however, we still  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2396
                      (* need to apply the interpretations in 'rec_operators' to *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2397
                      (* (recursively obtained) interpretations for recursive    *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2398
                      (* constructor arguments.  To do so more efficiently, we   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2399
                      (* copy 'rec_operators' into arrays first.  Each Boolean   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2400
                      (* indicates whether the recursive arguments have been     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2401
                      (* considered already.                                     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2402
                      (* (int * (bool * interpretation) Array.array) list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2403
                      val REC_OPERATORS = map (fn (idx, intrs) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2404
                        (idx, Array.fromList (map (pair false) intrs)))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2405
                        rec_operators
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2406
                      (* takes an interpretation, and if some leaf of this     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2407
                      (* interpretation is the 'elem'-th element of the type,  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2408
                      (* the indices of the arguments leading to this leaf are *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2409
                      (* returned                                              *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2410
                      (* interpretation -> int -> int list option *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2411
                      fun get_args (Leaf xs) elem =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2412
                            if find_index (fn x => x = True) xs = elem then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2413
                              SOME []
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2414
                            else
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2415
                              NONE
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2416
                        | get_args (Node xs) elem =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2417
                            let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2418
                              (* interpretation list * int -> int list option *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2419
                              fun search ([], _) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2420
                                NONE
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2421
                                | search (x::xs, n) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2422
                                (case get_args x elem of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2423
                                  SOME result => SOME (n::result)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2424
                                | NONE        => search (xs, n+1))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2425
                            in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2426
                              search (xs, 0)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2427
                            end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2428
                      (* returns the index of the constructor and indices for *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2429
                      (* its arguments that generate the 'elem'-th element of *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2430
                      (* the datatype given by 'idx'                          *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2431
                      (* int -> int -> int * int list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2432
                      fun get_cargs idx elem =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2433
                        let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2434
                          (* int * interpretation list -> int * int list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2435
                          fun get_cargs_rec (_, []) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2436
                                raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2437
                                  "no matching constructor found for datatype element")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2438
                            | get_cargs_rec (n, x::xs) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2439
                                (case get_args x elem of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2440
                                  SOME args => (n, args)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2441
                                | NONE => get_cargs_rec (n+1, xs))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2442
                        in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2443
                          get_cargs_rec (0, the (AList.lookup (op =) mc_intrs idx))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2444
                        end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2445
                      (* computes one entry in 'REC_OPERATORS', and recursively *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2446
                      (* all entries needed for it, where 'idx' gives the       *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2447
                      (* datatype and 'elem' the element of it                  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2448
                      (* int -> int -> interpretation *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2449
                      fun compute_array_entry idx elem =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2450
                        let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2451
                          val arr = the (AList.lookup (op =) REC_OPERATORS idx)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2452
                          val (flag, intr) = Array.sub (arr, elem)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2453
                        in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2454
                          if flag then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2455
                            (* simply return the previously computed result *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2456
                            intr
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2457
                          else
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2458
                            (* we have to apply 'intr' to interpretations for all *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2459
                            (* recursive arguments                                *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2460
                            let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2461
                              (* int * int list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2462
                              val (c, args) = get_cargs idx elem
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2463
                              (* find the indices of the constructor's /recursive/ *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2464
                              (* arguments                                         *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2465
                              val (_, _, constrs) = the (AList.lookup (op =) descr idx)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2466
                              val (_, dtyps)      = List.nth (constrs, c)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2467
                              val rec_dtyps_args  = filter
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2468
                                (Datatype_Aux.is_rec_type o fst) (dtyps ~~ args)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2469
                              (* map those indices to interpretations *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2470
                              val rec_dtyps_intrs = map (fn (dtyp, arg) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2471
                                let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2472
                                  val dT     = typ_of_dtyp descr typ_assoc dtyp
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2473
                                  val consts = make_constants ctxt (typs, []) dT
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2474
                                  val arg_i  = List.nth (consts, arg)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2475
                                in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2476
                                  (dtyp, arg_i)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2477
                                end) rec_dtyps_args
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2478
                              (* takes the dtyp and interpretation of an element, *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2479
                              (* and computes the interpretation for the          *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2480
                              (* corresponding recursive argument                 *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2481
                              fun rec_intr (Datatype_Aux.DtRec i) (Leaf xs) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2482
                                    (* recursive argument is "rec_i params elem" *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2483
                                    compute_array_entry i (find_index (fn x => x = True) xs)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2484
                                | rec_intr (Datatype_Aux.DtRec _) (Node _) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2485
                                    raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2486
                                      "interpretation for IDT is a node")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2487
                                | rec_intr (Datatype_Aux.DtType ("fun", [dt1, dt2])) (Node xs) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2488
                                    (* recursive argument is something like     *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2489
                                    (* "\<lambda>x::dt1. rec_? params (elem x)" *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2490
                                    Node (map (rec_intr dt2) xs)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2491
                                | rec_intr (Datatype_Aux.DtType ("fun", [_, _])) (Leaf _) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2492
                                    raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2493
                                      "interpretation for function dtyp is a leaf")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2494
                                | rec_intr _ _ =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2495
                                    (* admissibility ensures that every recursive type *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2496
                                    (* is of the form 'Dt_1 -> ... -> Dt_k ->          *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2497
                                    (* (DtRec i)'                                      *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2498
                                    raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2499
                                      "non-recursive codomain in recursive dtyp")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2500
                              (* obtain interpretations for recursive arguments *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2501
                              (* interpretation list *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2502
                              val arg_intrs = map (uncurry rec_intr) rec_dtyps_intrs
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2503
                              (* apply 'intr' to all recursive arguments *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2504
                              val result = fold (fn arg_i => fn i =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2505
                                interpretation_apply (i, arg_i)) arg_intrs intr
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2506
                              (* update 'REC_OPERATORS' *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2507
                              val _ = Array.update (arr, elem, (true, result))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2508
                            in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2509
                              result
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2510
                            end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2511
                        end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2512
                      val idt_size = Array.length (the (AList.lookup (op =) REC_OPERATORS idt_index))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2513
                      (* sanity check: the size of 'IDT' should be 'idt_size' *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2514
                      val _ =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2515
                          if idt_size <> size_of_type ctxt (typs, []) IDT then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2516
                            raise REFUTE ("IDT_recursion_interpreter",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2517
                              "unexpected size of IDT (wrong type associated?)")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2518
                          else ()
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2519
                      (* interpretation *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2520
                      val rec_op = Node (map_range (compute_array_entry idt_index) idt_size)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2521
                    in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2522
                      SOME (rec_op, model', args')
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2523
                    end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2524
                end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2525
              else
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2526
                NONE  (* not a recursion operator of this datatype *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2527
          ) (Datatype.get_all thy) NONE
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2528
    | _ =>  (* head of term is not a constant *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2529
      NONE
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2530
  end;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2531
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2532
fun set_interpreter ctxt model args t =
29956
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2533
  let
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2534
    val (typs, terms) = model
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2535
  in
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2536
    case AList.lookup (op =) terms t of
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2537
      SOME intr =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2538
        (* return an existing interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2539
        SOME (intr, model, args)
29956
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2540
    | NONE =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2541
        (case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2542
        (* 'Collect' == identity *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2543
          Const (@{const_name Collect}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2544
            SOME (interpret ctxt model args t1)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2545
        | Const (@{const_name Collect}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2546
            SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2547
        (* 'op :' == application *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2548
        | Const (@{const_name Set.member}, _) $ t1 $ t2 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2549
            SOME (interpret ctxt model args (t2 $ t1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2550
        | Const (@{const_name Set.member}, _) $ t1 =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2551
            SOME (interpret ctxt model args (eta_expand t 1))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2552
        | Const (@{const_name Set.member}, _) =>
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2553
            SOME (interpret ctxt model args (eta_expand t 2))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2554
        | _ => NONE)
29956
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2555
  end;
62f931b257b7 Reintroduce set_interpreter for Collect and op :.
blanchet
parents: 29820
diff changeset
  2556
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2557
(* only an optimization: 'card' could in principle be interpreted with *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2558
(* interpreters available already (using its definition), but the code *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2559
(* below is more efficient                                             *)
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
  2560
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2561
fun Finite_Set_card_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2562
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2563
    Const (@{const_name Finite_Set.card},
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2564
        Type ("fun", [Type ("fun", [T, @{typ bool}]), @{typ nat}])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2565
      let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2566
        (* interpretation -> int *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2567
        fun number_of_elements (Node xs) =
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2568
            fold (fn x => fn n =>
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2569
              if x = TT then
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2570
                n + 1
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2571
              else if x = FF then
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2572
                n
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2573
              else
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2574
                raise REFUTE ("Finite_Set_card_interpreter",
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2575
                  "interpretation for set type does not yield a Boolean"))
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2576
              xs 0
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2577
          | number_of_elements (Leaf _) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2578
              raise REFUTE ("Finite_Set_card_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2579
                "interpretation for set type is a leaf")
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2580
        val size_of_nat = size_of_type ctxt model (@{typ nat})
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2581
        (* takes an interpretation for a set and returns an interpretation *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2582
        (* for a 'nat' denoting the set's cardinality                      *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2583
        (* interpretation -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2584
        fun card i =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2585
          let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2586
            val n = number_of_elements i
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2587
          in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2588
            if n < size_of_nat then
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2589
              Leaf ((replicate n False) @ True ::
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2590
                (replicate (size_of_nat-n-1) False))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2591
            else
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2592
              Leaf (replicate size_of_nat False)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2593
          end
30312
0e0cb7ac0281 Fix remaining occurrences of "'a set" in Refute, by using "'a => bool" instead.
blanchet
parents: 30307
diff changeset
  2594
        val set_constants =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2595
          make_constants ctxt model (Type ("fun", [T, HOLogic.boolT]))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2596
      in
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2597
        SOME (Node (map card set_constants), model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2598
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2599
  | _ => NONE;
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
  2600
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2601
(* only an optimization: 'finite' could in principle be interpreted with  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2602
(* interpreters available already (using its definition), but the code    *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2603
(* below is more efficient                                                *)
22093
98e3e9f00192 interpreter for Finite_Set.finite added
webertj
parents: 22092
diff changeset
  2604
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2605
fun Finite_Set_finite_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2606
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2607
    Const (@{const_name Finite_Set.finite},
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2608
      Type ("fun", [Type ("fun", [T, @{typ bool}]),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2609
                    @{typ bool}])) $ _ =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2610
        (* we only consider finite models anyway, hence EVERY set is *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2611
        (* "finite"                                                  *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2612
        SOME (TT, model, args)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2613
  | Const (@{const_name Finite_Set.finite},
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2614
      Type ("fun", [Type ("fun", [T, @{typ bool}]),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2615
                    @{typ bool}])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2616
      let
30312
0e0cb7ac0281 Fix remaining occurrences of "'a set" in Refute, by using "'a => bool" instead.
blanchet
parents: 30307
diff changeset
  2617
        val size_of_set =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2618
          size_of_type ctxt model (Type ("fun", [T, HOLogic.boolT]))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2619
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2620
        (* we only consider finite models anyway, hence EVERY set is *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2621
        (* "finite"                                                  *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2622
        SOME (Node (replicate size_of_set TT), model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2623
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2624
  | _ => NONE;
22093
98e3e9f00192 interpreter for Finite_Set.finite added
webertj
parents: 22092
diff changeset
  2625
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2626
(* only an optimization: 'less' could in principle be interpreted with *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2627
(* interpreters available already (using its definition), but the code     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2628
(* below is more efficient                                                 *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2629
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2630
fun Nat_less_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2631
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2632
    Const (@{const_name Orderings.less}, Type ("fun", [@{typ nat},
38553
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 37677
diff changeset
  2633
        Type ("fun", [@{typ nat}, @{typ bool}])])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2634
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2635
        val size_of_nat = size_of_type ctxt model (@{typ nat})
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2636
        (* the 'n'-th nat is not less than the first 'n' nats, while it *)
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2637
        (* is less than the remaining 'size_of_nat - n' nats            *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2638
        (* int -> interpretation *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2639
        fun less n = Node ((replicate n FF) @ (replicate (size_of_nat - n) TT))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2640
      in
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2641
        SOME (Node (map less (1 upto size_of_nat)), model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2642
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2643
  | _ => NONE;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2644
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2645
(* only an optimization: 'plus' could in principle be interpreted with *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2646
(* interpreters available already (using its definition), but the code     *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2647
(* below is more efficient                                                 *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2648
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2649
fun Nat_plus_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2650
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2651
    Const (@{const_name Groups.plus}, Type ("fun", [@{typ nat},
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
  2652
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2653
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2654
        val size_of_nat = size_of_type ctxt model (@{typ nat})
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2655
        (* int -> int -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2656
        fun plus m n =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2657
          let
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2658
            val element = m + n
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2659
          in
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2660
            if element > size_of_nat - 1 then
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2661
              Leaf (replicate size_of_nat False)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2662
            else
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2663
              Leaf ((replicate element False) @ True ::
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2664
                (replicate (size_of_nat - element - 1) False))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2665
          end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2666
      in
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33055
diff changeset
  2667
        SOME (Node (map_range (fn m => Node (map_range (plus m) size_of_nat)) size_of_nat),
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33055
diff changeset
  2668
          model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2669
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2670
  | _ => NONE;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2671
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2672
(* only an optimization: 'minus' could in principle be interpreted *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2673
(* with interpreters available already (using its definition), but the *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2674
(* code below is more efficient                                        *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2675
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2676
fun Nat_minus_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2677
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2678
    Const (@{const_name Groups.minus}, Type ("fun", [@{typ nat},
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
  2679
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2680
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2681
        val size_of_nat = size_of_type ctxt model (@{typ nat})
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2682
        (* int -> int -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2683
        fun minus m n =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2684
          let
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2685
            val element = Int.max (m-n, 0)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2686
          in
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2687
            Leaf ((replicate element False) @ True ::
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2688
              (replicate (size_of_nat - element - 1) False))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2689
          end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2690
      in
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33055
diff changeset
  2691
        SOME (Node (map_range (fn m => Node (map_range (minus m) size_of_nat)) size_of_nat),
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33055
diff changeset
  2692
          model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2693
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2694
  | _ => NONE;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2695
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2696
(* only an optimization: 'times' could in principle be interpreted *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2697
(* with interpreters available already (using its definition), but the *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2698
(* code below is more efficient                                        *)
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2699
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2700
fun Nat_times_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2701
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2702
    Const (@{const_name Groups.times}, Type ("fun", [@{typ nat},
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 37254
diff changeset
  2703
        Type ("fun", [@{typ nat}, @{typ nat}])])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2704
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2705
        val size_of_nat = size_of_type ctxt model (@{typ nat})
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2706
        (* nat -> nat -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2707
        fun mult m n =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2708
          let
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2709
            val element = m * n
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2710
          in
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2711
            if element > size_of_nat - 1 then
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2712
              Leaf (replicate size_of_nat False)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2713
            else
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2714
              Leaf ((replicate element False) @ True ::
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2715
                (replicate (size_of_nat - element - 1) False))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2716
          end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2717
      in
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33055
diff changeset
  2718
        SOME (Node (map_range (fn m => Node (map_range (mult m) size_of_nat)) size_of_nat),
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33055
diff changeset
  2719
          model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2720
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2721
  | _ => NONE;
15547
f08e2d83681e major code change: refute can now handle recursion and axiomatic type classes; 3-valued logic with two kinds of equality; some bugfixes
webertj
parents: 15531
diff changeset
  2722
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2723
(* only an optimization: 'append' could in principle be interpreted with *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2724
(* interpreters available already (using its definition), but the code   *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2725
(* below is more efficient                                               *)
15767
8ed9fcc004fe support for recursion over mutually recursive IDTs
webertj
parents: 15611
diff changeset
  2726
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2727
fun List_append_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2728
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2729
    Const (@{const_name List.append}, Type ("fun", [Type ("List.list", [T]), Type ("fun",
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2730
        [Type ("List.list", [_]), Type ("List.list", [_])])])) =>
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2731
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2732
        val size_elem = size_of_type ctxt model T
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2733
        val size_list = size_of_type ctxt model (Type ("List.list", [T]))
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2734
        (* maximal length of lists; 0 if we only consider the empty list *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2735
        val list_length =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2736
          let
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2737
            (* int -> int -> int -> int *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2738
            fun list_length_acc len lists total =
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2739
              if lists = total then
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2740
                len
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2741
              else if lists < total then
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2742
                list_length_acc (len+1) (lists*size_elem) (total-lists)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2743
              else
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2744
                raise REFUTE ("List_append_interpreter",
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2745
                  "size_list not equal to 1 + size_elem + ... + " ^
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2746
                    "size_elem^len, for some len")
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2747
          in
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2748
            list_length_acc 0 1 size_list
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2749
          end
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2750
        val elements = 0 upto (size_list-1)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2751
        (* FIXME: there should be a nice formula, which computes the same as *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2752
        (*        the following, but without all this intermediate tree      *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2753
        (*        length/offset stuff                                        *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2754
        (* associate each list with its length and offset in a complete tree *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2755
        (* of width 'size_elem' and depth 'length_list' (with 'size_list'    *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2756
        (* nodes total)                                                      *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2757
        (* (int * (int * int)) list *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2758
        val (lenoff_lists, _) = fold_map (fn elem => fn (offsets, off) =>
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2759
          (* corresponds to a pre-order traversal of the tree *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2760
          let
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2761
            val len = length offsets
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2762
            (* associate the given element with len/off *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2763
            val assoc = (elem, (len, off))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2764
          in
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2765
            if len < list_length then
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2766
              (* go to first child node *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2767
              (assoc, (off :: offsets, off * size_elem))
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2768
            else if off mod size_elem < size_elem - 1 then
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2769
              (* go to next sibling node *)
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2770
              (assoc, (offsets, off + 1))
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2771
            else
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2772
              (* go back up the stack until we find a level where we can go *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2773
              (* to the next sibling node                                   *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2774
              let
39811
0659e84bdc5f chop_while replace drop_while and take_while
haftmann
parents: 39809
diff changeset
  2775
                val offsets' = snd (chop_while
0659e84bdc5f chop_while replace drop_while and take_while
haftmann
parents: 39809
diff changeset
  2776
                  (fn off' => off' mod size_elem = size_elem - 1) offsets)
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2777
              in
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2778
                case offsets' of
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2779
                  [] =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2780
                    (* we're at the last node in the tree; the next value *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2781
                    (* won't be used anyway                               *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2782
                    (assoc, ([], 0))
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2783
                | off'::offs' =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2784
                    (* go to next sibling node *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2785
                    (assoc, (offs', off' + 1))
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2786
              end
33246
46e47aa1558f eliminated some old folds;
wenzelm
parents: 33243
diff changeset
  2787
          end) elements ([], 0)
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2788
        (* we also need the reverse association (from length/offset to *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2789
        (* index)                                                      *)
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2790
        val lenoff'_lists = map Library.swap lenoff_lists
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2791
        (* returns the interpretation for "(list no. m) @ (list no. n)" *)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2792
        (* nat -> nat -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2793
        fun append m n =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2794
          let
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
  2795
            val (len_m, off_m) = the (AList.lookup (op =) lenoff_lists m)
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29272
diff changeset
  2796
            val (len_n, off_n) = the (AList.lookup (op =) lenoff_lists n)
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2797
            val len_elem = len_m + len_n
39047
cdff476ba39e use existing Integer.pow, despite its slightly odd argument order;
wenzelm
parents: 39046
diff changeset
  2798
            val off_elem = off_m * Integer.pow len_n size_elem + off_n
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2799
          in
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2800
            case AList.lookup op= lenoff'_lists (len_elem, off_elem) of
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2801
              NONE =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2802
                (* undefined *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2803
                Leaf (replicate size_list False)
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2804
            | SOME element =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2805
                Leaf ((replicate element False) @ True ::
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2806
                  (replicate (size_list - element - 1) False))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2807
          end
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2808
      in
25032
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2809
        SOME (Node (map (fn m => Node (map (append m) elements)) elements),
f7095d7cb9a3 interpreter for List.append added again
webertj
parents: 25014
diff changeset
  2810
          model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2811
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2812
  | _ => NONE;
15767
8ed9fcc004fe support for recursion over mutually recursive IDTs
webertj
parents: 15611
diff changeset
  2813
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
  2814
(* UNSOUND
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
  2815
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2816
(* only an optimization: 'lfp' could in principle be interpreted with  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2817
(* interpreters available already (using its definition), but the code *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2818
(* below is more efficient                                             *)
16050
828fc32f390f interpreters for lfp/gfp added
webertj
parents: 15794
diff changeset
  2819
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2820
fun lfp_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2821
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2822
    Const (@{const_name lfp}, Type ("fun", [Type ("fun",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2823
      [Type ("fun", [T, @{typ bool}]),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2824
       Type ("fun", [_, @{typ bool}])]),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2825
       Type ("fun", [_, @{typ bool}])])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2826
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2827
        val size_elem = size_of_type ctxt model T
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2828
        (* the universe (i.e. the set that contains every element) *)
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2829
        val i_univ = Node (replicate size_elem TT)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2830
        (* all sets with elements from type 'T' *)
30312
0e0cb7ac0281 Fix remaining occurrences of "'a set" in Refute, by using "'a => bool" instead.
blanchet
parents: 30307
diff changeset
  2831
        val i_sets =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2832
          make_constants ctxt model (Type ("fun", [T, HOLogic.boolT]))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2833
        (* all functions that map sets to sets *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2834
        val i_funs = make_constants ctxt model (Type ("fun",
38553
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 37677
diff changeset
  2835
          [Type ("fun", [T, @{typ bool}]),
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 37677
diff changeset
  2836
           Type ("fun", [T, @{typ bool}])]))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2837
        (* "lfp(f) == Inter({u. f(u) <= u})" *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2838
        (* interpretation * interpretation -> bool *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2839
        fun is_subset (Node subs, Node sups) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2840
              forall (fn (sub, sup) => (sub = FF) orelse (sup = TT)) (subs ~~ sups)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2841
          | is_subset (_, _) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2842
              raise REFUTE ("lfp_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2843
                "is_subset: interpretation for set is not a node")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2844
        (* interpretation * interpretation -> interpretation *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2845
        fun intersection (Node xs, Node ys) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2846
              Node (map (fn (x, y) => if x=TT andalso y=TT then TT else FF)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2847
                (xs ~~ ys))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2848
          | intersection (_, _) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2849
              raise REFUTE ("lfp_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2850
                "intersection: interpretation for set is not a node")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2851
        (* interpretation -> interpretaion *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2852
        fun lfp (Node resultsets) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2853
              fold (fn (set, resultset) => fn acc =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2854
                if is_subset (resultset, set) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2855
                  intersection (acc, set)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2856
                else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2857
                  acc) (i_sets ~~ resultsets) i_univ
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2858
          | lfp _ =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2859
              raise REFUTE ("lfp_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2860
                "lfp: interpretation for function is not a node")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2861
      in
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2862
        SOME (Node (map lfp i_funs), model, args)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2863
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2864
  | _ => NONE;
16050
828fc32f390f interpreters for lfp/gfp added
webertj
parents: 15794
diff changeset
  2865
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2866
(* only an optimization: 'gfp' could in principle be interpreted with  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2867
(* interpreters available already (using its definition), but the code *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2868
(* below is more efficient                                             *)
16050
828fc32f390f interpreters for lfp/gfp added
webertj
parents: 15794
diff changeset
  2869
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2870
fun gfp_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2871
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2872
    Const (@{const_name gfp}, Type ("fun", [Type ("fun",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2873
      [Type ("fun", [T, @{typ bool}]),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2874
       Type ("fun", [_, @{typ bool}])]),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2875
       Type ("fun", [_, @{typ bool}])])) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2876
    let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2877
      val size_elem = size_of_type ctxt model T
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2878
      (* the universe (i.e. the set that contains every element) *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2879
      val i_univ = Node (replicate size_elem TT)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2880
      (* all sets with elements from type 'T' *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2881
      val i_sets =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2882
        make_constants ctxt model (Type ("fun", [T, HOLogic.boolT]))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2883
      (* all functions that map sets to sets *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2884
      val i_funs = make_constants ctxt model (Type ("fun",
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2885
        [Type ("fun", [T, HOLogic.boolT]),
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2886
         Type ("fun", [T, HOLogic.boolT])]))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2887
      (* "gfp(f) == Union({u. u <= f(u)})" *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2888
      (* interpretation * interpretation -> bool *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2889
      fun is_subset (Node subs, Node sups) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2890
            forall (fn (sub, sup) => (sub = FF) orelse (sup = TT))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2891
              (subs ~~ sups)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2892
        | is_subset (_, _) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2893
            raise REFUTE ("gfp_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2894
              "is_subset: interpretation for set is not a node")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2895
      (* interpretation * interpretation -> interpretation *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2896
      fun union (Node xs, Node ys) =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2897
            Node (map (fn (x,y) => if x=TT orelse y=TT then TT else FF)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2898
                 (xs ~~ ys))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2899
        | union (_, _) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2900
            raise REFUTE ("gfp_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2901
              "union: interpretation for set is not a node")
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2902
      (* interpretation -> interpretaion *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2903
      fun gfp (Node resultsets) =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2904
            fold (fn (set, resultset) => fn acc =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2905
              if is_subset (set, resultset) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2906
                union (acc, set)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2907
              else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2908
                acc) (i_sets ~~ resultsets) i_univ
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2909
        | gfp _ =
29802
d201a838d0f7 Make some Refute functions public so I can use them in Nitpick,
blanchet
parents: 29288
diff changeset
  2910
            raise REFUTE ("gfp_interpreter",
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2911
              "gfp: interpretation for function is not a node")
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2912
    in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2913
      SOME (Node (map gfp i_funs), model, args)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2914
    end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2915
  | _ => NONE;
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
  2916
*)
16050
828fc32f390f interpreters for lfp/gfp added
webertj
parents: 15794
diff changeset
  2917
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2918
(* only an optimization: 'fst' could in principle be interpreted with  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2919
(* interpreters available already (using its definition), but the code *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2920
(* below is more efficient                                             *)
21267
5294ecae6708 interpreters for fst and snd added
webertj
parents: 21098
diff changeset
  2921
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2922
fun Product_Type_fst_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2923
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2924
    Const (@{const_name fst}, Type ("fun", [Type (@{type_name Product_Type.prod}, [T, U]), _])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2925
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2926
        val constants_T = make_constants ctxt model T
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2927
        val size_U = size_of_type ctxt model U
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2928
      in
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
  2929
        SOME (Node (maps (replicate size_U) constants_T), model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2930
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2931
  | _ => NONE;
21267
5294ecae6708 interpreters for fst and snd added
webertj
parents: 21098
diff changeset
  2932
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2933
(* only an optimization: 'snd' could in principle be interpreted with  *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2934
(* interpreters available already (using its definition), but the code *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2935
(* below is more efficient                                             *)
21267
5294ecae6708 interpreters for fst and snd added
webertj
parents: 21098
diff changeset
  2936
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2937
fun Product_Type_snd_interpreter ctxt model args t =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2938
  case t of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2939
    Const (@{const_name snd}, Type ("fun", [Type (@{type_name Product_Type.prod}, [T, U]), _])) =>
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2940
      let
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2941
        val size_T = size_of_type ctxt model T
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2942
        val constants_U = make_constants ctxt model U
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2943
      in
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
  2944
        SOME (Node (flat (replicate size_T constants_U)), model, args)
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2945
      end
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2946
  | _ => NONE;
21267
5294ecae6708 interpreters for fst and snd added
webertj
parents: 21098
diff changeset
  2947
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
  2948
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
  2949
(* ------------------------------------------------------------------------- *)
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
  2950
(* PRINTERS                                                                  *)
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
  2951
(* ------------------------------------------------------------------------- *)
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
  2952
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2953
fun stlc_printer ctxt model T intr assignment =
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2954
  let
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2955
    (* string -> string *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2956
    fun strip_leading_quote s =
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2957
      (implode o (fn [] => [] | x::xs => if x="'" then xs else x::xs)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2958
        o explode) s
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2959
    (* Term.typ -> string *)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2960
    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
  2961
      | string_of_typ (TFree (x, _)) = strip_leading_quote x
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2962
      | string_of_typ (TVar ((x,i), _)) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2963
          strip_leading_quote x ^ string_of_int i
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2964
    (* interpretation -> int *)
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2965
    fun index_from_interpretation (Leaf xs) =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2966
          find_index (PropLogic.eval assignment) xs
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2967
      | index_from_interpretation _ =
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2968
          raise REFUTE ("stlc_printer",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2969
            "interpretation for ground type is not a leaf")
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  2970
  in
25014
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2971
    case T of
b711b0af178e significant code overhaul, bugfix for inductive data types
webertj
parents: 24928
diff changeset
  2972
      Type ("fun", [T1, T2]) =>
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2973
        let
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2974
          (* create all constants of type 'T1' *)
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2975
          val constants = make_constants ctxt model T1
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2976
          (* interpretation list *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2977
          val results =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2978
            (case intr of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2979
              Node xs => xs
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2980
            | _ => raise REFUTE ("stlc_printer",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2981
              "interpretation for function type is a leaf"))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2982
          (* Term.term list *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2983
          val pairs = map (fn (arg, result) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2984
            HOLogic.mk_prod
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2985
              (print ctxt model T1 arg assignment,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  2986
               print ctxt model T2 result assignment))
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2987
            (constants ~~ results)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2988
          (* Term.typ *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2989
          val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2990
          val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2991
          (* Term.term *)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2992
          val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2993
          val HOLogic_insert    =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2994
            Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2995
        in
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2996
          SOME (fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) pairs HOLogic_empty_set)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2997
        end
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2998
    | Type ("prop", []) =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  2999
        (case index_from_interpretation intr of
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3000
          ~1 => SOME (HOLogic.mk_Trueprop (Const (@{const_name undefined}, HOLogic.boolT)))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3001
        | 0  => SOME (HOLogic.mk_Trueprop HOLogic.true_const)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3002
        | 1  => SOME (HOLogic.mk_Trueprop HOLogic.false_const)
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3003
        | _  => raise REFUTE ("stlc_interpreter",
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3004
          "illegal interpretation for a propositional value"))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3005
    | Type _  =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3006
        if index_from_interpretation intr = (~1) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3007
          SOME (Const (@{const_name undefined}, T))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3008
        else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3009
          SOME (Const (string_of_typ T ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3010
            string_of_int (index_from_interpretation intr), T))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3011
    | TFree _ =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3012
        if index_from_interpretation intr = (~1) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3013
          SOME (Const (@{const_name undefined}, T))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3014
        else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3015
          SOME (Const (string_of_typ T ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3016
            string_of_int (index_from_interpretation intr), T))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3017
    | TVar _  =>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3018
        if index_from_interpretation intr = (~1) then
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3019
          SOME (Const (@{const_name undefined}, T))
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3020
        else
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3021
          SOME (Const (string_of_typ T ^
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3022
            string_of_int (index_from_interpretation intr), T))
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22093
diff changeset
  3023
  end;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3024
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3025
fun IDT_printer ctxt model T intr assignment =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3026
  let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3027
    val thy = ProofContext.theory_of ctxt
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3028
  in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3029
    (case T of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3030
      Type (s, Ts) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3031
        (case Datatype.get_info thy s of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3032
          SOME info =>  (* inductive datatype *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3033
            let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3034
              val (typs, _)           = model
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3035
              val index               = #index info
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3036
              val descr               = #descr info
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3037
              val (_, dtyps, constrs) = the (AList.lookup (op =) descr index)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3038
              val typ_assoc           = dtyps ~~ Ts
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3039
              (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3040
              val _ =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3041
                if Library.exists (fn d =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3042
                  case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3043
                then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3044
                  raise REFUTE ("IDT_printer", "datatype argument (for type " ^
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3045
                    Syntax.string_of_typ ctxt (Type (s, Ts)) ^ ") is not a variable")
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3046
                else ()
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3047
              (* the index of the element in the datatype *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3048
              val element =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3049
                (case intr of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3050
                  Leaf xs => find_index (PropLogic.eval assignment) xs
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3051
                | Node _  => raise REFUTE ("IDT_printer",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3052
                  "interpretation is not a leaf"))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3053
            in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3054
              if element < 0 then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3055
                SOME (Const (@{const_name undefined}, Type (s, Ts)))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3056
              else
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3057
                let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3058
                  (* takes a datatype constructor, and if for some arguments this  *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3059
                  (* constructor generates the datatype's element that is given by *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3060
                  (* 'element', returns the constructor (as a term) as well as the *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3061
                  (* indices of the arguments                                      *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3062
                  fun get_constr_args (cname, cargs) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3063
                    let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3064
                      val cTerm      = Const (cname,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3065
                        map (typ_of_dtyp descr typ_assoc) cargs ---> Type (s, Ts))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3066
                      val (iC, _, _) = interpret ctxt (typs, []) {maxvars=0,
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3067
                        def_eq=false, next_idx=1, bounds=[], wellformed=True} cTerm
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3068
                      (* interpretation -> int list option *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3069
                      fun get_args (Leaf xs) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3070
                            if find_index (fn x => x = True) xs = element then
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3071
                              SOME []
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3072
                            else
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3073
                              NONE
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3074
                        | get_args (Node xs) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3075
                            let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3076
                              (* interpretation * int -> int list option *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3077
                              fun search ([], _) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3078
                                NONE
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3079
                                | search (x::xs, n) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3080
                                (case get_args x of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3081
                                  SOME result => SOME (n::result)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3082
                                | NONE        => search (xs, n+1))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3083
                            in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3084
                              search (xs, 0)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3085
                            end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3086
                    in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3087
                      Option.map (fn args => (cTerm, cargs, args)) (get_args iC)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3088
                    end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3089
                  val (cTerm, cargs, args) =
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3090
                    (* we could speed things up by computing the correct          *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3091
                    (* constructor directly (rather than testing all              *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3092
                    (* constructors), based on the order in which constructors    *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3093
                    (* generate elements of datatypes; the current implementation *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3094
                    (* of 'IDT_printer' however is independent of the internals   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3095
                    (* of 'IDT_constructor_interpreter'                           *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3096
                    (case get_first get_constr_args constrs of
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3097
                      SOME x => x
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3098
                    | NONE   => raise REFUTE ("IDT_printer",
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3099
                      "no matching constructor found for element " ^
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3100
                      string_of_int element))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3101
                  val argsTerms = map (fn (d, n) =>
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3102
                    let
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3103
                      val dT = typ_of_dtyp descr typ_assoc d
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3104
                      (* we only need the n-th element of this list, so there   *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3105
                      (* might be a more efficient implementation that does not *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3106
                      (* generate all constants                                 *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3107
                      val consts = make_constants ctxt (typs, []) dT
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3108
                    in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3109
                      print ctxt (typs, []) dT (List.nth (consts, n)) assignment
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3110
                    end) (cargs ~~ args)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3111
                in
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3112
                  SOME (list_comb (cTerm, argsTerms))
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3113
                end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3114
            end
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3115
        | NONE =>  (* not an inductive datatype *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3116
            NONE)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3117
    | _ =>  (* a (free or schematic) type variable *)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3118
        NONE)
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3119
  end;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3120
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3121
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3122
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  3123
(* use 'setup Refute.setup' in an Isabelle theory to initialize the 'Refute' *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  3124
(* structure                                                                 *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3125
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3126
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3127
(* ------------------------------------------------------------------------- *)
14456
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  3128
(* Note: the interpreters and printers are used in reverse order; however,   *)
cca28ec5f9a6 support for non-recursive IDTs, The, arbitrary, Hilbert_Choice.Eps
webertj
parents: 14351
diff changeset
  3129
(*       an interpreter that can handle non-atomic terms ends up being       *)
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
  3130
(*       applied before the 'stlc_interpreter' breaks the term apart into    *)
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
  3131
(*       subterms that are then passed to other interpreters!                *)
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3132
(* ------------------------------------------------------------------------- *)
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3133
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3134
val setup =
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3135
   add_interpreter "stlc"    stlc_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3136
   add_interpreter "Pure"    Pure_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3137
   add_interpreter "HOLogic" HOLogic_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3138
   add_interpreter "set"     set_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3139
   add_interpreter "IDT"             IDT_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3140
   add_interpreter "IDT_constructor" IDT_constructor_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3141
   add_interpreter "IDT_recursion"   IDT_recursion_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3142
   add_interpreter "Finite_Set.card"    Finite_Set_card_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3143
   add_interpreter "Finite_Set.finite"  Finite_Set_finite_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3144
   add_interpreter "Nat_Orderings.less" Nat_less_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3145
   add_interpreter "Nat_HOL.plus"       Nat_plus_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3146
   add_interpreter "Nat_HOL.minus"      Nat_minus_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3147
   add_interpreter "Nat_HOL.times"      Nat_times_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3148
   add_interpreter "List.append" List_append_interpreter #>
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
  3149
(* UNSOUND
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3150
   add_interpreter "lfp" lfp_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3151
   add_interpreter "gfp" gfp_interpreter #>
36130
9a672f7d488d commented out unsound "lfp"/"gfp" handling + fixed set output syntax;
blanchet
parents: 35746
diff changeset
  3152
*)
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3153
   add_interpreter "Product_Type.fst" Product_Type_fst_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3154
   add_interpreter "Product_Type.snd" Product_Type_snd_interpreter #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3155
   add_printer "stlc" stlc_printer #>
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3156
   add_printer "IDT"  IDT_printer;
14350
41b32020d0b3 Adding 'refute' to HOL.
webertj
parents:
diff changeset
  3157
39048
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3158
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3159
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3160
(** outer syntax commands 'refute' and 'refute_params' **)
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3161
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3162
(* argument parsing *)
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3163
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3164
(*optional list of arguments of the form [name1=value1, name2=value2, ...]*)
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3165
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3166
val scan_parm = Parse.name -- (Scan.optional (Parse.$$$ "=" |-- Parse.name) "true")
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3167
val scan_parms = Scan.optional (Parse.$$$ "[" |-- Parse.list scan_parm --| Parse.$$$ "]") [];
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3168
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3169
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3170
(* 'refute' command *)
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3171
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3172
val _ =
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3173
  Outer_Syntax.improper_command "refute"
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3174
    "try to find a model that refutes a given subgoal" Keyword.diag
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3175
    (scan_parms -- Scan.optional Parse.nat 1 >>
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3176
      (fn (parms, i) =>
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3177
        Toplevel.keep (fn state =>
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3178
          let
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3179
            val ctxt = Toplevel.context_of state;
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3180
            val {goal = st, ...} = Proof.raw_goal (Toplevel.proof_of state);
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3181
          in refute_goal ctxt parms st i end)));
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3182
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3183
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3184
(* 'refute_params' command *)
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3185
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3186
val _ =
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3187
  Outer_Syntax.command "refute_params"
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3188
    "show/store default parameters for the 'refute' command" Keyword.thy_decl
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3189
    (scan_parms >> (fn parms =>
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3190
      Toplevel.theory (fn thy =>
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3191
        let
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3192
          val thy' = fold set_default_param parms thy;
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3193
          val output =
39049
423b72f2d242 prefer regular Proof.context over background theory;
wenzelm
parents: 39048
diff changeset
  3194
            (case get_default_params (ProofContext.init_global thy') of
39048
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3195
              [] => "none"
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3196
            | new_defaults => cat_lines (map (fn (x, y) => x ^ "=" ^ y) new_defaults));
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3197
          val _ = writeln ("Default parameters for 'refute':\n" ^ output);
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3198
        in thy' end)));
4006f5c3f421 just one refute.ML;
wenzelm
parents: 39047
diff changeset
  3199
39046
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3200
end;
5b38730f3e12 tuned whitespace and indentation, emphasizing the logical structure of this long text;
wenzelm
parents: 38864
diff changeset
  3201