CookBook/Package/simple_inductive_package.ML
author berghofe
Fri, 10 Oct 2008 17:13:21 +0200
changeset 32 5bb2d29553c2
child 55 0b55402ae95e
permissions -rw-r--r--
Added new chapter about writing packages.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     1
(* @chunk SIMPLE_INDUCTIVE_PACKAGE *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     2
signature SIMPLE_INDUCTIVE_PACKAGE =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     3
sig
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     4
  val add_inductive_i:
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     5
    ((Name.binding * typ) * mixfix) list ->  (*{predicates}*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     6
    (Name.binding * typ) list ->  (*{parameters}*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     7
    (Attrib.binding * term) list ->  (*{rules}*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     8
    local_theory -> (thm list * thm list) * local_theory
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     9
  val add_inductive:
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    10
    (Name.binding * string option * mixfix) list ->  (*{predicates}*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    11
    (Name.binding * string option * mixfix) list ->  (*{parameters}*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    12
    (Attrib.binding * string) list ->  (*{rules}*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    13
    local_theory -> (thm list * thm list) * local_theory
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    14
end;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    15
(* @end *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    16
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    17
structure SimpleInductivePackage: SIMPLE_INDUCTIVE_PACKAGE =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    18
struct
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    19
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    20
fun add_inductive_i preds_syn params intrs lthy =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    21
  let
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    22
    val params' = map (fn (p, T) => Free (Name.name_of p, T)) params;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    23
    val preds = map (fn ((R, T), _) =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    24
      list_comb (Free (Name.name_of R, T), params')) preds_syn;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    25
    val Tss = map (binder_types o fastype_of) preds;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    26
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    27
    (* making the definition *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    28
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    29
    val intrs' = map
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    30
      (ObjectLogic.atomize_term (ProofContext.theory_of lthy) o snd) intrs;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    31
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    32
    fun mk_all x P = HOLogic.all_const (fastype_of x) $ lambda x P;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    33
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    34
    val (defs, lthy1) = fold_map (fn ((((R, _), syn), pred), Ts) =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    35
      let val zs = map Free (Variable.variant_frees lthy intrs'
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    36
        (map (pair "z") Ts))
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    37
      in
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    38
        LocalTheory.define Thm.internalK
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    39
          ((R, syn), (Attrib.no_binding, fold_rev lambda (params' @ zs)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    40
            (fold_rev mk_all preds (fold_rev (curry HOLogic.mk_imp)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    41
               intrs' (list_comb (pred, zs)))))) #>> snd #>> snd
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    42
       end) (preds_syn ~~ preds ~~ Tss) lthy;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    43
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    44
    val (_, lthy2) = Variable.add_fixes (map (Name.name_of o fst) params) lthy1;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    45
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    46
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    47
    (* proving the induction rules *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    48
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    49
    val (Pnames, lthy3) =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    50
      Variable.variant_fixes (replicate (length preds) "P") lthy2;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    51
    val Ps = map (fn (s, Ts) => Free (s, Ts ---> HOLogic.boolT))
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    52
      (Pnames ~~ Tss);
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    53
    val cPs = map (cterm_of (ProofContext.theory_of lthy3)) Ps;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    54
    val intrs'' = map (subst_free (preds ~~ Ps) o snd) intrs;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    55
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    56
    fun inst_spec ct = Drule.instantiate'
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    57
      [SOME (ctyp_of_term ct)] [NONE, SOME ct] spec;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    58
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    59
    fun prove_indrule ((R, P), Ts) =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    60
      let
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    61
        val (znames, lthy4) =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    62
          Variable.variant_fixes (replicate (length Ts) "z") lthy3;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    63
        val zs = map Free (znames ~~ Ts)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    64
      in
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    65
        Goal.prove lthy4 []
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    66
          [HOLogic.mk_Trueprop (list_comb (R, zs))]
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    67
          (Logic.list_implies (intrs'',
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    68
             HOLogic.mk_Trueprop (list_comb (P, zs))))
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    69
          (fn {prems, ...} => EVERY
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    70
             ([ObjectLogic.full_atomize_tac 1,
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    71
               cut_facts_tac prems 1,
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    72
               rewrite_goals_tac defs] @
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    73
              map (fn ct => dtac (inst_spec ct) 1) cPs @
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    74
              [assume_tac 1])) |>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    75
        singleton (ProofContext.export lthy4 lthy1)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    76
      end;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    77
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    78
    val indrules = map prove_indrule (preds ~~ Ps ~~ Tss);
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    79
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    80
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    81
    (* proving the introduction rules *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    82
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    83
    val all_elims = fold (fn ct => fn th => th RS inst_spec ct);
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    84
    val imp_elims = fold (fn th => fn th' => [th', th] MRS mp);
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    85
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    86
    fun prove_intr (i, (_, r)) =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    87
      Goal.prove lthy2 [] [] r
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    88
        (fn {prems, context = ctxt} => EVERY
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    89
           [ObjectLogic.rulify_tac 1,
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    90
            rewrite_goals_tac defs,
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    91
            REPEAT (resolve_tac [allI, impI] 1),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    92
            SUBPROOF (fn {params, prems, context = ctxt', ...} =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    93
              let
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    94
                val (prems1, prems2) =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    95
                  chop (length prems - length intrs) prems;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    96
                val (params1, params2) =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    97
                  chop (length params - length preds) params
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    98
              in
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    99
                rtac (ObjectLogic.rulify
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   100
                  (all_elims params1 (nth prems2 i))) 1 THEN
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   101
                EVERY (map (fn prem =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   102
                  SUBPROOF (fn {prems = prems', concl, ...} =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   103
                    let
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   104
                      val prem' = prems' MRS prem;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   105
                      val prem'' = case prop_of prem' of
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   106
                          _ $ (Const (@{const_name All}, _) $ _) =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   107
                            prem' |> all_elims params2 |>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   108
                            imp_elims prems2
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   109
                        | _ => prem'
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   110
                    in rtac prem'' 1 end) ctxt' 1) prems1)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   111
              end) ctxt 1]) |>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   112
      singleton (ProofContext.export lthy2 lthy1);
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   113
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   114
    val intr_ths = map_index prove_intr intrs;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   115
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   116
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   117
    (* storing the theorems *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   118
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   119
    val mut_name = space_implode "_" (map (Name.name_of o fst o fst) preds_syn);
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   120
    val case_names = map (Name.name_of o fst o fst) intrs
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   121
  in
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   122
    lthy1 |>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   123
    LocalTheory.notes Thm.theoremK (map (fn (((a, atts), _), th) =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   124
      ((Name.qualified mut_name a, atts), [([th], [])]))
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   125
        (intrs ~~ intr_ths)) |->
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   126
    (fn intr_thss => LocalTheory.note Thm.theoremK
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   127
       ((Name.qualified mut_name (Name.binding "intros"), []), maps snd intr_thss)) |>>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   128
    snd ||>>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   129
    (LocalTheory.notes Thm.theoremK (map (fn (((R, _), _), th) =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   130
       ((Name.qualified (Name.name_of R) (Name.binding "induct"),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   131
         [Attrib.internal (K (RuleCases.case_names case_names)),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   132
          Attrib.internal (K (RuleCases.consumes 1)),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   133
          Attrib.internal (K (Induct.induct_pred ""))]), [([th], [])]))
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   134
         (preds_syn ~~ indrules)) #>> maps snd)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   135
  end;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   136
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   137
(* @chunk add_inductive *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   138
fun add_inductive preds_syn params_syn intro_srcs lthy =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   139
  let
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   140
    val ((vars, specs), _) = Specification.read_specification
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   141
      (preds_syn @ params_syn) (map (fn (a, s) => [(a, [s])]) intro_srcs)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   142
      lthy;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   143
    val (preds_syn', params_syn') = chop (length preds_syn) vars;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   144
    val intrs = map (apsnd the_single) specs
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   145
  in
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   146
    add_inductive_i preds_syn' (map fst params_syn') intrs lthy
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   147
  end;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   148
(* @end *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   149
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   150
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   151
(* outer syntax *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   152
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   153
(* @chunk syntax *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   154
local structure P = OuterParse and K = OuterKeyword in
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   155
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   156
val ind_decl =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   157
  P.opt_target --
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   158
  P.fixes -- P.for_fixes --
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   159
  Scan.optional (P.$$$ "where" |--
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   160
    P.!!! (P.enum1 "|" (SpecParse.opt_thm_name ":" -- P.prop))) [] >>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   161
  (fn (((loc, preds), params), specs) =>
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   162
    Toplevel.local_theory loc (add_inductive preds params specs #> snd));
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   163
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   164
val _ = OuterSyntax.command "simple_inductive" "define inductive predicates"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   165
  K.thy_decl ind_decl;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   166
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   167
end;
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   168
(* @end *)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   169
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   170
end;