src/HOL/ex/Code_Antiq.thy
author wenzelm
Wed, 17 Sep 2008 21:27:14 +0200
changeset 28263 69eaa97e7e96
parent 27436 9581777503e9
permissions -rw-r--r--
moved global ML bindings to global place;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27436
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/ex/Code_Antiq.thy
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     2
    ID:         $Id$
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     4
*)
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     5
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     6
header {* A simple certificate check as toy example for the code antiquotation *}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     7
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     8
theory Code_Antiq
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
     9
imports Plain
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    10
begin
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    11
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    12
lemma div_cert1:
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    13
  fixes n m q r :: nat
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    14
  assumes "r < m"
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    15
    and "0 < m"
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    16
    and "n = m * q + r"
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    17
  shows "n div m = q"
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    18
using assms by (simp add: div_mult_self2 add_commute)
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    19
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    20
lemma div_cert2:
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    21
  fixes n :: nat
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    22
  shows "n div 0 = 0"
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    23
by simp
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    24
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    25
ML {*
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    26
local
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    27
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    28
fun code_of_val k = if k <= 0 then @{code "0::nat"}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    29
  else @{code Suc} (code_of_val (k - 1));
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    30
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    31
fun val_of_code @{code "0::nat"} = 0
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    32
  | val_of_code (@{code Suc} n) = val_of_code n + 1;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    33
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    34
val term_of_code = HOLogic.mk_nat o val_of_code;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    35
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    36
infix 9 &$;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    37
val op &$ = uncurry Thm.capply;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    38
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    39
val simpset = HOL_ss addsimps
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    40
  @{thms plus_nat.simps add_0_right add_Suc_right times_nat.simps mult_0_right mult_Suc_right  less_nat_zero_code le_simps(2) less_eq_nat.simps(1) le_simps(3)}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    41
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    42
fun prove prop = Goal.prove_internal [] (@{cterm Trueprop} &$ prop)
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    43
  (K (ALLGOALS (Simplifier.simp_tac simpset))); (*FIXME*)
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    44
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    45
in
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    46
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    47
fun simp_div ctxt ct_n ct_m =
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    48
  let
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    49
    val m = HOLogic.dest_nat (Thm.term_of ct_m);
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    50
  in if m = 0 then Drule.instantiate'[] [SOME ct_n] @{thm div_cert2} else
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    51
    let
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    52
      val thy = ProofContext.theory_of ctxt;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    53
      val n = HOLogic.dest_nat (Thm.term_of ct_n);
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    54
      val c_n = code_of_val n;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    55
      val c_m = code_of_val m;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    56
      val (c_q, c_r) = @{code divmod} c_n c_m;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    57
      val t_q = term_of_code c_q;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    58
      val t_r = term_of_code c_r;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    59
      val ct_q = Thm.cterm_of thy t_q;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    60
      val ct_r = Thm.cterm_of thy t_r;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    61
      val thm_r = prove (@{cterm "op < \<Colon> nat \<Rightarrow> _"} &$ ct_r &$ ct_m);
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    62
      val thm_m = prove (@{cterm "(op < \<Colon> nat \<Rightarrow> _) 0"} &$ ct_m);
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    63
      val thm_n = prove (@{cterm "(op = \<Colon> nat \<Rightarrow> _)"} &$ ct_n
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    64
        &$ (@{cterm "(op + \<Colon> nat \<Rightarrow> _)"}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    65
          &$ (@{cterm "(op * \<Colon> nat \<Rightarrow> _)"} &$ ct_m &$ ct_q) &$ ct_r));
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    66
    in @{thm div_cert1} OF [thm_r, thm_m, thm_n] end
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    67
  end;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    68
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    69
end;
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    70
*}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    71
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    72
ML_val {*
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    73
  simp_div @{context}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    74
    @{cterm "Suc (Suc (Suc (Suc (Suc 0))))"}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    75
    @{cterm "(Suc (Suc 0))"}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    76
*}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    77
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    78
ML_val {*
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    79
  simp_div @{context}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    80
    (Thm.cterm_of @{theory} (HOLogic.mk_nat 170))
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    81
    (Thm.cterm_of @{theory} (HOLogic.mk_nat 42))
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    82
*}
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    83
9581777503e9 code antiquotation roaring ahead
haftmann
parents:
diff changeset
    84
end