src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML
author bulwahn
Mon, 25 Oct 2010 21:17:10 +0200
changeset 40135 abc45472e48a
parent 40103 ef73a90ab6e6
child 40138 432a776c4aee
permissions -rw-r--r--
adding a global time limit to the values command
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
40053
3fa49ea76cbb using a signature in core_data and moving some more functions to core_data
bulwahn
parents: 40052
diff changeset
    19
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
    20
  val print_stored_rules : Proof.context -> unit
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
    21
  val print_all_modes : compilation -> Proof.context -> unit
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    22
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    23
  val put_pred_result : (unit -> term Predicate.pred) -> Proof.context -> Proof.context
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    24
  val put_pred_random_result : (unit -> int * int -> term Predicate.pred * (int * int)) ->
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    25
    Proof.context -> Proof.context
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    26
  val put_dseq_result : (unit -> term DSequence.dseq) -> Proof.context -> Proof.context
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    27
  val put_dseq_random_result : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int)) ->
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    28
    Proof.context -> Proof.context
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
    29
  val put_new_dseq_result : (unit -> int -> term Lazy_Sequence.lazy_sequence) ->
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
    30
    Proof.context -> Proof.context
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    31
  val put_lseq_random_result :
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    32
    (unit -> int -> int -> int * int -> int -> term Lazy_Sequence.lazy_sequence) ->
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    33
    Proof.context -> Proof.context
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    34
  val put_lseq_random_stats_result :
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    35
    (unit -> int -> int -> int * int -> int -> (term * int) Lazy_Sequence.lazy_sequence) ->
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    36
    Proof.context -> Proof.context
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
    37
33628
ed2111a5c3ed renaming code_pred_intros to code_pred_intro
bulwahn
parents: 33626
diff changeset
    38
  val code_pred_intro_attrib : attribute
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    39
  (* used by Quickcheck_Generator *) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    40
  (* temporary for testing of the compilation *)
36048
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    41
  val add_equations : options -> string list -> theory -> theory
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    42
  val add_depth_limited_random_equations : options -> string list -> theory -> theory
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    43
  val add_random_dseq_equations : options -> string list -> theory -> theory
1d2faa488166 clarifying the Predicate_Compile_Core signature
bulwahn
parents: 36046
diff changeset
    44
  val add_new_random_dseq_equations : options -> string list -> theory -> theory
40103
ef73a90ab6e6 adding generator quickcheck
bulwahn
parents: 40102
diff changeset
    45
  val add_generator_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
    46
  val mk_tracing : string -> term -> term
39310
17ef4415b17c removing obsolete argument in prepare_intrs; passing context instead of theory in prepare_intrs
bulwahn
parents: 39299
diff changeset
    47
  val prepare_intrs : options -> Proof.context -> string list -> thm list ->
38957
2eb265efa1b0 exporting mode analysis for use in prolog generation
bulwahn
parents: 38864
diff changeset
    48
    ((string * typ) list * string list * string list * (string * mode list) list *
2eb265efa1b0 exporting mode analysis for use in prolog generation
bulwahn
parents: 38864
diff changeset
    49
      (string *  (Term.term list * Predicate_Compile_Aux.indprem list) list) list)
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
    50
  type mode_analysis_options =
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
    51
   {use_generators : bool,
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
    52
    reorder_premises : bool,
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
    53
    infer_pos_and_neg_modes : bool}  
38957
2eb265efa1b0 exporting mode analysis for use in prolog generation
bulwahn
parents: 38864
diff changeset
    54
  datatype mode_derivation = Mode_App of mode_derivation * mode_derivation | Context of mode
39310
17ef4415b17c removing obsolete argument in prepare_intrs; passing context instead of theory in prepare_intrs
bulwahn
parents: 39299
diff changeset
    55
    | Mode_Pair of mode_derivation * mode_derivation | Term of mode
39461
0ed0f015d140 adding mode inference to prolog compilation; separate between (ad-hoc) code modifications and system_configuration; adapting quickcheck
bulwahn
parents: 39403
diff changeset
    56
  val head_mode_of : mode_derivation -> mode
38957
2eb265efa1b0 exporting mode analysis for use in prolog generation
bulwahn
parents: 38864
diff changeset
    57
  type moded_clause = term list * (Predicate_Compile_Aux.indprem * mode_derivation) list
2eb265efa1b0 exporting mode analysis for use in prolog generation
bulwahn
parents: 38864
diff changeset
    58
  type 'a pred_mode_table = (string * ((bool * mode) * 'a) list) list
2eb265efa1b0 exporting mode analysis for use in prolog generation
bulwahn
parents: 38864
diff changeset
    59
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    60
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    61
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    62
structure Predicate_Compile_Core : PREDICATE_COMPILE_CORE =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    63
struct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    64
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
    65
open Predicate_Compile_Aux;
40052
ea46574ca815 splitting large core file into core_data, mode_inference and predicate_compile_proof
bulwahn
parents: 40051
diff changeset
    66
open Core_Data;
ea46574ca815 splitting large core file into core_data, mode_inference and predicate_compile_proof
bulwahn
parents: 40051
diff changeset
    67
open Mode_Inference;
ea46574ca815 splitting large core file into core_data, mode_inference and predicate_compile_proof
bulwahn
parents: 40051
diff changeset
    68
