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