src/HOL/Library/Code_Abstract_Nat.thy
author haftmann
Sat Jun 28 21:09:17 2014 +0200 (2014-06-28)
changeset 57427 91f9e4148460
parent 57426 2cd2ccd81f93
child 58881 b9556a055632
permissions -rw-r--r--
proper trading of variables;
more appropriate ML variable names
haftmann@51113
     1
(*  Title:      HOL/Library/Code_Abstract_Nat.thy
haftmann@51113
     2
    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
haftmann@51113
     3
*)
haftmann@51113
     4
haftmann@51113
     5
header {* Avoidance of pattern matching on natural numbers *}
haftmann@51113
     6
haftmann@51113
     7
theory Code_Abstract_Nat
haftmann@51113
     8
imports Main
haftmann@51113
     9
begin
haftmann@51113
    10
haftmann@51113
    11
text {*
haftmann@51113
    12
  When natural numbers are implemented in another than the
haftmann@51113
    13
  conventional inductive @{term "0::nat"}/@{term Suc} representation,
haftmann@51113
    14
  it is necessary to avoid all pattern matching on natural numbers
haftmann@51113
    15
  altogether.  This is accomplished by this theory (up to a certain
haftmann@51113
    16
  extent).
haftmann@51113
    17
*}
haftmann@51113
    18
haftmann@51113
    19
subsection {* Case analysis *}
haftmann@51113
    20
haftmann@51113
    21
text {*
haftmann@51113
    22
  Case analysis on natural numbers is rephrased using a conditional
haftmann@51113
    23
  expression:
haftmann@51113
    24
*}
haftmann@51113
    25
haftmann@51113
    26
lemma [code, code_unfold]:
blanchet@55415
    27
  "case_nat = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
haftmann@51113
    28
  by (auto simp add: fun_eq_iff dest!: gr0_implies_Suc)
haftmann@51113
    29
haftmann@51113
    30
haftmann@51113
    31
subsection {* Preprocessors *}
haftmann@51113
    32
haftmann@51113
    33
text {*
haftmann@51113
    34
  The term @{term "Suc n"} is no longer a valid pattern.  Therefore,
haftmann@51113
    35
  all occurrences of this term in a position where a pattern is
haftmann@51113
    36
  expected (i.e.~on the left-hand side of a code equation) must be
wenzelm@56790
    37
  eliminated.  This can be accomplished -- as far as possible -- by
haftmann@51113
    38
  applying the following transformation rule:
haftmann@51113
    39
*}
haftmann@51113
    40
haftmann@57426
    41
lemma Suc_if_eq:
haftmann@57426
    42
  assumes "\<And>n. f (Suc n) \<equiv> h n"
haftmann@57426
    43
  assumes "f 0 \<equiv> g"
haftmann@57426
    44
  shows "f n \<equiv> if n = 0 then g else h (n - 1)"
haftmann@57426
    45
  by (rule eq_reflection) (cases n, insert assms, simp_all)
haftmann@51113
    46
haftmann@51113
    47
text {*
haftmann@51113
    48
  The rule above is built into a preprocessor that is plugged into
haftmann@51113
    49
  the code generator.
haftmann@51113
    50
*}
haftmann@51113
    51
haftmann@51113
    52
setup {*
haftmann@51113
    53
let
haftmann@51113
    54
haftmann@57427
    55
val Suc_if_eq = Thm.incr_indexes 1 @{thm Suc_if_eq};
haftmann@57427
    56
haftmann@55757
    57
fun remove_suc ctxt thms =
haftmann@51113
    58
  let
haftmann@55757
    59
    val thy = Proof_Context.theory_of ctxt;
haftmann@51113
    60
    val vname = singleton (Name.variant_list (map fst
haftmann@51113
    61
      (fold (Term.add_var_names o Thm.full_prop_of) thms []))) "n";
haftmann@51113
    62
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
haftmann@57426
    63
    val lhs_of = snd o Thm.dest_comb o fst o Thm.dest_comb o cprop_of;
haftmann@57426
    64
    val rhs_of = snd o Thm.dest_comb o cprop_of;
haftmann@51113
    65
    fun find_vars ct = (case term_of ct of
haftmann@51113
    66
        (Const (@{const_name Suc}, _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
haftmann@51113
    67
      | _ $ _ =>
haftmann@51113
    68
        let val (ct1, ct2) = Thm.dest_comb ct
haftmann@51113
    69
        in 
haftmann@51113
    70
          map (apfst (fn ct => Thm.apply ct ct2)) (find_vars ct1) @
haftmann@51113
    71
          map (apfst (Thm.apply ct1)) (find_vars ct2)
haftmann@51113
    72
        end
haftmann@51113
    73
      | _ => []);
haftmann@51113
    74
    val eqs = maps
haftmann@57426
    75
      (fn thm => map (pair thm) (find_vars (lhs_of thm))) thms;
haftmann@57426
    76
    fun mk_thms (thm, (ct, cv')) =
haftmann@51113
    77
      let
haftmann@57426
    78
        val thm' =
haftmann@51113
    79
          Thm.implies_elim
haftmann@51113
    80
           (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@51113
    81
             (Drule.instantiate'
haftmann@51113
    82
               [SOME (ctyp_of_term ct)] [SOME (Thm.lambda cv ct),
haftmann@57426
    83
                 SOME (Thm.lambda cv' (rhs_of thm)), NONE, SOME cv']
haftmann@57427
    84
               Suc_if_eq)) (Thm.forall_intr cv' thm)
haftmann@51113
    85
      in
haftmann@57426
    86
        case map_filter (fn thm'' =>
haftmann@57426
    87
            SOME (thm'', singleton
haftmann@57426
    88
              (Variable.trade (K (fn [thm'''] => [thm''' RS thm']))
haftmann@57426
    89
                (Variable.global_thm_context thm'')) thm'')
haftmann@51113
    90
          handle THM _ => NONE) thms of
haftmann@51113
    91
            [] => NONE
haftmann@57426
    92
          | thmps =>
haftmann@57426
    93
              let val (thms1, thms2) = split_list thmps
haftmann@57426
    94
              in SOME (subtract Thm.eq_thm (thm :: thms1) thms @ thms2) end
haftmann@51113
    95
      end
haftmann@51113
    96
  in get_first mk_thms eqs end;
haftmann@51113
    97
haftmann@51113
    98
fun eqn_suc_base_preproc thy thms =
haftmann@51113
    99
  let
haftmann@51113
   100
    val dest = fst o Logic.dest_equals o prop_of;
haftmann@51113
   101
    val contains_suc = exists_Const (fn (c, _) => c = @{const_name Suc});
haftmann@51113
   102
  in
haftmann@51113
   103
    if forall (can dest) thms andalso exists (contains_suc o dest) thms
haftmann@51113
   104
      then thms |> perhaps_loop (remove_suc thy) |> (Option.map o map) Drule.zero_var_indexes
haftmann@51113
   105
       else NONE
haftmann@51113
   106
  end;
haftmann@51113
   107
haftmann@51113
   108
val eqn_suc_preproc = Code_Preproc.simple_functrans eqn_suc_base_preproc;
haftmann@51113
   109
haftmann@51113
   110
in
haftmann@51113
   111
haftmann@51113
   112
  Code_Preproc.add_functrans ("eqn_Suc", eqn_suc_preproc)
haftmann@51113
   113
haftmann@51113
   114
end;
haftmann@51113
   115
*}
haftmann@51113
   116
haftmann@51113
   117
end