open Predicate_Compile_Proof;
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
    69
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    70
(** fundamentals **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    71
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    72
(* syntactic operations *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    73
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    74
fun mk_eq (x, xs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    75
  let fun mk_eqs _ [] = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    76
        | mk_eqs a (b::cs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    77
            HOLogic.mk_eq (Free (a, fastype_of b), b) :: mk_eqs a cs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    78
  in mk_eqs x xs end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    79
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    80
fun mk_scomp (t, u) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    81
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    82
    val T = fastype_of t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    83
    val U = fastype_of u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    84
    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
    85
    val D = body_type U                   
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    86
  in 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    87
    Const (@{const_name "scomp"}, T --> U --> A --> D) $ t $ u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    88
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    89
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    90
fun dest_funT (Type ("fun",[S, T])) = (S, T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    91
  | dest_funT T = raise TYPE ("dest_funT", [T], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    92
 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    93
fun mk_fun_comp (t, u) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    94
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    95
    val (_, B) = dest_funT (fastype_of t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    96
    val (C, A) = dest_funT (fastype_of u)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    97
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    98
    Const(@{const_name "Fun.comp"}, (A --> B) --> (C --> A) --> C --> B) $ t $ u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    99
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   100
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   101
fun dest_randomT (Type ("fun", [@{typ Random.seed},
37678
0040bafffdef "prod" and "sum" replace "*" and "+" respectively
haftmann
parents: 37591
diff changeset
   102
  Type (@{type_name Product_Type.prod}, [Type (@{type_name Product_Type.prod}, [T, @{typ "unit => Code_Evaluation.term"}]) ,@{typ Random.seed}])])) = T
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   103
  | dest_randomT T = raise TYPE ("dest_randomT", [T], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   104
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   105
fun mk_tracing s t =
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   106
  Const(@{const_name Code_Evaluation.tracing},
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
   107
    @{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
   108
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   109
(* representation of inferred clauses with modes *)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   110
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   111
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
   112
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
   113
type 'a pred_mode_table = (string * ((bool * mode) * 'a) list) list
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   114
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   115
(* diagnostic display functions *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   116
37004
c62f743e37d4 removing unused argument in print_modes function
bulwahn
parents: 37003
diff changeset
   117
fun print_modes options modes =
33251
4b13ab778b78 added option show_modes to predicate compiler
bulwahn
parents: 33250
diff changeset
   118
  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
   119
    tracing ("Inferred modes:\n" ^
33251
4b13ab778b78 added option show_modes to predicate compiler
bulwahn
parents: 33250
diff changeset
   120
      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
   121
        (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
   122
  else ()
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   123
37004
c62f743e37d4 removing unused argument in print_modes function
bulwahn
parents: 37003
diff changeset
   124
fun print_pred_mode_table string_of_entry pred_mode_table =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   125
  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
   126
    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
   127
      ^ string_of_entry pred mode entry
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   128
    fun print_pred (pred, modes) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   129
      "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
   130
    val _ = tracing (cat_lines (map print_pred pred_mode_table))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   131
  in () end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   132
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   133
fun print_compiled_terms options ctxt =
33139
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   134
  if show_compilation options then
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   135
    print_pred_mode_table (fn _ => fn _ => Syntax.string_of_term ctxt)
33139
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   136
  else K ()
9c01ee6f8ee9 added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents: 33138
diff changeset
   137
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
   138
fun print_stored_rules ctxt =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   139
  let
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
   140
    val preds = Graph.keys (PredData.get (ProofContext.theory_of ctxt))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   141
    fun print pred () = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   142
      val _ = writeln ("predicate: " ^ pred)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   143
      val _ = writeln ("introrules: ")
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
   144
      val _ = fold (fn thm => fn u => writeln (Display.string_of_thm ctxt thm))
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
   145
        (rev (intros_of ctxt pred)) ()
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   146
    in
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
   147
      if (has_elim ctxt pred) then
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
   148
        writeln ("elimrule: " ^ Display.string_of_thm ctxt (the_elim_of ctxt pred))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   149
      else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   150
        writeln ("no elimrule defined")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   151
    end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   152
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   153
    fold print preds ()
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   154
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   155
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
   156
fun print_all_modes compilation ctxt =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   157
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   158
    val _ = writeln ("Inferred modes:")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   159
    fun print (pred, modes) u =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   160
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   161
        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
   162
        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
   163
      in u end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   164
  in
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
   165
    fold print (all_modes_of compilation ctxt) ()
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   166
  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
   167
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
   168
(* validity checks *)
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
   169
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   170
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
   171
  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
   172
    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
   173
      SOME modes =>
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   174
        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
   175
          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
   176
        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
   177
          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
   178
            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
   179
            ^ "  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
   180
            ^ "  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
   181
          else ()
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   182
        end
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   183
      | NONE => ())
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   184
  | NONE => ()
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   185
39201
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
   186
fun check_proposed_modes preds options modes errors =
39382
c797f3ab2ae1 proposed modes for code_pred now supports modes for mutual predicates
bulwahn
parents: 39310
diff changeset
   187
  map (fn (s, _) => case proposed_modes options s of
c797f3ab2ae1 proposed modes for code_pred now supports modes for mutual predicates
bulwahn
parents: 39310
diff changeset
   188
    SOME ms => (case AList.lookup (op =) modes s of
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   189
      SOME inferred_ms =>
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   190
        let
39201
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
   191
          val preds_without_modes = map fst (filter (null o snd) 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
   192
          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
   193
        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
   194
          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
   195
            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
   196
            ^ "  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
   197
            ^ "  expected modes for " ^ s ^ ": " ^ commas (map string_of_mode ms) ^ "\n"
39383
ddfafa97da2f adding option show_invalid_clauses for a more detailed message when modes are not inferred
bulwahn
parents: 39382
diff changeset
   198
            ^ (if show_invalid_clauses options then
ddfafa97da2f adding option show_invalid_clauses for a more detailed message when modes are not inferred
bulwahn
parents: 39382
diff changeset
   199
            ("For the following clauses, the following modes could not be inferred: " ^ "\n"
ddfafa97da2f adding option show_invalid_clauses for a more detailed message when modes are not inferred
bulwahn
parents: 39382
diff changeset
   200
            ^ cat_lines errors) else "") ^
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   201
            (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
   202
              "\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
   203
            else ""))
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   204
          else ()
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   205
        end
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
   206
      | NONE => ())
39382
c797f3ab2ae1 proposed modes for code_pred now supports modes for mutual predicates
bulwahn
parents: 39310
diff changeset
   207
  | NONE => ()) preds
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
   208
39651
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   209
fun check_matches_type ctxt predname T ms =
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   210
  let
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   211
    fun check (m as Fun (m1, m2)) (Type("fun", [T1,T2])) = check m1 T1 andalso check m2 T2
39763
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   212
      | check m (T as Type("fun", _)) = (m = Input orelse m = Output)
39651
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   213
      | check (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) =
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   214
          check m1 T1 andalso check m2 T2 
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   215
      | check Input T = true
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   216
      | check Output T = true
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   217
      | check Bool @{typ bool} = true
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   218
      | check _ _ = false
39763
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   219
    fun check_consistent_modes ms =
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   220
      if forall (fn Fun (m1', m2') => true | _ => false) ms then
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   221
        pairself check_consistent_modes (split_list (map (fn Fun (m1, m2) => (m1, m2)) ms))
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   222
        |> (fn (res1, res2) => res1 andalso res2) 
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   223
      else if forall (fn Input => true | Output => true | Pair _ => true | _ => false) ms then
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   224
        true
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   225
      else if forall (fn Bool => true | _ => false) ms then
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   226
        true
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   227
      else
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   228
        false
39651
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   229
    val _ = map
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   230
      (fn mode =>
39763
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   231
        if length (strip_fun_mode mode) = length (binder_types T)
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   232
          andalso (forall (uncurry check) (strip_fun_mode mode ~~ binder_types T)) then ()
39651
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   233
        else error (string_of_mode mode ^ " is not a valid mode for " ^ Syntax.string_of_typ ctxt T
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   234
        ^ " at predicate " ^ predname)) ms
39763
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   235
    val _ =
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   236
     if check_consistent_modes ms then ()
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   237
     else error (commas (map string_of_mode ms) ^
03f95582ef63 weakening check for higher-order relations, but adding check for consistent modes
bulwahn
parents: 39762
diff changeset
   238
       " are inconsistent modes for predicate " ^ predname)
39651
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   239
  in
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   240
    ms
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   241
  end
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
   242
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   243
(* compilation modifiers *)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   244
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   245
structure Comp_Mod =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   246
struct
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   247
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   248
datatype comp_modifiers = Comp_Modifiers of
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   249
{
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   250
  compilation : compilation,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   251
  function_name_prefix : string,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   252
  compfuns : compilation_funs,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   253
  mk_random : typ -> term list -> term,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   254
  modify_funT : typ -> typ,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   255
  additional_arguments : string list -> term list,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   256
  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
   257
  transform_additional_arguments : indprem -> term list -> term list
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   258
}
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   259
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   260
fun dest_comp_modifiers (Comp_Modifiers c) = c
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   261
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   262
val compilation = #compilation o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   263
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
   264
val compfuns = #compfuns o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   265
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   266
val mk_random = #mk_random o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   267
val funT_of' = funT_of o compfuns
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   268
val modify_funT = #modify_funT o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   269
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
   270
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   271
val additional_arguments = #additional_arguments o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   272
val wrap_compilation = #wrap_compilation o dest_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   273
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
   274
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   275
fun set_compfuns compfuns' (Comp_Modifiers {compilation, function_name_prefix, compfuns, mk_random,
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   276
    modify_funT, additional_arguments, wrap_compilation, transform_additional_arguments}) =
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   277
    (Comp_Modifiers {compilation = compilation, function_name_prefix = function_name_prefix,
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   278
    compfuns = compfuns', mk_random = mk_random, modify_funT = modify_funT,
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   279
    additional_arguments = additional_arguments, wrap_compilation = wrap_compilation,
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   280
    transform_additional_arguments = transform_additional_arguments})
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   281
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   282
end;
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   283
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   284
fun unlimited_compfuns_of true New_Pos_Random_DSeq =
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   285
    New_Pos_Random_Sequence_CompFuns.depth_unlimited_compfuns
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   286
  | unlimited_compfuns_of false New_Pos_Random_DSeq =
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   287
    New_Neg_Random_Sequence_CompFuns.depth_unlimited_compfuns
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   288
  | unlimited_compfuns_of true Pos_Generator_DSeq =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   289
    New_Pos_DSequence_CompFuns.depth_unlimited_compfuns
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   290
  | unlimited_compfuns_of false Pos_Generator_DSeq =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   291
    New_Neg_DSequence_CompFuns.depth_unlimited_compfuns
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   292
  | unlimited_compfuns_of _ c =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   293
    raise Fail ("No unlimited compfuns for compilation " ^ string_of_compilation c)
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   294
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   295
fun limited_compfuns_of true Predicate_Compile_Aux.New_Pos_Random_DSeq =
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   296
    New_Pos_Random_Sequence_CompFuns.depth_limited_compfuns
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   297
  | limited_compfuns_of false Predicate_Compile_Aux.New_Pos_Random_DSeq =
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   298
    New_Neg_Random_Sequence_CompFuns.depth_limited_compfuns
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   299
  | limited_compfuns_of true Pos_Generator_DSeq =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   300
    New_Pos_DSequence_CompFuns.depth_limited_compfuns
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   301
  | limited_compfuns_of false Pos_Generator_DSeq =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   302
    New_Neg_DSequence_CompFuns.depth_limited_compfuns
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   303
  | limited_compfuns_of _ c =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   304
    raise Fail ("No limited compfuns for compilation " ^ string_of_compilation c)
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   305
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   306
val depth_limited_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   307
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   308
  compilation = Depth_Limited,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   309
  function_name_prefix = "depth_limited_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   310
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   311
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   312
  additional_arguments = fn names =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   313
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   314
      val depth_name = Name.variant names "depth"
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   315
    in [Free (depth_name, @{typ code_numeral})] end,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   316
  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
   317
  val Ts' = [@{typ code_numeral}] in (Ts @ Ts') ---> U end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   318
  wrap_compilation =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   319
    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
   320
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   321
      val [depth] = additional_arguments
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   322
      val (_, Ts) = split_modeT' mode (binder_types T)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   323
      val T' = mk_predT compfuns (HOLogic.mk_tupleT Ts)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   324
      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
   325
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   326
      if_const $ HOLogic.mk_eq (depth, @{term "0 :: code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   327
        $ mk_bot compfuns (dest_predT compfuns T')
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   328
        $ compilation
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   329
    end,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   330
  transform_additional_arguments =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   331
    fn prem => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   332
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   333
      val [depth] = additional_arguments
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   334
      val depth' =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   335
        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
   336
          $ 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
   337
    in [depth'] end
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   338
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   339
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   340
val random_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   341
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   342
  compilation = Random,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   343
  function_name_prefix = "random_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   344
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   345
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   346
  list_comb (Const(@{const_name Quickcheck.iter},
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   347
  [@{typ code_numeral}, @{typ code_numeral}, @{typ Random.seed}] ---> 
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   348
    PredicateCompFuns.mk_predT T), additional_arguments)),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   349
  modify_funT = (fn T =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   350
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   351
      val (Ts, U) = strip_type T
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   352
      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
   353
    in (Ts @ Ts') ---> U end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   354
  additional_arguments = (fn names =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   355
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   356
      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
   357
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   358
      [Free (nrandom, @{typ code_numeral}), Free (size, @{typ code_numeral}),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   359
        Free (seed, @{typ "code_numeral * code_numeral"})]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   360
    end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   361
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   362
    : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   363
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   364
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   365
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   366
val depth_limited_random_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   367
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   368
  compilation = Depth_Limited_Random,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   369
  function_name_prefix = "depth_limited_random_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   370
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   371
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   372
  list_comb (Const(@{const_name Quickcheck.iter},
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   373
  [@{typ code_numeral}, @{typ code_numeral}, @{typ Random.seed}] ---> 
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   374
    PredicateCompFuns.mk_predT T), tl additional_arguments)),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   375
  modify_funT = (fn T =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   376
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   377
      val (Ts, U) = strip_type T
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   378
      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
   379
        @{typ "code_numeral * code_numeral"}]
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   380
    in (Ts @ Ts') ---> U end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   381
  additional_arguments = (fn names =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   382
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   383
      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
   384
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   385
      [Free (depth, @{typ code_numeral}), Free (nrandom, @{typ code_numeral}),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   386
        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
   387
    end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   388
  wrap_compilation =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   389
  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
   390
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   391
      val depth = hd (additional_arguments)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   392
      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
   393
        mode (binder_types T)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   394
      val T' = mk_predT compfuns (HOLogic.mk_tupleT Ts)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   395
      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
   396
    in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   397
      if_const $ HOLogic.mk_eq (depth, @{term "0 :: code_numeral"})
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   398
        $ mk_bot compfuns (dest_predT compfuns T')
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   399
        $ compilation
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   400
    end,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   401
  transform_additional_arguments =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   402
    fn prem => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   403
    let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   404
      val [depth, nrandom, size, seed] = additional_arguments
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   405
      val depth' =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   406
        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
   407
          $ 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
   408
    in [depth', nrandom, size, seed] end
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   409
}
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   410
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   411
val predicate_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   412
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   413
  compilation = Pred,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   414
  function_name_prefix = "",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   415
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   416
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   417
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   418
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   419
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   420
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   421
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   422
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   423
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   424
val annotated_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   425
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   426
  compilation = Annotated,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   427
  function_name_prefix = "annotated_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   428
  compfuns = PredicateCompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   429
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   430
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   431
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   432
  wrap_compilation =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   433
    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
   434
      mk_tracing ("calling predicate " ^ s ^
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   435
        " with mode " ^ string_of_mode mode) compilation,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   436
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   437
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   438
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   439
val dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   440
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   441
  compilation = DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   442
  function_name_prefix = "dseq_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   443
  compfuns = DSequence_CompFuns.compfuns,
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   444
  mk_random = (fn _ => error "no random generation in dseq"),
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   445
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   446
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   447
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   448
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   449
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   450
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   451
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   452
val pos_random_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   453
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   454
  compilation = Pos_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   455
  function_name_prefix = "random_dseq_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   456
  compfuns = Random_Sequence_CompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   457
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   458
  let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   459
    val random = Const ("Quickcheck.random_class.random",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   460
      @{typ code_numeral} --> @{typ Random.seed} -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   461
        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
   462
  in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   463
    Const ("Random_Sequence.Random", (@{typ code_numeral} --> @{typ Random.seed} -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   464
      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
   465
      Random_Sequence_CompFuns.mk_random_dseqT T) $ random
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   466
  end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   467
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   468
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   469
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   470
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   471
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   472
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   473
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   474
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   475
val neg_random_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   476
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   477
  compilation = Neg_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   478
  function_name_prefix = "random_dseq_neg_",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   479
  compfuns = Random_Sequence_CompFuns.compfuns,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   480
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   481
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   482
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   483
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   484
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   485
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   486
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   487
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   488
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   489
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
   490
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   491
  compilation = New_Pos_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   492
  function_name_prefix = "new_random_dseq_",
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   493
  compfuns = New_Pos_Random_Sequence_CompFuns.depth_limited_compfuns,
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   494
  mk_random = (fn T => fn additional_arguments =>
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   495
  let
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   496
    val random = Const ("Quickcheck.random_class.random",
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   497
      @{typ code_numeral} --> @{typ Random.seed} -->
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   498
        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
   499
  in
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   500
    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
   501
      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
   502
      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
   503
  end),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   504
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   505
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   506
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   507
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   508
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   509
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   510
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   511
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
   512
  {
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   513
  compilation = New_Neg_Random_DSeq,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   514
  function_name_prefix = "new_random_dseq_neg_",
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   515
  compfuns = New_Neg_Random_Sequence_CompFuns.depth_limited_compfuns,
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   516
  mk_random = (fn _ => error "no random generation"),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   517
  modify_funT = I,
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   518
  additional_arguments = K [],
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   519
  wrap_compilation = K (K (K (K (K I))))
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   520
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   521
  transform_additional_arguments = K I : (indprem -> term list -> term list)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   522
  }
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   523
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   524
val pos_generator_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   525
  {
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   526
  compilation = Pos_Generator_DSeq,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   527
  function_name_prefix = "generator_dseq_",
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   528
  compfuns = New_Pos_DSequence_CompFuns.depth_limited_compfuns,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   529
  mk_random =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   530
    (fn T => fn additional_arguments =>
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   531
      let
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   532
        val small_lazy =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   533
         Const (@{const_name "Lazy_Sequence.small_lazy_class.small_lazy"},
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   534
         @{typ "Int.int"} --> Type (@{type_name "Lazy_Sequence.lazy_sequence"}, [T])) 
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   535
      in
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   536
        absdummy (@{typ code_numeral}, small_lazy $ HOLogic.mk_number @{typ int} 3)
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   537
      end
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   538
    ),
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   539
  modify_funT = I,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   540
  additional_arguments = K [],
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   541
  wrap_compilation = K (K (K (K (K I))))
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   542
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   543
  transform_additional_arguments = K I : (indprem -> term list -> term list)
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   544
  }
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   545
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   546
val neg_generator_dseq_comp_modifiers = Comp_Mod.Comp_Modifiers
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   547
  {
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   548
  compilation = Neg_Generator_DSeq,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   549
  function_name_prefix = "generator_dseq_neg_",
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   550
  compfuns = New_Neg_DSequence_CompFuns.depth_limited_compfuns,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   551
  mk_random = (fn _ => error "no random generation"),
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   552
  modify_funT = I,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   553
  additional_arguments = K [],
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   554
  wrap_compilation = K (K (K (K (K I))))
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   555
   : (compilation_funs -> string -> typ -> mode -> term list -> term -> term),
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   556
  transform_additional_arguments = K I : (indprem -> term list -> term list)
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   557
  }
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   558
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   559
fun negative_comp_modifiers_of comp_modifiers =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   560
    (case Comp_Mod.compilation comp_modifiers of
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   561
      Pos_Random_DSeq => neg_random_dseq_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   562
    | Neg_Random_DSeq => pos_random_dseq_comp_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   563
    | New_Pos_Random_DSeq => new_neg_random_dseq_comp_modifiers
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   564
    | New_Neg_Random_DSeq => new_pos_random_dseq_comp_modifiers 
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   565
    | Pos_Generator_DSeq => neg_generator_dseq_comp_modifiers
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
   566
    | Neg_Generator_DSeq => pos_generator_dseq_comp_modifiers
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   567
    | c => comp_modifiers)
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   568
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   569
(* term construction *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   570
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   571
fun mk_v (names, vs) s T = (case AList.lookup (op =) vs s of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   572
      NONE => (Free (s, T), (names, (s, [])::vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   573
    | SOME xs =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   574
        let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   575
          val s' = Name.variant names s;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   576
          val v = Free (s', T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   577
        in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   578
          (v, (s'::names, AList.update (op =) (s, v::xs) vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   579
        end);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   580
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   581
fun distinct_v (Free (s, T)) nvs = mk_v nvs s T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   582
  | distinct_v (t $ u) nvs =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   583
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   584
        val (t', nvs') = distinct_v t nvs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   585
        val (u', nvs'') = distinct_v u nvs';
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   586
      in (t' $ u', nvs'') end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   587
  | distinct_v x nvs = (x, nvs);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   588
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   589
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   590
(** specific rpred functions -- move them to the correct place in this file *)
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   591
fun mk_Eval_of (P as (Free (f, _)), T) mode =
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   592
let
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   593
  fun mk_bounds (Type (@{type_name Product_Type.prod}, [T1, T2])) i =
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   594
    let
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   595
      val (bs2, i') = mk_bounds T2 i 
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   596
      val (bs1, i'') = mk_bounds T1 i'
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   597
    in
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   598
      (HOLogic.pair_const T1 T2 $ bs1 $ bs2, i'' + 1)
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   599
    end
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   600
    | mk_bounds T i = (Bound i, i + 1)
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   601
  fun mk_prod ((t1, T1), (t2, T2)) = (HOLogic.pair_const T1 T2 $ t1 $ t2, HOLogic.mk_prodT (T1, T2))
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   602
  fun mk_tuple [] = (HOLogic.unit, HOLogic.unitT)
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   603
    | mk_tuple tTs = foldr1 mk_prod tTs;
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   604
  fun mk_split_abs (T as Type (@{type_name Product_Type.prod}, [T1, T2])) t = absdummy (T, HOLogic.split_const (T1, T2, @{typ bool}) $ (mk_split_abs T1 (mk_split_abs T2 t)))
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   605
    | mk_split_abs T t = absdummy (T, t)
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   606
  val args = rev (fst (fold_map mk_bounds (rev (binder_types T)) 0))
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   607
  val (inargs, outargs) = split_mode mode args
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   608
  val (inTs, outTs) = split_map_modeT (fn _ => fn T => (SOME T, NONE)) mode (binder_types T)
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   609
  val inner_term = PredicateCompFuns.mk_Eval (list_comb (P, inargs), fst (mk_tuple (outargs ~~ outTs)))
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   610
in
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   611
  fold_rev mk_split_abs (binder_types T) inner_term  
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   612
end
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   613
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   614
fun compile_arg compilation_modifiers additional_arguments ctxt param_modes arg = 
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   615
  let
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   616
    fun map_params (t as Free (f, T)) =
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   617
      (case (AList.lookup (op =) param_modes f) of
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   618
          SOME mode =>
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   619
            let
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   620
              val T' = Comp_Mod.funT_of compilation_modifiers mode T
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   621
            in
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   622
              mk_Eval_of (Free (f, T'), T) mode
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   623
            end
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   624
        | NONE => t)
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   625
      | map_params t = t
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   626
  in
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   627
    map_aterms map_params arg
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   628
  end
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   629
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   630
fun compile_match compilation_modifiers additional_arguments ctxt param_modes
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   631
      eqs eqs' out_ts success_t =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   632
  let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   633
    val compfuns = Comp_Mod.compfuns compilation_modifiers
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   634
    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
   635
    val eqs'' =
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   636
      map (compile_arg compilation_modifiers additional_arguments ctxt param_modes) eqs''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   637
    val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   638
    val name = Name.variant names "x";
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   639
    val name' = Name.variant (name :: names) "y";
33148
0808f7d0d0d7 removed tuple functions from the predicate compiler
bulwahn
parents: 33147
diff changeset
   640
    val T = HOLogic.mk_tupleT (map fastype_of out_ts);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   641
    val U = fastype_of success_t;
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   642
    val U' = dest_predT compfuns U;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   643
    val v = Free (name, T);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   644
    val v' = Free (name', T);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   645
  in
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   646
    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
   647
      [(HOLogic.mk_tuple out_ts,
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   648
        if null eqs'' then success_t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   649
        else Const (@{const_name HOL.If}, HOLogic.boolT --> U --> U --> U) $
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   650
          foldr1 HOLogic.mk_conj eqs'' $ success_t $
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   651
            mk_bot compfuns U'),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   652
       (v', mk_bot compfuns U')]))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   653
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   654
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   655
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
   656
  (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
   657
    (t1 $ t2, Mode_App (deriv1, deriv2)) =>
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   658
      string_of_tderiv ctxt (t1, deriv1) ^ " $ " ^ string_of_tderiv ctxt (t2, deriv2)
37391
476270a6c2dc tuned quotes, antiquotations and whitespace
haftmann
parents: 37146
diff changeset
   659
  | (Const (@{const_name Pair}, _) $ t1 $ t2, Mode_Pair (deriv1, deriv2)) =>
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   660
    "(" ^ 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
   661
  | (t, Term Input) => Syntax.string_of_term ctxt t ^ "[Input]"
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   662
  | (t, Term Output) => Syntax.string_of_term ctxt t ^ "[Output]"
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   663
  | (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
   664
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   665
fun compile_expr compilation_modifiers ctxt (t, deriv) param_modes additional_arguments =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   666
  let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   667
    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
   668
    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
   669
      (case (t, deriv) of
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   670
        (t, Term Input) => SOME (compile_arg compilation_modifiers additional_arguments ctxt param_modes 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
   671
      | (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
   672
      | (Const (name, T), Context mode) =>
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
   673
        (case alternative_compilation_of ctxt name mode of
36038
385f706eff24 generalized alternative functions to alternative compilation to handle arithmetic functions better
bulwahn
parents: 36037
diff changeset
   674
          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
   675
        | NONE =>
ee84eac07290 added book-keeping, registration and compilation with alternative functions for predicates in the predicate compiler
bulwahn
parents: 36031
diff changeset
   676
          SOME (Const (function_name_of (Comp_Mod.compilation compilation_modifiers)
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
   677
            ctxt name mode,
36034
ee84eac07290 added book-keeping, registration and compilation with alternative functions for predicates in the predicate compiler
bulwahn
parents: 36031
diff changeset
   678
            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
   679
      | (Free (s, T), Context m) =>
39785
05c4e9ecf5f6 improving the compilation to handle predicate arguments in higher-order argument positions
bulwahn
parents: 39783
diff changeset
   680
        (case (AList.lookup (op =) param_modes s) of
05c4e9ecf5f6 improving the compilation to handle predicate arguments in higher-order argument positions
bulwahn
parents: 39783
diff changeset
   681
          SOME mode => SOME (Free (s, Comp_Mod.funT_of compilation_modifiers m T))
05c4e9ecf5f6 improving the compilation to handle predicate arguments in higher-order argument positions
bulwahn
parents: 39783
diff changeset
   682
        | NONE =>
05c4e9ecf5f6 improving the compilation to handle predicate arguments in higher-order argument positions
bulwahn
parents: 39783
diff changeset
   683
        let
05c4e9ecf5f6 improving the compilation to handle predicate arguments in higher-order argument positions
bulwahn
parents: 39783
diff changeset
   684
          val bs = map (pair "x") (binder_types (fastype_of t))
05c4e9ecf5f6 improving the compilation to handle predicate arguments in higher-order argument positions
bulwahn
parents: 39783
diff changeset
   685
          val bounds = map Bound (rev (0 upto (length bs) - 1))
05c4e9ecf5f6 improving the compilation to handle predicate arguments in higher-order argument positions
bulwahn
parents: 39783
diff changeset
   686
        in SOME (list_abs (bs, mk_if compfuns (list_comb (t, bounds)))) 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
   687
      | (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
   688
        let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   689
          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
   690
          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
   691
        in SOME (list_abs (bs, mk_if compfuns (list_comb (t, bounds)))) end
37391
476270a6c2dc tuned quotes, antiquotations and whitespace
haftmann
parents: 37146
diff changeset
   692
      | (Const (@{const_name Pair}, _) $ t1 $ t2, Mode_Pair (d1, d2)) =>
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   693
        (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
   694
          (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
   695
        | (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
   696
        | (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
   697
        | (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
   698
      | (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
   699
        (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
   700
          (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
   701
         | (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
   702
         | _ => error "something went wrong here!"))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   703
  in
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
   704
    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
   705
  end
33145
1a22f7ca1dfc removed dead code; added examples
bulwahn
parents: 33144
diff changeset
   706
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   707
fun compile_clause compilation_modifiers ctxt all_vs param_modes additional_arguments
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   708
  inp (in_ts, out_ts) moded_ps =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   709
  let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   710
    val compfuns = Comp_Mod.compfuns compilation_modifiers
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   711
    val compile_match = compile_match compilation_modifiers
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   712
      additional_arguments ctxt param_modes
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   713
    val (in_ts', (all_vs', eqs)) =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   714
      fold_map (collect_non_invertible_subterms ctxt) in_ts (all_vs, []);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   715
    fun compile_prems out_ts' vs names [] =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   716
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   717
            val (out_ts'', (names', eqs')) =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   718
              fold_map (collect_non_invertible_subterms ctxt) out_ts' (names, []);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   719
            val (out_ts''', (names'', constr_vs)) = fold_map distinct_v
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   720
              out_ts'' (names', map (rpair []) vs);
39762
02fb709ab3cb handling higher-order relations in output terms; improving proof procedure; added test case
bulwahn
parents: 39761
diff changeset
   721
            val processed_out_ts = map (compile_arg compilation_modifiers additional_arguments
02fb709ab3cb handling higher-order relations in output terms; improving proof procedure; added test case
bulwahn
parents: 39761
diff changeset
   722
              ctxt param_modes) out_ts
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   723
          in
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   724
            compile_match constr_vs (eqs @ eqs') out_ts'''
39762
02fb709ab3cb handling higher-order relations in output terms; improving proof procedure; added test case
bulwahn
parents: 39761
diff changeset
   725
              (mk_single compfuns (HOLogic.mk_tuple processed_out_ts))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   726
          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
   727
      | compile_prems out_ts vs names ((p, deriv) :: ps) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   728
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   729
            val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   730
            val (out_ts', (names', eqs)) =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   731
              fold_map (collect_non_invertible_subterms ctxt) out_ts (names, [])
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   732
            val (out_ts'', (names'', constr_vs')) = fold_map distinct_v
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   733
              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
   734
            val mode = head_mode_of deriv
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
   735
            val additional_arguments' =
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
   736
              Comp_Mod.transform_additional_arguments compilation_modifiers p additional_arguments
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   737
            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
   738
               Prem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   739
                 let
33138
e2e23987c59a reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents: 33137
diff changeset
   740
                   val u =
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   741
                     compile_expr compilation_modifiers ctxt (t, deriv) param_modes 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
   742
                   val (_, out_ts''') = split_mode mode (snd (strip_comb t))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   743
                   val rest = compile_prems out_ts''' vs' names'' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   744
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   745
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   746
                 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
   747
             | Negprem t =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   748
                 let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   749
                   val neg_compilation_modifiers =
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   750
                     negative_comp_modifiers_of compilation_modifiers
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
   751
                   val u = mk_not compfuns
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   752
                     (compile_expr neg_compilation_modifiers ctxt (t, deriv) param_modes 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
   753
                   val (_, out_ts''') = split_mode mode (snd (strip_comb t))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   754
                   val rest = compile_prems out_ts''' vs' names'' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   755
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   756
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   757
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   758
             | Sidecond t =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   759
                 let
36019
64bbbd858c39 generalizing the compilation process of the predicate compiler
bulwahn
parents: 36018
diff changeset
   760
                   val t = compile_arg compilation_modifiers additional_arguments
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   761
                     ctxt param_modes t
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   762
                   val rest = compile_prems [] vs' names'' ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   763
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   764
                   (mk_if compfuns t, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   765
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   766
             | Generator (v, T) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   767
                 let
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
   768
                   val u = Comp_Mod.mk_random compilation_modifiers T additional_arguments
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   769
                   val rest = compile_prems [Free (v, T)]  vs' names'' ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   770
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   771
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   772
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   773
          in
33147
180dc60bd88c improving the compilation with higher-order arguments in the predicate compiler
bulwahn
parents: 33146
diff changeset
   774
            compile_match constr_vs' eqs out_ts''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   775
              (mk_bind compfuns (compiled_clause, rest))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   776
          end
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   777
    val prem_t = compile_prems in_ts' (map fst param_modes) all_vs' moded_ps;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   778
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   779
    mk_bind compfuns (mk_single compfuns inp, prem_t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   780
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   781
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   782
(* switch detection *)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   783
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   784
(** argument position of an inductive predicates and the executable functions **)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   785
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   786
type position = int * int list
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   787
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   788
fun input_positions_pair Input = [[]]
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   789
  | input_positions_pair Output = []
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   790
  | input_positions_pair (Fun _) = []
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   791
  | input_positions_pair (Pair (m1, m2)) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   792
    map (cons 1) (input_positions_pair m1) @ map (cons 2) (input_positions_pair m2)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   793
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   794
fun input_positions_of_mode mode = flat (map_index
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   795
   (fn (i, Input) => [(i, [])]
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   796
   | (_, Output) => []
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   797
   | (_, Fun _) => []
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   798
   | (i, m as Pair (m1, m2)) => map (pair i) (input_positions_pair m))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   799
     (Predicate_Compile_Aux.strip_fun_mode mode))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   800
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   801
fun argument_position_pair mode [] = []
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   802
  | argument_position_pair (Pair (Fun _, m2)) (2 :: is) = argument_position_pair m2 is
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   803
  | argument_position_pair (Pair (m1, m2)) (i :: is) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   804
    (if eq_mode (m1, Output) andalso i = 2 then
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   805
      argument_position_pair m2 is
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   806
    else if eq_mode (m2, Output) andalso i = 1 then
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   807
      argument_position_pair m1 is
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   808
    else (i :: argument_position_pair (if i = 1 then m1 else m2) is))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   809
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   810
fun argument_position_of mode (i, is) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   811
  (i - (length (filter (fn Output => true | Fun _ => true | _ => false)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   812
    (List.take (strip_fun_mode mode, i)))),
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   813
  argument_position_pair (nth (strip_fun_mode mode) i) is)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   814
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   815
fun nth_pair [] t = t
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   816
  | nth_pair (1 :: is) (Const (@{const_name Pair}, _) $ t1 $ _) = nth_pair is t1
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   817
  | nth_pair (2 :: is) (Const (@{const_name Pair}, _) $ _ $ t2) = nth_pair is t2
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   818
  | nth_pair _ _ = raise Fail "unexpected input for nth_tuple"
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   819
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   820
(** switch detection analysis **)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   821
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   822
fun find_switch_test ctxt (i, is) (ts, prems) =
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   823
  let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   824
    val t = nth_pair is (nth ts i)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   825
    val T = fastype_of t
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   826
  in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   827
    case T of
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   828
      TFree _ => NONE
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   829
    | Type (Tcon, _) =>
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   830
      (case Datatype_Data.get_constrs (ProofContext.theory_of ctxt) Tcon of
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   831
        NONE => NONE
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   832
      | SOME cs =>
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   833
        (case strip_comb t of
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   834
          (Var _, []) => NONE
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   835
        | (Free _, []) => NONE
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   836
        | (Const (c, T), _) => if AList.defined (op =) cs c then SOME (c, T) else NONE))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   837
  end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   838
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   839
fun partition_clause ctxt pos moded_clauses =
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   840
  let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   841
    fun insert_list eq (key, value) = AList.map_default eq (key, []) (cons value)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   842
    fun find_switch_test' moded_clause (cases, left) =
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   843
      case find_switch_test ctxt pos moded_clause of
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   844
        SOME (c, T) => (insert_list (op =) ((c, T), moded_clause) cases, left)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   845
      | NONE => (cases, moded_clause :: left)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   846
  in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   847
    fold find_switch_test' moded_clauses ([], [])
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   848
  end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   849
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   850
datatype switch_tree =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   851
  Atom of moded_clause list | Node of (position * ((string * typ) * switch_tree) list) * switch_tree
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   852
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   853
fun mk_switch_tree ctxt mode moded_clauses =
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   854
  let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   855
    fun select_best_switch moded_clauses input_position best_switch =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   856
      let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   857
        val ord = option_ord (rev_order o int_ord o (pairself (length o snd o snd)))
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   858
        val partition = partition_clause ctxt input_position moded_clauses
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   859
        val switch = if (length (fst partition) > 1) then SOME (input_position, partition) else NONE
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   860
      in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   861
        case ord (switch, best_switch) of LESS => best_switch
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   862
          | EQUAL => best_switch | GREATER => switch
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   863
      end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   864
    fun detect_switches moded_clauses =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   865
      case fold (select_best_switch moded_clauses) (input_positions_of_mode mode) NONE of
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   866
        SOME (best_pos, (switched_on, left_clauses)) =>
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   867
          Node ((best_pos, map (apsnd detect_switches) switched_on),
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   868
            detect_switches left_clauses)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   869
      | NONE => Atom moded_clauses
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   870
  in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   871
    detect_switches moded_clauses
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   872
  end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   873
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   874
(** compilation of detected switches **)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   875
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   876
fun destruct_constructor_pattern (pat, obj) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   877
  (case strip_comb pat of
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   878
    (f as Free _, []) => cons (pat, obj)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   879
  | (Const (c, T), pat_args) =>
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   880
    (case strip_comb obj of
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   881
      (Const (c', T'), obj_args) =>
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   882
        (if c = c' andalso T = T' then
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   883
          fold destruct_constructor_pattern (pat_args ~~ obj_args)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   884
        else raise Fail "pattern and object mismatch")
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   885
    | _ => raise Fail "unexpected object")
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   886
  | _ => raise Fail "unexpected pattern")
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   887
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   888
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   889
fun compile_switch compilation_modifiers ctxt all_vs param_modes additional_arguments mode
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   890
  in_ts' outTs switch_tree =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   891
  let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   892
    val compfuns = Comp_Mod.compfuns compilation_modifiers
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   893
    val thy = ProofContext.theory_of ctxt
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   894
    fun compile_switch_tree _ _ (Atom []) = NONE
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   895
      | compile_switch_tree all_vs ctxt_eqs (Atom moded_clauses) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   896
        let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   897
          val in_ts' = map (Pattern.rewrite_term thy ctxt_eqs []) in_ts'
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   898
          fun compile_clause' (ts, moded_ps) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   899
            let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   900
              val (ts, out_ts) = split_mode mode ts
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   901
              val subst = fold destruct_constructor_pattern (in_ts' ~~ ts) []
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   902
              val (fsubst, pat') = List.partition (fn (_, Free _) => true | _ => false) subst
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   903
              val moded_ps' = (map o apfst o map_indprem)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   904
                (Pattern.rewrite_term thy (map swap fsubst) []) moded_ps
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   905
              val inp = HOLogic.mk_tuple (map fst pat')
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   906
              val in_ts' = map (Pattern.rewrite_term thy (map swap fsubst) []) (map snd pat')
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   907
              val out_ts' = map (Pattern.rewrite_term thy (map swap fsubst) []) out_ts
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   908
            in
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   909
              compile_clause compilation_modifiers ctxt all_vs param_modes additional_arguments
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   910
                inp (in_ts', out_ts') moded_ps'
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   911
            end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   912
        in SOME (foldr1 (mk_sup compfuns) (map compile_clause' moded_clauses)) end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   913
    | compile_switch_tree all_vs ctxt_eqs (Node ((position, switched_clauses), left_clauses)) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   914
      let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   915
        val (i, is) = argument_position_of mode position
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   916
        val inp_var = nth_pair is (nth in_ts' i)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   917
        val x = Name.variant all_vs "x"
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   918
        val xt = Free (x, fastype_of inp_var)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   919
        fun compile_single_case ((c, T), switched) =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   920
          let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   921
            val Ts = binder_types T
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   922
            val argnames = Name.variant_list (x :: all_vs)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   923
              (map (fn i => "c" ^ string_of_int i) (1 upto length Ts))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   924
            val args = map2 (curry Free) argnames Ts
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   925
            val pattern = list_comb (Const (c, T), args)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   926
            val ctxt_eqs' = (inp_var, pattern) :: ctxt_eqs
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   927
            val compilation = the_default (mk_bot compfuns (HOLogic.mk_tupleT outTs))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   928
              (compile_switch_tree (argnames @ x :: all_vs) ctxt_eqs' switched)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   929
        in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   930
          (pattern, compilation)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   931
        end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   932
        val switch = fst (Datatype.make_case ctxt Datatype_Case.Quiet [] inp_var
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   933
          ((map compile_single_case switched_clauses) @
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   934
            [(xt, mk_bot compfuns (HOLogic.mk_tupleT outTs))]))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   935
      in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   936
        case compile_switch_tree all_vs ctxt_eqs left_clauses of
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   937
          NONE => SOME switch
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   938
        | SOME left_comp => SOME (mk_sup compfuns (switch, left_comp))
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   939
      end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   940
  in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   941
    compile_switch_tree all_vs [] switch_tree
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   942
  end
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   943
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   944
(* compilation of predicates *)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   945
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
   946
fun compile_pred options compilation_modifiers ctxt all_vs param_vs s T (pol, mode) moded_cls =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   947
  let
40050
638ce4dabe53 for now safely but unpractically assume no predicate is terminating
bulwahn
parents: 40049
diff changeset
   948
    val is_terminating = false (* FIXME: requires an termination analysis *)  
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   949
    val compilation_modifiers =
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   950
      (if pol then compilation_modifiers else
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   951
        negative_comp_modifiers_of compilation_modifiers)
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   952
      |> (if is_depth_limited_compilation (Comp_Mod.compilation compilation_modifiers) then
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   953
           (if is_terminating then
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   954
             (Comp_Mod.set_compfuns (unlimited_compfuns_of pol (Comp_Mod.compilation compilation_modifiers)))
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   955
           else
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   956
             (Comp_Mod.set_compfuns (limited_compfuns_of pol (Comp_Mod.compilation compilation_modifiers))))
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
   957
         else I)
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
   958
    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
   959
      (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
   960
    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
   961
    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
   962
      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
   963
      | 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
   964
    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
   965
      (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
   966
    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
   967
    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
   968
    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
   969
      (fn T => fn (param_vs, names) =>
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
   970
        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
   971
          (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
   972
        else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   973
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
   974
            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
   975
          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
   976
        (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
   977
    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
   978
      (fn t as Free (x, _) => if member (op =) param_vs x then NONE else SOME t | t => SOME t)) in_ts
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   979
    val param_modes = param_vs ~~ ho_arg_modes_of mode
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   980
    val compilation =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   981
      if detect_switches options then
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   982
        the_default (mk_bot compfuns (HOLogic.mk_tupleT outTs))
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   983
          (compile_switch compilation_modifiers ctxt all_vs param_modes additional_arguments mode
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   984
            in_ts' outTs (mk_switch_tree ctxt mode moded_cls))
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   985
      else
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   986
        let
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   987
          val cl_ts =
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   988
            map (fn (ts, moded_prems) => 
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   989
              compile_clause compilation_modifiers ctxt all_vs param_modes additional_arguments
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
   990
                (HOLogic.mk_tuple in_ts') (split_mode mode ts) moded_prems) moded_cls;
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   991
        in
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   992
          Comp_Mod.wrap_compilation compilation_modifiers compfuns s T mode additional_arguments
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   993
            (if null cl_ts then
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   994
              mk_bot compfuns (HOLogic.mk_tupleT outTs)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   995
            else
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   996
              foldr1 (mk_sup compfuns) cl_ts)
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
   997
        end
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
   998
    val fun_const =
35891
3122bdd95275 contextifying the compilation of the predicate compiler
bulwahn
parents: 35889
diff changeset
   999
      Const (function_name_of (Comp_Mod.compilation compilation_modifiers)
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1000
      ctxt s mode, funT)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1001
  in
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1002
    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
  1003
      (HOLogic.mk_eq (list_comb (fun_const, in_ts @ additional_arguments), compilation))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1004
  end;
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1005
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1006
(* Definition of executable functions and their intro and elim rules *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1007
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1008
fun print_arities arities = tracing ("Arities:\n" ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1009
  cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1010
    space_implode " -> " (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1011
      (fn NONE => "X" | SOME k' => string_of_int k')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1012
        (ks @ [SOME k]))) arities));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1013
37591
d3daea901123 merged constants "split" and "prod_case"
haftmann
parents: 37558
diff changeset
  1014
fun strip_split_abs (Const (@{const_name prod_case}, _) $ t) = strip_split_abs 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
  1015
  | 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
  1016
  | 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
  1017
37678
0040bafffdef "prod" and "sum" replace "*" and "+" respectively
haftmann
parents: 37591
diff changeset
  1018
fun mk_args is_eval (m as Pair (m1, m2), T as Type (@{type_name Product_Type.prod}, [T1, T2])) names =
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
  1019
    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
  1020
      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
  1021
        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
  1022
      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
  1023
        (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
  1024
      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
  1025
    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
  1026
      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
  1027
        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
  1028
        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
  1029
      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
  1030
        (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
  1031
      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
  1032
  | 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
  1033
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1034
      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
  1035
      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
  1036
      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
  1037
      val (inargs, outargs) = split_map_mode (fn _ => fn t => (SOME t, NONE)) m args
39756
6c8e83d94536 consolidated tupled_lambda; moved to structure HOLogic
krauss
parents: 39685
diff changeset
  1038
      val t = fold_rev HOLogic.tupled_lambda args (PredicateCompFuns.mk_Eval
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1039
        (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
  1040
    in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1041
      (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
  1042
    end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1043
  | 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
  1044
    let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1045
      val x = Name.variant names "x"
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1046
    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
  1047
      (Free (x, T), x :: names)
33268
02de0317f66f eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 33265
diff changeset
  1048
    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
  1049
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1050
fun create_intro_elim_rule ctxt mode defthm mode_id funT 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
  1051
  let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1052
    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
  1053
    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
  1054
    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
  1055
    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
  1056
      let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1057
        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
  1058
        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
  1059
      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
  1060
    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
  1061
    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
  1062
    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
  1063
    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
  1064
      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
  1065
    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
  1066
    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
  1067
    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
  1068
    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
  1069
    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
  1070
    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
  1071
                    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
  1072
    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
  1073
                     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
  1074
    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
  1075
    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
  1076
      @{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
  1077
    val unfolddef_tac = Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps simprules) 1
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1078
    val introthm = Goal.prove 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
  1079
      (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
  1080
    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
  1081
    val elimtrm = Logic.list_implies ([funpropE, Logic.mk_implies (predpropE, P)], P)
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1082
    val elimthm = Goal.prove 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
  1083
      (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
  1084
    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
  1085
      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
  1086
        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
  1087
          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
  1088
          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
  1089
            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
  1090
              (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
  1091
          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
  1092
          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
  1093
            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
  1094
              (@{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
  1095
            THEN rtac @{thm Predicate.singleI} 1
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1096
        in SOME (Goal.prove ctxt (argnames @ hoarg_names') []
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
  1097
            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
  1098
        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
  1099
      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
  1100
  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
  1101
    ((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
  1102
  end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1103
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1104
fun create_constname_of_mode options thy prefix name T mode = 
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1105
  let
33626
42f69386943a new names for predicate functions in the predicate compiler
bulwahn
parents: 33623
diff changeset
  1106
    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
  1107
      ^ "_" ^ 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
  1108
    val name = the_default system_proposal (proposed_names options name mode)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1109
  in
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1110
    Sign.full_bname thy name
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1111
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1112
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1113
fun create_definitions options preds (name, modes) thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1114
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1115
    val compfuns = PredicateCompFuns.compfuns
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1116
    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
  1117
    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
  1118
      let
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1119
        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
  1120
        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
  1121
        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
  1122
        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
  1123
        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
  1124
          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
  1125
            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
  1126
            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
  1127
          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
  1128
        val (inargs, outargs) = split_map_mode strip_eval mode args
39756
6c8e83d94536 consolidated tupled_lambda; moved to structure HOLogic
krauss
parents: 39685
diff changeset
  1129
        val predterm = fold_rev HOLogic.tupled_lambda inargs
6c8e83d94536 consolidated tupled_lambda; moved to structure HOLogic
krauss
parents: 39685
diff changeset
  1130
          (PredicateCompFuns.mk_Enum (HOLogic.tupled_lambda (HOLogic.mk_tuple outargs)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1131
            (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
  1132
        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
  1133
        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
  1134
        val ([definition], thy') = thy |>
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1135
          Sign.add_consts_i [(Binding.name mode_cbasename, funT, NoSyn)] |>
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39545
diff changeset
  1136
          Global_Theory.add_defs false [((Binding.name (mode_cbasename ^ "_def"), def), [])]
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1137
        val ctxt' = ProofContext.init_global thy'
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
  1138
        val rules as ((intro, elim), _) =
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1139
          create_intro_elim_rule ctxt' mode definition mode_cname funT (Const (name, T))
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1140
        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
  1141
          |> 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
  1142
          |> add_predfun_data name mode (definition, rules)
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39545
diff changeset
  1143
          |> Global_Theory.store_thm (Binding.name (mode_cbasename ^ "I"), intro) |> snd
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39545
diff changeset
  1144
          |> Global_Theory.store_thm (Binding.name (mode_cbasename ^ "E"), elim)  |> snd
33752
9aa8e961f850 adopting proposed_modes; adding a new dimension of complexity for nicer error messages; tuned
bulwahn
parents: 33671
diff changeset
  1145
          |> Theory.checkpoint
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1146
        end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1147
  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
  1148
    thy |> defined_function_of Pred name |> fold create_definition modes
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1149
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1150
33620
b6bf2dc5aed7 added interface of user proposals for names of generated constants
bulwahn
parents: 33619
diff changeset
  1151
fun define_functions comp_modifiers compfuns options preds (name, modes) thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1152
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1153
    val T = AList.lookup (op =) preds name |> the
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1154
    fun create_definition mode thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1155
      let
33485
0df4f6e46e5e made definition of functions generically for the different instances
bulwahn
parents: 33484
diff changeset
  1156
        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
  1157
        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
  1158
        val funT = Comp_Mod.funT_of comp_modifiers mode T
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1159
      in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1160
        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
  1161
        |> set_function_name (Comp_Mod.compilation comp_modifiers) name mode mode_cname
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1162
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1163
  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
  1164
    thy
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1165
    |> 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
  1166
    |> fold create_definition modes
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1167
  end;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1168
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1169
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1170
(* composition of mode inference, definition, compilation and proof *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1171
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1172
(** auxillary combinators for table of preds and modes **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1173
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1174
fun map_preds_modes f preds_modes_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1175
  map (fn (pred, modes) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1176
    (pred, map (fn (mode, value) => (mode, f pred mode value)) modes)) preds_modes_table
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1177
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1178
fun join_preds_modes table1 table2 =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1179
  map_preds_modes (fn pred => fn mode => fn value =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1180
    (value, the (AList.lookup (op =) (the (AList.lookup (op =) table2 pred)) mode))) table1
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
  1181
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1182
fun maps_modes preds_modes_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1183
  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
  1184
    (pred, map (fn (mode, value) => value) modes)) preds_modes_table
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
  1185
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
  1186
fun compile_preds options comp_modifiers ctxt all_vs param_vs preds moded_clauses =
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
  1187
  map_preds_modes (fn pred => compile_pred options comp_modifiers ctxt all_vs param_vs pred
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1188
      (the (AList.lookup (op =) preds pred))) moded_clauses
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1189
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
  1190
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
  1191
  map_preds_modes (prove_pred options thy clauses preds)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1192
    (join_preds_modes moded_clauses compiled_terms)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1193
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
  1194
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
  1195
  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
  1196
    (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
  1197
    compiled_terms
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1198
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1199
(* preparation of introduction rules into special datastructures *)
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1200
fun dest_prem ctxt params t =
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1201
  (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
  1202
    (v as Free _, ts) => if member (op =) params v then Prem t else Sidecond t
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1203
  | (c as Const (@{const_name Not}, _), [t]) => (case dest_prem ctxt 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
  1204
      Prem t => Negprem t
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1205
    | Negprem _ => error ("Double negation not allowed in premise: " ^
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1206
        Syntax.string_of_term ctxt (c $ t)) 
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1207
    | Sidecond t => Sidecond (c $ t))
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1208
  | (c as Const (s, _), ts) =>
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1209
    if is_registered ctxt 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
  1210
  | _ => 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
  1211
39310
17ef4415b17c removing obsolete argument in prepare_intrs; passing context instead of theory in prepare_intrs
bulwahn
parents: 39299
diff changeset
  1212
fun prepare_intrs options ctxt prednames intros =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1213
  let
39310
17ef4415b17c removing obsolete argument in prepare_intrs; passing context instead of theory in prepare_intrs
bulwahn
parents: 39299
diff changeset
  1214
    val thy = ProofContext.theory_of ctxt
33126
bb8806eb5da7 importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents: 33124
diff changeset
  1215
    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
  1216
    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
  1217
    val (preds, intrs) = unify_consts thy preds intrs
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1218
    val ([preds, intrs], _) = fold_burrow (Variable.import_terms false) [preds, intrs] ctxt
33126
bb8806eb5da7 importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents: 33124
diff changeset
  1219
    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
  1220
    val all_vs = terms_vs intrs
39201
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
  1221
    fun generate_modes s T =
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
  1222
      if member (op =) (no_higher_order_predicate options) s then
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
  1223
        all_smodes_of_typ T
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
  1224
      else
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
  1225
        all_modes_of_typ 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
  1226
    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
  1227
      map (fn (s, T) =>
39382
c797f3ab2ae1 proposed modes for code_pred now supports modes for mutual predicates
bulwahn
parents: 39310
diff changeset
  1228
        (s, case proposed_modes options s of
39651
2e06dad03dd3 adding check if user-given modes match type of predicates; removed useless function expand_tuples_elim
bulwahn
parents: 39649
diff changeset
  1229
            SOME ms => check_matches_type ctxt s T ms
39201
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
  1230
          | NONE => generate_modes s 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
  1231
    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
  1232
      case intrs of
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  1233
        [] =>
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  1234
          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
  1235
            val T = snd (hd preds)
39764
1cf2088cf035 only modes but not types are used to destruct terms and types; this allows to interpret arguments that are predicates simply as input
bulwahn
parents: 39763
diff changeset
  1236
            val one_mode = hd (the (AList.lookup (op =) all_modes (fst (hd 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
  1237
            val paramTs =
39764
1cf2088cf035 only modes but not types are used to destruct terms and types; this allows to interpret arguments that are predicates simply as input
bulwahn
parents: 39763
diff changeset
  1238
              ho_argsT_of one_mode (binder_types T)
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1239
            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
  1240
              (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
  1241
          in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1242
            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
  1243
          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
  1244
      | (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
  1245
        let
39764
1cf2088cf035 only modes but not types are used to destruct terms and types; this allows to interpret arguments that are predicates simply as input
bulwahn
parents: 39763
diff changeset
  1246
          val (p, args) = strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl intr))
1cf2088cf035 only modes but not types are used to destruct terms and types; this allows to interpret arguments that are predicates simply as input
bulwahn
parents: 39763
diff changeset
  1247
          val one_mode = hd (the (AList.lookup (op =) all_modes (fst (dest_Const p))))
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
  1248
        in
39764
1cf2088cf035 only modes but not types are used to destruct terms and types; this allows to interpret arguments that are predicates simply as input
bulwahn
parents: 39763
diff changeset
  1249
          ho_args_of one_mode 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
  1250
        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
  1251
    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
  1252
    fun add_clause intr clauses =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1253
      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
  1254
        val (Const (name, T), ts) = strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl intr))
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1255
        val prems = map (dest_prem ctxt params o HOLogic.dest_Trueprop) (Logic.strip_imp_prems intr)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1256
      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
  1257
        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
  1258
          [(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
  1259
      end;
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1260
    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
  1261
  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
  1262
    (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
  1263
  end;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1264
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1265
(* 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
  1266
(* 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
  1267
(*
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1268
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
  1269
  let
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1270
    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
  1271
    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
  1272
    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
  1273
    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
  1274
      (Ts as _ :: _ :: _) =>
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  1275
        if length (HOLogic.strip_tuple arg) = length Ts then
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  1276
          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
  1277
        else
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  1278
          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
  1279
          ^ (Syntax.string_of_term_global thy arg) ^ " in " ^
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  1280
          (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
  1281
      | _ => true
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1282
    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
  1283
    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
  1284
      | 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
  1285
      | check_prem _ = true
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1286
  in
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1287
    forall check_arg args andalso
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  1288
    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
  1289
  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
  1290
*)
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1291
(*
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1292
fun check_intros_elim_match thy prednames =
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1293
  let
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1294
    fun check predname =
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1295
      let
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1296
        val intros = intros_of thy predname
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1297
        val elim = the_elim_of thy predname
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1298
        val nparams = nparams_of thy predname
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1299
        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
  1300
          (Drule.export_without_context o Skip_Proof.make_thm thy)
36610
bafd82950e24 renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents: 36533
diff changeset
  1301
          (mk_casesrule (ProofContext.init_global thy) nparams intros)
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1302
      in
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1303
        if not (Thm.equiv_thm (elim, elim')) then
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1304
          error "Introduction and elimination rules do not match!"
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1305
        else true
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1306
      end
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1307
  in forall check prednames end
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1308
*)
33113
0f6e30b87cf1 processing of tuples in introduction rules
bulwahn
parents: 33112
diff changeset
  1309
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1310
(* create code equation *)
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1311
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1312
fun add_code_equations ctxt preds result_thmss =
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1313
  let
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  1314
    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
  1315
      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
  1316
        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
  1317
      in
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1318
        if member (op =) (modes_of Pred ctxt predname) full_mode then
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1319
          let
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1320
            val Ts = binder_types T
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1321
            val arg_names = Name.variant_list []
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1322
              (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
  1323
            val args = map2 (curry Free) arg_names Ts
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1324
            val predfun = Const (function_name_of Pred ctxt predname full_mode,
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1325
              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
  1326
            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
  1327
            val eq_term = HOLogic.mk_Trueprop
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1328
              (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
  1329
            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
  1330
            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
  1331
              (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
  1332
            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
  1333
          in
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1334
            (pred, result_thms @ [eq])
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1335
          end
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1336
        else
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1337
          (pred, result_thms)
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1338
      end
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1339
  in
33629
5f35cf91c6a4 improving code quality thanks to Florian's code review
bulwahn
parents: 33628
diff changeset
  1340
    map2 add_code_equation preds result_thmss
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1341
  end
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1342
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1343
(** main function of predicate compiler **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1344
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1345
datatype steps = Steps of
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1346
  {
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
  1347
  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
  1348
  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
  1349
    -> moded_clause list pred_mode_table -> term pred_mode_table -> thm pred_mode_table,
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1350
  add_code_equations : Proof.context -> (string * typ) list
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1351
    -> (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
  1352
  comp_modifiers : Comp_Mod.comp_modifiers,
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1353
  use_generators : bool,
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1354
  qname : bstring
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1355
  }
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1356
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
  1357
fun add_equations_of steps mode_analysis_options options prednames thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1358
  let
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1359
    fun dest_steps (Steps s) = s
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1360
    val compilation = Comp_Mod.compilation (#comp_modifiers (dest_steps steps))
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1361
    val ctxt = ProofContext.init_global thy
33482
5029ec373036 strictly respecting the line margin in the predicate compiler core
bulwahn
parents: 33479
diff changeset
  1362
    val _ = print_step options
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1363
      ("Starting predicate compiler (compilation: " ^ string_of_compilation compilation
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1364
        ^ ") for predicates " ^ commas prednames ^ "...")
33124
5378e61add1a continued cleaning up; moved tuple expanding to core
bulwahn
parents: 33123
diff changeset
  1365
      (*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
  1366
      (*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
  1367
    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
  1368
      if show_intermediate_results options then
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1369
        tracing (commas (map (Display.string_of_thm ctxt) (maps (intros_of ctxt) 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
  1370
      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
  1371
    val (preds, all_vs, param_vs, all_modes, clauses) =
39310
17ef4415b17c removing obsolete argument in prepare_intrs; passing context instead of theory in prepare_intrs
bulwahn
parents: 39299
diff changeset
  1372
      prepare_intrs options ctxt prednames (maps (intros_of ctxt) prednames)
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  1373
    val _ = print_step options "Infering modes..."
39273
92aa2a0f7399 refactoring mode inference so that the theory is not changed in the mode inference procedure
bulwahn
parents: 39201
diff changeset
  1374
    val (lookup_mode, lookup_neg_mode, needs_random) = (modes_of compilation ctxt,
92aa2a0f7399 refactoring mode inference so that the theory is not changed in the mode inference procedure
bulwahn
parents: 39201
diff changeset
  1375
      modes_of (negative_compilation_of compilation) ctxt, needs_random ctxt)
92aa2a0f7399 refactoring mode inference so that the theory is not changed in the mode inference procedure
bulwahn
parents: 39201
diff changeset
  1376
    val ((moded_clauses, needs_random), errors) =
37009
4ba91ea2bf6d deactivated timing of infering modes
bulwahn
parents: 37007
diff changeset
  1377
      Output.cond_timeit (!Quickcheck.timing) "Infering modes"
36251
5fd5d732a4ea only add relevant predicates to the list of extra modes
bulwahn
parents: 36247
diff changeset
  1378
      (fn _ => infer_modes mode_analysis_options
39273
92aa2a0f7399 refactoring mode inference so that the theory is not changed in the mode inference procedure
bulwahn
parents: 39201
diff changeset
  1379
        options (lookup_mode, lookup_neg_mode, needs_random) ctxt preds all_modes param_vs clauses)
92aa2a0f7399 refactoring mode inference so that the theory is not changed in the mode inference procedure
bulwahn
parents: 39201
diff changeset
  1380
    val thy' = fold (fn (s, ms) => set_needs_random s ms) needs_random 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
  1381
    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
  1382
    val _ = check_expected_modes preds options modes
39201
234e6ef4ff67 using the proposed modes for starting the fixpoint iteration in the mode analysis
bulwahn
parents: 39194
diff changeset
  1383
    val _ = check_proposed_modes preds options modes errors
37004
c62f743e37d4 removing unused argument in print_modes function
bulwahn
parents: 37003
diff changeset
  1384
    val _ = print_modes options modes
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  1385
    val _ = print_step options "Defining executable functions..."
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  1386
    val thy'' =
36261
01ccbaa3f49f make profiling depend on reference Quickcheck.timing
bulwahn
parents: 36258
diff changeset
  1387
      Output.cond_timeit (!Quickcheck.timing) "Defining executable functions..."
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  1388
      (fn _ => fold (#define_functions (dest_steps steps) options preds) modes thy'
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  1389
      |> Theory.checkpoint)
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
  1390
    val ctxt'' = ProofContext.init_global thy''
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  1391
    val _ = print_step options "Compiling equations..."
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1392
    val compiled_terms =
36261
01ccbaa3f49f make profiling depend on reference Quickcheck.timing
bulwahn
parents: 36258
diff changeset
  1393
      Output.cond_timeit (!Quickcheck.timing) "Compiling equations...." (fn _ =>
36254
95ef0a3cf31c added switch detection to the predicate compiler
bulwahn
parents: 36252
diff changeset
  1394
        compile_preds options
37005
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
  1395
          (#comp_modifiers (dest_steps steps)) ctxt'' all_vs param_vs preds moded_clauses)
842a73dc6d0e changing compilation to work only with contexts; adapting quickcheck
bulwahn
parents: 37004
diff changeset
  1396
    val _ = print_compiled_terms options ctxt'' compiled_terms
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33120
diff changeset
  1397
    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
  1398
    val result_thms =
36261
01ccbaa3f49f make profiling depend on reference Quickcheck.timing
bulwahn
parents: 36258
diff changeset
  1399
      Output.cond_timeit (!Quickcheck.timing) "Proving equations...." (fn _ =>
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  1400
      #prove (dest_steps steps) options thy'' clauses preds moded_clauses compiled_terms)
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1401
    val result_thms' = #add_code_equations (dest_steps steps) ctxt'' preds
33376
5cb663aa48ee predicate compiler creates code equations for predicates with full mode
bulwahn
parents: 33375
diff changeset
  1402
      (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
  1403
    val qname = #qname (dest_steps steps)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1404
    val attrib = fn thy => Attrib.attribute_i thy (Attrib.internal (K (Thm.declaration_attribute
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1405
      (fn thm => Context.mapping (Code.add_eqn thm) I))))
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  1406
    val thy''' =
36261
01ccbaa3f49f make profiling depend on reference Quickcheck.timing
bulwahn
parents: 36258
diff changeset
  1407
      Output.cond_timeit (!Quickcheck.timing) "Setting code equations...." (fn _ =>
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39545
diff changeset
  1408
      fold (fn (name, result_thms) => fn thy => snd (Global_Theory.add_thmss
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1409
      [((Binding.qualify true (Long_Name.base_name name) (Binding.name qname), result_thms),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1410
        [attrib thy ])] thy))
36252
beba03215d8f adding more profiling to the predicate compiler
bulwahn
parents: 36251
diff changeset
  1411
      result_thms' thy'' |> Theory.checkpoint)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1412
  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
  1413
    thy'''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1414
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1415
  
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
  1416
fun gen_add_equations steps options names thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1417
  let
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1418
    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
  1419
    val defined = defined_functions (Comp_Mod.compilation (#comp_modifiers (dest_steps steps)))
40053
3fa49ea76cbb using a signature in core_data and moving some more functions to core_data
bulwahn
parents: 40052
diff changeset
  1420
    val thy' = extend_intro_graph names thy |> Theory.checkpoint;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1421
    fun strong_conn_of gr keys =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1422
      Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1423
    val scc = strong_conn_of (PredData.get thy') names
40053
3fa49ea76cbb using a signature in core_data and moving some more functions to core_data
bulwahn
parents: 40052
diff changeset
  1424
    val thy'' = fold preprocess_intros (flat scc) thy'
39657
5e57675b7e40 moving the preprocessing of introduction rules after the code_pred command; added tuple expansion preprocessing of elimination rule
bulwahn
parents: 39653
diff changeset
  1425
    val thy''' = fold_rev
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1426
      (fn preds => fn thy =>
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1427
        if not (forall (defined (ProofContext.init_global 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
  1428
          let
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1429
            val mode_analysis_options = {use_generators = #use_generators (dest_steps steps),
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
              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
  1431
                not (no_topmost_reordering options andalso not (null (inter (op =) preds names))),
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1432
              infer_pos_and_neg_modes = #use_generators (dest_steps steps)}
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
          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
  1434
            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
  1435
          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
  1436
        else thy)
39657
5e57675b7e40 moving the preprocessing of introduction rules after the code_pred command; added tuple expansion preprocessing of elimination rule
bulwahn
parents: 39653
diff changeset
  1437
      scc thy'' |> Theory.checkpoint
5e57675b7e40 moving the preprocessing of introduction rules after the code_pred command; added tuple expansion preprocessing of elimination rule
bulwahn
parents: 39653
diff changeset
  1438
  in thy''' end
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1439
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1440
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
  1441
  (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
  1442
  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
  1443
    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
  1444
      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
  1445
      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
  1446
  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
  1447
  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
  1448
  comp_modifiers = predicate_comp_modifiers,
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1449
  use_generators = 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
  1450
  qname = "equation"})
33143
730a2e8a6ec6 modularized the compilation in the predicate compiler
bulwahn
parents: 33141
diff changeset
  1451
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
  1452
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
  1453
  (Steps {
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1454
  define_functions =
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1455
    fn options => fn preds => fn (s, modes) =>
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1456
    define_functions depth_limited_comp_modifiers PredicateCompFuns.compfuns
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1457
    options preds (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1458
  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
  1459
  add_code_equations = K (K I),
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1460
  comp_modifiers = depth_limited_comp_modifiers,
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1461
  use_generators = false,
33330
d6eb7f19bfc6 encapsulating records with datatype constructors and adding type annotations to make SML/NJ happy
bulwahn
parents: 33328
diff changeset
  1462
  qname = "depth_limited_equation"})
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1463
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
  1464
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
  1465
  (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
  1466
  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
  1467
    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
  1468
      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
  1469
      (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
  1470
  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
  1471
  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
  1472
  comp_modifiers = annotated_comp_modifiers,
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1473
  use_generators = false,
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 33377
diff changeset
  1474
  qname = "annotated_equation"})
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1475
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1476
val add_random_equations = gen_add_equations
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1477
  (Steps {
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1478
  define_functions =
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1479
    fn options => fn preds => fn (s, modes) =>
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1480
      define_functions random_comp_modifiers PredicateCompFuns.compfuns options preds
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1481
      (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
  1482
  comp_modifiers = random_comp_modifiers,
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1483
  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
  1484
  add_code_equations = K (K I),
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1485
  use_generators = true,
33375
fd3e861f8d31 renamed rpred to random
bulwahn
parents: 33333
diff changeset
  1486
  qname = "random_equation"})
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1487
35881
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1488
val add_depth_limited_random_equations = gen_add_equations
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1489
  (Steps {
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1490
  define_functions =
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1491
    fn options => fn preds => fn (s, modes) =>
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1492
      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
  1493
      (s, map_filter (fn (true, m) => SOME m | _ => NONE) modes),
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1494
  comp_modifiers = depth_limited_random_comp_modifiers,
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1495
  prove = prove_by_skip,
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1496
  add_code_equations = K (K I),
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1497
  use_generators = true,
35881
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1498
  qname = "depth_limited_random_equation"})
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1499
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1500
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
  1501
  (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
  1502
  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
  1503
  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
  1504
    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
  1505
    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
  1506
  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
  1507
  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
  1508
  comp_modifiers = dseq_comp_modifiers,
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1509
  use_generators = 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
  1510
  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
  1511
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1512
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
  1513
  (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
  1514
  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
  1515
    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
  1516
    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
  1517
      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
  1518
      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
  1519
    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
  1520
      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
  1521
      #> 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
  1522
      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
  1523
    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
  1524
  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
  1525
  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
  1526
  comp_modifiers = pos_random_dseq_comp_modifiers,
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1527
  use_generators = 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
  1528
  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
  1529
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1530
val add_new_random_dseq_equations = gen_add_equations
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1531
  (Steps {
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1532
  define_functions =
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1533
    fn options => fn preds => fn (s, modes) =>
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1534
    let
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1535
      val pos_modes = map_filter (fn (true, m) => SOME m | _ => NONE) modes
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1536
      val neg_modes = map_filter (fn (false, m) => SOME m | _ => NONE) modes
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
  1537
    in define_functions new_pos_random_dseq_comp_modifiers New_Pos_Random_Sequence_CompFuns.depth_limited_compfuns
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1538
      options preds (s, pos_modes)
40049
75d9f57123d6 adding decreasing bind and non-decreasing bind; depth-limited and depth-unlimited compilation possible
bulwahn
parents: 39785
diff changeset
  1539
      #> define_functions new_neg_random_dseq_comp_modifiers New_Neg_Random_Sequence_CompFuns.depth_limited_compfuns
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1540
      options preds (s, neg_modes)
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1541
    end,
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1542
  prove = prove_by_skip,
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1543
  add_code_equations = K (K I),
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1544
  comp_modifiers = new_pos_random_dseq_comp_modifiers,
39761
c2a76ec6e2d9 renaming use_random to use_generators in the predicate compiler
bulwahn
parents: 39760
diff changeset
  1545
  use_generators = true,
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1546
  qname = "new_random_dseq_equation"})
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1547
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1548
val add_generator_dseq_equations = gen_add_equations
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1549
  (Steps {
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1550
  define_functions =
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1551
  fn options => fn preds => fn (s, modes) =>
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1552
    let
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1553
      val pos_modes = map_filter (fn (true, m) => SOME m | _ => NONE) modes
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1554
      val neg_modes = map_filter (fn (false, m) => SOME m | _ => NONE) modes
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1555
    in 
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1556
      define_functions pos_generator_dseq_comp_modifiers New_Pos_DSequence_CompFuns.depth_limited_compfuns
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1557
        options preds (s, pos_modes)
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1558
      #> define_functions neg_generator_dseq_comp_modifiers New_Neg_DSequence_CompFuns.depth_limited_compfuns
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1559
        options preds (s, neg_modes)
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1560
    end,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1561
  prove = prove_by_skip,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1562
  add_code_equations = K (K I),
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1563
  comp_modifiers = pos_generator_dseq_comp_modifiers,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1564
  use_generators = true,
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1565
  qname = "generator_dseq_equation"})
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1566
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1567
(** user interface **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1568
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1569
(* code_pred_intro attribute *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1570
39545
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1571
fun attrib' f opt_case_name =
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1572
  Thm.declaration_attribute (fn thm => Context.mapping (f (opt_case_name, thm)) I);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1573
39545
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1574
val code_pred_intro_attrib = attrib' add_intro NONE;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1575
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1576
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1577
(*FIXME
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1578
- Naming of auxiliary rules necessary?
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1579
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1580
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1581
val setup = PredData.put (Graph.empty) #>
39545
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1582
  Attrib.setup @{binding code_pred_intro} (Scan.lift (Scan.option Args.name) >> attrib' add_intro)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1583
    "adding alternative introduction rules for code generation of inductive predicates"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1584
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
  1585
(* TODO: make Theory_Data to Generic_Data & remove duplication of local theory and theory *)
38757
2b3e054ae6fc renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents: 38558
diff changeset
  1586
(* FIXME ... this is important to avoid changing the background theory below *)
33132
07efd452a698 moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents: 33131
diff changeset
  1587
fun generic_code_pred prep_const options raw_const lthy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1588
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1589
    val thy = ProofContext.theory_of lthy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1590
    val const = prep_const thy raw_const
40053
3fa49ea76cbb using a signature in core_data and moving some more functions to core_data
bulwahn
parents: 40052
diff changeset
  1591
    val lthy' = Local_Theory.background_theory (extend_intro_graph [const]) lthy
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1592
    val thy' = ProofContext.theory_of lthy'
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1593
    val ctxt' = ProofContext.init_global thy'
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1594
    val preds = Graph.all_succs (PredData.get thy') [const] |> filter_out (has_elim ctxt')
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1595
    fun mk_cases const =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1596
      let
39299
e98a06145530 directly computing the values of interest instead of composing functions in an unintelligent way that causes exponential much garbage; using the latest theory
bulwahn
parents: 39273
diff changeset
  1597
        val T = Sign.the_const_type thy' const
33146
bf852ef586f2 now the predicate compilere handles the predicate without introduction rules better as before
bulwahn
parents: 33145
diff changeset
  1598
        val pred = Const (const, T)
37007
116670499530 changing operations for accessing data to work with contexts
bulwahn
parents: 37006
diff changeset
  1599
        val intros = intros_of ctxt' 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
  1600
      in mk_casesrule lthy' pred intros end  
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1601
    val cases_rules = map mk_cases preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1602
    val cases =
39545
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1603
      map2 (fn pred_name => fn case_rule => Rule_Cases.Case {fixes = [],
39652
b1febbbda458 improving naming of assumptions in code_pred
bulwahn
parents: 39651
diff changeset
  1604
        assumes = ("that", tl (Logic.strip_imp_prems case_rule))
39545
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1605
          :: (map_filter (fn (fact_name, fact) => Option.map (rpair [fact]) fact_name)
39652
b1febbbda458 improving naming of assumptions in code_pred
bulwahn
parents: 39651
diff changeset
  1606
            ((SOME (Long_Name.base_name pred_name ^ ".prems") :: names_of ctxt' pred_name) ~~ Logic.strip_imp_prems case_rule)),
39545
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1607
        binds = [], cases = []}) preds cases_rules
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1608
    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
  1609
    val lthy'' = lthy'
39545
631cf48c7894 code_pred_intro can be used to name facts for the code_pred command
bulwahn
parents: 39477
diff changeset
  1610
      |> fold Variable.auto_fixes cases_rules
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1611
      |> ProofContext.add_cases true case_env
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1612
    fun after_qed thms goal_ctxt =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1613
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1614
        val global_thms = ProofContext.export goal_ctxt
36610
bafd82950e24 renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents: 36533
diff changeset
  1615
          (ProofContext.init_global (ProofContext.theory_of goal_ctxt)) (map the_single thms)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1616
      in
38757
2b3e054ae6fc renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents: 38558
diff changeset
  1617
        goal_ctxt |> Local_Theory.background_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
  1618
          ((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
  1619
             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
  1620
           | DSeq => add_dseq_equations
35879
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1621
           | Pos_Random_DSeq => add_random_dseq_equations
99818df5b8f5 reviving the classical depth-limited computation in the predicate compiler
bulwahn
parents: 35845
diff changeset
  1622
           | Depth_Limited => add_depth_limited_equations
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1623
           | Random => add_random_equations
35881
aa412e08bfee adding depth_limited_random compilation to predicate compiler
bulwahn
parents: 35880
diff changeset
  1624
           | Depth_Limited_Random => add_depth_limited_random_equations
36018
096ec83348f3 added new compilation to predicate_compiler
bulwahn
parents: 36004
diff changeset
  1625
           | New_Pos_Random_DSeq => add_new_random_dseq_equations
40051
b6acda4d1c29 added generator_dseq compilation for a sound depth-limited compilation with small value generators
bulwahn
parents: 40050
diff changeset
  1626
           | Pos_Generator_DSeq => add_generator_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
  1627
           | 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
  1628
           ) options [const]))
33144
1831516747d4 removed obsolete GeneratorPrem; clean-up after modularization; tuned
bulwahn
parents: 33143
diff changeset
  1629
      end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1630
  in
36323
655e2d74de3a modernized naming conventions of main Isar proof elements;
wenzelm
parents: 36261
diff changeset
  1631
    Proof.theorem NONE after_qed (map (single o (rpair [])) cases_rules) lthy''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1632
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1633
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1634
val code_pred = generic_code_pred (K I);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1635
val code_pred_cmd = generic_code_pred Code.read_const
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1636
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1637
(* transformation for code generation *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1638
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1639
structure Pred_Result = Proof_Data (
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1640
  type T = unit -> term Predicate.pred
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1641
  fun init _ () = error "Pred_Result"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1642
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1643
val put_pred_result = Pred_Result.put;
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1644
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1645
structure Pred_Random_Result = Proof_Data (
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1646
  type T = unit -> int * int -> term Predicate.pred * (int * int)
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1647
  fun init _ () = error "Pred_Random_Result"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1648
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1649
val put_pred_random_result = Pred_Random_Result.put;
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1650
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1651
structure Dseq_Result = Proof_Data (
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1652
  type T = unit -> term DSequence.dseq
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1653
  fun init _ () = error "Dseq_Result"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1654
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1655
val put_dseq_result = Dseq_Result.put;
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1656
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1657
structure Dseq_Random_Result = Proof_Data (
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1658
  type T = unit -> int -> int -> int * int -> term DSequence.dseq * (int * int)
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1659
  fun init _ () = error "Dseq_Random_Result"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1660
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1661
val put_dseq_random_result = Dseq_Random_Result.put;
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1662
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1663
structure New_Dseq_Result = Proof_Data (
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1664
  type T = unit -> int -> term Lazy_Sequence.lazy_sequence
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1665
  fun init _ () = error "New_Dseq_Random_Result"
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1666
);
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1667
val put_new_dseq_result = New_Dseq_Result.put;
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1668
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1669
structure Lseq_Random_Result = Proof_Data (
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1670
  type T = unit -> int -> int -> int * int -> int -> term Lazy_Sequence.lazy_sequence
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1671
  fun init _ () = error "Lseq_Random_Result"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1672
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1673
val put_lseq_random_result = Lseq_Random_Result.put;
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1674
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1675
structure Lseq_Random_Stats_Result = Proof_Data (
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1676
  type T = unit -> int -> int -> int * int -> int -> (term * int) Lazy_Sequence.lazy_sequence
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1677
  fun init _ () = error "Lseq_Random_Stats_Result"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1678
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1679
val put_lseq_random_stats_result = Lseq_Random_Stats_Result.put;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1680
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1681
(*FIXME turn this into an LCF-guarded preprocessor for comprehensions*)
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1682
fun analyze_compr ctxt (comp_modifiers, additional_arguments) param_user_modes
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1683
  (compilation, arguments) t_compr =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1684
  let
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1685
    val compfuns = Comp_Mod.compfuns 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
  1686
    val all_modes_of = all_modes_of compilation
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1687
    val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1688
      | _ => error ("Not a set comprehension: " ^ Syntax.string_of_term ctxt t_compr);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1689
    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
  1690
    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
  1691
      (map (fn i => "x" ^ string_of_int i) (1 upto length Ts))
34963
haftmann
parents: 34948 34962
diff changeset
  1692
    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
  1693
    val body = subst_bounds (output_frees, body)
34963
haftmann
parents: 34948 34962
diff changeset
  1694
    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
  1695
    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
  1696
    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
  1697
      case strip_comb body of
199fe16cdaab returning an more understandable user error message in the values command
bulwahn
parents: 36030
diff changeset
  1698
        (Const (name, T), all_args) => (Const (name, T), all_args)
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1699
      | (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt 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
  1700
  in
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1701
    if defined_functions compilation ctxt name then
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1702
      let
37391
476270a6c2dc tuned quotes, antiquotations and whitespace
haftmann
parents: 37146
diff changeset
  1703
        fun extract_mode (Const (@{const_name Pair}, _) $ t1 $ t2) = Pair (extract_mode t1, extract_mode t2)
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1704
          | 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
  1705
          | 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
  1706
        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
  1707
        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
  1708
          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
  1709
            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
  1710
          | 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
  1711
          | 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
  1712
            (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
  1713
        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
  1714
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1715
            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
  1716
              | 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
  1717
              | 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
  1718
              | 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
  1719
                  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
  1720
              | 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
  1721
                  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
  1722
              | 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
  1723
                  instance_of (m1, Output) andalso instance_of (m2, Output)
37002
34e73e8bab66 improved values command to handle a special case with tuples and polymorphic predicates more correctly
bulwahn
parents: 37001
diff changeset
  1724
              | instance_of (Input, Pair (m1, m2)) =
34e73e8bab66 improved values command to handle a special case with tuples and polymorphic predicates more correctly
bulwahn
parents: 37001
diff changeset
  1725
                  instance_of (Input, m1) andalso instance_of (Input, m2)
34e73e8bab66 improved values command to handle a special case with tuples and polymorphic predicates more correctly
bulwahn
parents: 37001
diff changeset
  1726
              | instance_of (Output, Pair (m1, m2)) =
34e73e8bab66 improved values command to handle a special case with tuples and polymorphic predicates more correctly
bulwahn
parents: 37001
diff changeset
  1727
                  instance_of (Output, m1) andalso instance_of (Output, 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
  1728
              | 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
  1729
          in forall instance_of (strip_fun_mode m1 ~~ strip_fun_mode m2) end
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1730
        val derivs = all_derivations_of ctxt (all_modes_of ctxt) [] body
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1731
          |> 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
  1732
            let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1733
              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
  1734
            in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1735
              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
  1736
              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
  1737
              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
  1738
            end)
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1739
          |> 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
  1740
        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
  1741
            [] => error ("No mode possible for comprehension "
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1742
                    ^ Syntax.string_of_term ctxt 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
  1743
          | [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
  1744
          | d :: _ :: _ => (warning ("Multiple modes possible for comprehension "
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1745
                    ^ Syntax.string_of_term ctxt t_compr); d);
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1746
        val (_, outargs) = split_mode (head_mode_of deriv) all_args
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1747
        val t_pred = compile_expr comp_modifiers ctxt
39648
655307cb8489 rewriting function mk_Eval_of in predicate compiler
bulwahn
parents: 39557
diff changeset
  1748
          (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
  1749
        val T_pred = dest_predT compfuns (fastype_of t_pred)
39756
6c8e83d94536 consolidated tupled_lambda; moved to structure HOLogic
krauss
parents: 39685
diff changeset
  1750
        val arrange = HOLogic.tupled_lambda (HOLogic.mk_tuple outargs) output_tuple
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1751
      in
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1752
        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
  1753
      end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1754
    else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1755
      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
  1756
  end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1757
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1758
fun eval ctxt stats param_user_modes (options as (compilation, arguments)) k t_compr =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1759
  let
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1760
    fun count xs x =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1761
      let
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1762
        fun count' i [] = i
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1763
          | 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
  1764
      in count' 0 xs end
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1765
    fun accumulate xs = map (fn x => (x, count xs x)) (sort int_ord (distinct (op =) xs))
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1766
    val 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
  1767
      case compilation of
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1768
          Pred => predicate_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1769
        | Random => random_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1770
        | Depth_Limited => depth_limited_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1771
        | Depth_Limited_Random => depth_limited_random_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1772
        (*| Annotated => annotated_comp_modifiers*)
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1773
        | DSeq => dseq_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1774
        | Pos_Random_DSeq => pos_random_dseq_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1775
        | New_Pos_Random_DSeq => new_pos_random_dseq_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1776
        | Pos_Generator_DSeq => pos_generator_dseq_comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1777
    val compfuns = Comp_Mod.compfuns comp_modifiers
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1778
    val additional_arguments =
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1779
      case compilation of
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1780
        Pred => []
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1781
      | Random => map (HOLogic.mk_number @{typ "code_numeral"}) arguments @
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1782
        [@{term "(1, 1) :: code_numeral * code_numeral"}]
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1783
      | Annotated => []
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1784
      | Depth_Limited => [HOLogic.mk_number @{typ "code_numeral"} (hd arguments)]
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1785
      | Depth_Limited_Random => map (HOLogic.mk_number @{typ "code_numeral"}) arguments @
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1786
        [@{term "(1, 1) :: code_numeral * code_numeral"}]
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1787
      | DSeq => []
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1788
      | Pos_Random_DSeq => []
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1789
      | New_Pos_Random_DSeq => []
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1790
      | Pos_Generator_DSeq => []
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1791
    val t = analyze_compr ctxt (comp_modifiers, additional_arguments) 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
  1792
    val T = dest_predT compfuns (fastype_of t);
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1793
    val t' =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1794
      if stats andalso compilation = New_Pos_Random_DSeq then
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1795
        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
  1796
          (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
  1797
            @{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
  1798
      else
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1799
        mk_map compfuns T HOLogic.termT (HOLogic.term_of_const T) t
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1800
    val thy = ProofContext.theory_of ctxt
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1801
    val (ts, statistics) =
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1802
      (case compilation of
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1803
       (* 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
  1804
          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
  1805
          (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
  1806
            (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
  1807
            |> Random_Engine.run))*)
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35879
diff changeset
  1808
        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
  1809
          let
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1810
            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
  1811
          in
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1812
            rpair NONE (TimeLimit.timeLimit (Time.fromSeconds 20) (fn () => fst (DSequence.yieldn k
39471
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1813
              (Code_Runtime.dynamic_value_strict (Dseq_Random_Result.get, put_dseq_random_result, "Predicate_Compile_Core.put_dseq_random_result")
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1814
                thy NONE (fn proc => fn g => fn nrandom => fn size => fn s => g nrandom size s |>> DSequence.map proc)
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1815
                  t' [] nrandom size
34948
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1816
                |> Random_Engine.run)
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1817
              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
  1818
          end
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1819
      | DSeq =>
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1820
          rpair NONE (TimeLimit.timeLimit (Time.fromSeconds 20) (fn () => fst (DSequence.yieldn k
39471
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1821
            (Code_Runtime.dynamic_value_strict (Dseq_Result.get, put_dseq_result, "Predicate_Compile_Core.put_dseq_result")
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1822
              thy NONE DSequence.map t' []) (the_single arguments) true)) ())
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1823
      | Pos_Generator_DSeq =>
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1824
          let
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1825
            val depth = (the_single arguments)
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1826
          in
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1827
            rpair NONE (TimeLimit.timeLimit (Time.fromSeconds 20) (fn () => fst (Lazy_Sequence.yieldn k
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1828
              (Code_Runtime.dynamic_value_strict (New_Dseq_Result.get, put_new_dseq_result, "Predicate_Compile_Core.put_new_dseq_result")
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1829
              thy NONE (fn proc => fn g => fn depth => g depth |> Lazy_Sequence.mapa proc)
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1830
              t' [] depth))) ())
40102
a9e4e94b3022 restructuring values command and adding generator compilation
bulwahn
parents: 40053
diff changeset
  1831
          end
36020
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1832
      | 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
  1833
          let
3ee4c29ead7f adding values command for new monad; added new random monad compilation to predicate_compile_quickcheck
bulwahn
parents: 36019
diff changeset
  1834
            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
  1835
            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
  1836
          in
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1837
            if stats then
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1838
              apsnd (SOME o accumulate) (split_list (TimeLimit.timeLimit (Time.fromSeconds 20)
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1839
              (fn () => fst (Lazy_Sequence.yieldn k
39471
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1840
                (Code_Runtime.dynamic_value_strict
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1841
                  (Lseq_Random_Stats_Result.get, put_lseq_random_stats_result, "Predicate_Compile_Core.put_lseq_random_stats_result")
39471
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1842
                  thy NONE
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1843
                  (fn proc => fn g => fn nrandom => fn size => fn s => fn depth => g nrandom size s depth
36533
f8df589ca2a5 dropped unnecessary ML code
haftmann
parents: 36510
diff changeset
  1844
                    |> Lazy_Sequence.mapa (apfst proc))
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1845
                    t' [] nrandom size seed depth))) ()))
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1846
            else rpair NONE
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1847
              (TimeLimit.timeLimit (Time.fromSeconds 20) (fn () => fst (Lazy_Sequence.yieldn k
39471
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1848
                (Code_Runtime.dynamic_value_strict
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39310
diff changeset
  1849
                  (Lseq_Random_Result.get, put_lseq_random_result, "Predicate_Compile_Core.put_lseq_random_result")
39471
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1850
                  thy NONE 
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1851
                  (fn proc => fn g => fn nrandom => fn size => fn s => fn depth => g nrandom size s depth
36533
f8df589ca2a5 dropped unnecessary ML code
haftmann
parents: 36510
diff changeset
  1852
                    |> Lazy_Sequence.mapa proc)
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1853
                    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
  1854
          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
  1855
      | _ =>
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1856
          rpair NONE (TimeLimit.timeLimit (Time.fromSeconds 20) (fn () => fst (Predicate.yieldn k
39471
55e0ff582fa4 adjusted to changes in Code_Runtime
haftmann
parents: 39403
diff changeset
  1857
            (Code_Runtime.dynamic_value_strict (Pred_Result.get, put_pred_result, "Predicate_Compile_Core.put_pred_result")
40135
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1858
              thy NONE Predicate.map t' []))) ()))
abc45472e48a adding a global time limit to the values command
bulwahn
parents: 40103
diff changeset
  1859
     handle TimeLimit.TimeOut => error "Reached timeout during execution of values"
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1860
  in ((T, ts), statistics) end;
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1861
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1862
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
  1863
  let
37003
a393a588b82e moving towards working with proof contexts in the predicate compiler
bulwahn
parents: 37002
diff changeset
  1864
    val ((T, ts), statistics) = eval ctxt 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
  1865
    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
  1866
    val elems = HOLogic.mk_set T ts
33476
27cca5416a88 Adopted output of values command
bulwahn
parents: 33474
diff changeset
  1867
    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
  1868
    (* 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
  1869
    val () =
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1870
      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
  1871
        NONE => ()
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1872
      | 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
  1873
        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
  1874
        else
2d5f2a9f7601 refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
bulwahn
parents: 34028
diff changeset
  1875
          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
  1876
            "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
  1877
            "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
  1878
  in
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1879
    (if k = ~1 orelse length ts < k then elems
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1880
    else Const (@{const_abbrev Set.union}, setT --> setT --> setT) $ elems $ cont, statistics)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1881
  end;
33623
4ec42d38224f changed modes to expected_modes; added UNION to code_pred_inlining; fixed some examples; tuned
bulwahn
parents: 33620
diff changeset
  1882
33479
428ddcc16e7b added optional mode annotations for parameters in the values command
bulwahn
parents: 33478
diff changeset
  1883
fun values_cmd print_modes param_user_modes options k raw_t state =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1884
  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
  1885
    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
  1886
    val t = Syntax.read_term ctxt raw_t
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1887
    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
  1888
    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
  1889
    val ctxt' = Variable.auto_fixes t' ctxt
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1890
    val pretty_stat =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1891
      case stats of
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1892
          NONE => []
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1893
        | SOME xs =>
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1894
          let
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1895
            val total = fold (curry (op +)) (map snd xs) 0
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1896
            fun pretty_entry (s, n) =
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1897
              [Pretty.str "size", Pretty.brk 1,
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1898
               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
  1899
               Pretty.str (string_of_int n), Pretty.fbrk]
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1900
          in
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1901
            [Pretty.fbrk, Pretty.str "Statistics:", Pretty.fbrk,
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1902
             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
  1903
             @ maps pretty_entry xs
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1904
          end
37146
f652333bbf8e renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents: 37135
diff changeset
  1905
    val p = Print_Mode.with_modes print_modes (fn () =>
36027
29a15da9c63d added statistics to values command for random generation
bulwahn
parents: 36025
diff changeset
  1906
      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
  1907
        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
  1908
        @ pretty_stat)) ();
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1909
  in Pretty.writeln p end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1910
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1911
end;