src/HOL/ex/predicate_compile.ML
author haftmann
Thu, 04 Jun 2009 15:00:44 +0200
changeset 31440 ee1d5bec4ce3
parent 31320 72eeb1b4e006
child 31514 fed8a95f54db
permissions -rw-r--r--
made SML/NJ happy
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
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    10
  val prove_equation: string -> mode option -> theory -> theory
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    11
  val intro_rule: theory -> string -> mode -> thm
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    12
  val elim_rule: theory -> string -> mode -> thm
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    13
  val strip_intro_concl: term -> int -> term * (term list * term list)
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    14
  val modename_of: theory -> string -> mode -> string
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    15
  val modes_of: theory -> string -> mode list
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
    16
  val pred_intros: theory -> string -> thm list
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
    17
  val get_nparams: theory -> string -> int
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    18
  val setup: theory -> theory
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    19
  val code_pred: string -> Proof.context -> Proof.state
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    20
  val code_pred_cmd: string -> Proof.context -> Proof.state
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    21
  val print_alternative_rules: theory -> theory (*FIXME diagnostic command?*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    22
  val do_proofs: bool ref
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
    23
  val analyze_compr: theory -> term -> term
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
    24
  val eval_ref: (unit -> term Predicate.pred) option ref
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    25
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    26
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
    27
structure Predicate_Compile : PREDICATE_COMPILE =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    28
struct
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
    29
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    30
(** auxiliary **)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    31
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    32
(* debug stuff *)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    33
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
    34
fun makestring _ = "?";   (* FIXME dummy *)
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
    35
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    36
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
    37
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    38
fun print_tac s = (if ! Toplevel.debug then Tactical.print_tac s else Seq.single);
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    39
fun debug_tac msg = (fn st => (tracing msg; Seq.single st));
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
val do_proofs = ref true;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    42
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    43
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    44
(** fundamentals **)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    45
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    46
(* syntactic operations *)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    47
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    48
fun mk_eq (x, xs) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    49
  let fun mk_eqs _ [] = []
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    50
        | mk_eqs a (b::cs) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    51
            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
    52
  in mk_eqs x xs end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    53
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    54
fun mk_tupleT [] = HOLogic.unitT
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    55
  | mk_tupleT Ts = foldr1 HOLogic.mk_prodT Ts;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    56
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    57
fun mk_tuple [] = HOLogic.unit
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    58
  | mk_tuple ts = foldr1 HOLogic.mk_prod ts;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    59
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    60
fun dest_tuple (Const (@{const_name Product_Type.Unity}, _)) = []
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    61
  | 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
    62
  | dest_tuple t = [t]
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    63
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    64
fun mk_pred_enumT T = Type ("Predicate.pred", [T])
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    65
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    66
fun dest_pred_enumT (Type ("Predicate.pred", [T])) = T
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    67
  | dest_pred_enumT T = raise TYPE ("dest_pred_enumT", [T], []);
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    68
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    69
fun mk_Enum f =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    70
  let val T as Type ("fun", [T', _]) = fastype_of f
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    71
  in
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    72
    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
    73
  end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    74
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    75
fun mk_Eval (f, x) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    76
  let val T = fastype_of x
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    77
  in
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    78
    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
    79
  end;
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 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
    82
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    83
fun mk_single t =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    84
  let val T = fastype_of t
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    85
  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
    86
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    87
fun mk_bind (x, f) =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    88
  let val T as Type ("fun", [_, U]) = fastype_of f
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    89
  in
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    90
    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
    91
  end;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    92
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    93
val mk_sup = HOLogic.mk_binop @{const_name sup};
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    94
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    95
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
    96
  HOLogic.boolT --> mk_pred_enumT HOLogic.unitT) $ cond;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    97
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
    98
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
    99
  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
   100
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
(* data structures *)
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
type mode = int list option list * int list;
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   105
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   106
val mode_ord = prod_ord (list_ord (option_ord (list_ord int_ord))) (list_ord int_ord);
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   107
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   108
structure PredModetab = TableFun(
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   109
  type key = string * mode
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   110
  val ord = prod_ord fast_string_ord mode_ord
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   111
);
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   112
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   113
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   114
(*FIXME scrap boilerplate*)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   115
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   116
structure IndCodegenData = TheoryDataFun
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   117
(
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   118
  type T = {names : string PredModetab.table,
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   119
            modes : mode list Symtab.table,
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   120
            function_defs : Thm.thm Symtab.table,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   121
            function_intros : Thm.thm Symtab.table,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   122
            function_elims : Thm.thm Symtab.table,
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   123
            intro_rules : Thm.thm list Symtab.table,
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   124
            elim_rules : Thm.thm Symtab.table,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   125
            nparams : int Symtab.table
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   126
           }; (*FIXME: better group tables according to key*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   127
      (* names: map from inductive predicate and mode to function name (string).
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   128
         modes: map from inductive predicates to modes
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   129
         function_defs: map from function name to definition
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   130
         function_intros: map from function name to intro rule
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   131
         function_elims: map from function name to elim rule
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   132
         intro_rules: map from inductive predicate to alternative intro rules
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   133
         elim_rules: map from inductive predicate to alternative elimination rule
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   134
         nparams: map from const name to number of parameters (* assuming there exist intro and elimination rules *) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   135
       *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   136
  val empty = {names = PredModetab.empty,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   137
               modes = Symtab.empty,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   138
               function_defs = Symtab.empty,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   139
               function_intros = Symtab.empty,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   140
               function_elims = Symtab.empty,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   141
               intro_rules = Symtab.empty,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   142
               elim_rules = Symtab.empty,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   143
               nparams = Symtab.empty};
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   144
  val copy = I;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   145
  val extend = I;
31440
ee1d5bec4ce3 made SML/NJ happy
haftmann
parents: 31320
diff changeset
   146
  fun merge _ (r : T * T) = {names = PredModetab.merge (op =) (pairself #names r),
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   147
                   modes = Symtab.merge (op =) (pairself #modes r),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   148
                   function_defs = Symtab.merge Thm.eq_thm (pairself #function_defs r),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   149
                   function_intros = Symtab.merge Thm.eq_thm (pairself #function_intros r),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   150
                   function_elims = Symtab.merge Thm.eq_thm (pairself #function_elims r),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   151
                   intro_rules = Symtab.merge ((forall Thm.eq_thm) o (op ~~)) (pairself #intro_rules r),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   152
                   elim_rules = Symtab.merge Thm.eq_thm (pairself #elim_rules r),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   153
                   nparams = Symtab.merge (op =) (pairself #nparams r)};
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   154
);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   155
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   156
  fun map_names f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   157
    (fn x => {names = f (#names x), modes = #modes x, function_defs = #function_defs x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   158
            function_intros = #function_intros x, function_elims = #function_elims x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   159
            intro_rules = #intro_rules x, elim_rules = #elim_rules x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   160
            nparams = #nparams x}) thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   161
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   162
  fun map_modes f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   163
    (fn x => {names = #names x, modes = f (#modes x), function_defs = #function_defs x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   164
            function_intros = #function_intros x, function_elims = #function_elims x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   165
            intro_rules = #intro_rules x, elim_rules = #elim_rules x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   166
            nparams = #nparams x}) thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   167
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   168
  fun map_function_defs f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   169
    (fn x => {names = #names x, modes = #modes x, function_defs = f (#function_defs x),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   170
            function_intros = #function_intros x, function_elims = #function_elims x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   171
            intro_rules = #intro_rules x, elim_rules = #elim_rules x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   172
            nparams = #nparams x}) thy 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   173
  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   174
  fun map_function_elims f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   175
    (fn x => {names = #names x, modes = #modes x, function_defs = #function_defs x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   176
            function_intros = #function_intros x, function_elims = f (#function_elims x),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   177
            intro_rules = #intro_rules x, elim_rules = #elim_rules x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   178
            nparams = #nparams x}) thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   179
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   180
  fun map_function_intros f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   181
    (fn x => {names = #names x, modes = #modes x, function_defs = #function_defs x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   182
            function_intros = f (#function_intros x), function_elims = #function_elims x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   183
            intro_rules = #intro_rules x, elim_rules = #elim_rules x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   184
            nparams = #nparams x}) thy
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
  fun map_intro_rules f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   187
    (fn x => {names = #names x, modes = #modes x, function_defs = #function_defs x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   188
            function_intros = #function_intros x, function_elims = #function_elims x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   189
            intro_rules = f (#intro_rules x), elim_rules = #elim_rules x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   190
            nparams = #nparams x}) thy 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   191
  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   192
  fun map_elim_rules f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   193
    (fn x => {names = #names x, modes = #modes x, function_defs = #function_defs x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   194
            function_intros = #function_intros x, function_elims = #function_elims x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   195
            intro_rules = #intro_rules x, elim_rules = f (#elim_rules x),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   196
            nparams = #nparams x}) thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   197
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   198
  fun map_nparams f thy = IndCodegenData.map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   199
    (fn x => {names = #names x, modes = #modes x, function_defs = #function_defs x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   200
            function_intros = #function_intros x, function_elims = #function_elims x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   201
            intro_rules = #intro_rules x, elim_rules = #elim_rules x,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   202
            nparams = f (#nparams x)}) thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   203
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   204
(* removes first subgoal *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   205
fun mycheat_tac thy i st =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   206
  (Tactic.rtac (SkipProof.make_thm thy (Var (("A", 0), propT))) i) st
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   207
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   208
(* Lightweight mode analysis **********************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   209
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   210
(**************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   211
(* source code from old code generator ************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   212
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   213
(**** check if a term contains only constructor functions ****)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   214
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   215
fun is_constrt thy =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   216
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   217
    val cnstrs = flat (maps
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   218
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   219
      (Symtab.dest (DatatypePackage.get_datatypes thy)));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   220
    fun check t = (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   221
        (Free _, []) => true
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   222
      | (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
   223
            (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
   224
          | _ => false)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   225
      | _ => false)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   226
  in check end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   227
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   228
(**** check if a type is an equality type (i.e. doesn't contain fun)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   229
  FIXME this is only an approximation ****)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   230
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   231
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
   232
  | is_eqT _ = true;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   233
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   234
(**** mode inference ****)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   235
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   236
fun string_of_mode (iss, is) = space_implode " -> " (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   237
  (fn NONE => "X"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   238
    | SOME js => enclose "[" "]" (commas (map string_of_int js)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   239
       (iss @ [SOME is]));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   240
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   241
fun print_modes modes = tracing ("Inferred modes:\n" ^
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   242
  cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   243
    string_of_mode ms)) modes));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   244
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   245
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
   246
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
   247
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   248
(** collect all Frees in a term (with duplicates!) **)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   249
fun term_vTs tm =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   250
  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
   251
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   252
fun get_args is ts = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   253
  fun get_args' _ _ [] = ([], [])
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   254
    | 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
   255
        (get_args' is (i+1) ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   256
in get_args' is 1 ts end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   257
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   258
(*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
   259
fun merge xs [] = xs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   260
  | merge [] ys = ys
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   261
  | 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
   262
      else y::merge (x::xs) ys;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   263
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   264
fun subsets i j = if i <= j then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   265
       let val is = subsets (i+1) j
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   266
       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
   267
     else [[]];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   268
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   269
fun cprod ([], ys) = []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   270
  | 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
   271
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   272
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
   273
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   274
datatype hmode = Mode of mode * int list * hmode option list; (*FIXME don't understand
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   275
  why there is another mode type!?*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   276
31170
c6efe82fc652 added predicate transformation function for code generation
bulwahn
parents: 31156 31169
diff changeset
   277
fun modes_of_term modes t =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   278
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   279
    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
   280
    val default = [Mode (([], ks), ks, [])];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   281
    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
   282
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   283
          val (args1, args2) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   284
            if length args < length iss then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   285
              error ("Too few arguments for inductive predicate " ^ name)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   286
            else chop (length iss) args;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   287
          val k = length args2;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   288
          val prfx = 1 upto k
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   289
        in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   290
          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
   291
          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
   292
          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
   293
            (fn (NONE, _) => [NONE]
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   294
              | (SOME js, arg) => map SOME (filter
31170
c6efe82fc652 added predicate transformation function for code generation
bulwahn
parents: 31156 31169
diff changeset
   295
                  (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
   296
                    (iss ~~ args1)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   297
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   298
        end)) (AList.lookup op = modes name)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   299
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   300
  in (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   301
      (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
   302
    | (Var ((name, _), _), args) => the (mk_modes name args)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   303
    | (Free (name, _), args) => the (mk_modes name args)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   304
    | _ => default)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   305
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   306
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   307
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
   308
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   309
fun select_mode_prem thy modes vs ps =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   310
  find_first (is_some o snd) (ps ~~ map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   311
    (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
   312
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   313
            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
   314
            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
   315
            val vTs = maps term_vTs out_ts';
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   316
            val dupTs = map snd (duplicates (op =) vTs) @
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   317
              List.mapPartial (AList.lookup (op =) vTs) vs;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   318
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   319
            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
   320
            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
   321
            term_vs t subset vs andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   322
            forall is_eqT dupTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   323
          end)
31170
c6efe82fc652 added predicate transformation function for code generation
bulwahn
parents: 31156 31169
diff changeset
   324
            (modes_of_term modes t handle Option =>
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   325
               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
   326
      | Negprem (us, t) => find_first (fn Mode (_, is, _) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   327
            length us = length is andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   328
            terms_vs us subset vs andalso
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   329
            term_vs t subset vs)
31170
c6efe82fc652 added predicate transformation function for code generation
bulwahn
parents: 31156 31169
diff changeset
   330
            (modes_of_term modes t handle Option =>
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   331
               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
   332
      | 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
   333
          else NONE
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   334
      ) ps);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   335
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   336
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
   337
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   338
    val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   339
      (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
   340
        (param_vs ~~ iss); 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   341
    fun check_mode_prems vs [] = SOME vs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   342
      | 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
   343
          NONE => NONE
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   344
        | SOME (x, _) => check_mode_prems
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   345
            (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
   346
            (filter_out (equal x) ps))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   347
    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
   348
    val in_vs = terms_vs in_ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   349
    val concl_vs = terms_vs ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   350
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   351
    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
   352
    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
   353
    (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
   354
       NONE => false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   355
     | SOME vs => concl_vs subset vs)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   356
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   357
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   358
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
   359
  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
   360
  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
   361
    (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
   362
      ~1 => true
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   363
    | 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
   364
      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
   365
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   366
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   367
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
   368
  let val y = f x
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   369
  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
   370
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   371
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
   372
  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
   373
    (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
   374
      (fn NONE => [NONE]
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   375
        | SOME k' => map SOME (subsets 1 k')) ks),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   376
      subsets 1 k))) arities);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   377
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   378
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
(**** end of old source code *************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   381
(*****************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   382
(**** term construction ****)
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
(* 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
   385
(* 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
   386
fun funT_of T NONE = T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   387
  | funT_of T (SOME mode) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   388
     val Ts = binder_types T;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   389
     val (Us1, Us2) = get_args mode Ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   390
   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
   391
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   392
fun funT'_of (iss, is) T = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   393
    val Ts = binder_types T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   394
    val (paramTs, argTs) = chop (length iss) Ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   395
    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
   396
    val (inargTs, outargTs) = get_args is argTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   397
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   398
    (paramTs' @ inargTs) ---> (mk_pred_enumT (mk_tupleT outargTs))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   399
  end; 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   400
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   401
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   402
fun mk_v (names, vs) s T = (case AList.lookup (op =) vs s of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   403
      NONE => ((names, (s, [])::vs), Free (s, T))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   404
    | SOME xs =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   405
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   406
          val s' = Name.variant names s;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   407
          val v = Free (s', T)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   408
        in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   409
          ((s'::names, AList.update (op =) (s, v::xs) vs), v)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   410
        end);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   411
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   412
fun distinct_v (nvs, Free (s, T)) = mk_v nvs s T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   413
  | distinct_v (nvs, t $ u) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   414
      let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   415
        val (nvs', t') = distinct_v (nvs, t);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   416
        val (nvs'', u') = distinct_v (nvs', u);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   417
      in (nvs'', t' $ u') end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   418
  | distinct_v x = x;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   419
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   420
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
   421
  let 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   422
    val eqs'' = maps mk_eq eqs @ eqs'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   423
    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
   424
    val name = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   425
    val name' = Name.variant (name :: names) "y";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   426
    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
   427
    val U = fastype_of success_t;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   428
    val U' = dest_pred_enumT U;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   429
    val v = Free (name, T);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   430
    val v' = Free (name', T);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   431
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   432
    lambda v (fst (DatatypePackage.make_case
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   433
      (ProofContext.init thy) false [] v
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   434
      [(mk_tuple out_ts,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   435
        if null eqs'' then success_t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   436
        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
   437
          foldr1 HOLogic.mk_conj eqs'' $ success_t $
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   438
            mk_empty U'),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   439
       (v', mk_empty U')]))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   440
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   441
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   442
fun modename_of thy name mode = let
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   443
    val v = (PredModetab.lookup (#names (IndCodegenData.get thy)) (name, mode))
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   444
  in if (is_some v) then the v (*FIXME use case here*)
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   445
     else error ("fun modename_of - definition not found: name: " ^ name ^ " mode: " ^  (makestring mode))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   446
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   447
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   448
fun modes_of thy =
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   449
  these o Symtab.lookup ((#modes o IndCodegenData.get) thy);
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   450
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   451
(*FIXME function can be removed*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   452
fun mk_funcomp f t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   453
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   454
    val names = Term.add_free_names t [];
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   455
    val Ts = binder_types (fastype_of t);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   456
    val vs = map Free
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   457
      (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
   458
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   459
    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
   460
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   461
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   462
fun compile_param thy modes (NONE, t) = t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   463
  | compile_param thy modes (m as SOME (Mode ((iss, is'), is, ms)), t) = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   464
    val (f, args) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   465
    val (params, args') = chop (length ms) args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   466
    val params' = map (compile_param thy modes) (ms ~~ params)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   467
    val f' = case f of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   468
        Const (name, T) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   469
          if AList.defined op = modes name then
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   470
            Const (modename_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
   471
          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
   472
      | Free (name, T) => Free (name, funT_of T (SOME is'))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   473
    in list_comb (f', params' @ args') end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   474
  | compile_param _ _ _ = error "compile params"
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
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
   477
      (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   478
         (Const (name, T), params) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   479
           if AList.defined op = modes name then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   480
             let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   481
               val (Ts, Us) = get_args is
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   482
                 (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
   483
               val params' = map (compile_param thy modes) (ms ~~ params)
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   484
               val mode_id = modename_of thy name mode
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   485
             in list_comb (Const (mode_id, ((map fastype_of params') @ Ts) --->
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   486
               mk_pred_enumT (mk_tupleT Us)), params')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   487
             end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   488
           else error "not a valid inductive expression"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   489
       | (Free (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   490
         (*if name mem param_vs then *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   491
         (* Higher order mode call *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   492
         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
   493
         in list_comb (r, args) end)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   494
  | compile_expr _ _ _ = error "not a valid inductive expression"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   495
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   496
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   497
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
   498
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   499
    val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   500
      (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
   501
        (param_vs ~~ iss);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   502
    fun check_constrt ((names, eqs), t) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   503
      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
   504
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   505
          val s = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   506
          val v = Free (s, fastype_of t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   507
        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
   508
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   509
    val (in_ts, out_ts) = get_args is ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   510
    val ((all_vs', eqs), in_ts') =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   511
      (*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
   512
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   513
    fun compile_prems out_ts' vs names [] =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   514
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   515
            val ((names', eqs'), out_ts'') =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   516
              (*FIXME*) Library.foldl_map check_constrt ((names, []), out_ts');
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   517
            val (nvs, out_ts''') = (*FIXME*) Library.foldl_map distinct_v
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   518
              ((names', map (rpair []) vs), out_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
            compile_match thy (snd nvs) (eqs @ eqs') out_ts'''
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   521
              (mk_single (mk_tuple out_ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   522
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   523
      | compile_prems out_ts vs names ps =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   524
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   525
            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
   526
            val SOME (p, mode as SOME (Mode (_, js, _))) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   527
              select_mode_prem thy modes' vs' ps
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   528
            val ps' = filter_out (equal p) ps
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   529
            val ((names', eqs), out_ts') =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   530
              (*FIXME*) Library.foldl_map check_constrt ((names, []), out_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   531
            val (nvs, out_ts'') = (*FIXME*) Library.foldl_map distinct_v
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   532
              ((names', map (rpair []) vs), out_ts')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   533
            val (compiled_clause, rest) = case p of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   534
               Prem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   535
                 let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   536
                   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
   537
                   val u = list_comb (compile_expr thy modes (mode, t), in_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   538
                   val rest = compile_prems out_ts''' vs' (fst nvs) ps'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   539
                 in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   540
                   (u, rest)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   541
                 end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   542
             | Negprem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   543
                 let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   544
                   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
   545
                   val u = list_comb (compile_expr thy modes (mode, t), in_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   546
                   val rest = compile_prems out_ts''' vs' (fst nvs) ps'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   547
                 in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   548
                   (mk_not_pred u, rest)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   549
                 end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   550
             | Sidecond t =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   551
                 let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   552
                   val rest = compile_prems [] vs' (fst nvs) ps';
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   553
                 in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   554
                   (mk_if_predenum t, rest)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   555
                 end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   556
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   557
            compile_match thy (snd nvs) eqs out_ts'' 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   558
              (mk_bind (compiled_clause, rest))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   559
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   560
    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
   561
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   562
    mk_bind (mk_single inp, prem_t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   563
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   564
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   565
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
   566
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   567
    val Ts = binder_types T;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   568
    val (Ts1, Ts2) = chop (length param_vs) Ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   569
    val Ts1' = map2 funT_of Ts1 (fst mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   570
    val (Us1, Us2) = get_args (snd mode) Ts2;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   571
    val xnames = Name.variant_list param_vs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   572
      (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
   573
    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
   574
    val cl_ts =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   575
      map (fn cl => compile_clause thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   576
        all_vs param_vs modes mode cl (mk_tuple xs)) cls;
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   577
    val mode_id = modename_of thy s mode
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   578
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   579
    HOLogic.mk_Trueprop (HOLogic.mk_eq
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   580
      (list_comb (Const (mode_id, (Ts1' @ Us1) --->
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   581
           mk_pred_enumT (mk_tupleT Us2)),
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   582
         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
   583
       foldr1 mk_sup cl_ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   584
  end;
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_preds thy all_vs param_vs modes preds =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   587
  map (fn (s, (T, cls)) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   588
    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
   589
      ((the o AList.lookup (op =) modes) s)) preds;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   590
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   591
(* end of term construction ******************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   592
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   593
(* special setup for simpset *)                  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   594
val HOL_basic_ss' = HOL_basic_ss setSolver 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   595
  (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
   596
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   597
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   598
(* misc: constructing and proving tupleE rules ***********************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   599
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   600
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   601
(* Creating definitions of functional programs 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   602
   and proving intro and elim rules **********************************************) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   603
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   604
fun is_ind_pred thy c = 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   605
  (can (InductivePackage.the_inductive (ProofContext.init thy)) c) orelse
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   606
  (c mem_string (Symtab.keys (#intro_rules (IndCodegenData.get thy))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   607
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   608
fun get_name_of_ind_calls_of_clauses thy preds intrs =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   609
    fold Term.add_consts intrs [] |> map fst
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   610
    |> filter_out (member (op =) preds) |> filter (is_ind_pred thy)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   611
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   612
fun print_arities arities = tracing ("Arities:\n" ^
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   613
  cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   614
    space_implode " -> " (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   615
      (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
   616
        (ks @ [SOME k]))) arities));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   617
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   618
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
   619
  | 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
   620
  val Ts = binder_types T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   621
  val argnames = Name.variant_list names
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   622
        (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
   623
  val args = map Free (argnames ~~ Ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   624
  val (inargs, outargs) = get_args mode args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   625
  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
   626
  val t = fold_rev lambda args r 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   627
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   628
  (t, argnames @ names)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   629
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   630
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   631
fun create_intro_rule nparams mode defthm mode_id funT pred thy =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   632
let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   633
  val Ts = binder_types (fastype_of pred)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   634
  val funtrm = Const (mode_id, funT)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   635
  val argnames = Name.variant_list []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   636
        (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
   637
  val (Ts1, Ts2) = chop nparams Ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   638
  val Ts1' = map2 funT_of Ts1 (fst mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   639
  val args = map Free (argnames ~~ (Ts1' @ Ts2))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   640
  val (params, io_args) = chop nparams args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   641
  val (inargs, outargs) = get_args (snd mode) io_args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   642
  val (params', names) = fold_map mk_Eval_of ((params ~~ Ts1) ~~ (fst mode)) []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   643
  val predprop = HOLogic.mk_Trueprop (list_comb (pred, params' @ io_args))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   644
  val funargs = params @ inargs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   645
  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
   646
                  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
   647
  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
   648
                   mk_tuple outargs))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   649
  val introtrm = Logic.mk_implies (predprop, funpropI)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   650
  val simprules = [defthm, @{thm eval_pred},
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   651
                   @{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
   652
  val unfolddef_tac = (Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps simprules) 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   653
  val introthm = Goal.prove (ProofContext.init thy) (argnames @ ["y"]) [] introtrm (fn {...} => unfolddef_tac)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   654
  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
   655
  val elimtrm = Logic.list_implies ([funpropE, Logic.mk_implies (predprop, P)], P)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   656
  val elimthm = Goal.prove (ProofContext.init thy) (argnames @ ["y", "P"]) [] elimtrm (fn {...} => unfolddef_tac)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   657
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   658
  map_function_intros (Symtab.update_new (mode_id, introthm)) thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   659
  |> map_function_elims (Symtab.update_new (mode_id, elimthm))
30387
0affb9975547 NameSpace.base_name ~> Long_Name.base_name
haftmann
parents: 30379
diff changeset
   660
  |> PureThy.store_thm (Binding.name (Long_Name.base_name mode_id ^ "I"), introthm) |> snd
0affb9975547 NameSpace.base_name ~> Long_Name.base_name
haftmann
parents: 30379
diff changeset
   661
  |> PureThy.store_thm (Binding.name (Long_Name.base_name mode_id ^ "E"), elimthm)  |> snd
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   662
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   663
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   664
fun create_definitions preds nparams (name, modes) thy =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   665
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   666
    val _ = tracing "create definitions"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   667
    val T = AList.lookup (op =) preds name |> the
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   668
    fun create_definition mode thy = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   669
      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
   670
        else space_implode "_" (map string_of_int mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   671
      val HOmode = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   672
        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
   673
        in (fold string_of_HOmode (fst mode) "") end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   674
      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
   675
        ^ (string_of_mode (snd mode))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   676
      val Ts = binder_types T;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   677
      val (Ts1, Ts2) = chop nparams Ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   678
      val Ts1' = map2 funT_of Ts1 (fst mode)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   679
      val (Us1, Us2) = get_args (snd mode) Ts2;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   680
      val names = Name.variant_list []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   681
        (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
   682
      val xs = map Free (names ~~ (Ts1' @ Ts2));
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   683
      val (xparams, xargs) = chop nparams xs;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   684
      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
   685
      val (xins, xouts) = get_args (snd mode) xargs;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   686
      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
   687
       | mk_split_lambda [x] t = lambda x t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   688
       | mk_split_lambda xs t = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   689
         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
   690
           | 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
   691
         in mk_split_lambda' xs t end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   692
      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
   693
      val funT = (Ts1' @ Us1) ---> (mk_pred_enumT (mk_tupleT Us2))
30387
0affb9975547 NameSpace.base_name ~> Long_Name.base_name
haftmann
parents: 30379
diff changeset
   694
      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
   695
      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
   696
      val def = Logic.mk_equals (lhs, predterm)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   697
      val ([defthm], thy') = thy |>
30387
0affb9975547 NameSpace.base_name ~> Long_Name.base_name
haftmann
parents: 30379
diff changeset
   698
        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
   699
        PureThy.add_defs false [((Binding.name (Long_Name.base_name mode_id ^ "_def"), def), [])]
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   700
      in thy' |> map_names (PredModetab.update_new ((name, mode), mode_id))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   701
           |> map_function_defs (Symtab.update_new (mode_id, defthm))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   702
           |> create_intro_rule nparams mode defthm mode_id funT (Const (name, T))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   703
      end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   704
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   705
    fold create_definition modes thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   706
  end;
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
(**************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   709
(* Proving equivalence of term *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   710
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   711
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   712
fun intro_rule thy pred mode = modename_of thy pred mode
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   713
    |> Symtab.lookup (#function_intros (IndCodegenData.get thy)) |> the
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   714
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   715
fun elim_rule thy pred mode = modename_of thy pred mode
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   716
    |> Symtab.lookup (#function_elims (IndCodegenData.get thy)) |> the
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   717
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   718
fun pred_intros thy predname = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   719
    fun is_intro_of pred intro = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   720
      val const = fst (strip_comb (HOLogic.dest_Trueprop (concl_of intro)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   721
    in (fst (dest_Const const) = pred) end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   722
    val d = IndCodegenData.get thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   723
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   724
    if (Symtab.defined (#intro_rules d) predname) then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   725
      rev (Symtab.lookup_list (#intro_rules d) predname)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   726
    else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   727
      InductivePackage.the_inductive (ProofContext.init thy) predname
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   728
      |> snd |> #intrs |> filter (is_intro_of predname)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   729
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   730
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   731
fun function_definition thy pred mode =
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   732
  modename_of thy pred mode |> Symtab.lookup (#function_defs (IndCodegenData.get thy)) |> the
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   733
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   734
fun is_Type (Type _) = true
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   735
  | is_Type _ = false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   736
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   737
fun imp_prems_conv cv ct =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   738
  case Thm.term_of ct of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   739
    Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   740
  | _ => Conv.all_conv ct
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   741
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   742
fun Trueprop_conv cv ct =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   743
  case Thm.term_of ct of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   744
    Const ("Trueprop", _) $ _ => Conv.arg_conv cv ct  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   745
  | _ => error "Trueprop_conv"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   746
31105
95f66b234086 added general preprocessing of equality in predicates for code generation
bulwahn
parents: 30528
diff changeset
   747
fun preprocess_intro thy rule =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   748
  Conv.fconv_rule
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   749
    (imp_prems_conv
31105
95f66b234086 added general preprocessing of equality in predicates for code generation
bulwahn
parents: 30528
diff changeset
   750
      (Trueprop_conv (Conv.try_conv (Conv.rewr_conv (Thm.symmetric @{thm Predicate.eq_is_eq})))))
95f66b234086 added general preprocessing of equality in predicates for code generation
bulwahn
parents: 30528
diff changeset
   751
    (Thm.transfer thy rule)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   752
31105
95f66b234086 added general preprocessing of equality in predicates for code generation
bulwahn
parents: 30528
diff changeset
   753
fun preprocess_elim thy nargs elimrule = let
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   754
   fun replace_eqs (Const ("Trueprop", _) $ (Const ("op =", T) $ lhs $ rhs)) =
31105
95f66b234086 added general preprocessing of equality in predicates for code generation
bulwahn
parents: 30528
diff changeset
   755
      HOLogic.mk_Trueprop (Const (@{const_name Predicate.eq}, T) $ lhs $ rhs)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   756
    | replace_eqs t = t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   757
   fun preprocess_case t = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   758
     val params = Logic.strip_params t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   759
     val (assums1, assums2) = chop nargs (Logic.strip_assums_hyp t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   760
     val assums_hyp' = assums1 @ (map replace_eqs assums2)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   761
     in list_all (params, Logic.list_implies (assums_hyp', Logic.strip_assums_concl t)) end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   762
   val prems = Thm.prems_of elimrule
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   763
   val cases' = map preprocess_case (tl prems)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   764
   val elimrule' = Logic.list_implies ((hd prems) :: cases', Thm.concl_of elimrule)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   765
 in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   766
   Thm.equal_elim
31105
95f66b234086 added general preprocessing of equality in predicates for code generation
bulwahn
parents: 30528
diff changeset
   767
     (Thm.symmetric (Conv.implies_concl_conv (MetaSimplifier.rewrite true [@{thm eq_is_eq}])
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   768
        (cterm_of thy elimrule')))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   769
     elimrule
31105
95f66b234086 added general preprocessing of equality in predicates for code generation
bulwahn
parents: 30528
diff changeset
   770
 end;
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   771
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   772
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   773
(* 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
   774
(* which then consequently would be splitted *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   775
(* else false *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   776
fun is_constructor thy t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   777
  if (is_Type (fastype_of t)) then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   778
    (case DatatypePackage.get_datatype thy ((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
   779
      NONE => false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   780
    | SOME info => (let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   781
      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
   782
      val (c, _) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   783
      in (case c of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   784
        Const (name, _) => name mem_string constr_consts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   785
        | _ => false) end))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   786
  else false
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   787
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   788
(* MAJOR FIXME:  prove_params should be simple
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   789
 - different form of introrule for parameters ? *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   790
fun prove_param thy modes (NONE, t) = all_tac 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   791
  | prove_param 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
   792
    val  (f, args) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   793
    val (params, _) = chop (length ms) args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   794
    val f_tac = case f of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   795
        Const (name, T) => simp_tac (HOL_basic_ss addsimps 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   796
           @{thm eval_pred}::function_definition thy name mode::[]) 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   797
      | Free _ => all_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   798
  in  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   799
    print_tac "before simplification in prove_args:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   800
    THEN debug_tac ("mode" ^ (makestring mode))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   801
    THEN f_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   802
    THEN print_tac "after simplification in prove_args"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   803
    (* work with parameter arguments *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   804
    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
   805
    THEN (REPEAT_DETERM (atac 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   806
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   807
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   808
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
   809
  (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   810
    (Const (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   811
      if AList.defined op = modes name then (let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   812
          val introrule = intro_rule thy name mode
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   813
          (*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
   814
          val (pred, rargs) = strip_comb (HOLogic.dest_Trueprop
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   815
            (hd (Logic.strip_imp_prems (prop_of introrule))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   816
          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
   817
          val (_, args) = chop nparams rargs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   818
          val _ = tracing ("args: " ^ (makestring args))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   819
          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
   820
          val _ = tracing ("subst: " ^ (makestring subst))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   821
          val inst_introrule = Drule.cterm_instantiate subst introrule*)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   822
         (* the next line is old and probably wrong *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   823
          val (args1, args2) = chop (length ms) args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   824
          val _ = tracing ("premposition: " ^ (makestring premposition))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   825
        in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   826
        rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   827
        THEN print_tac "before intro rule:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   828
        THEN debug_tac ("mode" ^ (makestring mode))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   829
        THEN debug_tac (makestring introrule)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   830
        THEN debug_tac ("premposition: " ^ (makestring premposition))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   831
        (* for the right assumption in first position *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   832
        THEN rotate_tac premposition 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   833
        THEN rtac introrule 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   834
        THEN print_tac "after intro rule"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   835
        (* work with parameter arguments *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   836
        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
   837
        THEN (REPEAT_DETERM (atac 1)) end)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   838
      else error "Prove expr if case not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   839
    | _ => rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   840
           THEN atac 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   841
  | prove_expr _ _ _ _ =  error "Prove expr not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   842
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   843
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
   844
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   845
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
   846
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   847
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
   848
  fun get_case_rewrite t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   849
    if (is_constructor thy t) then let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   850
      val case_rewrites = (#case_rewrites (DatatypePackage.the_datatype thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   851
        ((fst o dest_Type o fastype_of) t)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   852
      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
   853
    else []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   854
  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
   855
(* 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
   856
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   857
  print_tac ("before prove_match rewriting: simprules = " ^ (makestring simprules))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   858
   (* make this simpset better! *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   859
  THEN asm_simp_tac (HOL_basic_ss' addsimps simprules) 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   860
  THEN print_tac "after prove_match:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   861
  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
   862
         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
   863
         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
   864
  THEN print_tac "after if simplification"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   865
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   866
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   867
(* 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
   868
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   869
fun prove_sidecond thy modes t = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   870
  val _ = tracing ("prove_sidecond:" ^ (makestring t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   871
  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
   872
    (f as Const (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   873
      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
   874
        else fold preds_of args nameTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   875
    | _ => nameTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   876
  val preds = preds_of t []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   877
  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   878
  val _ = tracing ("preds: " ^ (makestring preds))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   879
  val defs = map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   880
    (fn (pred, T) => function_definition thy pred ([], (1 upto (length (binder_types T)))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   881
      preds
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   882
  val _ = tracing ("defs: " ^ (makestring defs))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   883
in 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   884
   (* remove not_False_eq_True when simpset in prove_match is better *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   885
   simp_tac (HOL_basic_ss addsimps @{thm not_False_eq_True} :: @{thm eval_pred} :: defs) 1 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   886
   (* need better control here! *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   887
   THEN print_tac "after sidecond simplification"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   888
   end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   889
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   890
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
   891
  val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   892
   (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
   893
     (param_vs ~~ iss);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   894
  fun check_constrt ((names, eqs), t) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   895
      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
   896
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   897
          val s = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   898
          val v = Free (s, fastype_of t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   899
        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
   900
  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   901
  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
   902
  val ((all_vs', eqs), in_ts') =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   903
      (*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
   904
  fun prove_prems out_ts vs [] =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   905
    (prove_match thy out_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   906
    THEN asm_simp_tac HOL_basic_ss' 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   907
    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
   908
    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
   909
  | prove_prems out_ts vs rps =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   910
    let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   911
      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
   912
      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
   913
        select_mode_prem thy modes' vs' rps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   914
      val premposition = (find_index (equal p) ps) + nargs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   915
      val rps' = filter_out (equal p) rps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   916
      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
   917
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   918
            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
   919
            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
   920
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   921
            print_tac "before clause:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   922
            THEN asm_simp_tac HOL_basic_ss 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   923
            THEN print_tac "before prove_expr:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   924
            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
   925
            THEN print_tac "after prove_expr:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   926
            THEN rec_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   927
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   928
        | Negprem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   929
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   930
            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
   931
            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
   932
            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
   933
            val (_, params) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   934
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   935
            print_tac "before negated clause:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   936
            THEN rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   937
            THEN (if (is_some name) then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   938
                simp_tac (HOL_basic_ss addsimps [function_definition thy (the name) (iss, js)]) 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   939
                THEN rtac @{thm not_predI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   940
                THEN print_tac "after neg. intro rule"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   941
                THEN print_tac ("t = " ^ (makestring t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   942
                (* FIXME: work with parameter arguments *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   943
                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
   944
              else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   945
                rtac @{thm not_predI'} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   946
            THEN (REPEAT_DETERM (atac 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   947
            THEN rec_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   948
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   949
        | Sidecond t =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   950
         rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   951
         THEN rtac @{thm if_predI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   952
         THEN print_tac "before sidecond:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   953
         THEN prove_sidecond thy modes t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   954
         THEN print_tac "after sidecond:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   955
         THEN prove_prems [] vs' rps')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   956
    in (prove_match thy out_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   957
        THEN rest_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   958
    end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   959
  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
   960
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   961
  rtac @{thm bindI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   962
  THEN rtac @{thm singleI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   963
  THEN prems_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   964
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   965
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   966
fun select_sup 1 1 = []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   967
  | select_sup _ 1 = [rtac @{thm supI1}]
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   968
  | 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
   969
31284
4e87577544ae added remark to code
bulwahn
parents: 31217
diff changeset
   970
(* FIXME: This function relies on the derivation of an induction rule *)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   971
fun get_nparams thy s = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   972
    val _ = tracing ("get_nparams: " ^ s)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   973
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   974
  if Symtab.defined (#nparams (IndCodegenData.get thy)) s then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   975
    the (Symtab.lookup (#nparams (IndCodegenData.get thy)) s) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   976
  else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   977
    case try (InductivePackage.the_inductive (ProofContext.init thy)) s of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   978
      SOME info => info |> snd |> #raw_induct |> Thm.unvarify
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   979
        |> InductivePackage.params_of |> length
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   980
    | NONE => 0 (* default value *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   981
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   982
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   983
val ind_set_codegen_preproc = InductiveSetPackage.codegen_preproc;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   984
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   985
fun pred_elim thy predname =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   986
  if (Symtab.defined (#elim_rules (IndCodegenData.get thy)) predname) then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   987
    the (Symtab.lookup (#elim_rules (IndCodegenData.get thy)) predname)
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
    (let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   990
      val ind_result = InductivePackage.the_inductive (ProofContext.init thy) predname
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   991
      val index = find_index (fn s => s = predname) (#names (fst ind_result))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   992
    in nth (#elims (snd ind_result)) index end)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   993
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   994
fun prove_one_direction thy all_vs param_vs modes clauses ((pred, T), mode) = let
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
   995
  val elim_rule = the (Symtab.lookup (#function_elims (IndCodegenData.get thy)) (modename_of thy pred mode))
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   996
(*  val ind_result = InductivePackage.the_inductive (ProofContext.init thy) pred
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   997
  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
   998
  val (_, T) = dest_Const (nth (#preds (snd ind_result)) index) *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
   999
  val nargs = length (binder_types T) - get_nparams thy pred
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1000
  val pred_case_rule = singleton (ind_set_codegen_preproc thy)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1001
    (preprocess_elim thy nargs (pred_elim thy pred))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1002
  (* FIXME preprocessor |> 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
  1003
  val _ = tracing ("pred_case_rule " ^ (makestring pred_case_rule))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1004
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1005
  REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"}))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1006
  THEN etac elim_rule 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1007
  THEN etac pred_case_rule 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1008
  THEN (EVERY (map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1009
         (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
  1010
           (1 upto (length clauses))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1011
  THEN (EVERY (map (prove_clause thy nargs all_vs param_vs modes mode) clauses))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1012
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1013
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1014
(*******************************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1015
(* Proof in the other direction ************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1016
(*******************************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1017
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1018
fun prove_match2 thy out_ts = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1019
  fun split_term_tac (Free _) = all_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1020
    | split_term_tac t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1021
      if (is_constructor thy t) then let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1022
        val info = DatatypePackage.the_datatype thy ((fst o dest_Type o fastype_of) t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1023
        val num_of_constrs = length (#case_rewrites info)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1024
        (* special treatment of pairs -- because of fishing *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1025
        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
  1026
          "*" => [@{thm prod.split_asm}] 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1027
          | _ => 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
  1028
        val (_, ts) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1029
      in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1030
        print_tac ("splitting with t = " ^ (makestring t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1031
        THEN (Splitter.split_asm_tac split_rules 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1032
(*        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
  1033
          THEN (DETERM (TRY (etac @{thm Pair_inject} 1))) *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1034
        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
  1035
        THEN (EVERY (map split_term_tac ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1036
      end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1037
    else all_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1038
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1039
    split_term_tac (mk_tuple out_ts)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1040
    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
  1041
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1042
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1043
(* VERY LARGE SIMILIRATIY to function prove_param 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1044
-- join both functions
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1045
*) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1046
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
  1047
  | 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
  1048
    val  (f, args) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1049
    val (params, _) = chop (length ms) args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1050
    val f_tac = case f of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1051
        Const (name, T) => full_simp_tac (HOL_basic_ss addsimps 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1052
           @{thm eval_pred}::function_definition thy name mode::[]) 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1053
      | Free _ => all_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1054
  in  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1055
    print_tac "before simplification in prove_args:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1056
    THEN debug_tac ("function : " ^ (makestring f) ^ " - mode" ^ (makestring mode))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1057
    THEN f_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1058
    THEN print_tac "after simplification in prove_args"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1059
    (* work with parameter arguments *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1060
    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
  1061
  end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1062
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1063
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
  1064
  (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1065
    (Const (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1066
      if AList.defined op = modes name then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1067
        etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1068
        THEN (REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"})))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1069
        THEN (etac (elim_rule thy name mode) 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1070
        THEN (EVERY (map (prove_param2 thy modes) (ms ~~ args)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1071
      else error "Prove expr2 if case not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1072
    | _ => etac @{thm bindE} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1073
  | prove_expr2 _ _ _ = error "Prove expr2 not implemented"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1074
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1075
fun prove_sidecond2 thy modes t = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1076
  val _ = tracing ("prove_sidecond:" ^ (makestring t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1077
  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
  1078
    (f as Const (name, T), args) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1079
      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
  1080
        else fold preds_of args nameTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1081
    | _ => nameTs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1082
  val preds = preds_of t []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1083
  val _ = tracing ("preds: " ^ (makestring preds))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1084
  val defs = map
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1085
    (fn (pred, T) => function_definition thy pred ([], (1 upto (length (binder_types T)))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1086
      preds
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
   (* only simplify the one assumption *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1089
   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
  1090
   (* need better control here! *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1091
   THEN print_tac "after sidecond2 simplification"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1092
   end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1093
  
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1094
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
  1095
  val modes' = modes @ List.mapPartial
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1096
   (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
  1097
     (param_vs ~~ iss);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1098
  fun check_constrt ((names, eqs), t) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1099
      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
  1100
        let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1101
          val s = Name.variant names "x";
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1102
          val v = Free (s, fastype_of t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1103
        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
  1104
  val pred_intro_rule = nth (pred_intros thy pred) (i - 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1105
    |> preprocess_intro thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1106
    |> (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
  1107
    (* 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
  1108
  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
  1109
  val ((all_vs', eqs), in_ts') =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1110
      (*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
  1111
  fun prove_prems2 out_ts vs [] =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1112
    print_tac "before prove_match2 - last call:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1113
    THEN prove_match2 thy out_ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1114
    THEN print_tac "after prove_match2 - last call:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1115
    THEN (etac @{thm singleE} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1116
    THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1117
    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
  1118
    THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1119
    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
  1120
    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
  1121
      THEN (rtac pred_intro_rule 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1122
      (* How to handle equality correctly? *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1123
      THEN (print_tac "state before assumption matching")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1124
      THEN (REPEAT (atac 1 ORELSE 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1125
         (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
  1126
          THEN print_tac "state after simp_tac:"))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1127
  | prove_prems2 out_ts vs ps = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1128
      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
  1129
      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
  1130
        select_mode_prem thy modes' vs' ps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1131
      val ps' = filter_out (equal p) ps;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1132
      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
  1133
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1134
            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
  1135
            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
  1136
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1137
            (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
  1138
          end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1139
        | Negprem (us, t) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1140
          let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1141
            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
  1142
            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
  1143
            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
  1144
            val (_, params) = strip_comb t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1145
          in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1146
            print_tac "before neg prem 2"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1147
            THEN etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1148
            THEN (if is_some name then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1149
                full_simp_tac (HOL_basic_ss addsimps [function_definition thy (the name) (iss, js)]) 1 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1150
                THEN etac @{thm not_predE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1151
                THEN (EVERY (map (prove_param2 thy modes) (param_modes ~~ params)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1152
              else
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1153
                etac @{thm not_predE'} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1154
            THEN rec_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1155
          end 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1156
        | Sidecond t =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1157
            etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1158
            THEN etac @{thm if_predE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1159
            THEN prove_sidecond2 thy modes t 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1160
            THEN prove_prems2 [] vs' ps')
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1161
    in print_tac "before prove_match2:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1162
       THEN prove_match2 thy out_ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1163
       THEN print_tac "after prove_match2:"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1164
       THEN rest_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1165
    end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1166
  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
  1167
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1168
  print_tac "starting prove_clause2"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1169
  THEN etac @{thm bindE} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1170
  THEN (etac @{thm singleE'} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1171
  THEN (TRY (etac @{thm Pair_inject} 1))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1172
  THEN print_tac "after singleE':"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1173
  THEN prems_tac
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1174
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1175
 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1176
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
  1177
  fun prove_clause (clause, i) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1178
    (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
  1179
    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
  1180
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1181
  (DETERM (TRY (rtac @{thm unit.induct} 1)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1182
   THEN (REPEAT_DETERM (CHANGED (rewtac @{thm split_paired_all})))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1183
   THEN (rtac (intro_rule thy pred mode) 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1184
   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
  1185
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1186
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1187
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
  1188
  val ctxt = ProofContext.init thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1189
  val clauses' = the (AList.lookup (op =) clauses pred)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1190
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1191
  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
  1192
    (if !do_proofs then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1193
      (fn _ =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1194
      rtac @{thm pred_iffI} 1
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1195
      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
  1196
      THEN print_tac "proved one direction"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1197
      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
  1198
      THEN print_tac "proved other direction")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1199
     else (fn _ => mycheat_tac thy 1))
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_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
  1203
  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
  1204
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1205
(* look for other place where this functionality was used before *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1206
fun strip_intro_concl intro nparams = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1207
  val _ $ u = Logic.strip_imp_concl intro
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1208
  val (pred, all_args) = strip_comb u
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1209
  val (params, args) = chop nparams all_args
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1210
in (pred, (params, args)) end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1211
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1212
(* setup for alternative introduction and elimination rules *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1213
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1214
fun add_intro_thm thm thy = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1215
   val (pred, _) = dest_Const (fst (strip_intro_concl (prop_of thm) 0))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1216
 in map_intro_rules (Symtab.insert_list Thm.eq_thm (pred, thm)) thy end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1217
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1218
fun add_elim_thm thm thy = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1219
    val (pred, _) = dest_Const (fst 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1220
      (strip_comb (HOLogic.dest_Trueprop (hd (prems_of thm)))))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1221
  in map_elim_rules (Symtab.update (pred, thm)) thy end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1222
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1223
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1224
(* special case: inductive predicate with no clauses *)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1225
fun noclause (predname, T) thy = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1226
  val Ts = binder_types T
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1227
  val names = Name.variant_list []
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1228
        (map (fn i => "x" ^ (string_of_int i)) (1 upto (length Ts)))
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1229
  val vs = map2 (curry Free) names Ts
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1230
  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
  1231
  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
  1232
  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
  1233
  val elim_t = Logic.list_implies ([clausehd, Logic.mk_implies (@{prop False}, P)], P)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1234
  val intro_thm = Goal.prove (ProofContext.init thy) names [] intro_t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1235
        (fn {...} => etac @{thm FalseE} 1)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1236
  val elim_thm = Goal.prove (ProofContext.init thy) ("P" :: names) [] elim_t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1237
        (fn {...} => etac (pred_elim thy predname) 1) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1238
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1239
  add_intro_thm intro_thm thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1240
  |> add_elim_thm elim_thm
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1241
end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1242
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1243
(*************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1244
(* main function *********************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1245
(*************************************************************************************)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1246
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1247
fun prove_equation ind_name mode thy =
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1248
let
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1249
  val _ = tracing ("starting prove_equation' with " ^ ind_name)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1250
  val (prednames, preds) = 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1251
    case (try (InductivePackage.the_inductive (ProofContext.init thy)) ind_name) of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1252
      SOME info => let val preds = info |> snd |> #preds
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1253
        in (map (fst o dest_Const) preds, map ((apsnd Logic.unvarifyT) o dest_Const) preds) end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1254
    | NONE => let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1255
        val pred = Symtab.lookup (#intro_rules (IndCodegenData.get thy)) ind_name
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1256
          |> the |> hd |> prop_of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1257
          |> Logic.strip_imp_concl |> HOLogic.dest_Trueprop |> strip_comb
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1258
          |> fst |>  dest_Const |> apsnd Logic.unvarifyT
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1259
       in ([ind_name], [pred]) end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1260
  val thy' = fold (fn pred as (predname, T) => fn thy =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1261
    if null (pred_intros thy predname) then noclause pred thy else thy) preds thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1262
  val intrs = map (preprocess_intro thy') (maps (pred_intros thy') prednames)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1263
    |> ind_set_codegen_preproc thy' (*FIXME preprocessor
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1264
    |> map (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
  1265
    |> map (Logic.unvarify o prop_of)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1266
  val _ = tracing ("preprocessed intro rules:" ^ (makestring (map (cterm_of thy') intrs)))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1267
  val name_of_calls = get_name_of_ind_calls_of_clauses thy' prednames intrs 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1268
  val _ = tracing ("calling preds: " ^ makestring name_of_calls)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1269
  val _ = tracing "starting recursive compilations"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1270
  fun rec_call name thy = 
30972
5b65835ccc92 some experiements towards user interface for predicate compiler
haftmann
parents: 30528
diff changeset
  1271
    (*FIXME use member instead of infix mem*)
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1272
    if not (name mem (Symtab.keys (#modes (IndCodegenData.get thy)))) then
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1273
      prove_equation name NONE thy else thy
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1274
  val thy'' = fold rec_call name_of_calls thy'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1275
  val _ = tracing "returning from recursive calls"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1276
  val _ = tracing "starting mode inference"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1277
  val extra_modes = Symtab.dest (#modes (IndCodegenData.get thy''))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1278
  val nparams = get_nparams thy'' ind_name
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1279
  val _ $ u = Logic.strip_imp_concl (hd intrs);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1280
  val params = List.take (snd (strip_comb u), nparams);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1281
  val param_vs = maps term_vs params
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1282
  val all_vs = terms_vs intrs
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1283
  fun dest_prem t =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1284
      (case strip_comb t of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1285
        (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
  1286
      | (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
  1287
          Prem (ts, t) => Negprem (ts, t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1288
        | Negprem _ => error ("Double negation not allowed in premise: " ^ (makestring (c $ t))) 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1289
        | Sidecond t => Sidecond (c $ t))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1290
      | (c as Const (s, _), ts) =>
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1291
        if is_ind_pred thy'' s then
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1292
          let val (ts1, ts2) = chop (get_nparams thy'' s) ts
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1293
          in Prem (ts2, list_comb (c, ts1)) end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1294
        else Sidecond t
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1295
      | _ => Sidecond t)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1296
  fun add_clause intr (clauses, arities) =
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1297
  let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1298
    val _ $ t = Logic.strip_imp_concl intr;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1299
    val (Const (name, T), ts) = strip_comb t;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1300
    val (ts1, ts2) = chop nparams ts;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1301
    val prems = map (dest_prem o HOLogic.dest_Trueprop) (Logic.strip_imp_prems intr);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1302
    val (Ts, Us) = chop nparams (binder_types T)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1303
  in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1304
    (AList.update op = (name, these (AList.lookup op = clauses name) @
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1305
      [(ts2, prems)]) clauses,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1306
     AList.update op = (name, (map (fn U => (case strip_type U of
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1307
                 (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1308
               | _ => NONE)) Ts,
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1309
             length Us)) arities)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1310
  end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1311
  val (clauses, arities) = fold add_clause intrs ([], []);
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1312
  val modes = infer_modes thy'' extra_modes arities param_vs clauses
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1313
  val _ = print_arities arities;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1314
  val _ = print_modes modes;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1315
  val modes = if (is_some mode) then AList.update (op =) (ind_name, [the mode]) modes else modes
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1316
  val _ = print_modes modes
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1317
  val thy''' = fold (create_definitions preds nparams) modes thy''
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1318
    |> map_modes (fold Symtab.update_new modes)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1319
  val clauses' = map (fn (s, cls) => (s, (the (AList.lookup (op =) preds s), cls))) clauses
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1320
  val _ = tracing "compiling predicates..."
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1321
  val ts = compile_preds thy''' all_vs param_vs (extra_modes @ modes) clauses'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1322
  val _ = tracing "returned term from compile_preds"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1323
  val pred_mode = maps (fn (s, (T, _)) => map (pair (s, T)) ((the o AList.lookup (op =) modes) s)) clauses'
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1324
  val _ = tracing "starting proof"
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1325
  val result_thms = prove_preds thy''' all_vs param_vs (extra_modes @ modes) clauses (pred_mode ~~ (flat ts))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1326
  val (_, thy'''') = yield_singleton PureThy.add_thmss
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1327
    ((Binding.qualify true (Long_Name.base_name ind_name) (Binding.name "equation"), result_thms),
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1328
      [Attrib.attribute_i thy''' Code.add_default_eqn_attrib]) thy'''
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1329
in
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1330
  thy''''
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
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1333
fun set_nparams (pred, nparams) thy = map_nparams (Symtab.update (pred, nparams)) thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1334
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1335
fun print_alternative_rules thy = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1336
    val d = IndCodegenData.get thy
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1337
    val preds = (Symtab.keys (#intro_rules d)) union (Symtab.keys (#elim_rules d))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1338
    val _ = tracing ("preds: " ^ (makestring preds))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1339
    fun print pred = let
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1340
      val _ = tracing ("predicate: " ^ pred)
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1341
      val _ = tracing ("introrules: ")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1342
      val _ = fold (fn thm => fn u => tracing (makestring thm))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1343
        (rev (Symtab.lookup_list (#intro_rules d) pred)) ()
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1344
      val _ = tracing ("casesrule: ")
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1345
      val _ = tracing (makestring (Symtab.lookup (#elim_rules d) pred))
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1346
    in () end
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1347
    val _ = map print preds
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1348
 in thy end; 
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1349
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1350
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1351
(* generation of case rules from user-given introduction rules *)
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1352
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1353
fun mk_casesrule introrules nparams ctxt =
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1354
  let
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1355
    val intros = map prop_of introrules
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1356
    val (pred, (params, args)) = strip_intro_concl (hd intros) nparams
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1357
    val ([propname], ctxt1) = Variable.variant_fixes ["thesis"] ctxt
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1358
    val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT))
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1359
    val (argnames, ctxt2) = Variable.variant_fixes
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1360
      (map (fn i => "a" ^ string_of_int i) (1 upto (length args))) ctxt1
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1361
    val argvs = map Free (argnames ~~ (map fastype_of args))
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1362
      (*FIXME map2*)
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1363
    fun mk_case intro = let
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1364
        val (_, (_, args)) = strip_intro_concl intro nparams
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1365
        val prems = Logic.strip_imp_prems intro
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1366
        val eqprems = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (argvs ~~ args)
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1367
        val frees = (fold o fold_aterms)
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1368
          (fn t as Free _ =>
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1369
              if member (op aconv) params t then I else insert (op aconv) t
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1370
           | _ => I) (args @ prems) []
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1371
        in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1372
    val assm = HOLogic.mk_Trueprop (list_comb (pred, params @ argvs))
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1373
    val cases = map mk_case intros
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1374
    val (_, ctxt3) = ProofContext.add_assms_i Assumption.assume_export
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1375
              [((Binding.name AutoBind.assmsN, []), map (fn t => (t, [])) (assm :: cases))]
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1376
              ctxt2
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1377
  in (pred, prop, ctxt3) end;
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1378
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1379
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1380
(** user interface **)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1381
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1382
local
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1383
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1384
fun attrib f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1385
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1386
val add_elim_attrib = attrib add_elim_thm;
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1387
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1388
fun generic_code_pred prep_const raw_const lthy =
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1389
  let
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1390
    val thy = ProofContext.theory_of lthy
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1391
    val const = prep_const thy raw_const
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1392
    val nparams = get_nparams thy const
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1393
    val intro_rules = pred_intros thy const
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1394
    val (((tfrees, frees), fact), lthy') =
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1395
      Variable.import_thms true intro_rules lthy;
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1396
    val (pred, prop, lthy'') = mk_casesrule fact nparams lthy'
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1397
    val (predname, _) = dest_Const pred
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1398
    fun after_qed [[th]] lthy'' =
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1399
      lthy''
31177
c39994cb152a introduced Thm.generatedK
haftmann
parents: 31174
diff changeset
  1400
      |> LocalTheory.note Thm.generatedK
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1401
           ((Binding.empty, [Attrib.internal (K add_elim_attrib)]), [th])
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1402
      |> snd
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1403
      |> LocalTheory.theory (prove_equation predname NONE)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1404
  in
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1405
    Proof.theorem_i NONE after_qed [[(prop, [])]] lthy''
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1406
  end;
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1407
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1408
structure P = OuterParse
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1409
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1410
in
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1411
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1412
val code_pred = generic_code_pred (K I);
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31124
diff changeset
  1413
val code_pred_cmd = generic_code_pred Code.read_const
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1414
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1415
val setup =
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1416
  Attrib.setup @{binding code_ind_intros} (Scan.succeed (attrib add_intro_thm))
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1417
    "adding alternative introduction rules for code generation of inductive predicates" #>
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1418
  Attrib.setup @{binding code_ind_cases} (Scan.succeed add_elim_attrib)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1419
    "adding alternative elimination rules for code generation of inductive predicates";
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1420
  (*FIXME name discrepancy in attribs and ML code*)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1421
  (*FIXME intros should be better named intro*)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1422
  (*FIXME why distinguished atribute for cases?*)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1423
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1424
val _ = OuterSyntax.local_theory_to_proof "code_pred"
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1425
  "prove equations for predicate specified by intro/elim rules"
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1426
  OuterKeyword.thy_goal (P.term_group >> code_pred_cmd)
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1427
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1428
end
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1429
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1430
(*FIXME
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1431
- Naming of auxiliary rules necessary?
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1432
- add default code equations P x y z = P_i_i_i x y z
31124
58bc773c60e2 marginally tuned
haftmann
parents: 31111
diff changeset
  1433
*)
31106
9a1178204dc0 Added pred_code command
bulwahn
parents: 31105
diff changeset
  1434
31169
f4c61cbea49d added predicate transformation function for code generation
bulwahn
parents: 31107
diff changeset
  1435
(* transformation for code generation *)
f4c61cbea49d added predicate transformation function for code generation
bulwahn
parents: 31107
diff changeset
  1436
31217
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1437
val eval_ref = ref (NONE : (unit -> term Predicate.pred) option);
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1438
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1439
fun analyze_compr thy t_compr =
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1440
  let
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1441
    val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1442
      | _ => error ("Not a set comprehension: " ^ Syntax.string_of_term_global thy t_compr);
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1443
    val (body, Ts, fp) = HOLogic.strip_split split;
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1444
      (*FIXME former order of tuple positions must be restored*)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1445
    val (pred as Const (name, T), all_args) = strip_comb body
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1446
    val (params, args) = chop (get_nparams thy name) all_args
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1447
    val user_mode = map_filter I (map_index
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1448
      (fn (i, t) => case t of Bound j => if j < length Ts then NONE
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1449
        else SOME (i+1) | _ => SOME (i+1)) args) (*FIXME dangling bounds should not occur*)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1450
    val (inargs, _) = get_args user_mode args;
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1451
    val all_modes = Symtab.dest (#modes (IndCodegenData.get thy));
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1452
    val modes = filter (fn Mode (_, is, _) => is = user_mode)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1453
      (modes_of_term all_modes (list_comb (pred, params)));
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1454
    val m = case modes
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1455
     of [] => error ("No mode possible for comprehension "
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1456
                ^ Syntax.string_of_term_global thy t_compr)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1457
      | [m] => m
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1458
      | m :: _ :: _ => (warning ("Multiple modes possible for comprehension "
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1459
                ^ Syntax.string_of_term_global thy t_compr); m);
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1460
    val t_eval = list_comb (compile_expr thy all_modes (SOME m, list_comb (pred, params)),
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1461
      inargs)
c025f32afd4e experimental values command
haftmann
parents: 31177
diff changeset
  1462
  in t_eval end;
31169
f4c61cbea49d added predicate transformation function for code generation
bulwahn
parents: 31107
diff changeset
  1463
30374
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1464
end;
7311a1546d85 added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff changeset
  1465