src/HOL/ex/predicate_compile.ML
author haftmann
Wed, 29 Jul 2009 16:48:34 +0200
changeset 32287 65d5c5b30747
parent 32091 30e2ffbba718
child 32306 19f55947d4d5
child 32341 c8c17c2e6ceb
permissions -rw-r--r--
cleaned up abstract tuple operations and named them consistently
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
     1
(* Author: Lukas Bulwahn, TU Muenchen
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
     2
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
     3
(Prototype of) A compiler from predicates specified by intro/elim rules
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
     4
to equations.
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
     5
*)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
     6
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
     7
signature PREDICATE_COMPILE =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
     8
sig
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
     9
  type mode = int list option list * int list
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    10
  val add_equations_of: string list -> theory -> theory
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    11
  val register_predicate : (thm list * thm * int) -> theory -> theory
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
    12
  val is_registered : theory -> string -> bool
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
    13
  val fetch_pred_data : theory -> string -> (thm list * thm * int)  
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    14
  val predfun_intro_of: theory -> string -> mode -> thm
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    15
  val predfun_elim_of: theory -> string -> mode -> thm
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    16
  val strip_intro_concl: int -> term -> term * (term list * term list)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    17
  val predfun_name_of: theory -> string -> mode -> string
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    18
  val all_preds_of : theory -> string list
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    19
  val modes_of: theory -> string -> mode list
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    20
  val intros_of: theory -> string -> thm list
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    21
  val nparams_of: theory -> string -> int
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
    22
  val add_intro: thm -> theory -> theory
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
    23
  val set_elim: thm -> theory -> theory
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    24
  val setup: theory -> theory
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    25
  val code_pred: string -> Proof.context -> Proof.state
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    26
  val code_pred_cmd: string -> Proof.context -> Proof.state
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
    27
  val print_stored_rules: theory -> unit
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    28
  val do_proofs: bool ref
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
    29
  val mk_casesrule : Proof.context -> int -> thm list -> term
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
    30
  val analyze_compr: theory -> term -> term
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
    31
  val eval_ref: (unit -> term Predicate.pred) option ref
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    32
  val add_equations : string -> theory -> theory
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
    33
  val code_pred_intros_attrib : attribute
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    34
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    35
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    36
structure Predicate_Compile : PREDICATE_COMPILE =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    37
struct
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    38
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    39
(** auxiliary **)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    40
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    41
(* debug stuff *)
31320
72eeb1b4e006 provide local dummy version of makestring -- NB: makestring is fragile and not portable, it should not occur in repository sources;
wenzelm
parents: 31284
diff changeset
    42
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    43
fun tracing s = (if ! Toplevel.debug then Output.tracing s else ());
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    44
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    45
fun print_tac s = (if ! Toplevel.debug then Tactical.print_tac s else Seq.single);
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
    46
fun new_print_tac s = Tactical.print_tac s
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
    47
fun debug_tac msg = (fn st => (Output.tracing msg; Seq.single st));
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    48
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    49
val do_proofs = ref true;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    50
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    51
fun mycheat_tac thy i st =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    52
  (Tactic.rtac (SkipProof.make_thm thy (Var (("A", 0), propT))) i) st
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    53
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
    54
