src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML
author bulwahn
Wed, 21 Apr 2010 12:10:52 +0200
changeset 36252 beba03215d8f
parent 36251 5fd5d732a4ea
child 36254 95ef0a3cf31c
permissions -rw-r--r--
adding more profiling to the predicate compiler
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33265
01c9c6dbd890 proper headers;
wenzelm
parents: 33256
diff changeset
     1
(*  Title:      HOL/Tools/Predicate_Compile/predicate_compile_core.ML
01c9c6dbd890 proper headers;
wenzelm
parents: 33256
diff changeset
     2
    Author:     Lukas Bulwahn, TU Muenchen
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     3
33265
01c9c6dbd890 proper headers;
wenzelm
parents: 33256
diff changeset
     4
A compiler from predicates specified by intro/elim rules to equations.
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     5
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     6
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     7
signature PREDICATE_COMPILE_CORE =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     8
sig
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
     9
  type mode = Predicate_Compile_Aux.mode
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    10
  type options = Predicate_Compile_Aux.options
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    11
  type compilation = Predicate_Compile_Aux.compilation
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    12
  type compilation_funs = Predicate_Compile_Aux.compilation_funs
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    13
  
33478
b70fe083694d moved values command from core to predicate compile
bulwahn
parents: 33476
diff changeset
    14
  val setup : theory -> theory
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    15
  val code_pred : options -> string -> Proof.context -> Proof.state
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    16
  val code_pred_cmd : options -> string -> Proof.context -> Proof.state
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    17
  val values_cmd : string list -> mode option list option
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    18
    -> ((string option * bool) * (compilation * int list)) -> int -> string -> Toplevel.state -> unit
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
    19
  val register_predicate : (string * thm list * thm) -> theory -> theory
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
    20
  val register_intros : string * thm list -> theory -> theory
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    21
  val is_registered : theory -> string -> bool
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    22
  val function_name_of : compilation -> theory -> string -> mode -> string
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    23
  val predfun_intro_of: Proof.context -> string -> mode -> thm
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    24
  val predfun_elim_of: Proof.context -> string -> mode -> thm
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    25
  val all_preds_of : theory -> string list
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    26
  val modes_of: compilation -> theory -> string -> mode list
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    27
  val all_modes_of : compilation -> theory -> (string * mode list) list
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    28
  val all_random_modes_of : theory -> (string * mode list) list
33478
b70fe083694d moved values command from core to predicate compile
bulwahn
parents: 33476
diff changeset
    29
  val intros_of : theory -> string -> thm list
b70fe083694d moved values command from core to predicate compile
bulwahn
parents: 33476
diff changeset
    30
  val add_intro : thm -> theory -> theory
b70fe083694d moved values command from core to predicate compile
bulwahn
parents: 33476
diff changeset
    31
  val set_elim : thm -> theory -> theory
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    32
  val register_alternative_function : string -> mode -> string -> theory -> theory
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    33
  val alternative_compilation_of : theory -> string -> mode ->
36038
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
    34
    (compilation_funs -> typ -> term) option
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    35
  val functional_compilation : string -> mode -> compilation_funs -> typ -> term
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    36
  val force_modes_and_functions : string -> (mode * (string * bool)) list -> theory -> theory
36038
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
    37
  val force_modes_and_compilations : string ->
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    38
    (mode * ((compilation_funs -> typ -> term) * bool)) list -> theory -> theory
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
    39
  val preprocess_intro : theory -> thm -> thm
33478
b70fe083694d moved values command from core to predicate compile
bulwahn
parents: 33476
diff changeset
    40
  val print_stored_rules : theory -> unit
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    41
  val print_all_modes : compilation -> theory -> unit
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
    42
  val mk_casesrule : Proof.context -> term -> thm list -> term
33137
0d16c07f8d24 added option to generate random values to values command in the predicate compiler
bulwahn
parents: 33135
diff changeset
    43
  val eval_ref : (unit -> term Predicate.pred) option Unsynchronized.ref
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
    44
  val random_eval_ref : (unit -> int * int -> term Predicate.pred * (int * int))
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
    45
    option Unsynchronized.ref
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
    46
  val dseq_eval_ref : (unit -> term DSequence.dseq) option Unsynchronized.ref
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
    47
  val random_dseq_eval_ref : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
    48
    option Unsynchronized.ref
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
    49
  val new_random_dseq_eval_ref :
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
    50
    (unit -> int -> int -> int * int -> int -> term Lazy_Sequence.lazy_sequence)
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
    51
      option Unsynchronized.ref
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
    52
  val new_random_dseq_stats_eval_ref :
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
    53
    (unit -> int -> int -> int * int -> int -> (term * int) Lazy_Sequence.lazy_sequence)
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
    54
      option Unsynchronized.ref
33628
ed2111a5c3ed renaming code_pred_intros to code_pred_intro
bulwahn
parents: 33626
diff changeset
    55
  val code_pred_intro_attrib : attribute
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    56
  (* used by Quickcheck_Generator *) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    57
  (* temporary for testing of the compilation *)
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    58
  val add_equations : options -> string list -> theory -> theory
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    59
  val add_depth_limited_random_equations : options -> string list -> theory -> theory
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    60
  val add_random_dseq_equations : options -> string list -> theory -> theory
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    61
  val add_new_random_dseq_equations : options -> string list -> theory -> theory
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
    62
  val mk_tracing : string -> term -> term
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    63
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    64
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    65
structure Predicate_Compile_Core : PREDICATE_COMPILE_CORE =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    66
struct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    67
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
    68
open Predicate_Compile_Aux;
33144
1831516747d4 removed obsolete GeneratorPrem; clean-up after modularization; tuned
bulwahn
parents: 33143
diff changeset
    69
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    70
(** auxiliary **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    71
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    72
(* debug stuff *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    73
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
    74
fun print_tac options s = 
33127
eb91ec1ef6f0 added option show_proof_trace
bulwahn
parents: 33126
diff changeset
    75
  if show_proof_trace options then Tactical.print_tac s else Seq.single;
eb91ec1ef6f0 added option show_proof_trace
bulwahn
parents: 33126
diff changeset
    76
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
    77
fun assert b = if not b then raise Fail "Assertion failed" else warning "Assertion holds"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
    78
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
    79
datatype assertion = Max_number_of_subgoals of int
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
    80
fun assert_tac (Max_number_of_subgoals i) st =
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
    81
  if (nprems_of st <= i) then Seq.single st
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
    82
  else raise Fail ("assert_tac: Numbers of subgoals mismatch at goal state :"
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
    83
    ^ "\n" ^ Pretty.string_of (Pretty.chunks
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
    84
      (Goal_Display.pretty_goals_without_context (! Goal_Display.goals_limit) st)));
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
    85
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    86
(** fundamentals **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    87
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    88
(* syntactic operations *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    89
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    90
fun mk_eq (x, xs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    91
  let fun mk_eqs _ [] = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    92
        | mk_eqs a (b::cs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    93
            HOLogic.mk_eq (Free (a, fastype_of b), b) :: mk_eqs a cs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    94
  in mk_eqs x xs end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    95
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    96
fun mk_scomp (t, u) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    97
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    98
    val T = fastype_of t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    99
    val U = fastype_of u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   100
    val [A] = binder_types T
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   101
    val D = body_type U                   
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   102
  in 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   103
    Const (@{const_name "scomp"}, T --> U --> A --> D) $ t $ u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   104
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   105
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   106
fun dest_funT (Type ("fun",[S, T])) = (S, T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   107
  | dest_funT T = raise TYPE ("dest_funT", [T], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   108
 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   109
fun mk_fun_comp (t, u) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   110
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   111
    val (_, B) = dest_funT (fastype_of t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   112
    val (C, A) = dest_funT (fastype_of u)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   113
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   114
    Const(@{const_name "Fun.comp"}, (A --> B) --> (C --> A) --> C --> B) $ t $ u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   115
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   116
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   117
fun dest_randomT (Type ("fun", [@{typ Random.seed},
32674
b629fbcc5313 merged; adopted to changes from Code_Evaluation in the predicate compiler
bulwahn
parents: 32673
diff changeset
   118
  Type ("*", [Type ("*", [T, @{typ "unit => Code_Evaluation.term"}]) ,@{typ Random.seed}])])) = T
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   119
  | dest_randomT T = raise TYPE ("dest_randomT", [T], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   120
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   121
fun mk_tracing s t =
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   122
  Const(@{const_name Code_Evaluation.tracing},
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   123
    @{typ String.literal} --> (fastype_of t) --> (fastype_of t)) $ (HOLogic.mk_literal s) $ t
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   124
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   125
val strip_intro_concl = (strip_comb o HOLogic.dest_Trueprop o Logic.strip_imp_concl o prop_of)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   126
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   127
(* derivation trees for modes of premises *)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   128
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   129
datatype mode_derivation = Mode_App of mode_derivation * mode_derivation | Context of mode
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   130
  | Mode_Pair of mode_derivation * mode_derivation | Term of mode
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   131
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   132
fun string_of_derivation (Mode_App (m1, m2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   133
  "App (" ^ string_of_derivation m1 ^ ", " ^ string_of_derivation m2 ^ ")"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   134
  | string_of_derivation (Mode_Pair (m1, m2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   135
  "Pair (" ^ string_of_derivation m1 ^ ", " ^ string_of_derivation m2 ^ ")"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   136
  | string_of_derivation (Term m) = "Term (" ^ string_of_mode m ^ ")"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   137
  | string_of_derivation (Context m) = "Context (" ^ string_of_mode m ^ ")"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   138
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   139
fun strip_mode_derivation deriv =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   140
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   141
    fun strip (Mode_App (deriv1, deriv2)) ds = strip deriv1 (deriv2 :: ds)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   142
      | strip deriv ds = (deriv, ds)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   143
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   144
    strip deriv []
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   145
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   146
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   147
fun mode_of (Context m) = m
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   148
  | mode_of (Term m) = m
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   149
  | mode_of (Mode_App (d1, d2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   150
    (case mode_of d1 of Fun (m, m') =>
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
   151
        (if eq_mode (m, mode_of d2) then m' else raise Fail "mode_of")
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
   152
      | _ => raise Fail "mode_of2")
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   153
  | mode_of (Mode_Pair (d1, d2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   154
    Pair (mode_of d1, mode_of d2)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   155
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   156
fun head_mode_of deriv = mode_of (fst (strip_mode_derivation deriv))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   157
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   158
fun param_derivations_of deriv =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   159
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   160
    val (_, argument_derivs) = strip_mode_derivation deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   161
    fun param_derivation (Mode_Pair (m1, m2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   162
        param_derivation m1 @ param_derivation m2
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   163
      | param_derivation (Term _) = []
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   164
      | param_derivation m = [m]
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   165
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   166
    maps param_derivation argument_derivs
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   167
  end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   168
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   169
fun collect_context_modes (Mode_App (m1, m2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   170
      collect_context_modes m1 @ collect_context_modes m2
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   171
  | collect_context_modes (Mode_Pair (m1, m2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   172
      collect_context_modes m1 @ collect_context_modes m2
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   173
  | collect_context_modes (Context m) = [m]
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   174
  | collect_context_modes (Term _) = []
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   175
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   176
(* representation of inferred clauses with modes *)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   177
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   178
type moded_clause = term list * (indprem * mode_derivation) list
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   179
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
   180
type 'a pred_mode_table = (string * ((bool * mode) * 'a) list) list
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   181
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   182
(* book-keeping *)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   183
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   184
datatype predfun_data = PredfunData of {
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   185
  definition : thm,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   186
  intro : thm,
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   187
  elim : thm,
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   188
  neg_intro : thm option
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   189
};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   190
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   191
fun rep_predfun_data (PredfunData data) = data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   192
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   193
fun mk_predfun_data (definition, ((intro, elim), neg_intro)) =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   194
  PredfunData {definition = definition, intro = intro, elim = elim, neg_intro = neg_intro}
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   195
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   196
datatype pred_data = PredData of {
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   197
  intros : thm list,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   198
  elim : thm option,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   199
  function_names : (compilation * (mode * string) list) list,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   200
  predfun_data : (mode * predfun_data) list,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   201
  needs_random : mode list
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   202
};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   203
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   204
fun rep_pred_data (PredData data) = data;
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   205
35887
f704ba9875f6 making flat triples to nested tuple to remove general triple functions
bulwahn
parents: 35886
diff changeset
   206
fun mk_pred_data ((intros, elim), (function_names, (predfun_data, needs_random))) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   207
  PredData {intros = intros, elim = elim,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   208
    function_names = function_names, predfun_data = predfun_data, needs_random = needs_random}
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   209
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   210
fun map_pred_data f (PredData {intros, elim, function_names, predfun_data, needs_random}) =
35887
f704ba9875f6 making flat triples to nested tuple to remove general triple functions
bulwahn
parents: 35886
diff changeset
   211
  mk_pred_data (f ((intros, elim), (function_names, (predfun_data, needs_random))))
33144
1831516747d4 removed obsolete GeneratorPrem; clean-up after modularization; tuned
bulwahn
parents: 33143
diff changeset
   212
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   213
fun eq_option eq (NONE, NONE) = true
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   214
  | eq_option eq (SOME x, SOME y) = eq (x, y)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   215
  | eq_option eq _ = false
33144
1831516747d4 removed obsolete GeneratorPrem; clean-up after modularization; tuned
bulwahn
parents: 33143
diff changeset
   216
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   217
fun eq_pred_data (PredData d1, PredData d2) = 
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
   218
  eq_list Thm.eq_thm (#intros d1, #intros d2) andalso
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
   219
  eq_option Thm.eq_thm (#elim d1, #elim d2)
33144
1831516747d4 removed obsolete GeneratorPrem; clean-up after modularization; tuned
bulwahn
parents: 33143
diff changeset
   220
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
   221
structure PredData = Theory_Data
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   222
(
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   223
  type T = pred_data Graph.T;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   224
  val empty = Graph.empty;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   225
  val extend = I;
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
   226
  val merge = Graph.merge eq_pred_data;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   227
);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   228
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   229
(* queries *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   230
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   231
fun lookup_pred_data thy name =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   232
  Option.map rep_pred_data (try (Graph.get_node (PredData.get thy)) name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   233
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   234
fun the_pred_data thy name = case lookup_pred_data thy name
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   235
 of NONE => error ("No such predicate " ^ quote name)  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   236
  | SOME data => data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   237
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   238
val is_registered = is_some oo lookup_pred_data
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   239
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   240
val all_preds_of = Graph.keys o PredData.get
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   241
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   242
fun intros_of thy = map (Thm.transfer thy) o #intros o the_pred_data thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   243
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   244
fun the_elim_of thy name = case #elim (the_pred_data thy name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   245
 of NONE => error ("No elimination rule for predicate " ^ quote name)
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
   246
  | SOME thm => Thm.transfer thy thm
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   247
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   248
val has_elim = is_some o #elim oo the_pred_data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   249
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   250
fun function_names_of compilation thy name =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   251
  case AList.lookup (op =) (#function_names (the_pred_data thy name)) compilation of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   252
    NONE => error ("No " ^ string_of_compilation compilation
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   253
      ^ "functions defined for predicate " ^ quote name)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   254
  | SOME fun_names => fun_names
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   255
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   256
fun function_name_of compilation thy name mode =
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
   257
  case AList.lookup eq_mode
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   258
    (function_names_of compilation thy name) mode of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   259
    NONE => error ("No " ^ string_of_compilation compilation
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   260
      ^ " function defined for mode " ^ string_of_mode mode ^ " of predicate " ^ quote name)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   261
  | SOME function_name => function_name
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   262
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   263
fun modes_of compilation thy name = map fst (function_names_of compilation thy name)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   264
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   265
fun all_modes_of compilation thy =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   266
  map_filter (fn name => Option.map (pair name) (try (modes_of compilation thy) name))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   267
    (all_preds_of thy)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   268
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   269
val all_random_modes_of = all_modes_of Random
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   270
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   271
fun defined_functions compilation thy name =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   272
  AList.defined (op =) (#function_names (the_pred_data thy name)) compilation
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   273
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   274
fun lookup_predfun_data thy name mode =
33483
a15a2f7f7560 improved handling of already defined functions in the predicate compiler; could cause trouble before when no modes for a predicate were infered
bulwahn
parents: 33482
diff changeset
   275
  Option.map rep_predfun_data
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   276
    (AList.lookup (op =) (#predfun_data (the_pred_data thy name)) mode)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   277
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   278
fun the_predfun_data thy name mode =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   279
  case lookup_predfun_data thy name mode of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   280
    NONE => error ("No function defined for mode " ^ string_of_mode mode ^
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   281
      " of predicate " ^ name)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   282
  | SOME data => data;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   283
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
   284
val predfun_definition_of = #definition ooo the_predfun_data o ProofContext.theory_of
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   285
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
   286
val predfun_intro_of = #intro ooo the_predfun_data o ProofContext.theory_of
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   287
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
   288
val predfun_elim_of = #elim ooo the_predfun_data o ProofContext.theory_of
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   289
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
   290
val predfun_neg_intro_of = #neg_intro ooo the_predfun_data o ProofContext.theory_of
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   291
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   292
(* diagnostic display functions *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   293
33619
d93a3cb55068 first steps towards a new mode datastructure; new syntax for mode annotations and new output of modes
bulwahn
parents: 33526
diff changeset
   294
fun print_modes options thy modes =
33251
4b13ab778b78 added option show_modes to predicate compiler
bulwahn
parents: 33250
diff changeset
   295
  if show_modes options then
33326
7d0288d90535 improved handling of alternative rules; added test cases for alternative rules; normalized tracing in the predicate compiler
bulwahn
parents: 33268
diff changeset
   296
    tracing ("Inferred modes:\n" ^
33251
4b13ab778b78 added option show_modes to predicate compiler
bulwahn
parents: 33250
diff changeset
   297
      cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
   298
        (fn (p, m) => string_of_mode m ^ (if p then "pos" else "neg")) ms)) modes))
33251
4b13ab778b78 added option show_modes to predicate compiler
bulwahn
parents: 33250
diff changeset
   299
  else ()
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   300
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   301
fun print_pred_mode_table string_of_entry thy pred_mode_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   302
  let
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
   303
    fun print_mode pred ((pol, mode), entry) =  "mode : " ^ string_of_mode mode
33619
d93a3cb55068 first steps towards a new mode datastructure; new syntax for mode annotations and new output of modes
bulwahn
parents: 33526
diff changeset
   304
      ^ string_of_entry pred mode entry
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   305
    fun print_pred (pred, modes) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   306
      "predicate " ^ pred ^ ": " ^ cat_lines (map (print_mode pred) modes)
33326
7d0288d90535 improved handling of alternative rules; added test cases for alternative rules; normalized tracing in the predicate compiler
bulwahn
parents: 33268
diff changeset
   307
    val _ = tracing (cat_lines (map print_pred pred_mode_table))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   308
  in () end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   309
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   310
fun string_of_prem thy (Prem t) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   311
    (Syntax.string_of_term_global thy t) ^ "(premise)"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   312
  | string_of_prem thy (Negprem t) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   313
    (Syntax.string_of_term_global thy (HOLogic.mk_not t)) ^ "(negative premise)"
33130
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   314
  | string_of_prem thy (Sidecond t) =
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   315
    (Syntax.string_of_term_global thy t) ^ "(sidecondition)"
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
   316
  | string_of_prem thy _ = raise Fail "string_of_prem: unexpected input"
33130
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   317
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   318
fun string_of_clause thy pred (ts, prems) =
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   319
  (space_implode " --> "
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   320
  (map (string_of_prem thy) prems)) ^ " --> " ^ pred ^ " "
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   321
   ^ (space_implode " " (map (Syntax.string_of_term_global thy) ts))
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
   322
33139
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   323
fun print_compiled_terms options thy =
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   324
  if show_compilation options then
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   325
    print_pred_mode_table (fn _ => fn _ => Syntax.string_of_term_global thy) thy
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   326
  else K ()
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   327
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   328
fun print_stored_rules thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   329
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   330
    val preds = (Graph.keys o PredData.get) thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   331
    fun print pred () = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   332
      val _ = writeln ("predicate: " ^ pred)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   333
      val _ = writeln ("introrules: ")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   334
      val _ = fold (fn thm => fn u => writeln (Display.string_of_thm_global thy thm))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   335
        (rev (intros_of thy pred)) ()
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   336
    in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   337
      if (has_elim thy pred) then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   338
        writeln ("elimrule: " ^ Display.string_of_thm_global thy (the_elim_of thy pred))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   339
      else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   340
        writeln ("no elimrule defined")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   341
    end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   342
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   343
    fold print preds ()
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   344
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   345
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   346
fun print_all_modes compilation thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   347
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   348
    val _ = writeln ("Inferred modes:")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   349
    fun print (pred, modes) u =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   350
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   351
        val _ = writeln ("predicate: " ^ pred)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   352
        val _ = writeln ("modes: " ^ (commas (map string_of_mode modes)))
33619
d93a3cb55068 first steps towards a new mode datastructure; new syntax for mode annotations and new output of modes
bulwahn
parents: 33526
diff changeset
   353
      in u end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   354
  in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   355
    fold print (all_modes_of compilation thy) ()
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   356
  end
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   357
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
   358
(* validity checks *)
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   359
(* EXPECTED MODE and PROPOSED_MODE are largely the same; define a clear semantics for those! *)
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
   360
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   361
fun check_expected_modes preds options modes =
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   362
  case expected_modes options of
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   363
    SOME (s, ms) => (case AList.lookup (op =) modes s of
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   364
      SOME modes =>
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   365
        let
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
   366
          val modes' = map snd modes
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   367
        in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   368
          if not (eq_set eq_mode (ms, modes')) then
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   369
            error ("expected modes were not inferred:\n"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   370
            ^ "  inferred modes for " ^ s ^ ": " ^ commas (map string_of_mode modes')  ^ "\n"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   371
            ^ "  expected modes for " ^ s ^ ": " ^ commas (map string_of_mode ms))
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   372
          else ()
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   373
        end
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   374
      | NONE => ())
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   375
  | NONE => ()
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   376
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   377
fun check_proposed_modes preds options modes extra_modes errors =
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   378
  case proposed_modes options of
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   379
    SOME (s, ms) => (case AList.lookup (op =) modes s of
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   380
      SOME inferred_ms =>
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   381
        let
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   382
          val preds_without_modes = map fst (filter (null o snd) (modes @ extra_modes))
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
   383
          val modes' = map snd inferred_ms
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   384
        in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   385
          if not (eq_set eq_mode (ms, modes')) then
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   386
            error ("expected modes were not inferred:\n"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   387
            ^ "  inferred modes for " ^ s ^ ": " ^ commas (map string_of_mode modes')  ^ "\n"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   388
            ^ "  expected modes for " ^ s ^ ": " ^ commas (map string_of_mode ms) ^ "\n"
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   389
            ^ "For the following clauses, the following modes could not be inferred: " ^ "\n"
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   390
            ^ cat_lines errors ^
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   391
            (if not (null preds_without_modes) then
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   392
              "\n" ^ "No mode inferred for the predicates " ^ commas preds_without_modes
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   393
            else ""))
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   394
          else ()
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   395
        end
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   396
      | NONE => ())
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   397
  | NONE => ()
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
   398
33144
1831516747d4 removed obsolete GeneratorPrem; clean-up after modularization; tuned
bulwahn
parents: 33143
diff changeset
   399
(* importing introduction rules *)
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   400
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   401
fun unify_consts thy cs intr_ts =
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   402
  (let
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   403
     val add_term_consts_2 = fold_aterms (fn Const c => insert (op =) c | _ => I);
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   404
     fun varify (t, (i, ts)) =
35845
e5980f0ad025 renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents: 35411
diff changeset
   405
       let val t' = map_types (Logic.incr_tvar (i + 1)) (#2 (Type.varify_global [] t))
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   406
       in (maxidx_of_term t', t'::ts) end;
33150
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   407
     val (i, cs') = List.foldr varify (~1, []) cs;
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   408
     val (i', intr_ts') = List.foldr varify (i, []) intr_ts;
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   409
     val rec_consts = fold add_term_consts_2 cs' [];
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   410
     val intr_consts = fold add_term_consts_2 intr_ts' [];
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   411
     fun unify (cname, cT) =
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33268
diff changeset
   412
       let val consts = map snd (filter (fn c => fst c = cname) intr_consts)
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   413
       in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end;
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   414
     val (env, _) = fold unify rec_consts (Vartab.empty, i');
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   415
     val subst = map_types (Envir.norm_type env)
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   416
   in (map subst cs', map subst intr_ts')
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   417
   end) handle Type.TUNIFY =>
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   418
     (warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts));
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   419
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   420
fun import_intros inp_pred [] ctxt =
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   421
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   422
    val ([outp_pred], ctxt') = Variable.import_terms true [inp_pred] ctxt
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   423
    val T = fastype_of outp_pred
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   424
    (* TODO: put in a function for this next line! *)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   425
    val paramTs = ho_argsT_of (hd (all_modes_of_typ T)) (binder_types T)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   426
    val (param_names, ctxt'') = Variable.variant_fixes
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   427
      (map (fn i => "p" ^ (string_of_int i)) (1 upto (length paramTs))) ctxt'
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
   428
    val params = map2 (curry Free) param_names paramTs
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   429
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   430
    (((outp_pred, params), []), ctxt')
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   431
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   432
  | import_intros inp_pred (th :: ths) ctxt =
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   433
    let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   434
      val ((_, [th']), ctxt') = Variable.import true [th] ctxt
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   435
      val thy = ProofContext.theory_of ctxt'
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   436
      val (pred, args) = strip_intro_concl th'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   437
      val T = fastype_of pred
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   438
      val ho_args = ho_args_of (hd (all_modes_of_typ T)) args
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   439
      fun subst_of (pred', pred) =
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   440
        let
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   441
          val subst = Sign.typ_match thy (fastype_of pred', fastype_of pred) Vartab.empty
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   442
        in map (fn (indexname, (s, T)) => ((indexname, s), T)) (Vartab.dest subst) end
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   443
      fun instantiate_typ th =
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   444
        let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   445
          val (pred', _) = strip_intro_concl th
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   446
          val _ = if not (fst (dest_Const pred) = fst (dest_Const pred')) then
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
   447
            raise Fail "Trying to instantiate another predicate" else ()
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   448
        in Thm.certify_instantiate (subst_of (pred', pred), []) th end;
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   449
      fun instantiate_ho_args th =
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   450
        let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   451
          val (_, args') = (strip_comb o HOLogic.dest_Trueprop o Logic.strip_imp_concl o prop_of) th
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   452
          val ho_args' = map dest_Var (ho_args_of (hd (all_modes_of_typ T)) args')
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   453
        in Thm.certify_instantiate ([], ho_args' ~~ ho_args) th end
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   454
      val outp_pred =
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   455
        Term_Subst.instantiate (subst_of (inp_pred, pred), []) inp_pred
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   456
      val ((_, ths'), ctxt1) =
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   457
        Variable.import false (map (instantiate_typ #> instantiate_ho_args) ths) ctxt'
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   458
    in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   459
      (((outp_pred, ho_args), th' :: ths'), ctxt1)
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   460
    end
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   461
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   462
(* generation of case rules from user-given introduction rules *)
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   463
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   464
fun mk_args2 (Type ("*", [T1, T2])) st =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   465
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   466
      val (t1, st') = mk_args2 T1 st
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   467
      val (t2, st'') = mk_args2 T2 st'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   468
    in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   469
      (HOLogic.mk_prod (t1, t2), st'')
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   470
    end
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   471
  (*| mk_args2 (T as Type ("fun", _)) (params, ctxt) = 
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   472
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   473
      val (S, U) = strip_type T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   474
    in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   475
      if U = HOLogic.boolT then
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   476
        (hd params, (tl params, ctxt))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   477
      else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   478
        let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   479
          val ([x], ctxt') = Variable.variant_fixes ["x"] ctxt
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   480
        in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   481
          (Free (x, T), (params, ctxt'))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   482
        end
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   483
    end*)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   484
  | mk_args2 T (params, ctxt) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   485
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   486
      val ([x], ctxt') = Variable.variant_fixes ["x"] ctxt
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   487
    in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   488
      (Free (x, T), (params, ctxt'))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   489
    end
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   490
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   491
fun mk_casesrule ctxt pred introrules =
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   492
  let
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   493
    (* TODO: can be simplified if parameters are not treated specially ? *)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   494
    val (((pred, params), intros_th), ctxt1) = import_intros pred introrules ctxt
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   495
    (* TODO: distinct required ? -- test case with more than one parameter! *)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   496
    val params = distinct (op aconv) params
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   497
    val intros = map prop_of intros_th
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   498
    val ([propname], ctxt2) = Variable.variant_fixes ["thesis"] ctxt1
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   499
    val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   500
    val argsT = binder_types (fastype_of pred)
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   501
    (* TODO: can be simplified if parameters are not treated specially ? <-- see uncommented code! *)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   502
    val (argvs, _) = fold_map mk_args2 argsT (params, ctxt2)
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   503
    fun mk_case intro =
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   504
      let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   505
        val (_, args) = (strip_comb o HOLogic.dest_Trueprop o Logic.strip_imp_concl) intro
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   506
        val prems = Logic.strip_imp_prems intro
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   507
        val eqprems =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   508
          map2 (HOLogic.mk_Trueprop oo (curry HOLogic.mk_eq)) argvs args
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   509
        val frees = map Free (fold Term.add_frees (args @ prems) [])
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   510
      in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   511
    val assm = HOLogic.mk_Trueprop (list_comb (pred, argvs))
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   512
    val cases = map mk_case intros
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   513
  in Logic.list_implies (assm :: cases, prop) end;
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33128
diff changeset
   514
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   515
fun dest_conjunct_prem th =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   516
  case HOLogic.dest_Trueprop (prop_of th) of
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   517
    (Const ("op &", _) $ t $ t') =>
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   518
      dest_conjunct_prem (th RS @{thm conjunct1})
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   519
        @ dest_conjunct_prem (th RS @{thm conjunct2})
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   520
    | _ => [th]
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   521
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   522
fun prove_casesrule ctxt (pred, (pre_cases_rule, nparams)) cases_rule =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   523
  let
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   524
    val thy = ProofContext.theory_of ctxt
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   525
    val nargs = length (binder_types (fastype_of pred))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   526
    fun PEEK f dependent_tactic st = dependent_tactic (f st) st
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   527
    fun meta_eq_of th = th RS @{thm eq_reflection}
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   528
    val tuple_rew_rules = map meta_eq_of [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}]
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   529
    fun instantiate i n {context = ctxt, params = p, prems = prems,
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   530
      asms = a, concl = cl, schematics = s}  =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   531
      let
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   532
        val (cases, (eqs, prems)) = apsnd (chop (nargs - nparams)) (chop n prems)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   533
        val case_th = MetaSimplifier.simplify true
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   534
        (@{thm Predicate.eq_is_eq} :: map meta_eq_of eqs)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   535
          (nth cases (i - 1))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   536
        val prems' = maps (dest_conjunct_prem o MetaSimplifier.simplify true tuple_rew_rules) prems
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   537
        val pats = map (swap o HOLogic.dest_eq o HOLogic.dest_Trueprop) (take nargs (prems_of case_th))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   538
        val (_, tenv) = fold (Pattern.match thy) pats (Vartab.empty, Vartab.empty)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   539
        fun term_pair_of (ix, (ty,t)) = (Var (ix,ty), t)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   540
        val inst = map (pairself (cterm_of thy) o term_pair_of) (Vartab.dest tenv)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   541
        val thesis = Thm.instantiate ([], inst) case_th OF (replicate nargs @{thm refl}) OF prems'
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   542
      in
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   543
        (rtac thesis 1)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   544
      end
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   545
    val tac =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   546
      etac pre_cases_rule 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   547
      THEN
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   548
      (PEEK nprems_of
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   549
        (fn n =>
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   550
          ALLGOALS (fn i =>
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   551
            MetaSimplifier.rewrite_goal_tac [@{thm split_paired_all}] i
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   552
            THEN (SUBPROOF (instantiate i n) ctxt i))))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   553
  in
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   554
    Goal.prove ctxt (Term.add_free_names cases_rule []) [] cases_rule (fn _ => tac)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   555
  end
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   556
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   557
(** preprocessing rules **)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   558
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   559
fun imp_prems_conv cv ct =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   560
  case Thm.term_of ct of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   561
    Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   562
  | _ => Conv.all_conv ct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   563
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   564
fun Trueprop_conv cv ct =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   565
  case Thm.term_of ct of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   566
    Const ("Trueprop", _) $ _ => Conv.arg_conv cv ct  
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
   567
  | _ => raise Fail "Trueprop_conv"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   568
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   569
fun preprocess_intro thy rule =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   570
  Conv.fconv_rule
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   571
    (imp_prems_conv
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   572
      (Trueprop_conv (Conv.try_conv (Conv.rewr_conv (Thm.symmetric @{thm Predicate.eq_is_eq})))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   573
    (Thm.transfer thy rule)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   574
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   575
fun preprocess_elim thy elimrule =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   576
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   577
    fun replace_eqs (Const ("Trueprop", _) $ (Const ("op =", T) $ lhs $ rhs)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   578
       HOLogic.mk_Trueprop (Const (@{const_name Predicate.eq}, T) $ lhs $ rhs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   579
     | replace_eqs t = t
33128
1f990689349f further cleaning up
bulwahn
parents: 33127
diff changeset
   580
    val ctxt = ProofContext.init thy
1f990689349f further cleaning up
bulwahn
parents: 33127
diff changeset
   581
    val ((_, [elimrule]), ctxt') = Variable.import false [elimrule] ctxt
1f990689349f further cleaning up
bulwahn
parents: 33127
diff changeset
   582
    val prems = Thm.prems_of elimrule
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   583
    val nargs = length (snd (strip_comb (HOLogic.dest_Trueprop (hd prems))))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   584
    fun preprocess_case t =
33128
1f990689349f further cleaning up
bulwahn
parents: 33127
diff changeset
   585
      let
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   586
       val params = Logic.strip_params t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   587
       val (assums1, assums2) = chop nargs (Logic.strip_assums_hyp t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   588
       val assums_hyp' = assums1 @ (map replace_eqs assums2)
33128
1f990689349f further cleaning up
bulwahn
parents: 33127
diff changeset
   589
      in
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   590
       list_all (params, Logic.list_implies (assums_hyp', Logic.strip_assums_concl t))
33128
1f990689349f further cleaning up
bulwahn
parents: 33127
diff changeset
   591
      end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   592
    val cases' = map preprocess_case (tl prems)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   593
    val elimrule' = Logic.list_implies ((hd prems) :: cases', Thm.concl_of elimrule)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   594
    val bigeq = (Thm.symmetric (Conv.implies_concl_conv
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   595
      (MetaSimplifier.rewrite true [@{thm Predicate.eq_is_eq}])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   596
        (cterm_of thy elimrule')))
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   597
    val tac = (fn _ => Skip_Proof.cheat_tac thy)
33109
7025bc7a5054 changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents: 33108
diff changeset
   598
    val eq = Goal.prove ctxt' [] [] (Logic.mk_equals ((Thm.prop_of elimrule), elimrule')) tac
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   599
  in
33109
7025bc7a5054 changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents: 33108
diff changeset
   600
    Thm.equal_elim eq elimrule |> singleton (Variable.export ctxt' ctxt)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   601
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   602
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
   603
fun expand_tuples_elim th = th
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
   604
35887
f704ba9875f6 making flat triples to nested tuple to remove general triple functions
bulwahn
parents: 35886
diff changeset
   605
val no_compilation = ([], ([], []))
33483
a15a2f7f7560 improved handling of already defined functions in the predicate compiler; could cause trouble before when no modes for a predicate were infered
bulwahn
parents: 33482
diff changeset
   606
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   607
fun fetch_pred_data thy name =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   608
  case try (Inductive.the_inductive (ProofContext.init thy)) name of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   609
    SOME (info as (_, result)) => 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   610
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   611
        fun is_intro_of intro =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   612
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   613
            val (const, _) = strip_comb (HOLogic.dest_Trueprop (concl_of intro))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   614
          in (fst (dest_Const const) = name) end;      
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   615
        val intros =
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
   616
          (map (expand_tuples thy #> preprocess_intro thy) (filter is_intro_of (#intrs result)))
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   617
        val index = find_index (fn s => s = name) (#names (fst info))
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   618
        val pre_elim = nth (#elims result) index
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   619
        val pred = nth (#preds result) index
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   620
        val nparams = length (Inductive.params_of (#raw_induct result))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   621
        val ctxt = ProofContext.init thy
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   622
        val elim_t = mk_casesrule ctxt pred intros
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
   623
        val elim =
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
   624
          prove_casesrule ctxt (pred, (pre_elim, nparams)) elim_t
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   625
      in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   626
        mk_pred_data ((intros, SOME elim), no_compilation)
33483
a15a2f7f7560 improved handling of already defined functions in the predicate compiler; could cause trouble before when no modes for a predicate were infered
bulwahn
parents: 33482
diff changeset
   627
      end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   628
  | NONE => error ("No such predicate: " ^ quote name)
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
   629
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   630
fun add_predfun_data name mode data =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   631
  let
35887
f704ba9875f6 making flat triples to nested tuple to remove general triple functions
bulwahn
parents: 35886
diff changeset
   632
    val add = (apsnd o apsnd o apfst) (cons (mode, mk_predfun_data data))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   633
  in PredData.map (Graph.map_node name (map_pred_data add)) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   634
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   635
fun is_inductive_predicate thy name =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   636
  is_some (try (Inductive.the_inductive (ProofContext.init thy)) name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   637
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   638
fun depending_preds_of thy (key, value) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   639
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   640
    val intros = (#intros o rep_pred_data) value
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   641
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   642
    fold Term.add_const_names (map Thm.prop_of intros) []
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
   643
      |> filter (fn c => (not (c = key)) andalso
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
   644
        (is_inductive_predicate thy c orelse is_registered thy c))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   645
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   646
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
   647
fun add_intro thm thy =
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
   648
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   649
    val (name, T) = dest_Const (fst (strip_intro_concl thm))
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
   650
    fun cons_intro gr =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   651
     case try (Graph.get_node gr) name of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   652
       SOME pred_data => Graph.map_node name (map_pred_data
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   653
         (apfst (fn (intros, elim) => (intros @ [thm], elim)))) gr
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   654
     | NONE => Graph.new_node (name, mk_pred_data (([thm], NONE), no_compilation)) gr
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   655
  in PredData.map cons_intro thy end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   656
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
   657
fun set_elim thm =
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
   658
  let
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   659
    val (name, _) = dest_Const (fst 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   660
      (strip_comb (HOLogic.dest_Trueprop (hd (prems_of thm)))))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   661
    fun set (intros, _) = (intros, SOME thm)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   662
  in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   663
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   664
fun register_predicate (constname, pre_intros, pre_elim) thy =
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
   665
  let
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   666
    val intros = map (preprocess_intro thy) pre_intros
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   667
    val elim = preprocess_elim thy pre_elim
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   668
  in
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   669
    if not (member (op =) (Graph.keys (PredData.get thy)) constname) then
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   670
      PredData.map
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
   671
        (Graph.new_node (constname,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   672
          mk_pred_data ((intros, SOME elim), no_compilation))) thy
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   673
    else thy
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   674
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   675
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   676
fun register_intros (constname, pre_intros) thy =
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   677
  let
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   678
    val T = Sign.the_const_type thy constname
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   679
    fun constname_of_intro intr = fst (dest_Const (fst (strip_intro_concl intr)))
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   680
    val _ = if not (forall (fn intr => constname_of_intro intr = constname) pre_intros) then
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   681
      error ("register_intros: Introduction rules of different constants are used\n" ^
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   682
        "expected rules for " ^ constname ^ ", but received rules for " ^
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   683
          commas (map constname_of_intro pre_intros))
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   684
      else ()
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
   685
    val pred = Const (constname, T)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   686
    val pre_elim = 
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 34974
diff changeset
   687
      (Drule.export_without_context o Skip_Proof.make_thm thy)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   688
      (mk_casesrule (ProofContext.init thy) pred pre_intros)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   689
  in register_predicate (constname, pre_intros, pre_elim) thy end
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   690
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   691
fun defined_function_of compilation pred =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   692
  let
35887
f704ba9875f6 making flat triples to nested tuple to remove general triple functions
bulwahn
parents: 35886
diff changeset
   693
    val set = (apsnd o apfst) (cons (compilation, []))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   694
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   695
    PredData.map (Graph.map_node pred (map_pred_data set))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   696
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   697
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   698
fun set_function_name compilation pred mode name =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   699
  let
35887
f704ba9875f6 making flat triples to nested tuple to remove general triple functions
bulwahn
parents: 35886
diff changeset
   700
    val set = (apsnd o apfst)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   701
      (AList.map_default (op =) (compilation, [(mode, name)]) (cons (mode, name)))
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   702
  in
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   703
    PredData.map (Graph.map_node pred (map_pred_data set))
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   704
  end
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   705
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   706
fun set_needs_random name modes =
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   707
  let
35887
f704ba9875f6 making flat triples to nested tuple to remove general triple functions
bulwahn
parents: 35886
diff changeset
   708
    val set = (apsnd o apsnd o apsnd) (K modes)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   709
  in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   710
    PredData.map (Graph.map_node name (map_pred_data set))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   711
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   712
36038
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   713
(* registration of alternative function names *)
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   714
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   715
structure Alt_Compilations_Data = Theory_Data
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   716
(
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   717
  type T = (mode * (compilation_funs -> typ -> term)) list Symtab.table;
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   718
  val empty = Symtab.empty;
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   719
  val extend = I;
36056
0c128c2c310d made smlnj happy
bulwahn
parents: 36048
diff changeset
   720
  val merge = Symtab.merge ((K true)
0c128c2c310d made smlnj happy
bulwahn
parents: 36048
diff changeset
   721
    : ((mode * (compilation_funs -> typ -> term)) list *
0c128c2c310d made smlnj happy
bulwahn
parents: 36048
diff changeset
   722
      (mode * (compilation_funs -> typ -> term)) list -> bool));
36038
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   723
);
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   724
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   725
fun alternative_compilation_of thy pred_name mode =
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   726
  AList.lookup eq_mode (Symtab.lookup_list (Alt_Compilations_Data.get thy) pred_name) mode
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   727
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   728
fun force_modes_and_compilations pred_name compilations =
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   729
  let
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   730
    (* thm refl is a dummy thm *)
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   731
    val modes = map fst compilations
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   732
    val (needs_random, non_random_modes) = pairself (map fst)
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   733
      (List.partition (fn (m, (fun_name, random)) => random) compilations)
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   734
    val non_random_dummys = map (rpair "dummy") non_random_modes
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   735
    val all_dummys = map (rpair "dummy") modes
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   736
    val dummy_function_names = map (rpair all_dummys) Predicate_Compile_Aux.random_compilations
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   737
      @ map (rpair non_random_dummys) Predicate_Compile_Aux.non_random_compilations
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   738
    val alt_compilations = map (apsnd fst) compilations
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   739
  in
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   740
    PredData.map (Graph.new_node
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   741
      (pred_name, mk_pred_data (([], SOME @{thm refl}), (dummy_function_names, ([], needs_random)))))
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   742
    #> Alt_Compilations_Data.map (Symtab.insert (K false) (pred_name, alt_compilations))
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   743
  end
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   744
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   745
fun functional_compilation fun_name mode compfuns T =
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   746
  let
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   747
    val (inpTs, outpTs) = split_map_modeT (fn _ => fn T => (SOME T, NONE))
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   748
      mode (binder_types T)
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   749
    val bs = map (pair "x") inpTs
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   750
    val bounds = map Bound (rev (0 upto (length bs) - 1))
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   751
    val f = Const (fun_name, inpTs ---> HOLogic.mk_tupleT outpTs)
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   752
  in list_abs (bs, mk_single compfuns (list_comb (f, bounds))) end
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   753
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   754
fun register_alternative_function pred_name mode fun_name =
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   755
  Alt_Compilations_Data.map (Symtab.insert_list (eq_pair eq_mode (K false))
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   756
    (pred_name, (mode, functional_compilation fun_name mode)))
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   757
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   758
fun force_modes_and_functions pred_name fun_names =
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   759
  force_modes_and_compilations pred_name
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   760
    (map (fn (mode, (fun_name, random)) => (mode, (functional_compilation fun_name mode, random)))
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   761
    fun_names)
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   762
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   763
(* compilation modifiers *)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   764
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   765
structure Comp_Mod =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   766
struct
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   767
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   768
datatype comp_modifiers = Comp_Modifiers of
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   769
{
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   770
  compilation : compilation,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   771
  function_name_prefix : string,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   772
  compfuns : compilation_funs,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   773
  mk_random : typ -> term list -> term,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   774
  modify_funT : typ -> typ,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   775
  additional_arguments : string list -> term list,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   776
  wrap_compilation : compilation_funs -> string -> typ -> mode -> term list -> term -> term,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   777
  transform_additional_arguments : indprem -> term list -> term list
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   778
}
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   779
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   780
fun dest_comp_modifiers (Comp_Modifiers c) = c
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   781
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   782
val compilation = #compilation o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   783
val function_name_prefix = #function_name_prefix o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   784
val compfuns = #compfuns o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   785
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   786
val mk_random = #mk_random o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   787
val funT_of' = funT_of o compfuns
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   788
val modify_funT = #modify_funT o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   789
fun funT_of comp mode = modify_funT comp o funT_of' comp mode
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   790
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   791
val additional_arguments = #additional_arguments o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   792
val wrap_compilation = #wrap_compilation o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   793
val transform_additional_arguments = #transform_additional_arguments o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   794
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   795
end;
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   796
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   797
val depth_limited_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   798
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   799
  compilation = Depth_Limited,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   800
  function_name_prefix = "depth_limited_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   801
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   802
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   803
  additional_arguments = fn names =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   804
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   805
      val depth_name = Name.variant names "depth"
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   806
    in [Free (depth_name, @{typ code_numeral})] end,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   807
  modify_funT = (fn T => let val (Ts, U) = strip_type T
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   808
  val Ts' = [@{typ code_numeral}] in (Ts @ Ts') ---> U end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   809
  wrap_compilation =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   810
    fn compfuns => fn s => fn T => fn mode => fn additional_arguments => fn compilation =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   811
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   812
      val [depth] = additional_arguments
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   813
      val (_, Ts) = split_modeT' mode (binder_types T)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   814
      val T' = mk_predT compfuns (HOLogic.mk_tupleT Ts)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   815
      val if_const = Const (@{const_name "If"}, @{typ bool} --> T' --> T' --> T')
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   816
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   817
      if_const $ HOLogic.mk_eq (depth, @{term "0 :: code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   818
        $ mk_bot compfuns (dest_predT compfuns T')
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   819
        $ compilation
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   820
    end,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   821
  transform_additional_arguments =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   822
    fn prem => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   823
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   824
      val [depth] = additional_arguments
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   825
      val depth' =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   826
        Const (@{const_name Groups.minus}, @{typ "code_numeral => code_numeral => code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   827
          $ depth $ Const (@{const_name Groups.one}, @{typ "Code_Numeral.code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   828
    in [depth'] end
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   829
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   830
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   831
val random_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   832
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   833
  compilation = Random,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   834
  function_name_prefix = "random_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   835
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   836
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   837
  list_comb (Const(@{const_name Quickcheck.iter},
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   838
  [@{typ code_numeral}, @{typ code_numeral}, @{typ Random.seed}] ---> 
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   839
    PredicateCompFuns.mk_predT T), additional_arguments)),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   840
  modify_funT = (fn T =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   841
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   842
      val (Ts, U) = strip_type T
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   843
      val Ts' = [@{typ code_numeral}, @{typ code_numeral}, @{typ "code_numeral * code_numeral"}]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   844
    in (Ts @ Ts') ---> U end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   845
  additional_arguments = (fn names =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   846
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   847
      val [nrandom, size, seed] = Name.variant_list names ["nrandom", "size", "seed"]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   848
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   849
      [Free (nrandom, @{typ code_numeral}), Free (size, @{typ code_numeral}),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   850
        Free (seed, @{typ "code_numeral * code_numeral"})]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   851
    end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   852
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   853
    : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   854
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   855
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   856
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   857
val depth_limited_random_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   858
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   859
  compilation = Depth_Limited_Random,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   860
  function_name_prefix = "depth_limited_random_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   861
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   862
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   863
  list_comb (Const(@{const_name Quickcheck.iter},
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   864
  [@{typ code_numeral}, @{typ code_numeral}, @{typ Random.seed}] ---> 
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   865
    PredicateCompFuns.mk_predT T), tl additional_arguments)),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   866
  modify_funT = (fn T =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   867
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   868
      val (Ts, U) = strip_type T
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   869
      val Ts' = [@{typ code_numeral}, @{typ code_numeral}, @{typ code_numeral},
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   870
        @{typ "code_numeral * code_numeral"}]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   871
    in (Ts @ Ts') ---> U end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   872
  additional_arguments = (fn names =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   873
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   874
      val [depth, nrandom, size, seed] = Name.variant_list names ["depth", "nrandom", "size", "seed"]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   875
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   876
      [Free (depth, @{typ code_numeral}), Free (nrandom, @{typ code_numeral}),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   877
        Free (size, @{typ code_numeral}), Free (seed, @{typ "code_numeral * code_numeral"})]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   878
    end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   879
  wrap_compilation =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   880
  fn compfuns => fn s => fn T => fn mode => fn additional_arguments => fn compilation =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   881
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   882
      val depth = hd (additional_arguments)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   883
      val (_, Ts) = split_map_modeT (fn m => fn T => (SOME (funT_of compfuns m T), NONE))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   884
        mode (binder_types T)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   885
      val T' = mk_predT compfuns (HOLogic.mk_tupleT Ts)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   886
      val if_const = Const (@{const_name "If"}, @{typ bool} --> T' --> T' --> T')
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   887
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   888
      if_const $ HOLogic.mk_eq (depth, @{term "0 :: code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   889
        $ mk_bot compfuns (dest_predT compfuns T')
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   890
        $ compilation
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   891
    end,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   892
  transform_additional_arguments =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   893
    fn prem => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   894
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   895
      val [depth, nrandom, size, seed] = additional_arguments
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   896
      val depth' =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   897
        Const (@{const_name Groups.minus}, @{typ "code_numeral => code_numeral => code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   898
          $ depth $ Const (@{const_name Groups.one}, @{typ "Code_Numeral.code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   899
    in [depth', nrandom, size, seed] end
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   900
}
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   901
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   902
val predicate_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   903
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   904
  compilation = Pred,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   905
  function_name_prefix = "",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   906
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   907
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   908
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   909
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   910
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   911
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   912
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   913
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   914
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   915
val annotated_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   916
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   917
  compilation = Annotated,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   918
  function_name_prefix = "annotated_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   919
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   920
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   921
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   922
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   923
  wrap_compilation =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   924
    fn compfuns => fn s => fn T => fn mode => fn additional_arguments => fn compilation =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   925
      mk_tracing ("calling predicate " ^ s ^
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   926
        " with mode " ^ string_of_mode mode) compilation,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   927
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   928
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   929
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   930
val dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   931
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   932
  compilation = DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   933
  function_name_prefix = "dseq_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   934
  compfuns = DSequence_CompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   935
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   936
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   937
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   938
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   939
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   940
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   941
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   942
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   943
val pos_random_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   944
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   945
  compilation = Pos_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   946
  function_name_prefix = "random_dseq_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   947
  compfuns = Random_Sequence_CompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   948
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   949
  let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   950
    val random = Const ("Quickcheck.random_class.random",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   951
      @{typ code_numeral} --> @{typ Random.seed} -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   952
        HOLogic.mk_prodT (HOLogic.mk_prodT (T, @{typ "unit => term"}), @{typ Random.seed}))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   953
  in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   954
    Const ("Random_Sequence.Random", (@{typ code_numeral} --> @{typ Random.seed} -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   955
      HOLogic.mk_prodT (HOLogic.mk_prodT (T, @{typ "unit => term"}), @{typ Random.seed})) -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   956
      Random_Sequence_CompFuns.mk_random_dseqT T) $ random
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   957
  end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   958
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   959
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   960
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   961
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   962
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   963
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   964
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   965
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   966
val neg_random_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   967
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   968
  compilation = Neg_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   969
  function_name_prefix = "random_dseq_neg_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   970
  compfuns = Random_Sequence_CompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   971
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   972
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   973
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   974
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   975
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   976
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   977
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   978
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   979
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   980
val new_pos_random_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   981
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   982
  compilation = New_Pos_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   983
  function_name_prefix = "new_random_dseq_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   984
  compfuns = New_Pos_Random_Sequence_CompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   985
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   986
  let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   987
    val random = Const ("Quickcheck.random_class.random",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   988
      @{typ code_numeral} --> @{typ Random.seed} -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   989
        HOLogic.mk_prodT (HOLogic.mk_prodT (T, @{typ "unit => term"}), @{typ Random.seed}))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   990
  in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   991
    Const ("New_Random_Sequence.Random", (@{typ code_numeral} --> @{typ Random.seed} -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   992
      HOLogic.mk_prodT (HOLogic.mk_prodT (T, @{typ "unit => term"}), @{typ Random.seed})) -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   993
      New_Pos_Random_Sequence_CompFuns.mk_pos_random_dseqT T) $ random
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   994
  end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   995
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   996
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   997
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   998
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   999
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1000
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1001
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1002
val new_neg_random_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1003
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1004
  compilation = New_Neg_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1005
  function_name_prefix = "new_random_dseq_neg_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1006
  compfuns = New_Neg_Random_Sequence_CompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1007
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1008
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1009
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1010
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1011
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1012
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1013
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1014
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1015
fun negative_comp_modifiers_of comp_modifiers =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1016
    (case Comp_Mod.compilation comp_modifiers of
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1017
      Pos_Random_DSeq => neg_random_dseq_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1018
    | Neg_Random_DSeq => pos_random_dseq_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1019
    | New_Pos_Random_DSeq => new_neg_random_dseq_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1020
    | New_Neg_Random_DSeq => new_pos_random_dseq_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1021
    | c => comp_modifiers)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1022
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1023
(** mode analysis **)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1024
35411
cafb74a131da made smlnj happy
bulwahn
parents: 35402
diff changeset
  1025
type mode_analysis_options = {use_random : bool, reorder_premises : bool, infer_pos_and_neg_modes : bool}
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1026
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1027
fun is_constrt thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1028
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1029
    val cnstrs = flat (maps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1030
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1031
      (Symtab.dest (Datatype.get_all thy)));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1032
    fun check t = (case strip_comb t of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1033
        (Free _, []) => true
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1034
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1035
            (SOME (i, Tname), Type (Tname', _)) =>
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1036
              length ts = i andalso Tname = Tname' andalso forall check ts
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1037
          | _ => false)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1038
      | _ => false)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1039
  in check end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1040
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1041
(*** check if a type is an equality type (i.e. doesn't contain fun)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1042
  FIXME this is only an approximation ***)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1043
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1044
  | is_eqT _ = true;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1045
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1046
fun term_vs tm = fold_aterms (fn Free (x, T) => cons x | _ => I) tm [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1047
val terms_vs = distinct (op =) o maps term_vs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1048
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1049
(** collect all Frees in a term (with duplicates!) **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1050
fun term_vTs tm =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1051
  fold_aterms (fn Free xT => cons xT | _ => I) tm [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1052
33138
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1053
fun subsets i j =
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1054
  if i <= j then
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1055
    let
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1056
      fun merge xs [] = xs
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1057
        | merge [] ys = ys
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1058
        | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys)
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1059
            else y::merge (x::xs) ys;
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1060
      val is = subsets (i+1) j
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1061
    in merge (map (fn ks => i::ks) is) is end
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1062
  else [[]];
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1063
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1064
fun print_failed_mode options thy modes p (pol, m) rs is =
33130
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
  1065
  if show_mode_inference options then
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
  1066
    let
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1067
      val _ = tracing ("Clauses " ^ commas (map (fn i => string_of_int (i + 1)) is) ^ " of " ^
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1068
        p ^ " violates mode " ^ string_of_mode m)
33130
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
  1069
    in () end
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
  1070
  else ()
7eac458c2b22 added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents: 33129
diff changeset
  1071
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1072
fun error_of p (pol, m) is =
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
  1073
  "  Clauses " ^ commas (map (fn i => string_of_int (i + 1)) is) ^ " of " ^
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
  1074
        p ^ " violates mode " ^ string_of_mode m
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1075
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1076
fun is_all_input mode =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1077
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1078
    fun is_all_input' (Fun _) = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1079
      | is_all_input' (Pair (m1, m2)) = is_all_input' m1 andalso is_all_input' m2
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1080
      | is_all_input' Input = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1081
      | is_all_input' Output = false
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1082
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1083
    forall is_all_input' (strip_fun_mode mode)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1084
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1085
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1086
fun all_input_of T =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1087
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1088
    val (Ts, U) = strip_type T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1089
    fun input_of (Type ("*", [T1, T2])) = Pair (input_of T1, input_of T2)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1090
      | input_of _ = Input
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1091
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1092
    if U = HOLogic.boolT then
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1093
      fold_rev (curry Fun) (map input_of Ts) Bool
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1094
    else
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
  1095
      raise Fail "all_input_of: not a predicate"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1096
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1097
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1098
fun partial_hd [] = NONE
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1099
  | partial_hd (x :: xs) = SOME x
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1100
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1101
fun term_vs tm = fold_aterms (fn Free (x, T) => cons x | _ => I) tm [];
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1102
val terms_vs = distinct (op =) o maps term_vs;
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1103
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1104
fun input_mode T =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1105
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1106
    val (Ts, U) = strip_type T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1107
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1108
    fold_rev (curry Fun) (map (K Input) Ts) Input
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1109
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1110
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1111
fun output_mode T =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1112
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1113
    val (Ts, U) = strip_type T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1114
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1115
    fold_rev (curry Fun) (map (K Output) Ts) Output
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1116
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1117
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1118
fun is_invertible_function ctxt (Const (f, _)) = is_constr ctxt f
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1119
  | is_invertible_function ctxt _ = false
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1120
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1121
fun non_invertible_subterms ctxt (t as Free _) = []
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1122
  | non_invertible_subterms ctxt t = 
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1123
  let
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1124
    val (f, args) = strip_comb t
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1125
  in
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1126
    if is_invertible_function ctxt f then
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1127
      maps (non_invertible_subterms ctxt) args
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1128
    else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1129
      [t]
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1130
  end
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1131
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1132
fun collect_non_invertible_subterms ctxt (f as Free _) (names, eqs) = (f, (names, eqs))
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1133
  | collect_non_invertible_subterms ctxt t (names, eqs) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1134
    case (strip_comb t) of (f, args) =>
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1135
      if is_invertible_function ctxt f then
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1136
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1137
            val (args', (names', eqs')) =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1138
              fold_map (collect_non_invertible_subterms ctxt) args (names, eqs)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1139
          in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1140
            (list_comb (f, args'), (names', eqs'))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1141
          end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1142
        else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1143
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1144
            val s = Name.variant names "x"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1145
            val v = Free (s, fastype_of t)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1146
          in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1147
            (v, (s :: names, HOLogic.mk_eq (v, t) :: eqs))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1148
          end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1149
(*
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1150
  if is_constrt thy t then (t, (names, eqs)) else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1151
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1152
      val s = Name.variant names "x"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1153
      val v = Free (s, fastype_of t)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1154
    in (v, (s::names, HOLogic.mk_eq (v, t)::eqs)) end;
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1155
*)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1156
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1157
fun is_possible_output thy vs t =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1158
  forall
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1159
    (fn t => is_eqT (fastype_of t) andalso forall (member (op =) vs) (term_vs t))
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1160
      (non_invertible_subterms (ProofContext.init thy) t)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1161
  andalso
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1162
    (forall (is_eqT o snd)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1163
      (inter (fn ((f', _), f) => f = f') vs (Term.add_frees t [])))
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1164
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1165
fun vars_of_destructable_term ctxt (Free (x, _)) = [x]
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1166
  | vars_of_destructable_term ctxt t =
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1167
  let
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1168
    val (f, args) = strip_comb t
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1169
  in
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1170
    if is_invertible_function ctxt f then
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1171
      maps (vars_of_destructable_term ctxt) args
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1172
    else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1173
      []
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1174
  end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1175
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1176
fun is_constructable thy vs t = forall (member (op =) vs) (term_vs t)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1177
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1178
fun missing_vars vs t = subtract (op =) vs (term_vs t)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1179
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1180
fun output_terms (Const ("Pair", _) $ t1 $ t2, Mode_Pair (d1, d2)) =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1181
    output_terms (t1, d1)  @ output_terms (t2, d2)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1182
  | output_terms (t1 $ t2, Mode_App (d1, d2)) =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1183
    output_terms (t1, d1)  @ output_terms (t2, d2)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1184
  | output_terms (t, Term Output) = [t]
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1185
  | output_terms _ = []
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1186
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1187
fun lookup_mode modes (Const (s, T)) =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1188
   (case (AList.lookup (op =) modes s) of
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1189
      SOME ms => SOME (map (fn m => (Context m, [])) ms)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1190
    | NONE => NONE)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1191
  | lookup_mode modes (Free (x, _)) =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1192
    (case (AList.lookup (op =) modes x) of
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1193
      SOME ms => SOME (map (fn m => (Context m , [])) ms)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1194
    | NONE => NONE)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1195
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1196
fun derivations_of (thy : theory) modes vs (Const ("Pair", _) $ t1 $ t2) (Pair (m1, m2)) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1197
    map_product
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1198
      (fn (m1, mvars1) => fn (m2, mvars2) => (Mode_Pair (m1, m2), union (op =) mvars1 mvars2))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1199
        (derivations_of thy modes vs t1 m1) (derivations_of thy modes vs t2 m2)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1200
  | derivations_of thy modes vs t (m as Fun _) =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1201
    (*let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1202
      val (p, args) = strip_comb t
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1203
    in
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1204
      (case lookup_mode modes p of
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1205
        SOME ms => map_filter (fn (Context m, []) => let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1206
          val ms = strip_fun_mode m
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1207
          val (argms, restms) = chop (length args) ms
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1208
          val m' = fold_rev (curry Fun) restms Bool
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1209
        in
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1210
          if forall (fn m => eq_mode (Input, m)) argms andalso eq_mode (m', mode) then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1211
            SOME (fold (curry Mode_App) (map Term argms) (Context m), missing_vars vs t)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1212
          else NONE
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1213
        end) ms
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1214
      | NONE => (if is_all_input mode then [(Context mode, [])] else []))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1215
    end*)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1216
    (case try (all_derivations_of thy modes vs) t  of
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1217
      SOME derivs =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1218
        filter (fn (d, mvars) => eq_mode (mode_of d, m) andalso null (output_terms (t, d))) derivs
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1219
    | NONE => (if is_all_input m then [(Context m, [])] else []))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1220
  | derivations_of thy modes vs t m =
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1221
    if eq_mode (m, Input) then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1222
      [(Term Input, missing_vars vs t)]
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1223
    else if eq_mode (m, Output) then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1224
      (if is_possible_output thy vs t then [(Term Output, [])] else [])
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1225
    else []
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1226
and all_derivations_of thy modes vs (Const ("Pair", _) $ t1 $ t2) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1227
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1228
    val derivs1 = all_derivations_of thy modes vs t1
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1229
    val derivs2 = all_derivations_of thy modes vs t2
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1230
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1231
    map_product
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1232
      (fn (m1, mvars1) => fn (m2, mvars2) => (Mode_Pair (m1, m2), union (op =) mvars1 mvars2))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1233
        derivs1 derivs2
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1234
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1235
  | all_derivations_of thy modes vs (t1 $ t2) =
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  1236
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1237
    val derivs1 = all_derivations_of thy modes vs t1
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1238
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1239
    maps (fn (d1, mvars1) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1240
      case mode_of d1 of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1241
        Fun (m', _) => map (fn (d2, mvars2) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1242
          (Mode_App (d1, d2), union (op =) mvars1 mvars2)) (derivations_of thy modes vs t2 m')
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
  1243
        | _ => raise Fail "Something went wrong") derivs1
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1244
  end
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1245
  | all_derivations_of thy modes vs (Const (s, T)) = the (lookup_mode modes (Const (s, T)))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1246
  | all_derivations_of thy modes vs (Free (x, T)) = the (lookup_mode modes (Free (x, T)))
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
  1247
  | all_derivations_of _ modes vs _ = raise Fail "all_derivations_of"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1248
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1249
fun rev_option_ord ord (NONE, NONE) = EQUAL
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1250
  | rev_option_ord ord (NONE, SOME _) = GREATER
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1251
  | rev_option_ord ord (SOME _, NONE) = LESS
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1252
  | rev_option_ord ord (SOME x, SOME y) = ord (x, y)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1253
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1254
fun term_of_prem (Prem t) = t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1255
  | term_of_prem (Negprem t) = t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1256
  | term_of_prem (Sidecond t) = t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1257
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1258
fun random_mode_in_deriv modes t deriv =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1259
  case try dest_Const (fst (strip_comb t)) of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1260
    SOME (s, _) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1261
      (case AList.lookup (op =) modes s of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1262
        SOME ms =>
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1263
          (case AList.lookup (op =) (map (fn ((p, m), r) => (m, r)) ms) (head_mode_of deriv) of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1264
            SOME r => r
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1265
          | NONE => false)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1266
      | NONE => false)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1267
  | NONE => false
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1268
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1269
fun number_of_output_positions mode =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1270
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1271
    val args = strip_fun_mode mode
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1272
    fun contains_output (Fun _) = false
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1273
      | contains_output Input = false
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1274
      | contains_output Output = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1275
      | contains_output (Pair (m1, m2)) = contains_output m1 orelse contains_output m2
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1276
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1277
    length (filter contains_output args)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1278
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1279
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1280
fun lex_ord ord1 ord2 (x, x') =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1281
  case ord1 (x, x') of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1282
    EQUAL => ord2 (x, x')
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1283
  | ord => ord
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1284
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1285
fun lexl_ord [] (x, x') = EQUAL
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1286
  | lexl_ord (ord :: ords') (x, x') =
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1287
    case ord (x, x') of
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1288
      EQUAL => lexl_ord ords' (x, x')
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1289
    | ord => ord
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1290
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1291
fun deriv_ord2' thy pol pred modes t1 t2 ((deriv1, mvars1), (deriv2, mvars2)) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1292
  let
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1293
    (* prefer functional modes if it is a function *)
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1294
    fun fun_ord ((t1, deriv1, mvars1), (t2, deriv2, mvars2)) =
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1295
      let
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1296
        fun is_functional t mode =
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1297
          case try (fst o dest_Const o fst o strip_comb) t of
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1298
            NONE => false
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1299
          | SOME c => is_some (alternative_compilation_of thy c mode)
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1300
      in
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1301
        case (is_functional t1 (head_mode_of deriv1), is_functional t2 (head_mode_of deriv2)) of
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1302
          (true, true) => EQUAL
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1303
        | (true, false) => LESS
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1304
        | (false, true) => GREATER
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1305
        | (false, false) => EQUAL
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1306
      end
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1307
    (* prefer modes without requirement for generating random values *)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1308
    fun mvars_ord ((t1, deriv1, mvars1), (t2, deriv2, mvars2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1309
      int_ord (length mvars1, length mvars2)
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1310
    (* prefer non-random modes *)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1311
    fun random_mode_ord ((t1, deriv1, mvars1), (t2, deriv2, mvars2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1312
      int_ord (if random_mode_in_deriv modes t1 deriv1 then 1 else 0,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1313
        if random_mode_in_deriv modes t1 deriv1 then 1 else 0)
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1314
    (* prefer modes with more input and less output *)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1315
    fun output_mode_ord ((t1, deriv1, mvars1), (t2, deriv2, mvars2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1316
      int_ord (number_of_output_positions (head_mode_of deriv1),
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1317
        number_of_output_positions (head_mode_of deriv2))
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1318
    (* prefer recursive calls *)
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1319
    fun is_rec_premise t =
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1320
      case fst (strip_comb t) of Const (c, T) => c = pred | _ => false
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1321
    fun recursive_ord ((t1, deriv1, mvars1), (t2, deriv2, mvars2)) =
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1322
      int_ord (if is_rec_premise t1 then 0 else 1,
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1323
        if is_rec_premise t2 then 0 else 1)
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1324
    val ord = lexl_ord [mvars_ord, fun_ord, random_mode_ord, output_mode_ord, recursive_ord]
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1325
  in
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1326
    ord ((t1, deriv1, mvars1), (t2, deriv2, mvars2))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1327
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1328
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1329
fun deriv_ord2 thy pol pred modes t = deriv_ord2' thy pol pred modes t t
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1330
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1331
fun deriv_ord ((deriv1, mvars1), (deriv2, mvars2)) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1332
  int_ord (length mvars1, length mvars2)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1333
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1334
fun premise_ord thy pol pred modes ((prem1, a1), (prem2, a2)) =
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1335
  rev_option_ord (deriv_ord2' thy pol pred modes (term_of_prem prem1) (term_of_prem prem2)) (a1, a2)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1336
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1337
fun print_mode_list modes =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1338
  tracing ("modes: " ^ (commas (map (fn (s, ms) => s ^ ": " ^
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1339
    commas (map (fn (m, r) => string_of_mode m ^ (if r then " random " else " not ")) ms)) modes)))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1340
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1341
fun select_mode_prem (mode_analysis_options : mode_analysis_options) (thy : theory) pred
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1342
  pol (modes, (pos_modes, neg_modes)) vs ps =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1343
  let
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1344
    fun choose_mode_of_prem (Prem t) = partial_hd
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1345
        (sort (deriv_ord2 thy pol pred modes t) (all_derivations_of thy pos_modes vs t))
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1346
      | choose_mode_of_prem (Sidecond t) = SOME (Context Bool, missing_vars vs t)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1347
      | choose_mode_of_prem (Negprem t) = partial_hd
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1348
          (sort (deriv_ord2 thy (not pol) pred modes t)
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1349
          (filter (fn (d, missing_vars) => is_all_input (head_mode_of d))
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1350
             (all_derivations_of thy neg_modes vs t)))
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
  1351
      | choose_mode_of_prem p = raise Fail ("choose_mode_of_prem: " ^ string_of_prem thy p)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1352
  in
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1353
    if #reorder_premises mode_analysis_options then
36247
bcf23027bca2 prefer functional modes of functions in the mode analysis
bulwahn
parents: 36056
diff changeset
  1354
      partial_hd (sort (premise_ord thy pol pred modes) (ps ~~ map choose_mode_of_prem ps))
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1355
    else
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1356
      SOME (hd ps, choose_mode_of_prem (hd ps))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1357
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1358
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1359
fun check_mode_clause' (mode_analysis_options : mode_analysis_options) thy pred param_vs (modes :
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1360
  (string * ((bool * mode) * bool) list) list) ((pol, mode) : bool * mode) (ts, ps) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1361
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1362
    val vTs = distinct (op =) (fold Term.add_frees (map term_of_prem ps) (fold Term.add_frees ts []))
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1363
    val modes' = modes @ (param_vs ~~ map (fn x => [((true, x), false), ((false, x), false)]) (ho_arg_modes_of mode))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1364
    fun retrieve_modes_of_pol pol = map (fn (s, ms) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1365
      (s, map_filter (fn ((p, m), r) => if p = pol then SOME m else NONE | _ => NONE) ms))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1366
    val (pos_modes', neg_modes') =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1367
      if #infer_pos_and_neg_modes mode_analysis_options then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1368
        (retrieve_modes_of_pol pol modes', retrieve_modes_of_pol (not pol) modes')
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1369
      else
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1370
        let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1371
          val modes = map (fn (s, ms) => (s, map (fn ((p, m), r) => m) ms)) modes'
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1372
        in (modes, modes) end
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1373
    val (in_ts, out_ts) = split_mode mode ts
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1374
    val in_vs = maps (vars_of_destructable_term (ProofContext.init thy)) in_ts
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1375
    val out_vs = terms_vs out_ts
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1376
    fun known_vs_after p vs = (case p of
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1377
        Prem t => union (op =) vs (term_vs t)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1378
      | Sidecond t => union (op =) vs (term_vs t)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1379
      | Negprem t => union (op =) vs (term_vs t)
35885
7b39120a1494 some improvements thanks to Makarius source code review
bulwahn
parents: 35884
diff changeset
  1380
      | _ => raise Fail "I do not know")
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1381
    fun check_mode_prems acc_ps rnd vs [] = SOME (acc_ps, vs, rnd)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1382
      | check_mode_prems acc_ps rnd vs ps =
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1383
        (case
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1384
          (select_mode_prem mode_analysis_options thy pred pol (modes', (pos_modes', neg_modes')) vs ps) of
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1385
          SOME (p, SOME (deriv, [])) => check_mode_prems ((p, deriv) :: acc_ps) rnd
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1386
            (known_vs_after p vs) (filter_out (equal p) ps)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1387
        | SOME (p, SOME (deriv, missing_vars)) =>
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1388
          if #use_random mode_analysis_options andalso pol then
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1389
            check_mode_prems ((p, deriv) :: (map
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1390
              (fn v => (Generator (v, the (AList.lookup (op =) vTs v)), Term Output))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1391
                (distinct (op =) missing_vars))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1392
                @ acc_ps) true (known_vs_after p vs) (filter_out (equal p) ps)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1393
          else NONE
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1394
        | SOME (p, NONE) => NONE
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1395
        | NONE => NONE)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1396
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1397
    case check_mode_prems [] false in_vs ps of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1398
      NONE => NONE
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1399
    | SOME (acc_ps, vs, rnd) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1400
      if forall (is_constructable thy vs) (in_ts @ out_ts) then
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1401
        SOME (ts, rev acc_ps, rnd)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1402
      else
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1403
        if #use_random mode_analysis_options andalso pol then
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1404
          let
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1405
             val generators = map
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1406
              (fn v => (Generator (v, the (AList.lookup (op =) vTs v)), Term Output))
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1407
                (subtract (op =) vs (terms_vs (in_ts @ out_ts)))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1408
          in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1409
            SOME (ts, rev (generators @ acc_ps), true)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1410
          end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1411
        else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1412
          NONE
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1413
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1414
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1415
datatype result = Success of bool | Error of string
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1416
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1417
fun check_modes_pred' mode_analysis_options options thy param_vs clauses modes (p, (ms : ((bool * mode) * bool) list)) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1418
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1419
    fun split xs =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1420
      let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1421
        fun split' [] (ys, zs) = (rev ys, rev zs)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1422
          | split' ((m, Error z) :: xs) (ys, zs) = split' xs (ys, z :: zs)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1423
          | split' (((m : bool * mode), Success rnd) :: xs) (ys, zs) = split' xs ((m, rnd) :: ys, zs)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1424
       in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1425
         split' xs ([], [])
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1426
       end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1427
    val rs = these (AList.lookup (op =) clauses p)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1428
    fun check_mode m =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1429
      let
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1430
        val res = Output.cond_timeit false "work part of check_mode for one mode" (fn _ => 
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1431
          map (check_mode_clause' mode_analysis_options thy p param_vs modes m) rs)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1432
      in
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1433
        Output.cond_timeit false "aux part of check_mode for one mode" (fn _ => 
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1434
        case find_indices is_none res of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1435
          [] => Success (exists (fn SOME (_, _, true) => true | _ => false) res)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1436
        | is => (print_failed_mode options thy modes p m rs is; Error (error_of p m is)))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1437
      end
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1438
    val _ = if show_mode_inference options then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1439
        tracing ("checking " ^ string_of_int (length ms) ^ " modes ...")
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1440
      else ()
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1441
    val res = Output.cond_timeit false "check_mode" (fn _ => map (fn (m, _) => (m, check_mode m)) ms)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1442
    val (ms', errors) = split res
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1443
  in
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1444
    ((p, (ms' : ((bool * mode) * bool) list)), errors)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1445
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1446
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1447
fun get_modes_pred' mode_analysis_options thy param_vs clauses modes (p, ms) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1448
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1449
    val rs = these (AList.lookup (op =) clauses p)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1450
  in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1451
    (p, map (fn (m, rnd) =>
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1452
      (m, map
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1453
        ((fn (ts, ps, rnd) => (ts, ps)) o the o
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1454
          check_mode_clause' mode_analysis_options thy p param_vs modes m) rs)) ms)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1455
  end;
33137
0d16c07f8d24 added option to generate random values to values command in the predicate compiler
bulwahn
parents: 33135
diff changeset
  1456
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1457
fun fixp f (x : (string * ((bool * mode) * bool) list) list) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1458
  let val y = f x
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1459
  in if x = y then x else fixp f y end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1460
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1461
fun fixp_with_state f (x : (string * ((bool * mode) * bool) list) list, state) =
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1462
  let
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1463
    val (y, state') = f (x, state)
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1464
  in
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1465
    if x = y then (y, state') else fixp_with_state f (y, state')
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1466
  end
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1467
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1468
fun string_of_ext_mode ((pol, mode), rnd) =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1469
  string_of_mode mode ^ "(" ^ (if pol then "pos" else "neg") ^ ", "
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1470
  ^ (if rnd then "rnd" else "nornd") ^ ")"
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1471
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1472
fun print_extra_modes options modes =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1473
  if show_mode_inference options then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1474
    tracing ("Modes of inferred predicates: " ^
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1475
      cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map string_of_ext_mode ms)) modes))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1476
  else ()
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1477
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1478
fun infer_modes mode_analysis_options options compilation preds all_modes param_vs clauses thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1479
  let
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1480
    val collect_errors = false
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1481
    fun appair f (x1, x2) (y1, y2) = (f x1 y1, f x2 y2)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1482
    fun needs_random s (false, m) = ((false, m), false)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1483
      | needs_random s (true, m) = ((true, m), member (op =) (#needs_random (the_pred_data thy s)) m)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1484
    fun add_polarity_and_random_bit s b ms = map (fn m => needs_random s (b, m)) ms
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1485
    val prednames = map fst preds
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1486
    (* extramodes contains all modes of all constants, should we only use the necessary ones
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1487
       - what is the impact on performance? *)
36251
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1488
    fun predname_of (Prem t) =
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1489
        (case try dest_Const (fst (strip_comb t)) of SOME (c, _) => insert (op =) c | NONE => I)
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1490
      | predname_of (Negprem t) =
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1491
        (case try dest_Const (fst (strip_comb t)) of SOME (c, _) => insert (op =) c | NONE => I)
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1492
      | predname_of _ = I
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1493
    val relevant_prednames = fold (fn (_, clauses') =>
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1494
      fold (fn (_, ps) => fold Term.add_const_names (map dest_indprem ps)) clauses') clauses []
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1495
    val extra_modes =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1496
      if #infer_pos_and_neg_modes mode_analysis_options then
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1497
        let
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1498
          val pos_extra_modes =
36251
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1499
            map_filter (fn name => Option.map (pair name) (try (modes_of compilation thy) name))
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1500
            relevant_prednames
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1501
            (* all_modes_of compilation thy *)
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1502
            |> filter_out (fn (name, _) => member (op =) prednames name)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1503
          val neg_extra_modes =
36251
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1504
          map_filter (fn name => Option.map (pair name)
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1505
            (try (modes_of (negative_compilation_of compilation) thy) name))
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1506
            relevant_prednames
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1507
          (*all_modes_of (negative_compilation_of compilation) thy*)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1508
            |> filter_out (fn (name, _) => member (op =) prednames name)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1509
        in
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1510
          map (fn (s, ms) => (s, (add_polarity_and_random_bit s true ms)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1511
                @ add_polarity_and_random_bit s false (the (AList.lookup (op =) neg_extra_modes s))))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1512
            pos_extra_modes
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1513
        end
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1514
      else
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1515
        map (fn (s, ms) => (s, (add_polarity_and_random_bit s true ms)))
36251
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1516
          (map_filter (fn name => Option.map (pair name) (try (modes_of compilation thy) name))
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1517
            relevant_prednames
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1518
          (*all_modes_of compilation thy*)
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1519
          |> filter_out (fn (name, _) => member (op =) prednames name))
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1520
    val _ = print_extra_modes options extra_modes
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1521
    val start_modes =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1522
      if #infer_pos_and_neg_modes mode_analysis_options then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1523
        map (fn (s, ms) => (s, map (fn m => ((true, m), false)) ms @
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1524
          (map (fn m => ((false, m), false)) ms))) all_modes
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1525
      else
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1526
        map (fn (s, ms) => (s, map (fn m => ((true, m), false)) ms)) all_modes
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1527
    fun iteration modes = map
36028
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1528
      (check_modes_pred' mode_analysis_options options thy param_vs clauses
3837493fe4ab prefer recursive calls before others in the mode inference
bulwahn
parents: 36027
diff changeset
  1529
        (modes @ extra_modes)) modes
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1530
    val ((modes : (string * ((bool * mode) * bool) list) list), errors) =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1531
      Output.cond_timeit false "Fixpount computation of mode analysis" (fn () =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1532
      if collect_errors then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1533
        fixp_with_state (fn (modes, errors) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1534
          let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1535
            val (modes', new_errors) = split_list (iteration modes)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1536
          in (modes', errors @ flat new_errors) end) (start_modes, [])
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1537
        else
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1538
          (fixp (fn modes => map fst (iteration modes)) start_modes, []))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1539
    val moded_clauses = map (get_modes_pred' mode_analysis_options thy param_vs clauses
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1540
      (modes @ extra_modes)) modes
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1541
    val thy' = fold (fn (s, ms) => if member (op =) (map fst preds) s then
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1542
      set_needs_random s (map_filter (fn ((true, m), true) => SOME m | _ => NONE) ms) else I)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1543
      modes thy
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1544
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1545
  in
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1546
    ((moded_clauses, errors), thy')
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1547
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1548
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1549
(* term construction *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1550
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1551
fun mk_v (names, vs) s T = (case AList.lookup (op =) vs s of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1552
      NONE => (Free (s, T), (names, (s, [])::vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1553
    | SOME xs =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1554
        let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1555
          val s' = Name.variant names s;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1556
          val v = Free (s', T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1557
        in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1558
          (v, (s'::names, AList.update (op =) (s, v::xs) vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1559
        end);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1560
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1561
fun distinct_v (Free (s, T)) nvs = mk_v nvs s T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1562
  | distinct_v (t $ u) nvs =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1563
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1564
        val (t', nvs') = distinct_v t nvs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1565
        val (u', nvs'') = distinct_v u nvs';
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1566
      in (t' $ u', nvs'') end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1567
  | distinct_v x nvs = (x, nvs);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1568
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1569
(** specific rpred functions -- move them to the correct place in this file *)
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1570
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1571
fun mk_Eval_of additional_arguments ((x, T), NONE) names = (x, names)
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1572
  | mk_Eval_of additional_arguments ((x, T), SOME mode) names =
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1573
  let
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1574
    val Ts = binder_types T
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1575
    fun mk_split_lambda [] t = lambda (Free (Name.variant names "x", HOLogic.unitT)) t
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1576
      | mk_split_lambda [x] t = lambda x t
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1577
      | mk_split_lambda xs t =
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1578
      let
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1579
        fun mk_split_lambda' (x::y::[]) t = HOLogic.mk_split (lambda x (lambda y t))
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1580
          | mk_split_lambda' (x::xs) t = HOLogic.mk_split (lambda x (mk_split_lambda' xs t))
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1581
      in
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1582
        mk_split_lambda' xs t
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1583
      end;
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1584
    fun mk_arg (i, T) =
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1585
      let
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1586
        val vname = Name.variant names ("x" ^ string_of_int i)
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1587
        val default = Free (vname, T)
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1588
      in 
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1589
        case AList.lookup (op =) mode i of
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1590
          NONE => (([], [default]), [default])
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1591
        | SOME NONE => (([default], []), [default])
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1592
        | SOME (SOME pis) =>
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1593
          case HOLogic.strip_tupleT T of
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1594
            [] => error "pair mode but unit tuple" (*(([default], []), [default])*)
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1595
          | [_] => error "pair mode but not a tuple" (*(([default], []), [default])*)
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1596
          | Ts =>
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1597
            let
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1598
              val vnames = Name.variant_list names
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1599
                (map (fn j => "x" ^ string_of_int i ^ "p" ^ string_of_int j)
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1600
                  (1 upto length Ts))
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  1601
              val args = map2 (curry Free) vnames Ts
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1602
              fun split_args (i, arg) (ins, outs) =
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1603
                if member (op =) pis i then
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1604
                  (arg::ins, outs)
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1605
                else
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1606
                  (ins, arg::outs)
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1607
              val (inargs, outargs) = fold_rev split_args ((1 upto length Ts) ~~ args) ([], [])
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1608
              fun tuple args = if null args then [] else [HOLogic.mk_tuple args]
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1609
            in ((tuple inargs, tuple outargs), args) end
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1610
      end
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1611
    val (inoutargs, args) = split_list (map mk_arg (1 upto (length Ts) ~~ Ts))
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1612
    val (inargs, outargs) = pairself flat (split_list inoutargs)
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1613
    val r = PredicateCompFuns.mk_Eval 
33148
0808f7d0d0d7 removed tuple functions from the predicate compiler
bulwahn
parents: 33147
diff changeset
  1614
      (list_comb (x, inargs @ additional_arguments), HOLogic.mk_tuple outargs)
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1615
    val t = fold_rev mk_split_lambda args r
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1616
  in
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1617
    (t, names)
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1618
  end;
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1619
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1620
(* TODO: uses param_vs -- change necessary for compilation with new modes *)
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1621
fun compile_arg compilation_modifiers additional_arguments ctxt param_vs iss arg = 
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1622
  let
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1623
    fun map_params (t as Free (f, T)) =
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1624
      if member (op =) param_vs f then
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1625
        case (AList.lookup (op =) (param_vs ~~ iss) f) of
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1626
          SOME is =>
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1627
            let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1628
              val _ = error "compile_arg: A parameter in a input position -- do we have a test case?"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1629
              val T' = Comp_Mod.funT_of compilation_modifiers is T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1630
            in t(*fst (mk_Eval_of additional_arguments ((Free (f, T'), T), is) [])*) end
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1631
        | NONE => t
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1632
      else t
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1633
      | map_params t = t
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1634
    in map_aterms map_params arg end
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1635
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1636
fun compile_match compilation_modifiers additional_arguments
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1637
  param_vs iss ctxt eqs eqs' out_ts success_t =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1638
  let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1639
    val compfuns = Comp_Mod.compfuns compilation_modifiers
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1640
    val eqs'' = maps mk_eq eqs @ eqs'
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1641
    val eqs'' =
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1642
      map (compile_arg compilation_modifiers additional_arguments ctxt param_vs iss) eqs''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1643
    val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1644
    val name = Name.variant names "x";
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1645
    val name' = Name.variant (name :: names) "y";
33148
0808f7d0d0d7 removed tuple functions from the predicate compiler
bulwahn
parents: 33147
diff changeset
  1646
    val T = HOLogic.mk_tupleT (map fastype_of out_ts);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1647
    val U = fastype_of success_t;
36038
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
  1648
    val U' = dest_predT compfuns U;        
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1649
    val v = Free (name, T);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1650
    val v' = Free (name', T);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1651
  in
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1652
    lambda v (fst (Datatype.make_case ctxt Datatype_Case.Quiet [] v
33148
0808f7d0d0d7 removed tuple functions from the predicate compiler
bulwahn
parents: 33147
diff changeset
  1653
      [(HOLogic.mk_tuple out_ts,
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1654
        if null eqs'' then success_t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1655
        else Const (@{const_name HOL.If}, HOLogic.boolT --> U --> U --> U) $
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1656
          foldr1 HOLogic.mk_conj eqs'' $ success_t $
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1657
            mk_bot compfuns U'),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1658
       (v', mk_bot compfuns U')]))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1659
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1660
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1661
fun string_of_tderiv ctxt (t, deriv) = 
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1662
  (case (t, deriv) of
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1663
    (t1 $ t2, Mode_App (deriv1, deriv2)) =>
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1664
      string_of_tderiv ctxt (t1, deriv1) ^ " $ " ^ string_of_tderiv ctxt (t2, deriv2)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1665
  | (Const ("Pair", _) $ t1 $ t2, Mode_Pair (deriv1, deriv2)) =>
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1666
    "(" ^ string_of_tderiv ctxt (t1, deriv1) ^ ", " ^ string_of_tderiv ctxt (t2, deriv2) ^ ")"
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1667
  | (t, Term Input) => Syntax.string_of_term ctxt t ^ "[Input]"
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1668
  | (t, Term Output) => Syntax.string_of_term ctxt t ^ "[Output]"
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1669
  | (t, Context m) => Syntax.string_of_term ctxt t ^ "[" ^ string_of_mode m ^ "]")
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1670
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1671
fun compile_expr compilation_modifiers ctxt (t, deriv) additional_arguments =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1672
  let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1673
    val compfuns = Comp_Mod.compfuns compilation_modifiers
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1674
    fun expr_of (t, deriv) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1675
      (case (t, deriv) of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1676
        (t, Term Input) => SOME t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1677
      | (t, Term Output) => NONE
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1678
      | (Const (name, T), Context mode) =>
36038
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
  1679
        (case alternative_compilation_of (ProofContext.theory_of ctxt) name mode of
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
  1680
          SOME alt_comp => SOME (alt_comp compfuns T)
36034
ee84eac07290 added book-keeping, registration and compilation with alternative functions for predicates in the predicate compiler
bulwahn
parents: 36031
diff changeset
  1681
        | NONE =>
ee84eac07290 added book-keeping, registration and compilation with alternative functions for predicates in the predicate compiler
bulwahn
parents: 36031
diff changeset
  1682
          SOME (Const (function_name_of (Comp_Mod.compilation compilation_modifiers)
ee84eac07290 added book-keeping, registration and compilation with alternative functions for predicates in the predicate compiler
bulwahn
parents: 36031
diff changeset
  1683
            (ProofContext.theory_of ctxt) name mode,
ee84eac07290 added book-keeping, registration and compilation with alternative functions for predicates in the predicate compiler
bulwahn
parents: 36031
diff changeset
  1684
            Comp_Mod.funT_of compilation_modifiers mode T)))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1685
      | (Free (s, T), Context m) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1686
        SOME (Free (s, Comp_Mod.funT_of compilation_modifiers m T))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1687
      | (t, Context m) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1688
        let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1689
          val bs = map (pair "x") (binder_types (fastype_of t))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1690
          val bounds = map Bound (rev (0 upto (length bs) - 1))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1691
        in SOME (list_abs (bs, mk_if compfuns (list_comb (t, bounds)))) end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1692
      | (Const ("Pair", _) $ t1 $ t2, Mode_Pair (d1, d2)) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1693
        (case (expr_of (t1, d1), expr_of (t2, d2)) of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1694
          (NONE, NONE) => NONE
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1695
        | (NONE, SOME t) => SOME t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1696
        | (SOME t, NONE) => SOME t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1697
        | (SOME t1, SOME t2) => SOME (HOLogic.mk_prod (t1, t2)))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1698
      | (t1 $ t2, Mode_App (deriv1, deriv2)) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1699
        (case (expr_of (t1, deriv1), expr_of (t2, deriv2)) of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1700
          (SOME t, NONE) => SOME t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1701
         | (SOME t, SOME u) => SOME (t $ u)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1702
         | _ => error "something went wrong here!"))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1703
  in
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1704
    list_comb (the (expr_of (t, deriv)), additional_arguments)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1705
  end
33145
1a22f7ca1dfc removed dead code; added examples
bulwahn
parents: 33144
diff changeset
  1706
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1707
fun compile_clause compilation_modifiers ctxt all_vs param_vs additional_arguments
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1708
  mode inp (ts, moded_ps) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1709
  let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1710
    val compfuns = Comp_Mod.compfuns compilation_modifiers
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1711
    val iss = ho_arg_modes_of mode
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1712
    val compile_match = compile_match compilation_modifiers
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1713
      additional_arguments param_vs iss ctxt
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1714
    val (in_ts, out_ts) = split_mode mode ts;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1715
    val (in_ts', (all_vs', eqs)) =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1716
      fold_map (collect_non_invertible_subterms ctxt) in_ts (all_vs, []);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1717
    fun compile_prems out_ts' vs names [] =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1718
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1719
            val (out_ts'', (names', eqs')) =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1720
              fold_map (collect_non_invertible_subterms ctxt) out_ts' (names, []);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1721
            val (out_ts''', (names'', constr_vs)) = fold_map distinct_v
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1722
              out_ts'' (names', map (rpair []) vs);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1723
          in
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1724
            compile_match constr_vs (eqs @ eqs') out_ts'''
33148
0808f7d0d0d7 removed tuple functions from the predicate compiler
bulwahn
parents: 33147
diff changeset
  1725
              (mk_single compfuns (HOLogic.mk_tuple out_ts))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1726
          end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1727
      | compile_prems out_ts vs names ((p, deriv) :: ps) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1728
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1729
            val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1730
            val (out_ts', (names', eqs)) =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1731
              fold_map (collect_non_invertible_subterms ctxt) out_ts (names, [])
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1732
            val (out_ts'', (names'', constr_vs')) = fold_map distinct_v
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1733
              out_ts' ((names', map (rpair []) vs))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1734
            val mode = head_mode_of deriv
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1735
            val additional_arguments' =
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1736
              Comp_Mod.transform_additional_arguments compilation_modifiers p additional_arguments
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1737
            val (compiled_clause, rest) = case p of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1738
               Prem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1739
                 let
33138
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
  1740
                   val u =
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1741
                     compile_expr compilation_modifiers ctxt (t, deriv) additional_arguments'
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1742
                   val (_, out_ts''') = split_mode mode (snd (strip_comb t))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1743
                   val rest = compile_prems out_ts''' vs' names'' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1744
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1745
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1746
                 end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1747
             | Negprem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1748
                 let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1749
                   val neg_compilation_modifiers =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1750
                     negative_comp_modifiers_of compilation_modifiers
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1751
                   val u = mk_not compfuns
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1752
                     (compile_expr neg_compilation_modifiers ctxt (t, deriv) additional_arguments')
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1753
                   val (_, out_ts''') = split_mode mode (snd (strip_comb t))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1754
                   val rest = compile_prems out_ts''' vs' names'' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1755
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1756
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1757
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1758
             | Sidecond t =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1759
                 let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1760
                   val t = compile_arg compilation_modifiers additional_arguments
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1761
                     ctxt param_vs iss t
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1762
                   val rest = compile_prems [] vs' names'' ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1763
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1764
                   (mk_if compfuns t, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1765
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1766
             | Generator (v, T) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1767
                 let
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1768
                   val u = Comp_Mod.mk_random compilation_modifiers T additional_arguments
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1769
                   val rest = compile_prems [Free (v, T)]  vs' names'' ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1770
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1771
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1772
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1773
          in
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
  1774
            compile_match constr_vs' eqs out_ts''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1775
              (mk_bind compfuns (compiled_clause, rest))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1776
          end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1777
    val prem_t = compile_prems in_ts' param_vs all_vs' moded_ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1778
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1779
    mk_bind compfuns (mk_single compfuns inp, prem_t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1780
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1781
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1782
fun compile_pred compilation_modifiers thy all_vs param_vs s T (pol, mode) moded_cls =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1783
  let
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1784
    val ctxt = ProofContext.init thy
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1785
    val compilation_modifiers = if pol then compilation_modifiers else
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1786
      negative_comp_modifiers_of compilation_modifiers
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1787
    val additional_arguments = Comp_Mod.additional_arguments compilation_modifiers
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1788
      (all_vs @ param_vs)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1789
    val compfuns = Comp_Mod.compfuns compilation_modifiers
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1790
    fun is_param_type (T as Type ("fun",[_ , T'])) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1791
      is_some (try (dest_predT compfuns) T) orelse is_param_type T'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1792
      | is_param_type T = is_some (try (dest_predT compfuns) T)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1793
    val (inpTs, outTs) = split_map_modeT (fn m => fn T => (SOME (funT_of compfuns m T), NONE)) mode
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1794
      (binder_types T)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1795
    val predT = mk_predT compfuns (HOLogic.mk_tupleT outTs)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1796
    val funT = Comp_Mod.funT_of compilation_modifiers mode T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1797
    
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1798
    val (in_ts, _) = fold_map (fold_map_aterms_prodT (curry HOLogic.mk_prod)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1799
      (fn T => fn (param_vs, names) =>
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1800
        if is_param_type T then
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1801
          (Free (hd param_vs, T), (tl param_vs, names))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1802
        else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1803
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1804
            val new = Name.variant names "x"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1805
          in (Free (new, T), (param_vs, new :: names)) end)) inpTs
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1806
        (param_vs, (all_vs @ param_vs))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1807
    val in_ts' = map_filter (map_filter_prod
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1808
      (fn t as Free (x, _) => if member (op =) param_vs x then NONE else SOME t | t => SOME t)) in_ts
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1809
    val cl_ts =
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1810
      map (compile_clause compilation_modifiers
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1811
        ctxt all_vs param_vs additional_arguments mode (HOLogic.mk_tuple in_ts')) moded_cls;
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1812
    val compilation = Comp_Mod.wrap_compilation compilation_modifiers compfuns
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1813
      s T mode additional_arguments
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  1814
      (if null cl_ts then
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1815
        mk_bot compfuns (HOLogic.mk_tupleT outTs)
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  1816
      else foldr1 (mk_sup compfuns) cl_ts)
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1817
    val fun_const =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
  1818
      Const (function_name_of (Comp_Mod.compilation compilation_modifiers)
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  1819
      (ProofContext.theory_of ctxt) s mode, funT)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1820
  in
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1821
    HOLogic.mk_Trueprop
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1822
      (HOLogic.mk_eq (list_comb (fun_const, in_ts @ additional_arguments), compilation))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1823
  end;
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1824
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1825
(* special setup for simpset *)                  
34974
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 34963
diff changeset
  1826
val HOL_basic_ss' = HOL_basic_ss addsimps (@{thms HOL.simp_thms} @ [@{thm Pair_eq}])
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1827
  setSolver (mk_solver "all_tac_solver" (fn _ => fn _ => all_tac))
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1828
  setSolver (mk_solver "True_solver" (fn _ => rtac @{thm TrueI}))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1829
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1830
(* Definition of executable functions and their intro and elim rules *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1831
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1832
fun print_arities arities = tracing ("Arities:\n" ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1833
  cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1834
    space_implode " -> " (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1835
      (fn NONE => "X" | SOME k' => string_of_int k')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1836
        (ks @ [SOME k]))) arities));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1837
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1838
fun split_lambda (x as Free _) t = lambda x t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1839
  | split_lambda (Const ("Pair", _) $ t1 $ t2) t =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1840
    HOLogic.mk_split (split_lambda t1 (split_lambda t2 t))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1841
  | split_lambda (Const ("Product_Type.Unity", _)) t = Abs ("x", HOLogic.unitT, t)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1842
  | split_lambda t _ = raise (TERM ("split_lambda", [t]))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1843
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1844
fun strip_split_abs (Const ("split", _) $ t) = strip_split_abs t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1845
  | strip_split_abs (Abs (_, _, t)) = strip_split_abs t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1846
  | strip_split_abs t = t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1847
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1848
fun mk_args is_eval (m as Pair (m1, m2), T as Type ("*", [T1, T2])) names =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1849
    if eq_mode (m, Input) orelse eq_mode (m, Output) then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1850
      let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1851
        val x = Name.variant names "x"
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1852
      in
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1853
        (Free (x, T), x :: names)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1854
      end
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1855
    else
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1856
      let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1857
        val (t1, names') = mk_args is_eval (m1, T1) names
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1858
        val (t2, names'') = mk_args is_eval (m2, T2) names'
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1859
      in
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1860
        (HOLogic.mk_prod (t1, t2), names'')
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  1861
      end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1862
  | mk_args is_eval ((m as Fun _), T) names =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1863
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1864
      val funT = funT_of PredicateCompFuns.compfuns m T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1865
      val x = Name.variant names "x"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1866
      val (args, _) = fold_map (mk_args is_eval) (strip_fun_mode m ~~ binder_types T) (x :: names)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1867
      val (inargs, outargs) = split_map_mode (fn _ => fn t => (SOME t, NONE)) m args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1868
      val t = fold_rev split_lambda args (PredicateCompFuns.mk_Eval
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1869
        (list_comb (Free (x, funT), inargs), HOLogic.mk_tuple outargs))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1870
    in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1871
      (if is_eval then t else Free (x, funT), x :: names)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1872
    end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1873
  | mk_args is_eval (_, T) names =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1874
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1875
      val x = Name.variant names "x"
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1876
    in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1877
      (Free (x, T), x :: names)
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1878
    end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1879
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1880
fun create_intro_elim_rule mode defthm mode_id funT pred thy =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1881
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1882
    val funtrm = Const (mode_id, funT)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1883
    val Ts = binder_types (fastype_of pred)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1884
    val (args, argnames) = fold_map (mk_args true) (strip_fun_mode mode ~~ Ts) []
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1885
    fun strip_eval _ t =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1886
      let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1887
        val t' = strip_split_abs t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1888
        val (r, _) = PredicateCompFuns.dest_Eval t'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1889
      in (SOME (fst (strip_comb r)), NONE) end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1890
    val (inargs, outargs) = split_map_mode strip_eval mode args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1891
    val eval_hoargs = ho_args_of mode args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1892
    val hoargTs = ho_argsT_of mode Ts
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1893
    val hoarg_names' =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1894
      Name.variant_list argnames ((map (fn i => "x" ^ string_of_int i)) (1 upto (length hoargTs)))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1895
    val hoargs' = map2 (curry Free) hoarg_names' hoargTs
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1896
    val args' = replace_ho_args mode hoargs' args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1897
    val predpropI = HOLogic.mk_Trueprop (list_comb (pred, args'))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1898
    val predpropE = HOLogic.mk_Trueprop (list_comb (pred, args))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1899
    val param_eqs = map2 (HOLogic.mk_Trueprop oo (curry HOLogic.mk_eq)) eval_hoargs hoargs'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1900
    val funpropE = HOLogic.mk_Trueprop (PredicateCompFuns.mk_Eval (list_comb (funtrm, inargs),
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1901
                    if null outargs then Free("y", HOLogic.unitT) else HOLogic.mk_tuple outargs))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1902
    val funpropI = HOLogic.mk_Trueprop (PredicateCompFuns.mk_Eval (list_comb (funtrm, inargs),
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1903
                     HOLogic.mk_tuple outargs))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1904
    val introtrm = Logic.list_implies (predpropI :: param_eqs, funpropI)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1905
    val simprules = [defthm, @{thm eval_pred},
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1906
      @{thm "split_beta"}, @{thm "fst_conv"}, @{thm "snd_conv"}, @{thm pair_collapse}]
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1907
    val unfolddef_tac = Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps simprules) 1
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1908
    val introthm = Goal.prove (ProofContext.init thy)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1909
      (argnames @ hoarg_names' @ ["y"]) [] introtrm (fn _ => unfolddef_tac)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1910
    val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT));
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1911
    val elimtrm = Logic.list_implies ([funpropE, Logic.mk_implies (predpropE, P)], P)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1912
    val elimthm = Goal.prove (ProofContext.init thy)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1913
      (argnames @ ["y", "P"]) [] elimtrm (fn _ => unfolddef_tac)
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1914
    val opt_neg_introthm =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1915
      if is_all_input mode then
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1916
        let
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1917
          val neg_predpropI = HOLogic.mk_Trueprop (HOLogic.mk_not (list_comb (pred, args')))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1918
          val neg_funpropI =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1919
            HOLogic.mk_Trueprop (PredicateCompFuns.mk_Eval
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1920
              (PredicateCompFuns.mk_not (list_comb (funtrm, inargs)), HOLogic.unit))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1921
          val neg_introtrm = Logic.list_implies (neg_predpropI :: param_eqs, neg_funpropI)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1922
          val tac =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1923
            Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1924
              (@{thm if_False} :: @{thm Predicate.not_pred_eq} :: simprules)) 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1925
            THEN rtac @{thm Predicate.singleI} 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1926
        in SOME (Goal.prove (ProofContext.init thy) (argnames @ hoarg_names') []
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1927
            neg_introtrm (fn _ => tac))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1928
        end
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1929
      else NONE
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1930
  in
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1931
    ((introthm, elimthm), opt_neg_introthm)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1932
  end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1933
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1934
fun create_constname_of_mode options thy prefix name T mode = 
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1935
  let
33626
42f69386943a new names for predicate functions in the predicate compiler
bulwahn
parents: 33623
diff changeset
  1936
    val system_proposal = prefix ^ (Long_Name.base_name name)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1937
      ^ "_" ^ ascii_string_of_mode mode
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1938
    val name = the_default system_proposal (proposed_names options name mode)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1939
  in
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1940
    Sign.full_bname thy name
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1941
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1942
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1943
fun create_definitions options preds (name, modes) thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1944
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1945
    val compfuns = PredicateCompFuns.compfuns
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1946
    val T = AList.lookup (op =) preds name |> the
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1947
    fun create_definition mode thy =
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1948
      let
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1949
        val mode_cname = create_constname_of_mode options thy "" name T mode
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1950
        val mode_cbasename = Long_Name.base_name mode_cname
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1951
        val funT = funT_of compfuns mode T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1952
        val (args, _) = fold_map (mk_args true) ((strip_fun_mode mode) ~~ (binder_types T)) []
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1953
        fun strip_eval m t =
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1954
          let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1955
            val t' = strip_split_abs t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1956
            val (r, _) = PredicateCompFuns.dest_Eval t'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1957
          in (SOME (fst (strip_comb r)), NONE) end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1958
        val (inargs, outargs) = split_map_mode strip_eval mode args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1959
        val predterm = fold_rev split_lambda inargs
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1960
          (PredicateCompFuns.mk_Enum (split_lambda (HOLogic.mk_tuple outargs)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1961
            (list_comb (Const (name, T), args))))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1962
        val lhs = Const (mode_cname, funT)
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1963
        val def = Logic.mk_equals (lhs, predterm)
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1964
        val ([definition], thy') = thy |>
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1965
          Sign.add_consts_i [(Binding.name mode_cbasename, funT, NoSyn)] |>
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1966
          PureThy.add_defs false [((Binding.name (mode_cbasename ^ "_def"), def), [])]
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1967
        val rules as ((intro, elim), _) =
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1968
          create_intro_elim_rule mode definition mode_cname funT (Const (name, T)) thy'
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1969
        in thy'
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1970
          |> set_function_name Pred name mode mode_cname
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  1971
          |> add_predfun_data name mode (definition, rules)
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1972
          |> PureThy.store_thm (Binding.name (mode_cbasename ^ "I"), intro) |> snd
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1973
          |> PureThy.store_thm (Binding.name (mode_cbasename ^ "E"), elim)  |> snd
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1974
          |> Theory.checkpoint
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1975
        end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1976
  in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1977
    thy |> defined_function_of Pred name |> fold create_definition modes
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1978
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1979
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1980
fun define_functions comp_modifiers compfuns options preds (name, modes) thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1981
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1982
    val T = AList.lookup (op =) preds name |> the
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1983
    fun create_definition mode thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1984
      let
33485
0df4f6e46e5e made definition of functions generically for the different instances
bulwahn
parents: 33484
diff changeset
  1985
        val function_name_prefix = Comp_Mod.function_name_prefix comp_modifiers
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1986
        val mode_cname = create_constname_of_mode options thy function_name_prefix name T mode
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1987
        val funT = Comp_Mod.funT_of comp_modifiers mode T
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1988
      in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1989
        thy |> Sign.add_consts_i [(Binding.name (Long_Name.base_name mode_cname), funT, NoSyn)]
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1990
        |> set_function_name (Comp_Mod.compilation comp_modifiers) name mode mode_cname
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1991
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1992
  in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1993
    thy
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1994
    |> defined_function_of (Comp_Mod.compilation comp_modifiers) name
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1995
    |> fold create_definition modes
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1996
  end;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1997
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1998
(* Proving equivalence of term *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1999
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2000
fun is_Type (Type _) = true
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2001
  | is_Type _ = false
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2002
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2003
(* returns true if t is an application of an datatype constructor *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2004
(* which then consequently would be splitted *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2005
(* else false *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2006
fun is_constructor thy t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2007
  if (is_Type (fastype_of t)) then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2008
    (case Datatype.get_info thy ((fst o dest_Type o fastype_of) t) of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2009
      NONE => false
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2010
    | SOME info => (let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2011
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2012
      val (c, _) = strip_comb t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2013
      in (case c of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2014
        Const (name, _) => name mem_string constr_consts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2015
        | _ => false) end))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2016
  else false
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2017
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2018
(* MAJOR FIXME:  prove_params should be simple
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2019
 - different form of introrule for parameters ? *)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2020
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2021
fun prove_param options ctxt nargs t deriv =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2022
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2023
    val  (f, args) = strip_comb (Envir.eta_contract t)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2024
    val mode = head_mode_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2025
    val param_derivations = param_derivations_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2026
    val ho_args = ho_args_of mode args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2027
    val f_tac = case f of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2028
      Const (name, T) => simp_tac (HOL_basic_ss addsimps 
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2029
         [@{thm eval_pred}, predfun_definition_of ctxt name mode,
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2030
         @{thm split_eta}, @{thm split_beta}, @{thm fst_conv},
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2031
         @{thm snd_conv}, @{thm pair_collapse}, @{thm Product_Type.split_conv}]) 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2032
    | Free _ =>
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2033
      Subgoal.FOCUS_PREMS (fn {context = ctxt, params = params, prems, asms, concl, schematics} =>
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2034
        let
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2035
          val prems' = maps dest_conjunct_prem (take nargs prems)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2036
        in
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2037
          MetaSimplifier.rewrite_goal_tac
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2038
            (map (fn th => th RS @{thm sym} RS @{thm eq_reflection}) prems') 1
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2039
        end) ctxt 1
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2040
    | Abs _ => raise Fail "prove_param: No valid parameter term"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2041
  in
33753
1344e9bb611e changing the proof procedure for parameters; adding a testcase for negation and parameters; adopting print_tac to the latest function print_tac' in the predicate compiler
bulwahn
parents: 33752
diff changeset
  2042
    REPEAT_DETERM (rtac @{thm ext} 1)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2043
    THEN print_tac options "prove_param"
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2044
    THEN f_tac 
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2045
    THEN print_tac options "after prove_param"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2046
    THEN (REPEAT_DETERM (atac 1))
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2047
    THEN (EVERY (map2 (prove_param options ctxt nargs) ho_args param_derivations))
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2048
    THEN REPEAT_DETERM (rtac @{thm refl} 1)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2049
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2050
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2051
fun prove_expr options ctxt nargs (premposition : int) (t, deriv) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2052
  case strip_comb t of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2053
    (Const (name, T), args) =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2054
      let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2055
        val mode = head_mode_of deriv
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2056
        val introrule = predfun_intro_of ctxt name mode
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2057
        val param_derivations = param_derivations_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2058
        val ho_args = ho_args_of mode args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2059
      in
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2060
        print_tac options "before intro rule:"
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2061
        THEN rtac introrule 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2062
        THEN print_tac options "after intro rule"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2063
        (* for the right assumption in first position *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2064
        THEN rotate_tac premposition 1
33753
1344e9bb611e changing the proof procedure for parameters; adding a testcase for negation and parameters; adopting print_tac to the latest function print_tac' in the predicate compiler
bulwahn
parents: 33752
diff changeset
  2065
        THEN atac 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2066
        THEN print_tac options "parameter goal"
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2067
        (* work with parameter arguments *)
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2068
        THEN (EVERY (map2 (prove_param options ctxt nargs) ho_args param_derivations))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2069
        THEN (REPEAT_DETERM (atac 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2070
      end
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2071
  | (Free _, _) =>
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2072
    print_tac options "proving parameter call.."
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2073
    THEN Subgoal.FOCUS_PREMS (fn {context = ctxt, params, prems, asms, concl, schematics} =>
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2074
        let
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2075
          val param_prem = nth prems premposition
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2076
          val (param, _) = strip_comb (HOLogic.dest_Trueprop (prop_of param_prem))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2077
          val prems' = maps dest_conjunct_prem (take nargs prems)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2078
          fun param_rewrite prem =
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2079
            param = snd (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of prem)))
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2080
          val SOME rew_eq = find_first param_rewrite prems'
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2081
          val param_prem' = MetaSimplifier.rewrite_rule
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2082
            (map (fn th => th RS @{thm eq_reflection})
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2083
              [rew_eq RS @{thm sym}, @{thm split_beta}, @{thm fst_conv}, @{thm snd_conv}])
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2084
            param_prem
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2085
        in
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2086
          rtac param_prem' 1
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2087
        end) ctxt 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2088
    THEN print_tac options "after prove parameter call"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2089
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2090
fun SOLVED tac st = FILTER (fn st' => nprems_of st' = nprems_of st - 1) tac st;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2091
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2092
fun SOLVEDALL tac st = FILTER (fn st' => nprems_of st' = 0) tac st
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2093
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2094
fun check_format ctxt st =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2095
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2096
    val concl' = Logic.strip_assums_concl (hd (prems_of st))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2097
    val concl = HOLogic.dest_Trueprop concl'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2098
    val expr = fst (strip_comb (fst (PredicateCompFuns.dest_Eval concl)))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2099
    fun valid_expr (Const (@{const_name Predicate.bind}, _)) = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2100
      | valid_expr (Const (@{const_name Predicate.single}, _)) = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2101
      | valid_expr _ = false
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2102
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2103
    if valid_expr expr then
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2104
      ((*tracing "expression is valid";*) Seq.single st)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2105
    else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2106
      ((*tracing "expression is not valid";*) Seq.empty) (*error "check_format: wrong format"*)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2107
  end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2108
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2109
fun prove_match options ctxt out_ts =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2110
  let
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2111
    val thy = ProofContext.theory_of ctxt
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2112
    fun get_case_rewrite t =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2113
      if (is_constructor thy t) then let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2114
        val case_rewrites = (#case_rewrites (Datatype.the_info thy
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2115
          ((fst o dest_Type o fastype_of) t)))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2116
        in case_rewrites @ maps get_case_rewrite (snd (strip_comb t)) end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2117
      else []
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2118
    val simprules = @{thm "unit.cases"} :: @{thm "prod.cases"} :: maps get_case_rewrite out_ts
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2119
  (* replace TRY by determining if it necessary - are there equations when calling compile match? *)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2120
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2121
     (* make this simpset better! *)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2122
    asm_full_simp_tac (HOL_basic_ss' addsimps simprules) 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2123
    THEN print_tac options "after prove_match:"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2124
    THEN (DETERM (TRY (EqSubst.eqsubst_tac ctxt [0] [@{thm HOL.if_P}] 1
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2125
           THEN (REPEAT_DETERM (rtac @{thm conjI} 1 THEN (SOLVED (asm_simp_tac HOL_basic_ss' 1))))
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2126
           THEN print_tac options "if condition to be solved:"
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2127
           THEN (SOLVED (asm_simp_tac HOL_basic_ss' 1 THEN print_tac options "after if simp; in SOLVED:"))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2128
           THEN check_format thy
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2129
           THEN print_tac options "after if simplification - a TRY block")))
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2130
    THEN print_tac options "after if simplification"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2131
  end;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2132
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2133
(* corresponds to compile_fun -- maybe call that also compile_sidecond? *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2134
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2135
fun prove_sidecond ctxt t =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2136
  let
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2137
    val thy = ProofContext.theory_of ctxt
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2138
    fun preds_of t nameTs = case strip_comb t of 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2139
      (f as Const (name, T), args) =>
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2140
        if is_registered thy name then (name, T) :: nameTs
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2141
          else fold preds_of args nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2142
      | _ => nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2143
    val preds = preds_of t []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2144
    val defs = map
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2145
      (fn (pred, T) => predfun_definition_of ctxt pred
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2146
        (all_input_of T))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2147
        preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2148
  in 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2149
    (* remove not_False_eq_True when simpset in prove_match is better *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2150
    simp_tac (HOL_basic_ss addsimps
34974
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 34963
diff changeset
  2151
      (@{thms HOL.simp_thms} @ (@{thm not_False_eq_True} :: @{thm eval_pred} :: defs))) 1 
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2152
    (* need better control here! *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2153
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2154
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2155
fun prove_clause options ctxt nargs mode (_, clauses) (ts, moded_ps) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2156
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2157
    val (in_ts, clause_out_ts) = split_mode mode ts;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2158
    fun prove_prems out_ts [] =
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2159
      (prove_match options ctxt out_ts)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2160
      THEN print_tac options "before simplifying assumptions"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2161
      THEN asm_full_simp_tac HOL_basic_ss' 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2162
      THEN print_tac options "before single intro rule"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2163
      THEN (rtac (if null clause_out_ts then @{thm singleI_unit} else @{thm singleI}) 1)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2164
    | prove_prems out_ts ((p, deriv) :: ps) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2165
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2166
        val premposition = (find_index (equal p) clauses) + nargs
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2167
        val mode = head_mode_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2168
        val rest_tac =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2169
          rtac @{thm bindI} 1
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2170
          THEN (case p of Prem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2171
            let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2172
              val (_, us) = strip_comb t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2173
              val (_, out_ts''') = split_mode mode us
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2174
              val rec_tac = prove_prems out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2175
            in
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2176
              print_tac options "before clause:"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2177
              (*THEN asm_simp_tac HOL_basic_ss 1*)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2178
              THEN print_tac options "before prove_expr:"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2179
              THEN prove_expr options ctxt nargs premposition (t, deriv)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2180
              THEN print_tac options "after prove_expr:"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2181
              THEN rec_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2182
            end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2183
          | Negprem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2184
            let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2185
              val (t, args) = strip_comb t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2186
              val (_, out_ts''') = split_mode mode args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2187
              val rec_tac = prove_prems out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2188
              val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE)
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2189
              val neg_intro_rule =
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2190
                Option.map (fn name =>
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2191
                  the (predfun_neg_intro_of ctxt name mode)) name
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2192
              val param_derivations = param_derivations_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2193
              val params = ho_args_of mode args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2194
            in
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2195
              print_tac options "before prove_neg_expr:"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2196
              THEN full_simp_tac (HOL_basic_ss addsimps
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2197
                [@{thm split_eta}, @{thm split_beta}, @{thm fst_conv},
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2198
                 @{thm snd_conv}, @{thm pair_collapse}, @{thm Product_Type.split_conv}]) 1
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2199
              THEN (if (is_some name) then
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2200
                  print_tac options "before applying not introduction rule"
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2201
                  THEN rotate_tac premposition 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2202
                  THEN etac (the neg_intro_rule) 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2203
                  THEN rotate_tac (~premposition) 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2204
                  THEN print_tac options "after applying not introduction rule"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2205
                  THEN (EVERY (map2 (prove_param options ctxt nargs) params param_derivations))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2206
                  THEN (REPEAT_DETERM (atac 1))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2207
                else
35884
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2208
                  rtac @{thm not_predI'} 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2209
                  (* test: *)
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2210
                  THEN dtac @{thm sym} 1
362bfc2ca0ee adding proof procedure for cases rule with tuples; adding introduction rule for negated premises; improving proof procedure with negated premises
bulwahn
parents: 35881
diff changeset
  2211
                  THEN asm_full_simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2212
                  THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2213
              THEN rec_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2214
            end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2215
          | Sidecond t =>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2216
           rtac @{thm if_predI} 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2217
           THEN print_tac options "before sidecond:"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2218
           THEN prove_sidecond ctxt t
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2219
           THEN print_tac options "after sidecond:"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2220
           THEN prove_prems [] ps)
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2221
      in (prove_match options ctxt out_ts)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2222
          THEN rest_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2223
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2224
    val prems_tac = prove_prems in_ts moded_ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2225
  in
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2226
    print_tac options "Proving clause..."
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2227
    THEN rtac @{thm bindI} 1
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2228
    THEN rtac @{thm singleI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2229
    THEN prems_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2230
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2231
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2232
fun select_sup 1 1 = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2233
  | select_sup _ 1 = [rtac @{thm supI1}]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2234
  | select_sup n i = (rtac @{thm supI2})::(select_sup (n - 1) (i - 1));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2235
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2236
fun prove_one_direction options ctxt clauses preds pred mode moded_clauses =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2237
  let
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2238
    val thy = ProofContext.theory_of ctxt
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2239
    val T = the (AList.lookup (op =) preds pred)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2240
    val nargs = length (binder_types T)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2241
    val pred_case_rule = the_elim_of thy pred
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2242
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2243
    REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"}))
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2244
    THEN print_tac options "before applying elim rule"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2245
    THEN etac (predfun_elim_of ctxt pred mode) 1
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2246
    THEN etac pred_case_rule 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2247
    THEN print_tac options "after applying elim rule"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2248
    THEN (EVERY (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2249
           (fn i => EVERY' (select_sup (length moded_clauses) i) i) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2250
             (1 upto (length moded_clauses))))
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2251
    THEN (EVERY (map2 (prove_clause options ctxt nargs mode) clauses moded_clauses))
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2252
    THEN print_tac options "proved one direction"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2253
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2254
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2255
(** Proof in the other direction **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2256
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2257
fun prove_match2 options ctxt out_ts =
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2258
  let
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2259
    val thy = ProofContext.theory_of ctxt
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2260
    fun split_term_tac (Free _) = all_tac
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2261
      | split_term_tac t =
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2262
        if (is_constructor thy t) then
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2263
          let
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2264
            val info = Datatype.the_info thy ((fst o dest_Type o fastype_of) t)
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2265
            val num_of_constrs = length (#case_rewrites info)
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2266
            val (_, ts) = strip_comb t
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2267
          in
35889
c1f86c5d3827 avoiding fishing for split_asm rule in the predicate compiler
bulwahn
parents: 35888
diff changeset
  2268
            print_tac options ("Term " ^ (Syntax.string_of_term ctxt t) ^ 
c1f86c5d3827 avoiding fishing for split_asm rule in the predicate compiler
bulwahn
parents: 35888
diff changeset
  2269
              "splitting with rules \n" ^ Display.string_of_thm ctxt (#split_asm info))
c1f86c5d3827 avoiding fishing for split_asm rule in the predicate compiler
bulwahn
parents: 35888
diff changeset
  2270
            THEN TRY ((Splitter.split_asm_tac [#split_asm info] 1)
c1f86c5d3827 avoiding fishing for split_asm rule in the predicate compiler
bulwahn
parents: 35888
diff changeset
  2271
              THEN (print_tac options "after splitting with split_asm rules")
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2272
            (* THEN (Simplifier.asm_full_simp_tac HOL_basic_ss 1)
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2273
              THEN (DETERM (TRY (etac @{thm Pair_inject} 1)))*)
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2274
              THEN (REPEAT_DETERM_N (num_of_constrs - 1)
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2275
                (etac @{thm botE} 1 ORELSE etac @{thm botE} 2)))
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2276
            THEN (assert_tac (Max_number_of_subgoals 2))
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2277
            THEN (EVERY (map split_term_tac ts))
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2278
          end
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2279
      else all_tac
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2280
  in
33148
0808f7d0d0d7 removed tuple functions from the predicate compiler
bulwahn
parents: 33147
diff changeset
  2281
    split_term_tac (HOLogic.mk_tuple out_ts)
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2282
    THEN (DETERM (TRY ((Splitter.split_asm_tac [@{thm "split_if_asm"}] 1)
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2283
    THEN (etac @{thm botE} 2))))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2284
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2285
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2286
(* VERY LARGE SIMILIRATIY to function prove_param 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2287
-- join both functions
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2288
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2289
(* TODO: remove function *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2290
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2291
fun prove_param2 options ctxt t deriv =
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2292
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2293
    val (f, args) = strip_comb (Envir.eta_contract t)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2294
    val mode = head_mode_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2295
    val param_derivations = param_derivations_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2296
    val ho_args = ho_args_of mode args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2297
    val f_tac = case f of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2298
        Const (name, T) => full_simp_tac (HOL_basic_ss addsimps 
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2299
           (@{thm eval_pred}::(predfun_definition_of ctxt name mode)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2300
           :: @{thm "Product_Type.split_conv"}::[])) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2301
      | Free _ => all_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2302
      | _ => error "prove_param2: illegal parameter term"
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2303
  in
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2304
    print_tac options "before simplification in prove_args:"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2305
    THEN f_tac
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2306
    THEN print_tac options "after simplification in prove_args"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2307
    THEN EVERY (map2 (prove_param2 options ctxt) ho_args param_derivations)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2308
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2309
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2310
fun prove_expr2 options ctxt (t, deriv) = 
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2311
  (case strip_comb t of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2312
      (Const (name, T), args) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2313
        let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2314
          val mode = head_mode_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2315
          val param_derivations = param_derivations_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2316
          val ho_args = ho_args_of mode args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2317
        in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2318
          etac @{thm bindE} 1
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2319
          THEN (REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"})))
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2320
          THEN print_tac options "prove_expr2-before"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2321
          THEN etac (predfun_elim_of ctxt name mode) 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2322
          THEN print_tac options "prove_expr2"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2323
          THEN (EVERY (map2 (prove_param2 options ctxt) ho_args param_derivations))
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2324
          THEN print_tac options "finished prove_expr2"
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2325
        end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2326
      | _ => etac @{thm bindE} 1)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2327
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2328
fun prove_sidecond2 options ctxt t = let
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2329
  fun preds_of t nameTs = case strip_comb t of 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2330
    (f as Const (name, T), args) =>
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2331
      if is_registered (ProofContext.theory_of ctxt) name then (name, T) :: nameTs
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2332
        else fold preds_of args nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2333
    | _ => nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2334
  val preds = preds_of t []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2335
  val defs = map
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2336
    (fn (pred, T) => predfun_definition_of ctxt pred 
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2337
      (all_input_of T))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2338
      preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2339
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2340
   (* only simplify the one assumption *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2341
   full_simp_tac (HOL_basic_ss' addsimps @{thm eval_pred} :: defs) 1 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2342
   (* need better control here! *)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2343
   THEN print_tac options "after sidecond2 simplification"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2344
   end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2345
  
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2346
fun prove_clause2 options ctxt pred mode (ts, ps) i =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2347
  let
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2348
    val pred_intro_rule = nth (intros_of (ProofContext.theory_of ctxt) pred) (i - 1)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2349
    val (in_ts, clause_out_ts) = split_mode mode ts;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2350
    fun prove_prems2 out_ts [] =
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2351
      print_tac options "before prove_match2 - last call:"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2352
      THEN prove_match2 options ctxt out_ts
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2353
      THEN print_tac options "after prove_match2 - last call:"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2354
      THEN (etac @{thm singleE} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2355
      THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2356
      THEN (asm_full_simp_tac HOL_basic_ss' 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2357
      THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2358
      THEN (asm_full_simp_tac HOL_basic_ss' 1)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2359
      THEN SOLVED (print_tac options "state before applying intro rule:"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2360
      THEN (rtac pred_intro_rule 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2361
      (* How to handle equality correctly? *)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2362
      THEN (print_tac options "state before assumption matching")
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2363
      THEN (REPEAT (atac 1 ORELSE 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2364
         (CHANGED (asm_full_simp_tac (HOL_basic_ss' addsimps
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2365
           [@{thm split_eta}, @{thm "split_beta"}, @{thm "fst_conv"},
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2366
             @{thm "snd_conv"}, @{thm pair_collapse}]) 1)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2367
          THEN print_tac options "state after simp_tac:"))))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2368
    | prove_prems2 out_ts ((p, deriv) :: ps) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2369
      let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2370
        val mode = head_mode_of deriv
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2371
        val rest_tac = (case p of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2372
          Prem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2373
          let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2374
            val (_, us) = strip_comb t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2375
            val (_, out_ts''') = split_mode mode us
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2376
            val rec_tac = prove_prems2 out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2377
          in
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2378
            (prove_expr2 options ctxt (t, deriv)) THEN rec_tac
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2379
          end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2380
        | Negprem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2381
          let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2382
            val (_, args) = strip_comb t
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2383
            val (_, out_ts''') = split_mode mode args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2384
            val rec_tac = prove_prems2 out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2385
            val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2386
            val param_derivations = param_derivations_of deriv
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2387
            val ho_args = ho_args_of mode args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2388
          in
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2389
            print_tac options "before neg prem 2"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2390
            THEN etac @{thm bindE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2391
            THEN (if is_some name then
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2392
                full_simp_tac (HOL_basic_ss addsimps
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2393
                  [predfun_definition_of ctxt (the name) mode]) 1
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2394
                THEN etac @{thm not_predE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2395
                THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2396
                THEN (EVERY (map2 (prove_param2 options ctxt) ho_args param_derivations))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2397
              else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2398
                etac @{thm not_predE'} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2399
            THEN rec_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2400
          end 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2401
        | Sidecond t =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2402
          etac @{thm bindE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2403
          THEN etac @{thm if_predE} 1
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2404
          THEN prove_sidecond2 options ctxt t
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2405
          THEN prove_prems2 [] ps)
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2406
      in print_tac options "before prove_match2:"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2407
         THEN prove_match2 options ctxt out_ts
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2408
         THEN print_tac options "after prove_match2:"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2409
         THEN rest_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2410
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2411
    val prems_tac = prove_prems2 in_ts ps 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2412
  in
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2413
    print_tac options "starting prove_clause2"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2414
    THEN etac @{thm bindE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2415
    THEN (etac @{thm singleE'} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2416
    THEN (TRY (etac @{thm Pair_inject} 1))
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2417
    THEN print_tac options "after singleE':"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2418
    THEN prems_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2419
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2420
 
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2421
fun prove_other_direction options ctxt pred mode moded_clauses =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2422
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2423
    fun prove_clause clause i =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2424
      (if i < length moded_clauses then etac @{thm supE} 1 else all_tac)
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2425
      THEN (prove_clause2 options ctxt pred mode clause i)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2426
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2427
    (DETERM (TRY (rtac @{thm unit.induct} 1)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2428
     THEN (REPEAT_DETERM (CHANGED (rewtac @{thm split_paired_all})))
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2429
     THEN (rtac (predfun_intro_of ctxt pred mode) 1)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2430
     THEN (REPEAT_DETERM (rtac @{thm refl} 2))
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2431
     THEN (if null moded_clauses then
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2432
         etac @{thm botE} 1
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2433
       else EVERY (map2 prove_clause moded_clauses (1 upto (length moded_clauses))))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2434
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2435
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2436
(** proof procedure **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2437
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2438
fun prove_pred options thy clauses preds pred (pol, mode) (moded_clauses, compiled_term) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2439
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2440
    val ctxt = ProofContext.init thy
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2441
    val clauses = case AList.lookup (op =) clauses pred of SOME rs => rs | NONE => []
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2442
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2443
    Goal.prove ctxt (Term.add_free_names compiled_term []) [] compiled_term
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  2444
      (if not (skip_proof options) then
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2445
        (fn _ =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2446
        rtac @{thm pred_iffI} 1
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2447
        THEN print_tac options "after pred_iffI"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2448
        THEN prove_one_direction options ctxt clauses preds pred mode moded_clauses
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2449
        THEN print_tac options "proved one direction"
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2450
        THEN prove_other_direction options ctxt pred mode moded_clauses
35886
f5422b736c44 reduced the debug output functions from 2 to 1
bulwahn
parents: 35885
diff changeset
  2451
        THEN print_tac options "proved other direction")
33150
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
  2452
      else (fn _ => Skip_Proof.cheat_tac thy))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2453
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2454
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2455
(* composition of mode inference, definition, compilation and proof *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2456
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2457
(** auxillary combinators for table of preds and modes **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2458
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2459
fun map_preds_modes f preds_modes_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2460
  map (fn (pred, modes) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2461
    (pred, map (fn (mode, value) => (mode, f pred mode value)) modes)) preds_modes_table
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2462
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2463
fun join_preds_modes table1 table2 =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2464
  map_preds_modes (fn pred => fn mode => fn value =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2465
    (value, the (AList.lookup (op =) (the (AList.lookup (op =) table2 pred)) mode))) table1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2466
    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2467
fun maps_modes preds_modes_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2468
  map (fn (pred, modes) =>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2469
    (pred, map (fn (mode, value) => value) modes)) preds_modes_table
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2470
    
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2471
fun compile_preds comp_modifiers thy all_vs param_vs preds moded_clauses =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2472
  map_preds_modes (fn pred => compile_pred comp_modifiers thy all_vs param_vs pred
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  2473
      (the (AList.lookup (op =) preds pred))) moded_clauses
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  2474
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2475
fun prove options thy clauses preds moded_clauses compiled_terms =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2476
  map_preds_modes (prove_pred options thy clauses preds)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2477
    (join_preds_modes moded_clauses compiled_terms)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2478
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2479
fun prove_by_skip options thy _ _ _ compiled_terms =
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 34974
diff changeset
  2480
  map_preds_modes
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 34974
diff changeset
  2481
    (fn pred => fn mode => fn t => Drule.export_without_context (Skip_Proof.make_thm thy t))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2482
    compiled_terms
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2483
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2484
(* preparation of introduction rules into special datastructures *)
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2485
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2486
fun dest_prem thy params t =
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2487
  (case strip_comb t of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2488
    (v as Free _, ts) => if member (op =) params v then Prem t else Sidecond t
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2489
  | (c as Const (@{const_name Not}, _), [t]) => (case dest_prem thy params t of
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2490
      Prem t => Negprem t
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2491
    | Negprem _ => error ("Double negation not allowed in premise: " ^
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2492
        Syntax.string_of_term_global thy (c $ t)) 
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2493
    | Sidecond t => Sidecond (c $ t))
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2494
  | (c as Const (s, _), ts) =>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2495
    if is_registered thy s then Prem t else Sidecond t
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2496
  | _ => Sidecond t)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2497
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2498
fun prepare_intrs options compilation thy prednames intros =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2499
  let
33126
bb8806eb5da7 importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents: 33124
diff changeset
  2500
    val intrs = map prop_of intros
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2501
    val preds = map (fn c => Const (c, Sign.the_const_type thy c)) prednames
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2502
    val (preds, intrs) = unify_consts thy preds intrs
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2503
    val ([preds, intrs], _) = fold_burrow (Variable.import_terms false) [preds, intrs]
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2504
      (ProofContext.init thy)
33126
bb8806eb5da7 importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents: 33124
diff changeset
  2505
    val preds = map dest_Const preds
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2506
    val all_vs = terms_vs intrs
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2507
    val all_modes = 
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2508
      map (fn (s, T) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2509
        (s,
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2510
            (if member (op =) (no_higher_order_predicate options) s then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2511
               (all_smodes_of_typ T)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2512
            else (all_modes_of_typ T)))) preds
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2513
    val params =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2514
      case intrs of
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2515
        [] =>
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2516
          let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2517
            val T = snd (hd preds)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2518
            val paramTs =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2519
              ho_argsT_of (hd (all_modes_of_typ T)) (binder_types T)
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2520
            val param_names = Name.variant_list [] (map (fn i => "p" ^ string_of_int i)
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2521
              (1 upto length paramTs))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2522
          in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2523
            map2 (curry Free) param_names paramTs
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2524
          end
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2525
      | (intr :: _) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2526
        let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2527
          val (p, args) = strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl intr)) 
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2528
        in
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2529
          ho_args_of (hd (the (AList.lookup (op =) all_modes (fst (dest_Const p))))) args
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2530
        end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2531
    val param_vs = map (fst o dest_Free) params
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2532
    fun add_clause intr clauses =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2533
      let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2534
        val (Const (name, T), ts) = strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl intr))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2535
        val prems = map (dest_prem thy params o HOLogic.dest_Trueprop) (Logic.strip_imp_prems intr)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2536
      in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2537
        AList.update op = (name, these (AList.lookup op = clauses name) @
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2538
          [(ts, prems)]) clauses
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2539
      end;
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2540
    val clauses = fold add_clause intrs []
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2541
  in
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2542
    (preds, all_vs, param_vs, all_modes, clauses)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2543
  end;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2544
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2545
(* sanity check of introduction rules *)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2546
(* TODO: rethink check with new modes *)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2547
(*
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2548
fun check_format_of_intro_rule thy intro =
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2549
  let
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2550
    val concl = Logic.strip_imp_concl (prop_of intro)
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2551
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2552
    val params = fst (chop (nparams_of thy (fst (dest_Const p))) args)
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2553
    fun check_arg arg = case HOLogic.strip_tupleT (fastype_of arg) of
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2554
      (Ts as _ :: _ :: _) =>
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2555
        if length (HOLogic.strip_tuple arg) = length Ts then
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2556
          true
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2557
        else
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2558
          error ("Format of introduction rule is invalid: tuples must be expanded:"
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2559
          ^ (Syntax.string_of_term_global thy arg) ^ " in " ^
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2560
          (Display.string_of_thm_global thy intro)) 
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2561
      | _ => true
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2562
    val prems = Logic.strip_imp_prems (prop_of intro)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2563
    fun check_prem (Prem t) = forall check_arg args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2564
      | check_prem (Negprem t) = forall check_arg args
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2565
      | check_prem _ = true
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2566
  in
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2567
    forall check_arg args andalso
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2568
    forall (check_prem o dest_prem thy params o HOLogic.dest_Trueprop) prems
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2569
  end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2570
*)
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2571
(*
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2572
fun check_intros_elim_match thy prednames =
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2573
  let
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2574
    fun check predname =
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2575
      let
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2576
        val intros = intros_of thy predname
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2577
        val elim = the_elim_of thy predname
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2578
        val nparams = nparams_of thy predname
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2579
        val elim' =
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 34974
diff changeset
  2580
          (Drule.export_without_context o Skip_Proof.make_thm thy)
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2581
          (mk_casesrule (ProofContext.init thy) nparams intros)
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2582
      in
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2583
        if not (Thm.equiv_thm (elim, elim')) then
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2584
          error "Introduction and elimination rules do not match!"
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2585
        else true
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2586
      end
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2587
  in forall check prednames end
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2588
*)
33113
0f6e30b87cf1 processing of tuples in introduction rules
bulwahn
parents: 33112
diff changeset
  2589
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2590
(* create code equation *)
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2591
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2592
fun add_code_equations thy preds result_thmss =
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2593
  let
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2594
    val ctxt = ProofContext.init thy
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2595
    fun add_code_equation (predname, T) (pred, result_thms) =
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2596
      let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2597
        val full_mode = fold_rev (curry Fun) (map (K Input) (binder_types T)) Bool
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2598
      in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2599
        if member (op =) (modes_of Pred thy predname) full_mode then
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2600
          let
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2601
            val Ts = binder_types T
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2602
            val arg_names = Name.variant_list []
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2603
              (map (fn i => "x" ^ string_of_int i) (1 upto length Ts))
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2604
            val args = map2 (curry Free) arg_names Ts
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  2605
            val predfun = Const (function_name_of Pred thy predname full_mode,
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2606
              Ts ---> PredicateCompFuns.mk_predT @{typ unit})
33754
f2957bd46faf adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
bulwahn
parents: 33753
diff changeset
  2607
            val rhs = @{term Predicate.holds} $ (list_comb (predfun, args))
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2608
            val eq_term = HOLogic.mk_Trueprop
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2609
              (HOLogic.mk_eq (list_comb (Const (predname, T), args), rhs))
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2610
            val def = predfun_definition_of ctxt predname full_mode
33441
99a5f22a967d eliminated funny record patterns and made SML/NJ happy;
wenzelm
parents: 33377
diff changeset
  2611
            val tac = fn _ => Simplifier.simp_tac
33754
f2957bd46faf adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
bulwahn
parents: 33753
diff changeset
  2612
              (HOL_basic_ss addsimps [def, @{thm holds_eq}, @{thm eval_pred}]) 1
35888
d902054e7ac6 contextifying the proof procedure in the predicate compiler
bulwahn
parents: 35887
diff changeset
  2613
            val eq = Goal.prove ctxt arg_names [] eq_term tac
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2614
          in
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2615
            (pred, result_thms @ [eq])
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2616
          end
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2617
        else
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2618
          (pred, result_thms)
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2619
      end
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2620
  in
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  2621
    map2 add_code_equation preds result_thmss
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2622
  end
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2623
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2624
(** main function of predicate compiler **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2625
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2626
datatype steps = Steps of
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2627
  {
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2628
  define_functions : options -> (string * typ) list -> string * (bool * mode) list -> theory -> theory,
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2629
  (*infer_modes : options -> (string * typ) list -> (string * mode list) list
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2630
    -> string list -> (string * (term list * indprem list) list) list
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2631
    -> theory -> ((moded_clause list pred_mode_table * string list) * theory),*)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2632
  prove : options -> theory -> (string * (term list * indprem list) list) list -> (string * typ) list
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2633
    -> moded_clause list pred_mode_table -> term pred_mode_table -> thm pred_mode_table,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2634
  add_code_equations : theory -> (string * typ) list
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2635
    -> (string * thm list) list -> (string * thm list) list,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2636
  comp_modifiers : Comp_Mod.comp_modifiers,
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2637
  use_random : bool,
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2638
  qname : bstring
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2639
  }
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2640
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2641
fun add_equations_of steps mode_analysis_options options prednames thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2642
  let
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2643
    fun dest_steps (Steps s) = s
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2644
    val compilation = Comp_Mod.compilation (#comp_modifiers (dest_steps steps))
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2645
    val _ = print_step options
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2646
      ("Starting predicate compiler (compilation: " ^ string_of_compilation compilation
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2647
        ^ ") for predicates " ^ commas prednames ^ "...")
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  2648
      (*val _ = check_intros_elim_match thy prednames*)
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2649
      (*val _ = map (check_format_of_intro_rule thy) (maps (intros_of thy) prednames)*)
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2650
    val _ =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2651
      if show_intermediate_results options then
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2652
        tracing (commas (map (Display.string_of_thm_global thy) (maps (intros_of thy) prednames)))
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2653
      else ()
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2654
    val (preds, all_vs, param_vs, all_modes, clauses) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2655
      prepare_intrs options compilation thy prednames (maps (intros_of thy) prednames)
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  2656
    val _ = print_step options "Infering modes..."
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2657
    val ((moded_clauses, errors), thy') =
36251
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  2658
      Output.cond_timeit true "Infering modes"
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  2659
      (fn _ => infer_modes mode_analysis_options
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  2660
        options compilation preds all_modes param_vs clauses thy)
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
  2661
    val modes = map (fn (p, mps) => (p, map fst mps)) moded_clauses
33619
d93a3cb55068 first steps towards a new mode datastructure; new syntax for mode annotations and new output of modes
bulwahn
parents: 33526
diff changeset
  2662
    val _ = check_expected_modes preds options modes
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2663
    (*val _ = check_proposed_modes preds options modes (fst extra_modes) errors*)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2664
    val _ = print_modes options thy' modes
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  2665
    val _ = print_step options "Defining executable functions..."
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2666
    val thy'' =
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2667
      Output.cond_timeit true "Defining executable functions..."
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2668
      (fn _ => fold (#define_functions (dest_steps steps) options preds) modes thy'
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2669
      |> Theory.checkpoint)
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  2670
    val _ = print_step options "Compiling equations..."
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2671
    val compiled_terms =
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2672
      Output.cond_timeit true "Compiling equations...." (fn _ =>
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2673
      compile_preds (#comp_modifiers (dest_steps steps)) thy'' all_vs param_vs preds moded_clauses)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2674
    val _ = print_compiled_terms options thy'' compiled_terms
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  2675
    val _ = print_step options "Proving equations..."
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2676
    val result_thms =
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2677
      Output.cond_timeit true "Proving equations...." (fn _ =>
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2678
      #prove (dest_steps steps) options thy'' clauses preds moded_clauses compiled_terms)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2679
    val result_thms' = #add_code_equations (dest_steps steps) thy'' preds
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  2680
      (maps_modes result_thms)
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2681
    val qname = #qname (dest_steps steps)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2682
    val attrib = fn thy => Attrib.attribute_i thy (Attrib.internal (K (Thm.declaration_attribute
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2683
      (fn thm => Context.mapping (Code.add_eqn thm) I))))
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2684
    val thy''' =
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2685
      Output.cond_timeit true "Setting code equations...." (fn _ =>
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2686
      fold (fn (name, result_thms) => fn thy => snd (PureThy.add_thmss
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2687
      [((Binding.qualify true (Long_Name.base_name name) (Binding.name qname), result_thms),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2688
        [attrib thy ])] thy))
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  2689
      result_thms' thy'' |> Theory.checkpoint)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2690
  in
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2691
    thy'''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2692
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2693
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2694
fun extend' value_of edges_of key (G, visited) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2695
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2696
    val (G', v) = case try (Graph.get_node G) key of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2697
        SOME v => (G, v)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2698
      | NONE => (Graph.new_node (key, value_of key) G, value_of key)
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2699
    val (G'', visited') = fold (extend' value_of edges_of)
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2700
      (subtract (op =) visited (edges_of (key, v)))
33326
7d0288d90535 improved handling of alternative rules; added test cases for alternative rules; normalized tracing in the predicate compiler
bulwahn
parents: 33268
diff changeset
  2701
      (G', key :: visited)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2702
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2703
    (fold (Graph.add_edge o (pair key)) (edges_of (key, v)) G'', visited')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2704
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2705
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2706
fun extend value_of edges_of key G = fst (extend' value_of edges_of key (G, [])) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2707
  
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
  2708
fun gen_add_equations steps options names thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2709
  let
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2710
    fun dest_steps (Steps s) = s
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2711
    val defined = defined_functions (Comp_Mod.compilation (#comp_modifiers (dest_steps steps)))
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2712
    val thy' = thy
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2713
      |> PredData.map (fold (extend (fetch_pred_data thy) (depending_preds_of thy)) names)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2714
      |> Theory.checkpoint;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2715
    fun strong_conn_of gr keys =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2716
      Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2717
    val scc = strong_conn_of (PredData.get thy') names
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2718
    
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2719
    val thy'' = fold_rev
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2720
      (fn preds => fn thy =>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2721
        if not (forall (defined thy) preds) then
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2722
          let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2723
            val mode_analysis_options = {use_random = #use_random (dest_steps steps),
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2724
              reorder_premises =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2725
                not (no_topmost_reordering options andalso not (null (inter (op =) preds names))),
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2726
              infer_pos_and_neg_modes = #use_random (dest_steps steps)}
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2727
          in
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2728
            add_equations_of steps mode_analysis_options options preds thy
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2729
          end
33483
a15a2f7f7560 improved handling of already defined functions in the predicate compiler; could cause trouble before when no modes for a predicate were infered
bulwahn
parents: 33482
diff changeset
  2730
        else thy)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2731
      scc thy' |> Theory.checkpoint
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2732
  in thy'' end
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2733
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2734
val add_equations = gen_add_equations
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2735
  (Steps {
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2736
  define_functions =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2737
    fn options => fn preds => fn (s, modes) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2738
      create_definitions
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2739
      options preds (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2740
  prove = prove,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2741
  add_code_equations = add_code_equations,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2742
  comp_modifiers = predicate_comp_modifiers,
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2743
  use_random = false,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2744
  qname = "equation"})
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  2745
33134
88c9c3460fe7 renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents: 33133
diff changeset
  2746
val add_depth_limited_equations = gen_add_equations
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2747
  (Steps {
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2748
  define_functions =
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2749
    fn options => fn preds => fn (s, modes) =>
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2750
    define_functions depth_limited_comp_modifiers PredicateCompFuns.compfuns
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2751
    options preds (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2752
  prove = prove_by_skip,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2753
  add_code_equations = K (K I),
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2754
  comp_modifiers = depth_limited_comp_modifiers,
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2755
  use_random = false,
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  2756
  qname = "depth_limited_equation"})
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2757
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
  2758
val add_annotated_equations = gen_add_equations
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2759
  (Steps {
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2760
  define_functions =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2761
    fn options => fn preds => fn (s, modes) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2762
      define_functions annotated_comp_modifiers PredicateCompFuns.compfuns options preds
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2763
      (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
  2764
  prove = prove_by_skip,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2765
  add_code_equations = K (K I),
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2766
  comp_modifiers = annotated_comp_modifiers,
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2767
  use_random = false,
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
  2768
  qname = "annotated_equation"})
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2769
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2770
val add_random_equations = gen_add_equations
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2771
  (Steps {
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2772
  define_functions =
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2773
    fn options => fn preds => fn (s, modes) =>
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2774
      define_functions random_comp_modifiers PredicateCompFuns.compfuns options preds
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2775
      (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2776
  comp_modifiers = random_comp_modifiers,
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2777
  prove = prove_by_skip,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2778
  add_code_equations = K (K I),
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2779
  use_random = true,
33375
fd3e861f8d31 renamed rpred to random
bulwahn
parents: 33333
diff changeset
  2780
  qname = "random_equation"})
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2781
35881
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2782
val add_depth_limited_random_equations = gen_add_equations
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2783
  (Steps {
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2784
  define_functions =
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2785
    fn options => fn preds => fn (s, modes) =>
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2786
      define_functions depth_limited_random_comp_modifiers PredicateCompFuns.compfuns options preds
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2787
      (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2788
  comp_modifiers = depth_limited_random_comp_modifiers,
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2789
  prove = prove_by_skip,
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2790
  add_code_equations = K (K I),
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2791
  use_random = true,
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2792
  qname = "depth_limited_random_equation"})
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2793
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2794
val add_dseq_equations = gen_add_equations
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2795
  (Steps {
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2796
  define_functions =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2797
  fn options => fn preds => fn (s, modes) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2798
    define_functions dseq_comp_modifiers DSequence_CompFuns.compfuns
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2799
    options preds (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2800
  prove = prove_by_skip,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2801
  add_code_equations = K (K I),
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2802
  comp_modifiers = dseq_comp_modifiers,
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2803
  use_random = false,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2804
  qname = "dseq_equation"})
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2805
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2806
val add_random_dseq_equations = gen_add_equations
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2807
  (Steps {
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2808
  define_functions =
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2809
    fn options => fn preds => fn (s, modes) =>
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2810
    let
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2811
      val pos_modes = map_filter (fn (true, m) => SOME m | _ => NONE) modes
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2812
      val neg_modes = map_filter (fn (false, m) => SOME m | _ => NONE) modes
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2813
    in define_functions pos_random_dseq_comp_modifiers Random_Sequence_CompFuns.compfuns
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2814
      options preds (s, pos_modes)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2815
      #> define_functions neg_random_dseq_comp_modifiers Random_Sequence_CompFuns.compfuns
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2816
      options preds (s, neg_modes)
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2817
    end,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2818
  prove = prove_by_skip,
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2819
  add_code_equations = K (K I),
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2820
  comp_modifiers = pos_random_dseq_comp_modifiers,
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  2821
  use_random = true,
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2822
  qname = "random_dseq_equation"})
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2823
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2824
val add_new_random_dseq_equations = gen_add_equations
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2825
  (Steps {
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2826
  define_functions =
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2827
    fn options => fn preds => fn (s, modes) =>
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2828
    let
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2829
      val pos_modes = map_filter (fn (true, m) => SOME m | _ => NONE) modes
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2830
      val neg_modes = map_filter (fn (false, m) => SOME m | _ => NONE) modes
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2831
    in define_functions new_pos_random_dseq_comp_modifiers New_Pos_Random_Sequence_CompFuns.compfuns
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2832
      options preds (s, pos_modes)
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2833
      #> define_functions new_neg_random_dseq_comp_modifiers New_Neg_Random_Sequence_CompFuns.compfuns
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2834
      options preds (s, neg_modes)
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2835
    end,
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2836
  prove = prove_by_skip,
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2837
  add_code_equations = K (K I),
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2838
  comp_modifiers = new_pos_random_dseq_comp_modifiers,
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2839
  use_random = true,
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2840
  qname = "new_random_dseq_equation"})
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2841
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2842
(** user interface **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2843
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2844
(* code_pred_intro attribute *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2845
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2846
fun attrib f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2847
33628
ed2111a5c3ed renaming code_pred_intros to code_pred_intro
bulwahn
parents: 33626
diff changeset
  2848
val code_pred_intro_attrib = attrib add_intro;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2849
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2850
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2851
(*FIXME
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2852
- Naming of auxiliary rules necessary?
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2853
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2854
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2855
val setup = PredData.put (Graph.empty) #>
33628
ed2111a5c3ed renaming code_pred_intros to code_pred_intro
bulwahn
parents: 33626
diff changeset
  2856
  Attrib.setup @{binding code_pred_intro} (Scan.succeed (attrib add_intro))
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2857
    "adding alternative introduction rules for code generation of inductive predicates"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2858
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
  2859
(* TODO: make Theory_Data to Generic_Data & remove duplication of local theory and theory *)
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
  2860
fun generic_code_pred prep_const options raw_const lthy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2861
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2862
    val thy = ProofContext.theory_of lthy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2863
    val const = prep_const thy raw_const
33671
4b0f2599ed48 modernized structure Local_Theory;
wenzelm
parents: 33650
diff changeset
  2864
    val lthy' = Local_Theory.theory (PredData.map
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2865
        (extend (fetch_pred_data thy) (depending_preds_of thy) const)) lthy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2866
    val thy' = ProofContext.theory_of lthy'
33326
7d0288d90535 improved handling of alternative rules; added test cases for alternative rules; normalized tracing in the predicate compiler
bulwahn
parents: 33268
diff changeset
  2867
    val preds = Graph.all_succs (PredData.get thy') [const] |> filter_out (has_elim thy')
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2868
    fun mk_cases const =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2869
      let
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2870
        val T = Sign.the_const_type thy const
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  2871
        val pred = Const (const, T)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2872
        val intros = intros_of thy' const
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2873
      in mk_casesrule lthy' pred intros end  
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2874
    val cases_rules = map mk_cases preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2875
    val cases =
33368
b1cf34f1855c modernized structure Rule_Cases;
wenzelm
parents: 33333
diff changeset
  2876
      map (fn case_rule => Rule_Cases.Case {fixes = [],
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2877
        assumes = [("", Logic.strip_imp_prems case_rule)],
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2878
        binds = [], cases = []}) cases_rules
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2879
    val case_env = map2 (fn p => fn c => (Long_Name.base_name p, SOME c)) preds cases
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2880
    val lthy'' = lthy'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2881
      |> fold Variable.auto_fixes cases_rules 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2882
      |> ProofContext.add_cases true case_env
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2883
    fun after_qed thms goal_ctxt =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2884
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2885
        val global_thms = ProofContext.export goal_ctxt
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2886
          (ProofContext.init (ProofContext.theory_of goal_ctxt)) (map the_single thms)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2887
      in
33671
4b0f2599ed48 modernized structure Local_Theory;
wenzelm
parents: 33650
diff changeset
  2888
        goal_ctxt |> Local_Theory.theory (fold set_elim global_thms #>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2889
          ((case compilation options of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2890
             Pred => add_equations
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2891
           | DSeq => add_dseq_equations
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2892
           | Pos_Random_DSeq => add_random_dseq_equations
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2893
           | Depth_Limited => add_depth_limited_equations
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2894
           | Random => add_random_equations
35881
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2895
           | Depth_Limited_Random => add_depth_limited_random_equations
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  2896
           | New_Pos_Random_DSeq => add_new_random_dseq_equations
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2897
           | compilation => error ("Compilation not supported")
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2898
           ) options [const]))
33144
1831516747d4 removed obsolete GeneratorPrem; clean-up after modularization; tuned
bulwahn
parents: 33143
diff changeset
  2899
      end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2900
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2901
    Proof.theorem_i NONE after_qed (map (single o (rpair [])) cases_rules) lthy''
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2902
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2903
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2904
val code_pred = generic_code_pred (K I);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2905
val code_pred_cmd = generic_code_pred Code.read_const
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2906
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2907
(* transformation for code generation *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2908
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32674
diff changeset
  2909
val eval_ref = Unsynchronized.ref (NONE : (unit -> term Predicate.pred) option);
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2910
val random_eval_ref =
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  2911
  Unsynchronized.ref (NONE : (unit -> int * int -> term Predicate.pred * (int * int)) option);
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2912
val dseq_eval_ref = Unsynchronized.ref (NONE : (unit -> term DSequence.dseq) option);
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2913
val random_dseq_eval_ref =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2914
  Unsynchronized.ref (NONE : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int)) option);
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  2915
val new_random_dseq_eval_ref =
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  2916
  Unsynchronized.ref (NONE : (unit -> int -> int -> int * int -> int -> term Lazy_Sequence.lazy_sequence) option)
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  2917
val new_random_dseq_stats_eval_ref =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  2918
    Unsynchronized.ref (NONE :
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  2919
      (unit -> int -> int -> int * int -> int -> (term * int) Lazy_Sequence.lazy_sequence) option)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2920
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2921
(*FIXME turn this into an LCF-guarded preprocessor for comprehensions*)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2922
fun analyze_compr thy compfuns param_user_modes (compilation, arguments) t_compr =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2923
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2924
    val all_modes_of = all_modes_of compilation
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2925
    val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2926
      | _ => error ("Not a set comprehension: " ^ Syntax.string_of_term_global thy t_compr);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2927
    val (body, Ts, fp) = HOLogic.strip_psplits split;
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2928
    val output_names = Name.variant_list (Term.add_free_names body [])
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2929
      (map (fn i => "x" ^ string_of_int i) (1 upto length Ts))
34963
haftmann
parents: 34948 34962
diff changeset
  2930
    val output_frees = map2 (curry Free) output_names (rev Ts)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2931
    val body = subst_bounds (output_frees, body)
34963
haftmann
parents: 34948 34962
diff changeset
  2932
    val T_compr = HOLogic.mk_ptupleT fp Ts
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2933
    val output_tuple = HOLogic.mk_ptuple fp T_compr (rev output_frees)
36031
199fe16cdaab returning an more understandable user error message in the values command
bulwahn
parents: 36030
diff changeset
  2934
    val (pred as Const (name, T), all_args) =
199fe16cdaab returning an more understandable user error message in the values command
bulwahn
parents: 36030
diff changeset
  2935
      case strip_comb body of
199fe16cdaab returning an more understandable user error message in the values command
bulwahn
parents: 36030
diff changeset
  2936
        (Const (name, T), all_args) => (Const (name, T), all_args)
199fe16cdaab returning an more understandable user error message in the values command
bulwahn
parents: 36030
diff changeset
  2937
      | (head, _) => error ("Not a constant: " ^ Syntax.string_of_term_global thy head)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2938
  in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2939
    if defined_functions compilation thy name then
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2940
      let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2941
        fun extract_mode (Const ("Pair", _) $ t1 $ t2) = Pair (extract_mode t1, extract_mode t2)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2942
          | extract_mode (Free (x, _)) = if member (op =) output_names x then Output else Input
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2943
          | extract_mode _ = Input
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2944
        val user_mode = fold_rev (curry Fun) (map extract_mode all_args) Bool
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2945
        fun valid modes1 modes2 =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2946
          case int_ord (length modes1, length modes2) of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2947
            GREATER => error "Not enough mode annotations"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2948
          | LESS => error "Too many mode annotations"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2949
          | EQUAL => forall (fn (m, NONE) => true | (m, SOME m2) => eq_mode (m, m2))
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2950
            (modes1 ~~ modes2)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2951
        fun mode_instance_of (m1, m2) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2952
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2953
            fun instance_of (Fun _, Input) = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2954
              | instance_of (Input, Input) = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2955
              | instance_of (Output, Output) = true
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2956
              | instance_of (Pair (m1, m2), Pair (m1', m2')) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2957
                  instance_of  (m1, m1') andalso instance_of (m2, m2')
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2958
              | instance_of (Pair (m1, m2), Input) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2959
                  instance_of (m1, Input) andalso instance_of (m2, Input)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2960
              | instance_of (Pair (m1, m2), Output) =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2961
                  instance_of (m1, Output) andalso instance_of (m2, Output)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2962
              | instance_of _ = false
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2963
          in forall instance_of (strip_fun_mode m1 ~~ strip_fun_mode m2) end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2964
        val derivs = all_derivations_of thy (all_modes_of thy) [] body
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2965
          |> filter (fn (d, missing_vars) =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2966
            let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2967
              val (p_mode :: modes) = collect_context_modes d
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2968
            in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2969
              null missing_vars andalso
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2970
              mode_instance_of (p_mode, user_mode) andalso
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2971
              the_default true (Option.map (valid modes) param_user_modes)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2972
            end)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2973
          |> map fst
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2974
        val deriv = case derivs of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2975
            [] => error ("No mode possible for comprehension "
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2976
                    ^ Syntax.string_of_term_global thy t_compr)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2977
          | [d] => d
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2978
          | d :: _ :: _ => (warning ("Multiple modes possible for comprehension "
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2979
                    ^ Syntax.string_of_term_global thy t_compr); d);
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2980
        val (_, outargs) = split_mode (head_mode_of deriv) all_args
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2981
        val additional_arguments =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2982
          case compilation of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2983
            Pred => []
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2984
          | Random => map (HOLogic.mk_number @{typ "code_numeral"}) arguments @
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  2985
            [@{term "(1, 1) :: code_numeral * code_numeral"}]
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2986
          | Annotated => []
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2987
          | Depth_Limited => [HOLogic.mk_number @{typ "code_numeral"} (hd arguments)]
35881
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2988
          | Depth_Limited_Random => map (HOLogic.mk_number @{typ "code_numeral"}) arguments @
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2989
            [@{term "(1, 1) :: code_numeral * code_numeral"}]
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2990
          | DSeq => []
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2991
          | Pos_Random_DSeq => []
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  2992
          | New_Pos_Random_DSeq => []
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2993
        val comp_modifiers =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2994
          case compilation of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2995
            Pred => predicate_comp_modifiers
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2996
          | Random => random_comp_modifiers
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  2997
          | Depth_Limited => depth_limited_comp_modifiers
35881
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  2998
          | Depth_Limited_Random => depth_limited_random_comp_modifiers
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  2999
          (*| Annotated => annotated_comp_modifiers*)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3000
          | DSeq => dseq_comp_modifiers
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  3001
          | Pos_Random_DSeq => pos_random_dseq_comp_modifiers
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3002
          | New_Pos_Random_DSeq => new_pos_random_dseq_comp_modifiers
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
  3003
        val t_pred = compile_expr comp_modifiers (ProofContext.init thy)
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3004
          (body, deriv) additional_arguments;
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3005
        val T_pred = dest_predT compfuns (fastype_of t_pred)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3006
        val arrange = split_lambda (HOLogic.mk_tuple outargs) output_tuple
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3007
      in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3008
        if null outargs then t_pred else mk_map compfuns T_pred T_compr arrange t_pred
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3009
      end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3010
    else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3011
      error "Evaluation with values is not possible because compilation with code_pred was not invoked"
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3012
  end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3013
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3014
fun eval thy stats param_user_modes (options as (compilation, arguments)) k t_compr =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3015
  let
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3016
    fun count xs x =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3017
      let
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3018
        fun count' i [] = i
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3019
          | count' i (x' :: xs) = if x = x' then count' (i + 1) xs else count' i xs
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3020
      in count' 0 xs end
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3021
    fun accumulate xs = map (fn x => (x, count xs x)) (sort int_ord (distinct (op =) xs))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3022
    val compfuns =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3023
      case compilation of
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  3024
        Random => PredicateCompFuns.compfuns
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3025
      | DSeq => DSequence_CompFuns.compfuns
35324
c9f428269b38 adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents: 35267
diff changeset
  3026
      | Pos_Random_DSeq => Random_Sequence_CompFuns.compfuns
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3027
      | New_Pos_Random_DSeq => New_Pos_Random_Sequence_CompFuns.compfuns
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3028
      | _ => PredicateCompFuns.compfuns
33479
428ddcc16e7b added optional mode annotations for parameters in the values command
bulwahn
parents: 33478
diff changeset
  3029
    val t = analyze_compr thy compfuns param_user_modes options t_compr;
33137
0d16c07f8d24 added option to generate random values to values command in the predicate compiler
bulwahn
parents: 33135
diff changeset
  3030
    val T = dest_predT compfuns (fastype_of t);
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3031
    val t' =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3032
      if stats andalso compilation = New_Pos_Random_DSeq then
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3033
        mk_map compfuns T (HOLogic.mk_prodT (HOLogic.termT, @{typ code_numeral}))
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3034
          (absdummy (T, HOLogic.mk_prod (HOLogic.term_of_const T $ Bound 0,
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3035
            @{term Code_Numeral.of_nat} $ (HOLogic.size_const T $ Bound 0)))) t
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3036
      else
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3037
        mk_map compfuns T HOLogic.termT (HOLogic.term_of_const T) t
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3038
    val (ts, statistics) =
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3039
      case compilation of
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  3040
       (* Random =>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3041
          fst (Predicate.yieldn k
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3042
          (Code_Eval.eval NONE ("Predicate_Compile_Core.random_eval_ref", random_eval_ref)
33137
0d16c07f8d24 added option to generate random values to values command in the predicate compiler
bulwahn
parents: 33135
diff changeset
  3043
            (fn proc => fn g => fn s => g s |>> Predicate.map proc) thy t' []
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  3044
            |> Random_Engine.run))*)
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  3045
        Pos_Random_DSeq =>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3046
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3047
            val [nrandom, size, depth] = arguments
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3048
          in
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3049
            rpair NONE (fst (DSequence.yieldn k
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3050
              (Code_Eval.eval NONE ("Predicate_Compile_Core.random_dseq_eval_ref", random_dseq_eval_ref)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3051
                (fn proc => fn g => fn nrandom => fn size => fn s => g nrandom size s |>> DSequence.map proc)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3052
                  thy t' [] nrandom size
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3053
                |> Random_Engine.run)
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3054
              depth true))
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3055
          end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3056
      | DSeq =>
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3057
          rpair NONE (fst (DSequence.yieldn k
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3058
            (Code_Eval.eval NONE ("Predicate_Compile_Core.dseq_eval_ref", dseq_eval_ref)
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3059
              DSequence.map thy t' []) (the_single arguments) true))
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3060
      | New_Pos_Random_DSeq =>
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3061
          let
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3062
            val [nrandom, size, depth] = arguments
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3063
            val seed = Random_Engine.next_seed ()
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3064
          in
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3065
            if stats then
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3066
              apsnd (SOME o accumulate) (split_list
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3067
              (fst (Lazy_Sequence.yieldn k
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3068
                (Code_Eval.eval NONE
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3069
                  ("Predicate_Compile_Core.new_random_dseq_stats_eval_ref", new_random_dseq_stats_eval_ref)
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3070
                  (fn proc => fn g => fn nrandom => fn size => fn s => fn depth => g nrandom size s depth
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3071
                    |> Lazy_Sequence.map (apfst proc))
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3072
                    thy t' [] nrandom size seed depth))))
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3073
            else rpair NONE
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3074
              (fst (Lazy_Sequence.yieldn k
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3075
                (Code_Eval.eval NONE
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3076
                  ("Predicate_Compile_Core.new_random_dseq_eval_ref", new_random_dseq_eval_ref)
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3077
                  (fn proc => fn g => fn nrandom => fn size => fn s => fn depth => g nrandom size s depth
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3078
                    |> Lazy_Sequence.map proc)
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3079
                    thy t' [] nrandom size seed depth)))
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  3080
          end
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3081
      | _ =>
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3082
          rpair NONE (fst (Predicate.yieldn k
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3083
            (Code_Eval.eval NONE ("Predicate_Compile_Core.eval_ref", eval_ref)
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3084
              Predicate.map thy t' [])))
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3085
  in ((T, ts), statistics) end;
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3086
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3087
fun values ctxt param_user_modes ((raw_expected, stats), comp_options) k t_compr =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3088
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3089
    val thy = ProofContext.theory_of ctxt
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3090
    val ((T, ts), statistics) = eval thy stats param_user_modes comp_options k t_compr
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3091
    val setT = HOLogic.mk_setT T
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3092
    val elems = HOLogic.mk_set T ts
33476
27cca5416a88 Adopted output of values command
bulwahn
parents: 33474
diff changeset
  3093
    val cont = Free ("...", setT)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3094
    (* check expected values *)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3095
    val () =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3096
      case raw_expected of
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3097
        NONE => ()
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3098
      | SOME s =>
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3099
        if eq_set (op =) (HOLogic.dest_set (Syntax.read_term ctxt s), ts) then ()
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3100
        else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3101
          error ("expected and computed values do not match:\n" ^
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3102
            "expected values: " ^ Syntax.string_of_term ctxt (Syntax.read_term ctxt s) ^ "\n" ^
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3103
            "computed values: " ^ Syntax.string_of_term ctxt elems ^ "\n")
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3104
  in
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3105
    (if k = ~1 orelse length ts < k then elems
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3106
    else Const (@{const_abbrev Set.union}, setT --> setT --> setT) $ elems $ cont, statistics)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3107
  end;
33623
4ec42d38224f changed modes to expected_modes; added UNION to code_pred_inlining; fixed some examples; tuned
bulwahn
parents: 33620
diff changeset
  3108
33479
428ddcc16e7b added optional mode annotations for parameters in the values command
bulwahn
parents: 33478
diff changeset
  3109
fun values_cmd print_modes param_user_modes options k raw_t state =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3110
  let
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3111
    val ctxt = Toplevel.context_of state
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3112
    val t = Syntax.read_term ctxt raw_t
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3113
    val (t', stats) = values ctxt param_user_modes options k t
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3114
    val ty' = Term.type_of t'
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  3115
    val ctxt' = Variable.auto_fixes t' ctxt
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3116
    val pretty_stat =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3117
      case stats of
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3118
          NONE => []
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3119
        | SOME xs =>
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3120
          let
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3121
            val total = fold (curry (op +)) (map snd xs) 0
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3122
            fun pretty_entry (s, n) =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3123
              [Pretty.str "size", Pretty.brk 1,
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3124
               Pretty.str (string_of_int s), Pretty.str ":", Pretty.brk 1,
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3125
               Pretty.str (string_of_int n), Pretty.fbrk]
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3126
          in
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3127
            [Pretty.fbrk, Pretty.str "Statistics:", Pretty.fbrk,
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3128
             Pretty.str "total:", Pretty.brk 1, Pretty.str (string_of_int total), Pretty.fbrk]
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3129
             @ maps pretty_entry xs
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3130
          end
33478
b70fe083694d moved values command from core to predicate compile
bulwahn
parents: 33476
diff changeset
  3131
    val p = PrintMode.with_modes print_modes (fn () =>
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3132
      Pretty.block ([Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3133
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  3134
        @ pretty_stat)) ();
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3135
  in Pretty.writeln p end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3136
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  3137
end;