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