fun remove_last_goal thy st =
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
    55
  (Tactic.rtac (SkipProof.make_thm thy (Var (("A", 0), propT))) (nprems_of st)) st
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
    56
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    57
(* reference to preprocessing of InductiveSet package *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    58
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31580
diff changeset
    59
val ind_set_codegen_preproc = Inductive_Set.codegen_preproc;
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    60
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    61
(** fundamentals **)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    62
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    63
(* syntactic operations *)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    64
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    65
fun mk_eq (x, xs) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    66
  let fun mk_eqs _ [] = []
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    67
        | mk_eqs a (b::cs) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    68
            HOLogic.mk_eq (Free (a, fastype_of b), b) :: mk_eqs a cs
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    69
  in mk_eqs x xs end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    70
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    71
fun mk_tupleT [] = HOLogic.unitT
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    72
  | mk_tupleT Ts = foldr1 HOLogic.mk_prodT Ts;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    73
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    74
fun dest_tupleT (Type (@{type_name Product_Type.unit}, [])) = []
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    75
  | dest_tupleT (Type (@{type_name "*"}, [T1, T2])) = T1 :: (dest_tupleT T2)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    76
  | dest_tupleT t = [t]
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    77
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    78
fun mk_tuple [] = HOLogic.unit
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    79
  | mk_tuple ts = foldr1 HOLogic.mk_prod ts;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    80
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    81
fun dest_tuple (Const (@{const_name Product_Type.Unity}, _)) = []
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    82
  | dest_tuple (Const (@{const_name Pair}, _) $ t1 $ t2) = t1 :: (dest_tuple t2)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    83
  | dest_tuple t = [t]
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    84
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    85
fun mk_pred_enumT T = Type (@{type_name "Predicate.pred"}, [T])
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    86
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
    87
fun dest_pred_enumT (Type (@{type_name "Predicate.pred"}, [T])) = T
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    88
  | dest_pred_enumT T = raise TYPE ("dest_pred_enumT", [T], []);
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    89
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    90
fun mk_Enum f =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    91
  let val T as Type ("fun", [T', _]) = fastype_of f
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    92
  in
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    93
    Const (@{const_name Predicate.Pred}, T --> mk_pred_enumT T') $ f    
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    94
  end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    95
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    96
fun mk_Eval (f, x) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    97
  let val T = fastype_of x
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    98
  in
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    99
    Const (@{const_name Predicate.eval}, mk_pred_enumT T --> T --> HOLogic.boolT) $ f $ x
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   100
  end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   101
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   102
fun mk_empty T = Const (@{const_name Orderings.bot}, mk_pred_enumT T);
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   103
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   104
fun mk_single t =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   105
  let val T = fastype_of t
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   106
  in Const(@{const_name Predicate.single}, T --> mk_pred_enumT T) $ t end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   107
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   108
fun mk_bind (x, f) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   109
  let val T as Type ("fun", [_, U]) = fastype_of f
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   110
  in
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   111
    Const (@{const_name Predicate.bind}, fastype_of x --> T --> U) $ x $ f
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   112
  end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   113
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   114
val mk_sup = HOLogic.mk_binop @{const_name sup};
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   115
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   116
fun mk_if_predenum cond = Const (@{const_name Predicate.if_pred},
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   117
  HOLogic.boolT --> mk_pred_enumT HOLogic.unitT) $ cond;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   118
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   119
fun mk_not_pred t = let val T = mk_pred_enumT HOLogic.unitT
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   120
  in Const (@{const_name Predicate.not_pred}, T --> T) $ t end
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   121
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   122
(* destruction of intro rules *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   123
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   124
(* FIXME: look for other place where this functionality was used before *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   125
fun strip_intro_concl nparams intro = let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   126
  val _ $ u = Logic.strip_imp_concl intro
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   127
  val (pred, all_args) = strip_comb u
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   128
  val (params, args) = chop nparams all_args
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   129
in (pred, (params, args)) end
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   130
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   131
(* data structures *)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   132
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   133
type mode = int list option list * int list; (*pmode FIMXE*)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   134
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   135
fun string_of_mode (iss, is) = space_implode " -> " (map
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   136
  (fn NONE => "X"
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   137
    | SOME js => enclose "[" "]" (commas (map string_of_int js)))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   138
       (iss @ [SOME is]));
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   139
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   140
fun print_modes modes = Output.tracing ("Inferred modes:\n" ^
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   141
  cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   142
    string_of_mode ms)) modes));
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   143
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   144
    
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   145
datatype predfun_data = PredfunData of {
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   146
  name : string,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   147
  definition : thm,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   148
  intro : thm,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   149
  elim : thm
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   150
};
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   151
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   152
fun rep_predfun_data (PredfunData data) = data;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   153
fun mk_predfun_data (name, definition, intro, elim) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   154
  PredfunData {name = name, definition = definition, intro = intro, elim = elim}
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   155
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   156
datatype pred_data = PredData of {
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   157
  intros : thm list,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   158
  elim : thm option,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   159
  nparams : int,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   160
  functions : (mode * predfun_data) list
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   161
};
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   162
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   163
fun rep_pred_data (PredData data) = data;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   164
fun mk_pred_data ((intros, elim, nparams), functions) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   165
  PredData {intros = intros, elim = elim, nparams = nparams, functions = functions}
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   166
fun map_pred_data f (PredData {intros, elim, nparams, functions}) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   167
  mk_pred_data (f ((intros, elim, nparams), functions))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   168
  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   169
fun eq_option eq (NONE, NONE) = true
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   170
  | eq_option eq (SOME x, SOME y) = eq (x, y)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   171
  | eq_option eq _ = false
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   172
  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   173
fun eq_pred_data (PredData d1, PredData d2) = 
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   174
  eq_list (Thm.eq_thm) (#intros d1, #intros d2) andalso
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   175
  eq_option (Thm.eq_thm) (#elim d1, #elim d2) andalso
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   176
  #nparams d1 = #nparams d2
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   177
  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   178
structure PredData = TheoryDataFun
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   179
(
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   180
  type T = pred_data Graph.T;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   181
  val empty = Graph.empty;
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   182
  val copy = I;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   183
  val extend = I;
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   184
  fun merge _ = Graph.merge eq_pred_data;
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   185
);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   186
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   187
(* queries *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   188
31549
f7379ea2c949 tuned; corrected exception handling
bulwahn
parents: 31541
diff changeset
   189
fun lookup_pred_data thy name =
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   190
  Option.map rep_pred_data (try (Graph.get_node (PredData.get thy)) name)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   191
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   192
fun the_pred_data thy name = case lookup_pred_data thy name
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   193
 of NONE => error ("No such predicate " ^ quote name)  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   194
  | SOME data => data;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   195
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   196
val is_registered = is_some oo lookup_pred_data 
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   197
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   198
val all_preds_of = Graph.keys o PredData.get
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   199
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   200
val intros_of = #intros oo the_pred_data
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   201
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   202
fun the_elim_of thy name = case #elim (the_pred_data thy name)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   203
 of NONE => error ("No elimination rule for predicate " ^ quote name)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   204
  | SOME thm => thm 
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   205
  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   206
val has_elim = is_some o #elim oo the_pred_data;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   207
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   208
val nparams_of = #nparams oo the_pred_data
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   209
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   210
val modes_of = (map fst) o #functions oo the_pred_data
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   211
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   212
fun all_modes_of thy = map (fn name => (name, modes_of thy name)) (all_preds_of thy) 
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   213
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   214
val is_compiled = not o null o #functions oo the_pred_data
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   215
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   216
fun lookup_predfun_data thy name mode =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   217
  Option.map rep_predfun_data (AList.lookup (op =)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   218
  (#functions (the_pred_data thy name)) mode)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   219
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   220
fun the_predfun_data thy name mode = case lookup_predfun_data thy name mode
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   221
 of NONE => error ("No such mode" ^ string_of_mode mode)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   222
  | SOME data => data;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   223
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   224
val predfun_name_of = #name ooo the_predfun_data
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   225
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   226
val predfun_definition_of = #definition ooo the_predfun_data
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   227
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   228
val predfun_intro_of = #intro ooo the_predfun_data
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   229
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   230
val predfun_elim_of = #elim ooo the_predfun_data
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   231
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   232
fun print_stored_rules thy =
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   233
  let
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   234
    val preds = (Graph.keys o PredData.get) thy
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   235
    fun print pred () = let
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   236
      val _ = writeln ("predicate: " ^ pred)
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   237
      val _ = writeln ("number of parameters: " ^ string_of_int (nparams_of thy pred))
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   238
      val _ = writeln ("introrules: ")
32091
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 31986
diff changeset
   239
      val _ = fold (fn thm => fn u => writeln (Display.string_of_thm_global thy thm))
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   240
        (rev (intros_of thy pred)) ()
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   241
    in
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   242
      if (has_elim thy pred) then
32091
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 31986
diff changeset
   243
        writeln ("elimrule: " ^ Display.string_of_thm_global thy (the_elim_of thy pred))
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   244
      else
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   245
        writeln ("no elimrule defined")
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   246
    end
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   247
  in
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   248
    fold print preds ()
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   249
  end;
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   250
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   251
(** preprocessing rules **)  
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   252
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   253
fun imp_prems_conv cv ct =
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   254
  case Thm.term_of ct of
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   255
    Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   256
  | _ => Conv.all_conv ct
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   257
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   258
fun Trueprop_conv cv ct =
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   259
  case Thm.term_of ct of
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   260
    Const ("Trueprop", _) $ _ => Conv.arg_conv cv ct  
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   261
  | _ => error "Trueprop_conv"
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   262
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   263
fun preprocess_intro thy rule =
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   264
  Conv.fconv_rule
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   265
    (imp_prems_conv
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   266
      (Trueprop_conv (Conv.try_conv (Conv.rewr_conv (Thm.symmetric @{thm Predicate.eq_is_eq})))))
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   267
    (Thm.transfer thy rule)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   268
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   269
fun preprocess_elim thy nargs elimrule = let
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   270
   fun replace_eqs (Const ("Trueprop", _) $ (Const ("op =", T) $ lhs $ rhs)) =
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   271
      HOLogic.mk_Trueprop (Const (@{const_name Predicate.eq}, T) $ lhs $ rhs)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   272
    | replace_eqs t = t
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   273
   fun preprocess_case t = let
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   274
     val params = Logic.strip_params t
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   275
     val (assums1, assums2) = chop nargs (Logic.strip_assums_hyp t)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   276
     val assums_hyp' = assums1 @ (map replace_eqs assums2)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   277
     in list_all (params, Logic.list_implies (assums_hyp', Logic.strip_assums_concl t)) end
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   278
   val prems = Thm.prems_of elimrule
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   279
   val cases' = map preprocess_case (tl prems)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   280
   val elimrule' = Logic.list_implies ((hd prems) :: cases', Thm.concl_of elimrule)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   281
 in
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   282
   Thm.equal_elim
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   283
     (Thm.symmetric (Conv.implies_concl_conv (MetaSimplifier.rewrite true [@{thm eq_is_eq}])
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   284
        (cterm_of thy elimrule')))
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   285
     elimrule
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   286
 end;
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   287
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   288
fun fetch_pred_data thy name =
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31580
diff changeset
   289
  case try (Inductive.the_inductive (ProofContext.init thy)) name of
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   290
    SOME (info as (_, result)) => 
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   291
      let
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   292
        fun is_intro_of intro =
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   293
          let
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   294
            val (const, _) = strip_comb (HOLogic.dest_Trueprop (concl_of intro))
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   295
          in (fst (dest_Const const) = name) end;
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   296
        val intros = map (preprocess_intro thy) (filter is_intro_of (#intrs result)) 
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   297
        val elim = nth (#elims result) (find_index (fn s => s = name) (#names (fst info)))
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31580
diff changeset
   298
        val nparams = length (Inductive.params_of (#raw_induct result))
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   299
      in (intros, elim, nparams) end
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   300
  | NONE => error ("No such predicate: " ^ quote name)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   301
  
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   302
(* updaters *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   303
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   304
fun add_predfun name mode data = let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   305
    val add = apsnd (cons (mode, mk_predfun_data data))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   306
  in PredData.map (Graph.map_node name (map_pred_data add)) end
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   307
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   308
fun is_inductive_predicate thy name =
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   309
  is_some (try (Inductive.the_inductive (ProofContext.init thy)) name)
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   310
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   311
fun depending_preds_of thy intros = fold Term.add_consts (map Thm.prop_of intros) [] |> map fst
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   312
    |> filter (fn c => is_inductive_predicate thy c orelse is_registered thy c)
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   313
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   314
(* code dependency graph *)    
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   315
fun dependencies_of thy name =
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   316
  let
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   317
    val (intros, elim, nparams) = fetch_pred_data thy name 
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   318
    val data = mk_pred_data ((intros, SOME elim, nparams), [])
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   319
    val keys = depending_preds_of thy intros
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   320
  in
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   321
    (data, keys)
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   322
  end;
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   323
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   324
(* TODO: add_edges - by analysing dependencies *)
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   325
fun add_intro thm thy = let
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   326
   val (name, _) = dest_Const (fst (strip_intro_concl 0 (prop_of thm)))
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   327
   fun cons_intro gr =
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   328
     case try (Graph.get_node gr) name of
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   329
       SOME pred_data => Graph.map_node name (map_pred_data
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   330
         (apfst (fn (intro, elim, nparams) => (thm::intro, elim, nparams)))) gr
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   331
     | NONE =>
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   332
       let
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   333
         val nparams = the_default 0 (try (#3 o fetch_pred_data thy) name)
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   334
       in Graph.new_node (name, mk_pred_data (([thm], NONE, nparams), [])) gr end;
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
   335
  in PredData.map cons_intro thy end
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   336
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   337
fun set_elim thm = let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   338
    val (name, _) = dest_Const (fst 
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   339
      (strip_comb (HOLogic.dest_Trueprop (hd (prems_of thm)))))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   340
    fun set (intros, _, nparams) = (intros, SOME thm, nparams)  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   341
  in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   342
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   343
fun set_nparams name nparams = let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   344
    fun set (intros, elim, _ ) = (intros, elim, nparams) 
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   345
  in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   346
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   347
fun register_predicate (intros, elim, nparams) thy = let
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   348
    val (name, _) = dest_Const (fst (strip_intro_concl nparams (prop_of (hd intros))))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   349
    fun set _ = (intros, SOME elim, nparams)
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   350
  in
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   351
    PredData.map (Graph.new_node (name, mk_pred_data ((intros, SOME elim, nparams), []))
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   352
      #> fold Graph.add_edge (map (pair name) (depending_preds_of thy intros))) thy
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
   353
  end
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   354
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   355
  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   356
(* Mode analysis *)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   357
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   358
(*** check if a term contains only constructor functions ***)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   359
fun is_constrt thy =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   360
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   361
    val cnstrs = flat (maps
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   362
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
   363
      (Symtab.dest (Datatype.get_all thy)));
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   364
    fun check t = (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   365
        (Free _, []) => true
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   366
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   367
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   368
          | _ => false)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   369
      | _ => false)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   370
  in check end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   371
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   372
(*** check if a type is an equality type (i.e. doesn't contain fun)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   373
  FIXME this is only an approximation ***)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   374
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   375
  | is_eqT _ = true;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   376
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   377
fun term_vs tm = fold_aterms (fn Free (x, T) => cons x | _ => I) tm [];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   378
val terms_vs = distinct (op =) o maps term_vs;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   379
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   380
(** collect all Frees in a term (with duplicates!) **)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   381
fun term_vTs tm =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   382
  fold_aterms (fn Free xT => cons xT | _ => I) tm [];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   383
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   384
fun get_args is ts = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   385
  fun get_args' _ _ [] = ([], [])
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   386
    | get_args' is i (t::ts) = (if i mem is then apfst else apsnd) (cons t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   387
        (get_args' is (i+1) ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   388
in get_args' is 1 ts end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   389
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   390
(*FIXME this function should not be named merge... make it local instead*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   391
fun merge xs [] = xs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   392
  | merge [] ys = ys
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   393
  | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   394
      else y::merge (x::xs) ys;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   395
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   396
fun subsets i j = if i <= j then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   397
       let val is = subsets (i+1) j
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   398
       in merge (map (fn ks => i::ks) is) is end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   399
     else [[]];
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   400
     
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   401
(* FIXME: should be in library - map_prod *)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   402
fun cprod ([], ys) = []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   403
  | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   404
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   405
fun cprods xss = foldr (map op :: o cprod) [[]] xss;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   406
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   407
datatype hmode = Mode of mode * int list * hmode option list; (*FIXME don't understand
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   408
  why there is another mode type tmode !?*)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   409
31515
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
   410
  
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   411
(*TODO: cleanup function and put together with modes_of_term *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   412
fun modes_of_param default modes t = let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   413
    val (vs, t') = strip_abs t
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   414
    val b = length vs
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   415
    fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) =>
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   416
        let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   417
          val (args1, args2) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   418
            if length args < length iss then
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   419
              error ("Too few arguments for inductive predicate " ^ name)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   420
            else chop (length iss) args;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   421
          val k = length args2;
31986
a68f88d264f7 dropped find_index_eq
haftmann
parents: 31879
diff changeset
   422
          val perm = map (fn i => (find_index (fn t => t = Bound (b - i)) args2) + 1)
a68f88d264f7 dropped find_index_eq
haftmann
parents: 31879
diff changeset
   423
            (1 upto b)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   424
          val partial_mode = (1 upto k) \\ perm
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   425
        in
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   426
          if not (partial_mode subset is) then [] else
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   427
          let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   428
            val is' = 
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   429
            (fold_index (fn (i, j) => if j mem is then cons (i + 1) else I) perm [])
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   430
            |> fold (fn i => if i > k then cons (i - k + b) else I) is
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   431
              
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   432
           val res = map (fn x => Mode (m, is', x)) (cprods (map
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   433
            (fn (NONE, _) => [NONE]
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   434
              | (SOME js, arg) => map SOME (filter
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   435
                  (fn Mode (_, js', _) => js=js') (modes_of_term modes arg)))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   436
                    (iss ~~ args1)))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   437
          in res end
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   438
        end)) (AList.lookup op = modes name)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   439
  in case strip_comb t' of
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   440
    (Const (name, _), args) => the_default default (mk_modes name args)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   441
    | (Var ((name, _), _), args) => the (mk_modes name args)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   442
    | (Free (name, _), args) => the (mk_modes name args)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   443
    | _ => default end
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   444
  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   445
and modes_of_term modes t =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   446
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   447
    val ks = 1 upto length (binder_types (fastype_of t));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   448
    val default = [Mode (([], ks), ks, [])];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   449
    fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   450
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   451
          val (args1, args2) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   452
            if length args < length iss then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   453
              error ("Too few arguments for inductive predicate " ^ name)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   454
            else chop (length iss) args;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   455
          val k = length args2;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   456
          val prfx = 1 upto k
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   457
        in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   458
          if not (is_prefix op = prfx is) then [] else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   459
          let val is' = map (fn i => i - k) (List.drop (is, k))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   460
          in map (fn x => Mode (m, is', x)) (cprods (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   461
            (fn (NONE, _) => [NONE]
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   462
              | (SOME js, arg) => map SOME (filter
31170
c6efe82fc652 added predicate transformation function for code generation
bulwahn
parents: 31156 31169
diff changeset
   463
                  (fn Mode (_, js', _) => js=js') (modes_of_term modes arg)))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   464
                    (iss ~~ args1)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   465
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   466
        end)) (AList.lookup op = modes name)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   467
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   468
  in (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   469
      (Const (name, _), args) => the_default default (mk_modes name args)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   470
    | (Var ((name, _), _), args) => the (mk_modes name args)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   471
    | (Free (name, _), args) => the (mk_modes name args)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   472
    | (Abs _, []) => modes_of_param default modes t 
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   473
    | _ => default)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   474
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   475
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   476
datatype indprem = Prem of term list * term | Negprem of term list * term | Sidecond of term;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   477
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   478
fun select_mode_prem thy modes vs ps =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   479
  find_first (is_some o snd) (ps ~~ map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   480
    (fn Prem (us, t) => find_first (fn Mode (_, is, _) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   481
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   482
            val (in_ts, out_ts) = get_args is us;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   483
            val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   484
            val vTs = maps term_vTs out_ts';
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   485
            val dupTs = map snd (duplicates (op =) vTs) @
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   486
              List.mapPartial (AList.lookup (op =) vTs) vs;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   487
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   488
            terms_vs (in_ts @ in_ts') subset vs andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   489
            forall (is_eqT o fastype_of) in_ts' andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   490
            term_vs t subset vs andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   491
            forall is_eqT dupTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   492
          end)
31170
c6efe82fc652 added predicate transformation function for code generation
bulwahn
parents: 31156 31169
diff changeset
   493
            (modes_of_term modes t handle Option =>
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   494
               error ("Bad predicate: " ^ Syntax.string_of_term_global thy t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   495
      | Negprem (us, t) => find_first (fn Mode (_, is, _) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   496
            length us = length is andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   497
            terms_vs us subset vs andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   498
            term_vs t subset vs)
31170
c6efe82fc652 added predicate transformation function for code generation
bulwahn
parents: 31156 31169
diff changeset
   499
            (modes_of_term modes t handle Option =>
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   500
               error ("Bad predicate: " ^ Syntax.string_of_term_global thy t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   501
      | Sidecond t => if term_vs t subset vs then SOME (Mode (([], []), [], []))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   502
          else NONE
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   503
      ) ps);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   504
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   505
fun check_mode_clause thy param_vs modes (iss, is) (ts, ps) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   506
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   507
    val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   508
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   509
        (param_vs ~~ iss); 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   510
    fun check_mode_prems vs [] = SOME vs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   511
      | check_mode_prems vs ps = (case select_mode_prem thy modes' vs ps of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   512
          NONE => NONE
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   513
        | SOME (x, _) => check_mode_prems
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   514
            (case x of Prem (us, _) => vs union terms_vs us | _ => vs)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   515
            (filter_out (equal x) ps))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   516
    val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (get_args is ts));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   517
    val in_vs = terms_vs in_ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   518
    val concl_vs = terms_vs ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   519
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   520
    forall is_eqT (map snd (duplicates (op =) (maps term_vTs in_ts))) andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   521
    forall (is_eqT o fastype_of) in_ts' andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   522
    (case check_mode_prems (param_vs union in_vs) ps of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   523
       NONE => false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   524
     | SOME vs => concl_vs subset vs)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   525
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   526
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   527
fun check_modes_pred thy param_vs preds modes (p, ms) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   528
  let val SOME rs = AList.lookup (op =) preds p
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   529
  in (p, List.filter (fn m => case find_index
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   530
    (not o check_mode_clause thy param_vs modes m) rs of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   531
      ~1 => true
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   532
    | i => (tracing ("Clause " ^ string_of_int (i+1) ^ " of " ^
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   533
      p ^ " violates mode " ^ string_of_mode m); false)) ms)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   534
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   535
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   536
fun fixp f (x : (string * mode list) list) =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   537
  let val y = f x
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   538
  in if x = y then x else fixp f y end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   539
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   540
fun infer_modes thy extra_modes arities param_vs preds = fixp (fn modes =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   541
  map (check_modes_pred thy param_vs preds (modes @ extra_modes)) modes)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   542
    (map (fn (s, (ks, k)) => (s, cprod (cprods (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   543
      (fn NONE => [NONE]
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   544
        | SOME k' => map SOME (subsets 1 k')) ks),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   545
      subsets 1 k))) arities);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   546
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   547
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   548
(* term construction *)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   549
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   550
(* for simple modes (e.g. parameters) only: better call it param_funT *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   551
(* or even better: remove it and only use funT'_of - some modifications to funT'_of necessary *) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   552
fun funT_of T NONE = T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   553
  | funT_of T (SOME mode) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   554
     val Ts = binder_types T;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   555
     val (Us1, Us2) = get_args mode Ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   556
   in Us1 ---> (mk_pred_enumT (mk_tupleT Us2)) end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   557
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   558
fun funT'_of (iss, is) T = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   559
    val Ts = binder_types T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   560
    val (paramTs, argTs) = chop (length iss) Ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   561
    val paramTs' = map2 (fn SOME is => funT'_of ([], is) | NONE => I) iss paramTs 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   562
    val (inargTs, outargTs) = get_args is argTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   563
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   564
    (paramTs' @ inargTs) ---> (mk_pred_enumT (mk_tupleT outargTs))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   565
  end; 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   566
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   567
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   568
fun mk_v (names, vs) s T = (case AList.lookup (op =) vs s of
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   569
      NONE => (Free (s, T), (names, (s, [])::vs))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   570
    | SOME xs =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   571
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   572
          val s' = Name.variant names s;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   573
          val v = Free (s', T)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   574
        in
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   575
          (v, (s'::names, AList.update (op =) (s, v::xs) vs))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   576
        end);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   577
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   578
fun distinct_v (Free (s, T)) nvs = mk_v nvs s T
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   579
  | distinct_v (t $ u) nvs =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   580
      let
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   581
        val (t', nvs') = distinct_v t nvs;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   582
        val (u', nvs'') = distinct_v u nvs';
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   583
      in (t' $ u', nvs'') end
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   584
  | distinct_v x nvs = (x, nvs);
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   585
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   586
fun compile_match thy eqs eqs' out_ts success_t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   587
  let 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   588
    val eqs'' = maps mk_eq eqs @ eqs'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   589
    val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) [];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   590
    val name = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   591
    val name' = Name.variant (name :: names) "y";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   592
    val T = mk_tupleT (map fastype_of out_ts);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   593
    val U = fastype_of success_t;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   594
    val U' = dest_pred_enumT U;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   595
    val v = Free (name, T);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   596
    val v' = Free (name', T);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   597
  in
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31580
diff changeset
   598
    lambda v (fst (Datatype.make_case
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   599
      (ProofContext.init thy) false [] v
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   600
      [(mk_tuple out_ts,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   601
        if null eqs'' then success_t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   602
        else Const (@{const_name HOL.If}, HOLogic.boolT --> U --> U --> U) $
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   603
          foldr1 HOLogic.mk_conj eqs'' $ success_t $
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   604
            mk_empty U'),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   605
       (v', mk_empty U')]))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   606
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   607
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   608
(*FIXME function can be removed*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   609
fun mk_funcomp f t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   610
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   611
    val names = Term.add_free_names t [];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   612
    val Ts = binder_types (fastype_of t);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   613
    val vs = map Free
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   614
      (Name.variant_list names (replicate (length Ts) "x") ~~ Ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   615
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   616
    fold_rev lambda vs (f (list_comb (t, vs)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   617
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   618
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   619
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   620
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   621
fun compile_param_ext thy modes (NONE, t) = t
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   622
  | compile_param_ext thy modes (m as SOME (Mode ((iss, is'), is, ms)), t) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   623
      let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   624
        val (vs, u) = strip_abs t
31515
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
   625
        val (ivs, ovs) = get_args is vs    
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   626
        val (f, args) = strip_comb u
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   627
        val (params, args') = chop (length ms) args
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   628
        val (inargs, outargs) = get_args is' args'
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   629
        val b = length vs
31986
a68f88d264f7 dropped find_index_eq
haftmann
parents: 31879
diff changeset
   630
        val perm = map (fn i => find_index (fn t => t = Bound (b - i)) args' + 1) (1 upto b)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   631
        val outp_perm =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   632
          snd (get_args is perm)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   633
          |> map (fn i => i - length (filter (fn x => x < i) is'))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   634
        val names = [] (* TODO *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   635
        val out_names = Name.variant_list names (replicate (length outargs) "x")
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   636
        val f' = case f of
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   637
            Const (name, T) =>
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   638
              if AList.defined op = modes name then
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   639
                Const (predfun_name_of thy name (iss, is'), funT'_of (iss, is') T)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   640
              else error "compile param: Not an inductive predicate with correct mode"
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   641
          | Free (name, T) => Free (name, funT_of T (SOME is'))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   642
        val outTs = dest_tupleT (dest_pred_enumT (body_type (fastype_of f')))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   643
        val out_vs = map Free (out_names ~~ outTs)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   644
        val params' = map (compile_param thy modes) (ms ~~ params)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   645
        val f_app = list_comb (f', params' @ inargs)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   646
        val single_t = (mk_single (mk_tuple (map (fn i => nth out_vs (i - 1)) outp_perm)))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   647
        val match_t = compile_match thy [] [] out_vs single_t
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   648
      in list_abs (ivs,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   649
        mk_bind (f_app, match_t))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   650
      end
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   651
  | compile_param_ext _ _ _ = error "compile params"
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   652
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   653
and compile_param thy modes (NONE, t) = t
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   654
 | compile_param thy modes (m as SOME (Mode ((iss, is'), is, ms)), t) =
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   655
   (* (case t of
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   656
     Abs _ => error "compile_param: Invalid term" *) (* compile_param_ext thy modes (m, t) *)
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   657
   (*  |  _ => let *)
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   658
   let  
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   659
     val (f, args) = strip_comb (Envir.eta_contract t)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   660
     val (params, args') = chop (length ms) args
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   661
     val params' = map (compile_param thy modes) (ms ~~ params)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   662
     val f' = case f of
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   663
        Const (name, T) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   664
          if AList.defined op = modes name then
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   665
             Const (predfun_name_of thy name (iss, is'), funT'_of (iss, is') T)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   666
          else error "compile param: Not an inductive predicate with correct mode"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   667
      | Free (name, T) => Free (name, funT_of T (SOME is'))
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
   668
   in list_comb (f', params' @ args') end
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   669
 | compile_param _ _ _ = error "compile params"
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   670
  
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   671
  
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   672
fun compile_expr thy modes (SOME (Mode (mode, is, ms)), t) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   673
      (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   674
         (Const (name, T), params) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   675
           if AList.defined op = modes name then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   676
             let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   677
               val (Ts, Us) = get_args is
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   678
                 (curry Library.drop (length ms) (fst (strip_type T)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   679
               val params' = map (compile_param thy modes) (ms ~~ params)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   680
             in list_comb (Const (predfun_name_of thy name mode, ((map fastype_of params') @ Ts) --->
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   681
               mk_pred_enumT (mk_tupleT Us)), params')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   682
             end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   683
           else error "not a valid inductive expression"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   684
       | (Free (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   685
         (*if name mem param_vs then *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   686
         (* Higher order mode call *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   687
         let val r = Free (name, funT_of T (SOME is))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   688
         in list_comb (r, args) end)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   689
  | compile_expr _ _ _ = error "not a valid inductive expression"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   690
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   691
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   692
fun compile_clause thy all_vs param_vs modes (iss, is) (ts, ps) inp =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   693
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   694
    val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   695
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   696
        (param_vs ~~ iss);
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   697
    fun check_constrt t (names, eqs) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   698
      if is_constrt thy t then (t, (names, eqs)) else
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   699
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   700
          val s = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   701
          val v = Free (s, fastype_of t)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   702
        in (v, (s::names, HOLogic.mk_eq (v, t)::eqs)) end;
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   703
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   704
    val (in_ts, out_ts) = get_args is ts;
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   705
    val (in_ts', (all_vs', eqs)) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   706
      fold_map check_constrt in_ts (all_vs, []);
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   707
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   708
    fun compile_prems out_ts' vs names [] =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   709
          let
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   710
            val (out_ts'', (names', eqs')) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   711
              fold_map check_constrt out_ts' (names, []);
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   712
            val (out_ts''', (names'', constr_vs)) = fold_map distinct_v
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   713
              out_ts'' (names', map (rpair []) vs);
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   714
          in
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   715
            compile_match thy constr_vs (eqs @ eqs') out_ts'''
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   716
              (mk_single (mk_tuple out_ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   717
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   718
      | compile_prems out_ts vs names ps =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   719
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   720
            val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   721
            val SOME (p, mode as SOME (Mode (_, js, _))) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   722
              select_mode_prem thy modes' vs' ps
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   723
            val ps' = filter_out (equal p) ps
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   724
            val (out_ts', (names', eqs)) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   725
              fold_map check_constrt out_ts (names, [])
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   726
            val (out_ts'', (names'', constr_vs')) = fold_map distinct_v
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   727
              out_ts' ((names', map (rpair []) vs))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   728
            val (compiled_clause, rest) = case p of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   729
               Prem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   730
                 let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   731
                   val (in_ts, out_ts''') = get_args js us;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   732
                   val u = list_comb (compile_expr thy modes (mode, t), in_ts)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   733
                   val rest = compile_prems out_ts''' vs' names'' ps'
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   734
                 in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   735
                   (u, rest)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   736
                 end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   737
             | Negprem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   738
                 let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   739
                   val (in_ts, out_ts''') = get_args js us
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   740
                   val u = list_comb (compile_expr thy modes (mode, t), in_ts)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   741
                   val rest = compile_prems out_ts''' vs' names'' ps'
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   742
                 in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   743
                   (mk_not_pred u, rest)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   744
                 end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   745
             | Sidecond t =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   746
                 let
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   747
                   val rest = compile_prems [] vs' names'' ps';
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   748
                 in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   749
                   (mk_if_predenum t, rest)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   750
                 end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   751
          in
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   752
            compile_match thy constr_vs' eqs out_ts'' 
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   753
              (mk_bind (compiled_clause, rest))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   754
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   755
    val prem_t = compile_prems in_ts' param_vs all_vs' ps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   756
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   757
    mk_bind (mk_single inp, prem_t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   758
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   759
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   760
fun compile_pred thy all_vs param_vs modes s T cls mode =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   761
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   762
    val Ts = binder_types T;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   763
    val (Ts1, Ts2) = chop (length param_vs) Ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   764
    val Ts1' = map2 funT_of Ts1 (fst mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   765
    val (Us1, Us2) = get_args (snd mode) Ts2;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   766
    val xnames = Name.variant_list param_vs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   767
      (map (fn i => "x" ^ string_of_int i) (snd mode));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   768
    val xs = map2 (fn s => fn T => Free (s, T)) xnames Us1;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   769
    val cl_ts =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   770
      map (fn cl => compile_clause thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   771
        all_vs param_vs modes mode cl (mk_tuple xs)) cls;
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   772
    val mode_id = predfun_name_of thy s mode
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   773
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   774
    HOLogic.mk_Trueprop (HOLogic.mk_eq
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   775
      (list_comb (Const (mode_id, (Ts1' @ Us1) --->
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   776
           mk_pred_enumT (mk_tupleT Us2)),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   777
         map2 (fn s => fn T => Free (s, T)) param_vs Ts1' @ xs),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   778
       foldr1 mk_sup cl_ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   779
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   780
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   781
fun compile_preds thy all_vs param_vs modes preds =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   782
  map (fn (s, (T, cls)) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   783
    map (compile_pred thy all_vs param_vs modes s T cls)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   784
      ((the o AList.lookup (op =) modes) s)) preds;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   785
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   786
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   787
(* special setup for simpset *)                  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   788
val HOL_basic_ss' = HOL_basic_ss setSolver 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   789
  (mk_solver "all_tac_solver" (fn _ => fn _ => all_tac))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   790
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   791
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   792
(* Definition of executable functions and their intro and elim rules *)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   793
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   794
fun print_arities arities = tracing ("Arities:\n" ^
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   795
  cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   796
    space_implode " -> " (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   797
      (fn NONE => "X" | SOME k' => string_of_int k')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   798
        (ks @ [SOME k]))) arities));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   799
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   800
fun mk_Eval_of ((x, T), NONE) names = (x, names)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   801
  | mk_Eval_of ((x, T), SOME mode) names = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   802
  val Ts = binder_types T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   803
  val argnames = Name.variant_list names
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   804
        (map (fn i => "x" ^ string_of_int i) (1 upto (length Ts)));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   805
  val args = map Free (argnames ~~ Ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   806
  val (inargs, outargs) = get_args mode args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   807
  val r = mk_Eval (list_comb (x, inargs), mk_tuple outargs)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   808
  val t = fold_rev lambda args r 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   809
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   810
  (t, argnames @ names)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   811
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   812
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   813
fun create_intro_elim_rule nparams mode defthm mode_id funT pred thy =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   814
let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   815
  val Ts = binder_types (fastype_of pred)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   816
  val funtrm = Const (mode_id, funT)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   817
  val argnames = Name.variant_list []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   818
        (map (fn i => "x" ^ string_of_int i) (1 upto (length Ts)));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   819
  val (Ts1, Ts2) = chop nparams Ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   820
  val Ts1' = map2 funT_of Ts1 (fst mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   821
  val args = map Free (argnames ~~ (Ts1' @ Ts2))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   822
  val (params, io_args) = chop nparams args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   823
  val (inargs, outargs) = get_args (snd mode) io_args
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   824
  val param_names = Name.variant_list argnames
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   825
    (map (fn i => "p" ^ string_of_int i) (1 upto nparams))
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   826
  val param_vs = map Free (param_names ~~ Ts1)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   827
  val (params', names) = fold_map mk_Eval_of ((params ~~ Ts1) ~~ (fst mode)) []
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   828
  val predpropI = HOLogic.mk_Trueprop (list_comb (pred, param_vs @ io_args))
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   829
  val predpropE = HOLogic.mk_Trueprop (list_comb (pred, params' @ io_args))
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   830
  val param_eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (param_vs ~~ params')
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   831
  val funargs = params @ inargs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   832
  val funpropE = HOLogic.mk_Trueprop (mk_Eval (list_comb (funtrm, funargs),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   833
                  if null outargs then Free("y", HOLogic.unitT) else mk_tuple outargs))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   834
  val funpropI = HOLogic.mk_Trueprop (mk_Eval (list_comb (funtrm, funargs),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   835
                   mk_tuple outargs))
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   836
  val introtrm = Logic.list_implies (predpropI :: param_eqs, funpropI)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   837
  val _ = Output.tracing (Syntax.string_of_term_global thy introtrm) 
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   838
  val simprules = [defthm, @{thm eval_pred},
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   839
                   @{thm "split_beta"}, @{thm "fst_conv"}, @{thm "snd_conv"}]
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   840
  val unfolddef_tac = (Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps simprules) 1)
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   841
  val introthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ ["y"]) [] introtrm (fn {...} => unfolddef_tac)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   842
  val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT));
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   843
  val elimtrm = Logic.list_implies ([funpropE, Logic.mk_implies (predpropE, P)], P)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   844
  val elimthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ ["y", "P"]) [] elimtrm (fn {...} => unfolddef_tac)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   845
in 
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   846
  (introthm, elimthm)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   847
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   848
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   849
fun create_definitions preds nparams (name, modes) thy =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   850
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   851
    val _ = tracing "create definitions"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   852
    val T = AList.lookup (op =) preds name |> the
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   853
    fun create_definition mode thy = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   854
      fun string_of_mode mode = if null mode then "0"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   855
        else space_implode "_" (map string_of_int mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   856
      val HOmode = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   857
        fun string_of_HOmode m s = case m of NONE => s | SOME mode => s ^ "__" ^ (string_of_mode mode)    
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   858
        in (fold string_of_HOmode (fst mode) "") end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   859
      val mode_id = name ^ (if HOmode = "" then "_" else HOmode ^ "___")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   860
        ^ (string_of_mode (snd mode))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   861
      val Ts = binder_types T;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   862
      val (Ts1, Ts2) = chop nparams Ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   863
      val Ts1' = map2 funT_of Ts1 (fst mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   864
      val (Us1, Us2) = get_args (snd mode) Ts2;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   865
      val names = Name.variant_list []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   866
        (map (fn i => "x" ^ string_of_int i) (1 upto (length Ts)));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   867
      val xs = map Free (names ~~ (Ts1' @ Ts2));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   868
      val (xparams, xargs) = chop nparams xs;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   869
      val (xparams', names') = fold_map mk_Eval_of ((xparams ~~ Ts1) ~~ (fst mode)) names
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   870
      val (xins, xouts) = get_args (snd mode) xargs;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   871
      fun mk_split_lambda [] t = lambda (Free (Name.variant names' "x", HOLogic.unitT)) t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   872
       | mk_split_lambda [x] t = lambda x t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   873
       | mk_split_lambda xs t = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   874
         fun mk_split_lambda' (x::y::[]) t = HOLogic.mk_split (lambda x (lambda y t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   875
           | mk_split_lambda' (x::xs) t = HOLogic.mk_split (lambda x (mk_split_lambda' xs t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   876
         in mk_split_lambda' xs t end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   877
      val predterm = mk_Enum (mk_split_lambda xouts (list_comb (Const (name, T), xparams' @ xargs)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   878
      val funT = (Ts1' @ Us1) ---> (mk_pred_enumT (mk_tupleT Us2))
30387
0affb9975547 NameSpace.base_name ~> Long_Name.base_name
haftmann
parents: 30379
diff changeset
   879
      val mode_id = Sign.full_bname thy (Long_Name.base_name mode_id)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   880
      val lhs = list_comb (Const (mode_id, funT), xparams @ xins)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   881
      val def = Logic.mk_equals (lhs, predterm)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   882
      val ([definition], thy') = thy |>
30387
0affb9975547 NameSpace.base_name ~> Long_Name.base_name
haftmann
parents: 30379
diff changeset
   883
        Sign.add_consts_i [(Binding.name (Long_Name.base_name mode_id), funT, NoSyn)] |>
0affb9975547 NameSpace.base_name ~> Long_Name.base_name
haftmann
parents: 30379
diff changeset
   884
        PureThy.add_defs false [((Binding.name (Long_Name.base_name mode_id ^ "_def"), def), [])]
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   885
      val (intro, elim) = create_intro_elim_rule nparams mode definition mode_id funT (Const (name, T)) thy'
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   886
      in thy' |> add_predfun name mode (mode_id, definition, intro, elim)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   887
        |> PureThy.store_thm (Binding.name (Long_Name.base_name mode_id ^ "I"), intro) |> snd
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   888
        |> PureThy.store_thm (Binding.name (Long_Name.base_name mode_id ^ "E"), elim)  |> snd
31554
a6c6bf2751a0 added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents: 31550
diff changeset
   889
        |> Theory.checkpoint
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   890
      end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   891
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   892
    fold create_definition modes thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   893
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   894
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   895
(**************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   896
(* Proving equivalence of term *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   897
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   898
fun is_Type (Type _) = true
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   899
  | is_Type _ = false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   900
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   901
(* returns true if t is an application of an datatype constructor *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   902
(* which then consequently would be splitted *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   903
(* else false *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   904
fun is_constructor thy t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   905
  if (is_Type (fastype_of t)) then
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
   906
    (case Datatype.get_info thy ((fst o dest_Type o fastype_of) t) of
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   907
      NONE => false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   908
    | SOME info => (let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   909
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   910
      val (c, _) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   911
      in (case c of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   912
        Const (name, _) => name mem_string constr_consts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   913
        | _ => false) end))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   914
  else false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   915
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   916
(* MAJOR FIXME:  prove_params should be simple
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   917
 - different form of introrule for parameters ? *)
31549
f7379ea2c949 tuned; corrected exception handling
bulwahn
parents: 31541
diff changeset
   918
fun prove_param thy modes (NONE, t) =
f7379ea2c949 tuned; corrected exception handling
bulwahn
parents: 31541
diff changeset
   919
  all_tac 
f7379ea2c949 tuned; corrected exception handling
bulwahn
parents: 31541
diff changeset
   920
| prove_param thy modes (m as SOME (Mode (mode, is, ms)), t) =
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   921
  REPEAT_DETERM (etac @{thm thin_rl} 1)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   922
  THEN REPEAT_DETERM (rtac @{thm ext} 1)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   923
  THEN (rtac @{thm iffI} 1)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   924
  THEN new_print_tac "prove_param"
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   925
  (* proof in one direction *)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   926
  THEN (atac 1)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   927
  (* proof in the other direction *)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   928
  THEN (atac 1)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   929
  THEN new_print_tac "after prove_param"
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   930
(*  let
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   931
    val  (f, args) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   932
    val (params, _) = chop (length ms) args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   933
    val f_tac = case f of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   934
        Const (name, T) => simp_tac (HOL_basic_ss addsimps 
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   935
           (@{thm eval_pred}::(predfun_definition_of thy name mode)::
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   936
           @{thm "Product_Type.split_conv"}::[])) 1
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   937
      | Free _ => all_tac
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   938
      | Abs _ => error "TODO: implement here"
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   939
  in  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   940
    print_tac "before simplification in prove_args:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   941
    THEN f_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   942
    THEN print_tac "after simplification in prove_args"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   943
    THEN (EVERY (map (prove_param thy modes) (ms ~~ params)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   944
    THEN (REPEAT_DETERM (atac 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   945
  end
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   946
*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   947
fun prove_expr thy modes (SOME (Mode (mode, is, ms)), t, us) (premposition : int) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   948
  (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   949
    (Const (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   950
      if AList.defined op = modes name then (let
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
   951
          val introrule = predfun_intro_of thy name mode
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   952
          (*val (in_args, out_args) = get_args is us
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   953
          val (pred, rargs) = strip_comb (HOLogic.dest_Trueprop
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   954
            (hd (Logic.strip_imp_prems (prop_of introrule))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   955
          val nparams = length ms (* get_nparams thy (fst (dest_Const pred)) *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   956
          val (_, args) = chop nparams rargs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   957
          val subst = map (pairself (cterm_of thy)) (args ~~ us)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   958
          val inst_introrule = Drule.cterm_instantiate subst introrule*)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   959
         (* the next line is old and probably wrong *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   960
          val (args1, args2) = chop (length ms) args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   961
        in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   962
        rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   963
        THEN print_tac "before intro rule:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   964
        (* for the right assumption in first position *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   965
        THEN rotate_tac premposition 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   966
        THEN rtac introrule 1
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   967
        THEN new_print_tac "after intro rule"
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   968
        (* work with parameter arguments *)
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   969
        THEN (atac 1)
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
   970
        THEN (new_print_tac "parameter goal")
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   971
        THEN (EVERY (map (prove_param thy modes) (ms ~~ args1)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   972
        THEN (REPEAT_DETERM (atac 1)) end)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   973
      else error "Prove expr if case not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   974
    | _ => rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   975
           THEN atac 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   976
  | prove_expr _ _ _ _ =  error "Prove expr not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   977
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   978
fun SOLVED tac st = FILTER (fn st' => nprems_of st' = nprems_of st - 1) tac st; 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   979
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   980
fun SOLVEDALL tac st = FILTER (fn st' => nprems_of st' = 0) tac st
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   981
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   982
fun prove_match thy (out_ts : term list) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   983
  fun get_case_rewrite t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   984
    if (is_constructor thy t) then let
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
   985
      val case_rewrites = (#case_rewrites (Datatype.the_info thy
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   986
        ((fst o dest_Type o fastype_of) t)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   987
      in case_rewrites @ (flat (map get_case_rewrite (snd (strip_comb t)))) end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   988
    else []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   989
  val simprules = @{thm "unit.cases"} :: @{thm "prod.cases"} :: (flat (map get_case_rewrite out_ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   990
(* replace TRY by determining if it necessary - are there equations when calling compile match? *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   991
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   992
   (* make this simpset better! *)
31515
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
   993
  asm_simp_tac (HOL_basic_ss' addsimps simprules) 1
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   994
  THEN print_tac "after prove_match:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   995
  THEN (DETERM (TRY (EqSubst.eqsubst_tac (ProofContext.init thy) [0] [@{thm "HOL.if_P"}] 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   996
         THEN (REPEAT_DETERM (rtac @{thm conjI} 1 THEN (SOLVED (asm_simp_tac HOL_basic_ss 1))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   997
         THEN (SOLVED (asm_simp_tac HOL_basic_ss 1)))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   998
  THEN print_tac "after if simplification"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   999
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1000
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1001
(* corresponds to compile_fun -- maybe call that also compile_sidecond? *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1002
31515
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1003
fun prove_sidecond thy modes t =
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1004
  let
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1005
    fun preds_of t nameTs = case strip_comb t of 
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1006
      (f as Const (name, T), args) =>
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1007
        if AList.defined (op =) modes name then (name, T) :: nameTs
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1008
          else fold preds_of args nameTs
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1009
      | _ => nameTs
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1010
    val preds = preds_of t []
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1011
    val defs = map
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1012
      (fn (pred, T) => predfun_definition_of thy pred ([], (1 upto (length (binder_types T)))))
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1013
        preds
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1014
  in 
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1015
    (* remove not_False_eq_True when simpset in prove_match is better *)
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1016
    simp_tac (HOL_basic_ss addsimps @{thm not_False_eq_True} :: @{thm eval_pred} :: defs) 1 
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1017
    (* need better control here! *)
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1018
  end
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1019
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1020
fun prove_clause thy nargs all_vs param_vs modes (iss, is) (ts, ps) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1021
  val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1022
   (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1023
     (param_vs ~~ iss);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1024
  fun check_constrt ((names, eqs), t) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1025
      if is_constrt thy t then ((names, eqs), t) else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1026
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1027
          val s = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1028
          val v = Free (s, fastype_of t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1029
        in ((s::names, HOLogic.mk_eq (v, t)::eqs), v) end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1030
  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1031
  val (in_ts, clause_out_ts) = get_args is ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1032
  val ((all_vs', eqs), in_ts') =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1033
      (*FIXME*) Library.foldl_map check_constrt ((all_vs, []), in_ts);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1034
  fun prove_prems out_ts vs [] =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1035
    (prove_match thy out_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1036
    THEN asm_simp_tac HOL_basic_ss' 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1037
    THEN print_tac "before the last rule of singleI:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1038
    THEN (rtac (if null clause_out_ts then @{thm singleI_unit} else @{thm singleI}) 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1039
  | prove_prems out_ts vs rps =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1040
    let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1041
      val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1042
      val SOME (p, mode as SOME (Mode ((iss, js), _, param_modes))) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1043
        select_mode_prem thy modes' vs' rps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1044
      val premposition = (find_index (equal p) ps) + nargs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1045
      val rps' = filter_out (equal p) rps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1046
      val rest_tac = (case p of Prem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1047
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1048
            val (in_ts, out_ts''') = get_args js us
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1049
            val rec_tac = prove_prems out_ts''' vs' rps'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1050
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1051
            print_tac "before clause:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1052
            THEN asm_simp_tac HOL_basic_ss 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1053
            THEN print_tac "before prove_expr:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1054
            THEN prove_expr thy modes (mode, t, us) premposition
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1055
            THEN print_tac "after prove_expr:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1056
            THEN rec_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1057
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1058
        | Negprem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1059
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1060
            val (in_ts, out_ts''') = get_args js us
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1061
            val rec_tac = prove_prems out_ts''' vs' rps'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1062
            val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1063
            val (_, params) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1064
          in
31515
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1065
            rtac @{thm bindI} 1
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1066
            THEN (if (is_some name) then
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1067
                simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, js)]) 1
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1068
                THEN rtac @{thm not_predI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1069
                (* FIXME: work with parameter arguments *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1070
                THEN (EVERY (map (prove_param thy modes) (param_modes ~~ params)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1071
              else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1072
                rtac @{thm not_predI'} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1073
            THEN (REPEAT_DETERM (atac 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1074
            THEN rec_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1075
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1076
        | Sidecond t =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1077
         rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1078
         THEN rtac @{thm if_predI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1079
         THEN print_tac "before sidecond:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1080
         THEN prove_sidecond thy modes t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1081
         THEN print_tac "after sidecond:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1082
         THEN prove_prems [] vs' rps')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1083
    in (prove_match thy out_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1084
        THEN rest_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1085
    end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1086
  val prems_tac = prove_prems in_ts' param_vs ps
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1087
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1088
  rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1089
  THEN rtac @{thm singleI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1090
  THEN prems_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1091
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1092
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1093
fun select_sup 1 1 = []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1094
  | select_sup _ 1 = [rtac @{thm supI1}]
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1095
  | select_sup n i = (rtac @{thm supI2})::(select_sup (n - 1) (i - 1));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1096
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1097
fun prove_one_direction thy all_vs param_vs modes clauses ((pred, T), mode) = let
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31580
diff changeset
  1098
(*  val ind_result = Inductive.the_inductive (ProofContext.init thy) pred
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1099
  val index = find_index (fn s => s = pred) (#names (fst ind_result))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1100
  val (_, T) = dest_Const (nth (#preds (snd ind_result)) index) *)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1101
  val nargs = length (binder_types T) - nparams_of thy pred
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1102
  val pred_case_rule = singleton (ind_set_codegen_preproc thy)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1103
    (preprocess_elim thy nargs (the_elim_of thy pred))
31554
a6c6bf2751a0 added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents: 31550
diff changeset
  1104
  (* FIXME preprocessor |> Simplifier.full_simplify (HOL_basic_ss addsimps [@{thm Predicate.memb_code}])*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1105
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1106
  REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"}))
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1107
  THEN etac (predfun_elim_of thy pred mode) 1
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1108
  THEN etac pred_case_rule 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1109
  THEN (EVERY (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1110
         (fn i => EVERY' (select_sup (length clauses) i) i) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1111
           (1 upto (length clauses))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1112
  THEN (EVERY (map (prove_clause thy nargs all_vs param_vs modes mode) clauses))
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1113
  THEN new_print_tac "proved one direction"
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1114
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1115
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1116
(*******************************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1117
(* Proof in the other direction ************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1118
(*******************************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1119
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1120
fun prove_match2 thy out_ts = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1121
  fun split_term_tac (Free _) = all_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1122
    | split_term_tac t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1123
      if (is_constructor thy t) then let
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
  1124
        val info = Datatype.the_info thy ((fst o dest_Type o fastype_of) t)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1125
        val num_of_constrs = length (#case_rewrites info)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1126
        (* special treatment of pairs -- because of fishing *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1127
        val split_rules = case (fst o dest_Type o fastype_of) t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1128
          "*" => [@{thm prod.split_asm}] 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1129
          | _ => PureThy.get_thms thy (((fst o dest_Type o fastype_of) t) ^ ".split_asm")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1130
        val (_, ts) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1131
      in
31515
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1132
        (Splitter.split_asm_tac split_rules 1)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1133
(*        THEN (Simplifier.asm_full_simp_tac HOL_basic_ss 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1134
          THEN (DETERM (TRY (etac @{thm Pair_inject} 1))) *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1135
        THEN (REPEAT_DETERM_N (num_of_constrs - 1) (etac @{thm botE} 1 ORELSE etac @{thm botE} 2))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1136
        THEN (EVERY (map split_term_tac ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1137
      end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1138
    else all_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1139
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1140
    split_term_tac (mk_tuple out_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1141
    THEN (DETERM (TRY ((Splitter.split_asm_tac [@{thm "split_if_asm"}] 1) THEN (etac @{thm botE} 2))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1142
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1143
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1144
(* VERY LARGE SIMILIRATIY to function prove_param 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1145
-- join both functions
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1146
*)
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
  1147
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1148
fun prove_param2 thy modes (NONE, t) = all_tac 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1149
  | prove_param2 thy modes (m as SOME (Mode (mode, is, ms)), t) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1150
    val  (f, args) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1151
    val (params, _) = chop (length ms) args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1152
    val f_tac = case f of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1153
        Const (name, T) => full_simp_tac (HOL_basic_ss addsimps 
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1154
           (@{thm eval_pred}::(predfun_definition_of thy name mode)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1155
           :: @{thm "Product_Type.split_conv"}::[])) 1
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1156
      | Free _ => all_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1157
  in  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1158
    print_tac "before simplification in prove_args:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1159
    THEN f_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1160
    THEN print_tac "after simplification in prove_args"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1161
    THEN (EVERY (map (prove_param2 thy modes) (ms ~~ params)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1162
  end
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
  1163
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1164
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1165
fun prove_expr2 thy modes (SOME (Mode (mode, is, ms)), t) = 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1166
  (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1167
    (Const (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1168
      if AList.defined op = modes name then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1169
        etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1170
        THEN (REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"})))
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
  1171
        THEN new_print_tac "prove_expr2-before"
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1172
        THEN (debug_tac (Syntax.string_of_term_global thy
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1173
          (prop_of (predfun_elim_of thy name mode))))
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1174
        THEN (etac (predfun_elim_of thy name mode) 1)
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1175
        THEN new_print_tac "prove_expr2"
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1176
        (* TODO -- FIXME: replace remove_last_goal*)
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
  1177
        (* THEN (EVERY (replicate (length args) (remove_last_goal thy))) *)
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
  1178
        THEN (EVERY (map (prove_param thy modes) (ms ~~ args)))
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1179
        THEN new_print_tac "finished prove_expr2"
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
  1180
      
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1181
      else error "Prove expr2 if case not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1182
    | _ => etac @{thm bindE} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1183
  | prove_expr2 _ _ _ = error "Prove expr2 not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1184
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1185
fun prove_sidecond2 thy modes t = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1186
  fun preds_of t nameTs = case strip_comb t of 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1187
    (f as Const (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1188
      if AList.defined (op =) modes name then (name, T) :: nameTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1189
        else fold preds_of args nameTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1190
    | _ => nameTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1191
  val preds = preds_of t []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1192
  val defs = map
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1193
    (fn (pred, T) => predfun_definition_of thy pred ([], (1 upto (length (binder_types T)))))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1194
      preds
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1195
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1196
   (* only simplify the one assumption *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1197
   full_simp_tac (HOL_basic_ss' addsimps @{thm eval_pred} :: defs) 1 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1198
   (* need better control here! *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1199
   THEN print_tac "after sidecond2 simplification"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1200
   end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1201
  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1202
fun prove_clause2 thy all_vs param_vs modes (iss, is) (ts, ps) pred i = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1203
  val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1204
   (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1205
     (param_vs ~~ iss);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1206
  fun check_constrt ((names, eqs), t) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1207
      if is_constrt thy t then ((names, eqs), t) else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1208
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1209
          val s = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1210
          val v = Free (s, fastype_of t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1211
        in ((s::names, HOLogic.mk_eq (v, t)::eqs), v) end;
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1212
  val pred_intro_rule = nth (intros_of thy pred) (i - 1)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1213
    |> preprocess_intro thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1214
    |> (fn thm => hd (ind_set_codegen_preproc thy [thm]))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1215
    (* FIXME preprocess |> Simplifier.full_simplify (HOL_basic_ss addsimps [@ {thm Predicate.memb_code}]) *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1216
  val (in_ts, clause_out_ts) = get_args is ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1217
  val ((all_vs', eqs), in_ts') =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1218
      (*FIXME*) Library.foldl_map check_constrt ((all_vs, []), in_ts);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1219
  fun prove_prems2 out_ts vs [] =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1220
    print_tac "before prove_match2 - last call:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1221
    THEN prove_match2 thy out_ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1222
    THEN print_tac "after prove_match2 - last call:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1223
    THEN (etac @{thm singleE} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1224
    THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1225
    THEN (asm_full_simp_tac HOL_basic_ss' 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1226
    THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1227
    THEN (asm_full_simp_tac HOL_basic_ss' 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1228
    THEN SOLVED (print_tac "state before applying intro rule:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1229
      THEN (rtac pred_intro_rule 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1230
      (* How to handle equality correctly? *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1231
      THEN (print_tac "state before assumption matching")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1232
      THEN (REPEAT (atac 1 ORELSE 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1233
         (CHANGED (asm_full_simp_tac HOL_basic_ss' 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1234
          THEN print_tac "state after simp_tac:"))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1235
  | prove_prems2 out_ts vs ps = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1236
      val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1237
      val SOME (p, mode as SOME (Mode ((iss, js), _, param_modes))) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1238
        select_mode_prem thy modes' vs' ps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1239
      val ps' = filter_out (equal p) ps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1240
      val rest_tac = (case p of Prem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1241
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1242
            val (in_ts, out_ts''') = get_args js us
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1243
            val rec_tac = prove_prems2 out_ts''' vs' ps'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1244
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1245
            (prove_expr2 thy modes (mode, t)) THEN rec_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1246
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1247
        | Negprem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1248
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1249
            val (in_ts, out_ts''') = get_args js us
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1250
            val rec_tac = prove_prems2 out_ts''' vs' ps'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1251
            val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1252
            val (_, params) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1253
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1254
            print_tac "before neg prem 2"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1255
            THEN etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1256
            THEN (if is_some name then
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1257
                full_simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, js)]) 1 
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1258
                THEN etac @{thm not_predE} 1
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1259
                THEN (EVERY (map (prove_param thy modes) (param_modes ~~ params)))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1260
              else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1261
                etac @{thm not_predE'} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1262
            THEN rec_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1263
          end 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1264
        | Sidecond t =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1265
            etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1266
            THEN etac @{thm if_predE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1267
            THEN prove_sidecond2 thy modes t 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1268
            THEN prove_prems2 [] vs' ps')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1269
    in print_tac "before prove_match2:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1270
       THEN prove_match2 thy out_ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1271
       THEN print_tac "after prove_match2:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1272
       THEN rest_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1273
    end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1274
  val prems_tac = prove_prems2 in_ts' param_vs ps 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1275
in
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
  1276
  new_print_tac "starting prove_clause2"
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1277
  THEN etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1278
  THEN (etac @{thm singleE'} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1279
  THEN (TRY (etac @{thm Pair_inject} 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1280
  THEN print_tac "after singleE':"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1281
  THEN prems_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1282
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1283
 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1284
fun prove_other_direction thy all_vs param_vs modes clauses (pred, mode) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1285
  fun prove_clause (clause, i) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1286
    (if i < length clauses then etac @{thm supE} 1 else all_tac)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1287
    THEN (prove_clause2 thy all_vs param_vs modes mode clause pred i)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1288
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1289
  (DETERM (TRY (rtac @{thm unit.induct} 1)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1290
   THEN (REPEAT_DETERM (CHANGED (rewtac @{thm split_paired_all})))
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1291
   THEN (rtac (predfun_intro_of thy pred mode) 1)
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
  1292
   THEN (REPEAT_DETERM (rtac @{thm refl} 2))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1293
   THEN (EVERY (map prove_clause (clauses ~~ (1 upto (length clauses)))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1294
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1295
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1296
fun prove_pred thy all_vs param_vs modes clauses (((pred, T), mode), t) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1297
  val ctxt = ProofContext.init thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1298
  val clauses' = the (AList.lookup (op =) clauses pred)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1299
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1300
  Goal.prove ctxt (Term.fold_aterms (fn Free (x, _) => insert (op =) x | _ => I) t []) [] t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1301
    (if !do_proofs then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1302
      (fn _ =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1303
      rtac @{thm pred_iffI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1304
      THEN prove_one_direction thy all_vs param_vs modes clauses' ((pred, T), mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1305
      THEN print_tac "proved one direction"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1306
      THEN prove_other_direction thy all_vs param_vs modes clauses' (pred, mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1307
      THEN print_tac "proved other direction")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1308
     else (fn _ => mycheat_tac thy 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1309
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1310
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1311
fun prove_preds thy all_vs param_vs modes clauses pmts =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1312
  map (prove_pred thy all_vs param_vs modes clauses) pmts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1313
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1314
(* special case: inductive predicate with no clauses *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1315
fun noclause (predname, T) thy = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1316
  val Ts = binder_types T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1317
  val names = Name.variant_list []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1318
        (map (fn i => "x" ^ (string_of_int i)) (1 upto (length Ts)))
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1319
  val vs = map2 (curry Free) names Ts
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1320
  val clausehd =  HOLogic.mk_Trueprop (list_comb(Const (predname, T), vs))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1321
  val intro_t = Logic.mk_implies (@{prop False}, clausehd)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1322
  val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1323
  val elim_t = Logic.list_implies ([clausehd, Logic.mk_implies (@{prop False}, P)], P)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1324
  val intro = Goal.prove (ProofContext.init thy) names [] intro_t
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1325
        (fn {...} => etac @{thm FalseE} 1)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1326
  val elim = Goal.prove (ProofContext.init thy) ("P" :: names) [] elim_t
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1327
        (fn {...} => etac (the_elim_of thy predname) 1) 
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1328
in
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1329
  add_intro intro thy
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1330
  |> set_elim elim
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1331
end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1332
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1333
fun prepare_intrs thy prednames =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1334
  let
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1335
    (* FIXME: preprocessing moved to fetch_pred_data *)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1336
    val intrs = map (preprocess_intro thy) (maps (intros_of thy) prednames)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1337
      |> ind_set_codegen_preproc thy (*FIXME preprocessor
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1338
      |> map (Simplifier.full_simplify (HOL_basic_ss addsimps [@ {thm Predicate.memb_code}]))*)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1339
      |> map (Logic.unvarify o prop_of)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1340
    val nparams = nparams_of thy (hd prednames)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1341
    val preds = distinct (op =) (map (dest_Const o fst o (strip_intro_concl nparams)) intrs)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1342
    val extra_modes = all_modes_of thy |> filter_out (fn (name, _) => member (op =) prednames name)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1343
    val _ $ u = Logic.strip_imp_concl (hd intrs);
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1344
    val params = List.take (snd (strip_comb u), nparams);
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1345
    val param_vs = maps term_vs params
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1346
    val all_vs = terms_vs intrs
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1347
    fun dest_prem t =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1348
      (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1349
        (v as Free _, ts) => if v mem params then Prem (ts, v) else Sidecond t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1350
      | (c as Const (@{const_name Not}, _), [t]) => (case dest_prem t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1351
          Prem (ts, t) => Negprem (ts, t)
31515
62fc203eed88 removed debug messages
bulwahn
parents: 31514
diff changeset
  1352
        | Negprem _ => error ("Double negation not allowed in premise: " ^ (Syntax.string_of_term_global thy (c $ t))) 
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1353
        | Sidecond t => Sidecond (c $ t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1354
      | (c as Const (s, _), ts) =>
31877
e3de75d3b898 exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents: 31876
diff changeset
  1355
        if is_registered thy s then
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1356
          let val (ts1, ts2) = chop (nparams_of thy s) ts
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1357
          in Prem (ts2, list_comb (c, ts1)) end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1358
        else Sidecond t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1359
      | _ => Sidecond t)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1360
    fun add_clause intr (clauses, arities) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1361
    let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1362
      val _ $ t = Logic.strip_imp_concl intr;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1363
      val (Const (name, T), ts) = strip_comb t;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1364
      val (ts1, ts2) = chop nparams ts;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1365
      val prems = map (dest_prem o HOLogic.dest_Trueprop) (Logic.strip_imp_prems intr);
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1366
      val (Ts, Us) = chop nparams (binder_types T)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1367
    in
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1368
      (AList.update op = (name, these (AList.lookup op = clauses name) @
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1369
        [(ts2, prems)]) clauses,
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1370
       AList.update op = (name, (map (fn U => (case strip_type U of
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1371
                 (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1372
               | _ => NONE)) Ts,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1373
             length Us)) arities)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1374
    end;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1375
    val (clauses, arities) = fold add_clause intrs ([], []);
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1376
  in (preds, nparams, all_vs, param_vs, extra_modes, clauses, arities) end;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1377
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1378
fun arrange kvs =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1379
  let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1380
    fun add (key, value) table =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1381
      AList.update op = (key, these (AList.lookup op = table key) @ [value]) table
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1382
  in fold add kvs [] end;
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1383
        
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1384
(* main function *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1385
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1386
fun add_equations_of prednames thy =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1387
let
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1388
  val _ = tracing ("starting add_equations with " ^ commas prednames ^ "...")
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1389
  (* null clause handling *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1390
  (*
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1391
  val thy' = fold (fn pred as (predname, T) => fn thy =>
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1392
    if null (intros_of thy predname) then noclause pred thy else thy) preds thy
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1393
    *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1394
  val (preds, nparams, all_vs, param_vs, extra_modes, clauses, arities) =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1395
    prepare_intrs thy prednames
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1396
  val _ = tracing "Infering modes..."
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1397
  val modes = infer_modes thy extra_modes arities param_vs clauses
31549
f7379ea2c949 tuned; corrected exception handling
bulwahn
parents: 31541
diff changeset
  1398
  val _ = print_modes modes
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1399
  val _ = tracing "Defining executable functions..."
31554
a6c6bf2751a0 added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents: 31550
diff changeset
  1400
  val thy' = fold (create_definitions preds nparams) modes thy |> Theory.checkpoint
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1401
  val clauses' = map (fn (s, cls) => (s, (the (AList.lookup (op =) preds s), cls))) clauses
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1402
  val _ = tracing "Compiling equations..."
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1403
  val ts = compile_preds thy' all_vs param_vs (extra_modes @ modes) clauses'
31549
f7379ea2c949 tuned; corrected exception handling
bulwahn
parents: 31541
diff changeset
  1404
  val _ = map (Output.tracing o (Syntax.string_of_term_global thy')) (flat ts)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1405
  val pred_mode =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1406
    maps (fn (s, (T, _)) => map (pair (s, T)) ((the o AList.lookup (op =) modes) s)) clauses' 
31879
39bff8d9b032 commented trancl example; added debug message
bulwahn
parents: 31877
diff changeset
  1407
  val _ = Output.tracing "Proving equations..."
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1408
  val result_thms =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1409
    prove_preds thy' all_vs param_vs (extra_modes @ modes) clauses (pred_mode ~~ (flat ts))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1410
  val thy'' = fold (fn (name, result_thms) => fn thy => snd (PureThy.add_thmss
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1411
    [((Binding.qualify true (Long_Name.base_name name) (Binding.name "equation"), result_thms),
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1412
      [Attrib.attribute_i thy Code.add_default_eqn_attrib])] thy))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1413
    (arrange ((map (fn ((name, _), _) => name) pred_mode) ~~ result_thms)) thy'
31554
a6c6bf2751a0 added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents: 31550
diff changeset
  1414
    |> Theory.checkpoint
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1415
in
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1416
  thy''
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1417
end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1418
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1419
(* generation of case rules from user-given introduction rules *)
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1420
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1421
fun mk_casesrule ctxt nparams introrules =
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1422
  let
31573
0047df9eb347 improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents: 31551
diff changeset
  1423
    val intros = map (Logic.unvarify o prop_of) introrules
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1424
    val (pred, (params, args)) = strip_intro_concl nparams (hd intros)
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1425
    val ([propname], ctxt1) = Variable.variant_fixes ["thesis"] ctxt
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1426
    val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT))
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1427
    val (argnames, ctxt2) = Variable.variant_fixes
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1428
      (map (fn i => "a" ^ string_of_int i) (1 upto (length args))) ctxt1
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1429
    val argvs = map2 (curry Free) argnames (map fastype_of args)
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1430
    fun mk_case intro = let
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1431
        val (_, (_, args)) = strip_intro_concl nparams intro
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1432
        val prems = Logic.strip_imp_prems intro
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1433
        val eqprems = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (argvs ~~ args)
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1434
        val frees = (fold o fold_aterms)
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1435
          (fn t as Free _ =>
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1436
              if member (op aconv) params t then I else insert (op aconv) t
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1437
           | _ => I) (args @ prems) []
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1438
        in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1439
    val assm = HOLogic.mk_Trueprop (list_comb (pred, params @ argvs))
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1440
    val cases = map mk_case intros
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1441
  in Logic.list_implies (assm :: cases, prop) end;
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1442
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1443
fun add_equations name thy =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1444
  let
31554
a6c6bf2751a0 added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents: 31550
diff changeset
  1445
    val thy' = PredData.map (Graph.extend (dependencies_of thy) name) thy |> Theory.checkpoint;
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1446
    (*val preds = Graph.all_preds (PredData.get thy') [name] |> filter_out (has_elim thy') *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1447
    fun strong_conn_of gr keys =
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1448
      Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1449
    val scc = strong_conn_of (PredData.get thy') [name]
31524
8abf99ab669c avoiding duplicate definitions of executable functions
bulwahn
parents: 31517
diff changeset
  1450
    val thy'' = fold_rev
8abf99ab669c avoiding duplicate definitions of executable functions
bulwahn
parents: 31517
diff changeset
  1451
      (fn preds => fn thy =>
8abf99ab669c avoiding duplicate definitions of executable functions
bulwahn
parents: 31517
diff changeset
  1452
        if forall (null o modes_of thy) preds then add_equations_of preds thy else thy)
31554
a6c6bf2751a0 added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents: 31550
diff changeset
  1453
      scc thy' |> Theory.checkpoint
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1454
  in thy'' end
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1455
31876
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
  1456
  
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
  1457
fun attrib f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
  1458
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
  1459
val code_pred_intros_attrib = attrib add_intro;
9ab571673059 added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents: 31784
diff changeset
  1460
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1461
(** user interface **)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1462
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1463
local
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1464
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1465
(* TODO: make TheoryDataFun to GenericDataFun & remove duplication of local theory and theory *)
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1466
(* TODO: must create state to prove multiple cases *)
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1467
fun generic_code_pred prep_const raw_const lthy =
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1468
  let
31551
995d6b90e9d6 making isatest happy; but misunderstanding remains
bulwahn
parents: 31550
diff changeset
  1469
  
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1470
    val thy = ProofContext.theory_of lthy
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1471
    val const = prep_const thy raw_const
31551
995d6b90e9d6 making isatest happy; but misunderstanding remains
bulwahn
parents: 31550
diff changeset
  1472
    
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1473
    val lthy' = LocalTheory.theory (PredData.map (Graph.extend (dependencies_of thy) const)) lthy
31554
a6c6bf2751a0 added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents: 31550
diff changeset
  1474
      |> LocalTheory.checkpoint
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1475
    val thy' = ProofContext.theory_of lthy'
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1476
    val preds = Graph.all_preds (PredData.get thy') [const] |> filter_out (has_elim thy')
31551
995d6b90e9d6 making isatest happy; but misunderstanding remains
bulwahn
parents: 31550
diff changeset
  1477
    
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1478
    fun mk_cases const =
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1479
      let
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1480
        val nparams = nparams_of thy' const
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1481
        val intros = intros_of thy' const
31580
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1482
      in mk_casesrule lthy' nparams intros end  
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1483
    val cases_rules = map mk_cases preds
31580
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1484
    val cases =
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1485
      map (fn case_rule => RuleCases.Case {fixes = [],
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1486
        assumes = [("", Logic.strip_imp_prems case_rule)],
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1487
        binds = [], cases = []}) cases_rules
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1488
    val case_env = map2 (fn p => fn c => (Long_Name.base_name p, SOME c)) preds cases
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1489
    val _ = Output.tracing (commas (map fst case_env))
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1490
    val lthy'' = ProofContext.add_cases true case_env lthy'
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1491
    
31550
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1492
    fun after_qed thms =
b63d253ed9e2 code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents: 31549
diff changeset
  1493
      LocalTheory.theory (fold set_elim (map the_single thms) #> add_equations const)
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1494
  in
31580
1c143f6a2226 added cases to code_pred command
bulwahn
parents: 31574
diff changeset
  1495
    Proof.theorem_i NONE after_qed (map (single o (rpair [])) cases_rules) lthy''
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1496
  end;
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1497
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1498
structure P = OuterParse
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1499
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1500
in
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1501
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1502
val code_pred = generic_code_pred (K I);
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31124
diff changeset
  1503
val code_pred_cmd = generic_code_pred Code.read_const
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1504
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1505
val setup = PredData.put (Graph.empty) #>
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1506
  Attrib.setup @{binding code_pred_intros} (Scan.succeed (attrib add_intro))
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1507
    "adding alternative introduction rules for code generation of inductive predicates"
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1508
(*  Attrib.setup @{binding code_ind_cases} (Scan.succeed add_elim_attrib)
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1509
    "adding alternative elimination rules for code generation of inductive predicates";
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1510
    *)
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1511
  (*FIXME name discrepancy in attribs and ML code*)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1512
  (*FIXME intros should be better named intro*)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1513
  (*FIXME why distinguished attribute for cases?*)
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1514
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1515
val _ = OuterSyntax.local_theory_to_proof "code_pred"
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1516
  "prove equations for predicate specified by intro/elim rules"
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1517
  OuterKeyword.thy_goal (P.term_group >> code_pred_cmd)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1518
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1519
end
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1520
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1521
(*FIXME
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1522
- Naming of auxiliary rules necessary?
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1523
- add default code equations P x y z = P_i_i_i x y z
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1524
*)
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1525
31169
f4c61cbea49d added predicate transformation function for code generation
bulwahn
parents: 31107
diff changeset
  1526
(* transformation for code generation *)
f4c61cbea49d added predicate transformation function for code generation
bulwahn
parents: 31107
diff changeset
  1527
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1528
val eval_ref = ref (NONE : (unit -> term Predicate.pred) option);
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1529
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1530
fun analyze_compr thy t_compr =
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1531
  let
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1532
    val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1533
      | _ => error ("Not a set comprehension: " ^ Syntax.string_of_term_global thy t_compr);
32287
65d5c5b30747 cleaned up abstract tuple operations and named them consistently
haftmann
parents: 32091
diff changeset
  1534
    val (body, Ts, fp) = HOLogic.strip_splits split;
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1535
      (*FIXME former order of tuple positions must be restored*)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1536
    val (pred as Const (name, T), all_args) = strip_comb body
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1537
    val (params, args) = chop (nparams_of thy name) all_args
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1538
    val user_mode = map_filter I (map_index
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1539
      (fn (i, t) => case t of Bound j => if j < length Ts then NONE
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1540
        else SOME (i+1) | _ => SOME (i+1)) args) (*FIXME dangling bounds should not occur*)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1541
    val (inargs, _) = get_args user_mode args;
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1542
    val modes = filter (fn Mode (_, is, _) => is = user_mode)
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1543
      (modes_of_term (all_modes_of thy) (list_comb (pred, params)));
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1544
    val m = case modes
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1545
     of [] => error ("No mode possible for comprehension "
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1546
                ^ Syntax.string_of_term_global thy t_compr)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1547
      | [m] => m
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1548
      | m :: _ :: _ => (warning ("Multiple modes possible for comprehension "
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1549
                ^ Syntax.string_of_term_global thy t_compr); m);
31514
fed8a95f54db refactoring the predicate compiler
bulwahn
parents: 31440
diff changeset
  1550
    val t_eval = list_comb (compile_expr thy (all_modes_of thy) (SOME m, list_comb (pred, params)),
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1551
      inargs)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1552
  in t_eval end;
31169
f4c61cbea49d added predicate transformation function for code generation
bulwahn
parents: 31107
diff changeset
  1553
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1554
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1555