src/HOL/Proofs/Extraction/QuotRem.thy
author wenzelm
Fri, 01 Jul 2016 16:52:35 +0200
changeset 63361 d10eab0672f9
parent 61986 2461779da2b8
child 66258 2b83dd24b301
permissions -rw-r--r--
misc tuning and modernization;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39157
b98909faaea8 more explicit HOL-Proofs sessions, including former ex/Hilbert_Classical.thy which works in parallel mode without the antiquotation option "margin" (which is still critical);
wenzelm
parents: 36862
diff changeset
     1
(*  Title:      HOL/Proofs/Extraction/QuotRem.thy
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     3
*)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     4
61986
2461779da2b8 isabelle update_cartouches -c -t;
wenzelm
parents: 58889
diff changeset
     5
section \<open>Quotient and remainder\<close>
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     6
27436
9581777503e9 code antiquotation roaring ahead
haftmann
parents: 25419
diff changeset
     7
theory QuotRem
9581777503e9 code antiquotation roaring ahead
haftmann
parents: 25419
diff changeset
     8
imports Util
9581777503e9 code antiquotation roaring ahead
haftmann
parents: 25419
diff changeset
     9
begin
9581777503e9 code antiquotation roaring ahead
haftmann
parents: 25419
diff changeset
    10
61986
2461779da2b8 isabelle update_cartouches -c -t;
wenzelm
parents: 58889
diff changeset
    11
text \<open>Derivation of quotient and remainder using program extraction.\<close>
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    12
13931
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    13
theorem division: "\<exists>r q. a = Suc b * q + r \<and> r \<le> b"
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    14
proof (induct a)
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    15
  case 0
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    16
  have "0 = Suc b * 0 + 0 \<and> 0 \<le> b" by simp
63361
d10eab0672f9 misc tuning and modernization;
wenzelm
parents: 61986
diff changeset
    17
  then show ?case by iprover
13931
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    18
next
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    19
  case (Suc a)
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
    20
  then obtain r q where I: "a = Suc b * q + r" and "r \<le> b" by iprover
13931
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    21
  from nat_eq_dec show ?case
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    22
  proof
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    23
    assume "r = b"
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    24
    with I have "Suc a = Suc b * (Suc q) + 0 \<and> 0 \<le> b" by simp
63361
d10eab0672f9 misc tuning and modernization;
wenzelm
parents: 61986
diff changeset
    25
    then show ?case by iprover
13931
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    26
  next
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    27
    assume "r \<noteq> b"
61986
2461779da2b8 isabelle update_cartouches -c -t;
wenzelm
parents: 58889
diff changeset
    28
    with \<open>r \<le> b\<close> have "r < b" by (simp add: order_less_le)
13931
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    29
    with I have "Suc a = Suc b * q + (Suc r) \<and> (Suc r) \<le> b" by simp
63361
d10eab0672f9 misc tuning and modernization;
wenzelm
parents: 61986
diff changeset
    30
    then show ?case by iprover
13931
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    31
  qed
38d43d168e87 Converted main proof to Isar.
berghofe
parents: 13405
diff changeset
    32
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    33
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    34
extract division
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    35
61986
2461779da2b8 isabelle update_cartouches -c -t;
wenzelm
parents: 58889
diff changeset
    36
text \<open>
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    37
  The program extracted from the above proof looks as follows
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    38
  @{thm [display] division_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    39
  The corresponding correctness theorem is
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    40
  @{thm [display] division_correctness [no_vars]}
61986
2461779da2b8 isabelle update_cartouches -c -t;
wenzelm
parents: 58889
diff changeset
    41
\<close>
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    42
27436
9581777503e9 code antiquotation roaring ahead
haftmann
parents: 25419
diff changeset
    43
lemma "division 9 2 = (0, 3)" by eval
20593
5af400cc64d5 added some stuff for code generation 2
haftmann
parents: 17604
diff changeset
    44
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    45
end