# HG changeset patch # User berghofe # Date 1027258950 -7200 # Node ID bc2b32ee62fda8059ff939248860d8acf98feb99 # Parent e6e826bb8c3cd4ac80c7760b22d1a43a2ee576dc Added theory for setting up program extraction. diff -r e6e826bb8c3c -r bc2b32ee62fd src/HOL/Extraction.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Extraction.thy Sun Jul 21 15:42:30 2002 +0200 @@ -0,0 +1,443 @@ +(* Title: HOL/Extraction.thy + ID: $Id$ + Author: Stefan Berghofer, TU Muenchen + License: GPL (GNU GENERAL PUBLIC LICENSE) +*) + +header {* Program extraction for HOL *} + +theory Extraction = Datatype +files + "Tools/rewrite_hol_proof.ML": + +subsection {* Setup *} + +ML_setup {* + Context.>> (fn thy => thy |> + Extraction.set_preprocessor (fn sg => + Proofterm.rewrite_proof_notypes + ([], ("HOL/elim_cong", RewriteHOLProof.elim_cong) :: + ProofRewriteRules.rprocs true) o + Proofterm.rewrite_proof (Sign.tsig_of sg) + (RewriteHOLProof.rews, ProofRewriteRules.rprocs true))) +*} + +lemmas [extraction_expand] = + nat.exhaust atomize_eq atomize_all atomize_imp + allE rev_mp conjE Eq_TrueI Eq_FalseI eqTrueI eqTrueE eq_cong2 + notE' impE' impE iffE imp_cong simp_thms + induct_forall_eq induct_implies_eq induct_equal_eq + induct_forall_def induct_implies_def + induct_atomize induct_rulify1 induct_rulify2 + +datatype sumbool = Left | Right + +subsection {* Type of extracted program *} + +extract_type + "typeof (Trueprop P) \ typeof P" + + "typeof P \ Type (TYPE(Null)) \ typeof Q \ Type (TYPE('Q)) \ + typeof (P \ Q) \ Type (TYPE('Q))" + + "typeof Q \ Type (TYPE(Null)) \ typeof (P \ Q) \ Type (TYPE(Null))" + + "typeof P \ Type (TYPE('P)) \ typeof Q \ Type (TYPE('Q)) \ + typeof (P \ Q) \ Type (TYPE('P \ 'Q))" + + "(\x. typeof (P x)) \ (\x. Type (TYPE(Null))) \ + typeof (\x. P x) \ Type (TYPE(Null))" + + "(\x. typeof (P x)) \ (\x. Type (TYPE('P))) \ + typeof (\x::'a. P x) \ Type (TYPE('a \ 'P))" + + "(\x. typeof (P x)) \ (\x. Type (TYPE(Null))) \ + typeof (\x::'a. P x) \ Type (TYPE('a))" + + "(\x. typeof (P x)) \ (\x. Type (TYPE('P))) \ + typeof (\x::'a. P x) \ Type (TYPE('a \ 'P))" + + "typeof P \ Type (TYPE(Null)) \ typeof Q \ Type (TYPE(Null)) \ + typeof (P \ Q) \ Type (TYPE(sumbool))" + + "typeof P \ Type (TYPE(Null)) \ typeof Q \ Type (TYPE('Q)) \ + typeof (P \ Q) \ Type (TYPE('Q option))" + + "typeof P \ Type (TYPE('P)) \ typeof Q \ Type (TYPE(Null)) \ + typeof (P \ Q) \ Type (TYPE('P option))" + + "typeof P \ Type (TYPE('P)) \ typeof Q \ Type (TYPE('Q)) \ + typeof (P \ Q) \ Type (TYPE('P + 'Q))" + + "typeof P \ Type (TYPE(Null)) \ typeof Q \ Type (TYPE('Q)) \ + typeof (P \ Q) \ Type (TYPE('Q))" + + "typeof P \ Type (TYPE('P)) \ typeof Q \ Type (TYPE(Null)) \ + typeof (P \ Q) \ Type (TYPE('P))" + + "typeof P \ Type (TYPE('P)) \ typeof Q \ Type (TYPE('Q)) \ + typeof (P \ Q) \ Type (TYPE('P \ 'Q))" + + "typeof (P = Q) \ typeof ((P \ Q) \ (Q \ P))" + + "typeof (x \ P) \ typeof P" + +subsection {* Realizability *} + +realizability + "(realizes t (Trueprop P)) \ (Trueprop (realizes t P))" + + "(typeof P) \ (Type (TYPE(Null))) \ + (realizes t (P \ Q)) \ (realizes Null P \ realizes t Q)" + + "(typeof P) \ (Type (TYPE('P))) \ + (typeof Q) \ (Type (TYPE(Null))) \ + (realizes t (P \ Q)) \ (\x::'P. realizes x P \ realizes Null Q)" + + "(realizes t (P \ Q)) \ (\x. realizes x P \ realizes (t x) Q)" + + "(\x. typeof (P x)) \ (\x. Type (TYPE(Null))) \ + (realizes t (\x. P x)) \ (\x. realizes Null (P x))" + + "(realizes t (\x. P x)) \ (\x. realizes (t x) (P x))" + + "(\x. typeof (P x)) \ (\x. Type (TYPE(Null))) \ + (realizes t (\x. P x)) \ (realizes Null (P t))" + + "(realizes t (\x. P x)) \ (realizes (snd t) (P (fst t)))" + + "(typeof P) \ (Type (TYPE(Null))) \ + (typeof Q) \ (Type (TYPE(Null))) \ + (realizes t (P \ Q)) \ + (case t of Left \ realizes Null P | Right \ realizes Null Q)" + + "(typeof P) \ (Type (TYPE(Null))) \ + (realizes t (P \ Q)) \ + (case t of None \ realizes Null P | Some q \ realizes q Q)" + + "(typeof Q) \ (Type (TYPE(Null))) \ + (realizes t (P \ Q)) \ + (case t of None \ realizes Null Q | Some p \ realizes p P)" + + "(realizes t (P \ Q)) \ + (case t of Inl p \ realizes p P | Inr q \ realizes q Q)" + + "(typeof P) \ (Type (TYPE(Null))) \ + (realizes t (P \ Q)) \ (realizes Null P \ realizes t Q)" + + "(typeof Q) \ (Type (TYPE(Null))) \ + (realizes t (P \ Q)) \ (realizes t P \ realizes Null Q)" + + "(realizes t (P \ Q)) \ (realizes (fst t) P \ realizes (snd t) Q)" + + "typeof P \ Type (TYPE(Null)) \ + realizes t (\ P) \ \ realizes Null P" + + "typeof P \ Type (TYPE('P)) \ + realizes t (\ P) \ (\x::'P. \ realizes x P)" + + "typeof (P::bool) \ Type (TYPE(Null)) \ + typeof Q \ Type (TYPE(Null)) \ + realizes t (P = Q) \ realizes Null P = realizes Null Q" + + "(realizes t (P = Q)) \ (realizes t ((P \ Q) \ (Q \ P)))" + +subsection {* Computational content of basic inference rules *} + +theorem disjE_realizer: + assumes r: "case x of Inl p \ P p | Inr q \ Q q" + and r1: "\p. P p \ R (f p)" and r2: "\q. Q q \ R (g q)" + shows "R (case x of Inl p \ f p | Inr q \ g q)" +proof (cases x) + case Inl + with r show ?thesis by simp (rule r1) +next + case Inr + with r show ?thesis by simp (rule r2) +qed + +theorem disjE_realizer2: + assumes r: "case x of None \ P | Some q \ Q q" + and r1: "P \ R f" and r2: "\q. Q q \ R (g q)" + shows "R (case x of None \ f | Some q \ g q)" +proof (cases x) + case None + with r show ?thesis by simp (rule r1) +next + case Some + with r show ?thesis by simp (rule r2) +qed + +theorem disjE_realizer3: + assumes r: "case x of Left \ P | Right \ Q" + and r1: "P \ R f" and r2: "Q \ R g" + shows "R (case x of Left \ f | Right \ g)" +proof (cases x) + case Left + with r show ?thesis by simp (rule r1) +next + case Right + with r show ?thesis by simp (rule r2) +qed + +theorem conjI_realizer: + "P p \ Q q \ P (fst (p, q)) \ Q (snd (p, q))" + by simp + +theorem exI_realizer: + "P x y \ P (fst (x, y)) (snd (x, y))" by simp + +realizers + impI (P, Q): "\P Q pq. pq" + "\P Q pq (h: _). allI \ _ \ (\x. impI \ _ \ _ \ (h \ x))" + + impI (P): "Null" + "\P Q (h: _). allI \ _ \ (\x. impI \ _ \ _ \ (h \ x))" + + impI (Q): "\P Q q. q" "\P Q q. impI \ _ \ _" + + impI: "Null" "\P Q. impI \ _ \ _" + + mp (P, Q): "\P Q pq. pq" + "\P Q pq (h: _) p. mp \ _ \ _ \ (spec \ _ \ p \ h)" + + mp (P): "Null" + "\P Q (h: _) p. mp \ _ \ _ \ (spec \ _ \ p \ h)" + + mp (Q): "\P Q q. q" "\P Q q. mp \ _ \ _" + + mp: "Null" "\P Q. mp \ _ \ _" + + allI (P): "\P p. p" "\P p. allI \ _" + + allI: "Null" "\P. allI \ _" + + spec (P): "\P x p. p x" "\P x p. spec \ _ \ x" + + spec: "Null" "\P x. spec \ _ \ x" + + exI (P): "\P x p. (x, p)" "\P. exI_realizer \ _" + + exI: "\P x. x" "\P x (h: _). h" + + exE (P, Q): "\P Q p pq. pq (fst p) (snd p)" + "\P Q p (h1: _) pq (h2: _). h2 \ (fst p) \ (snd p) \ h1" + + exE (P): "Null" + "\P Q p (h1: _) (h2: _). h2 \ (fst p) \ (snd p) \ h1" + + exE (Q): "\P Q x pq. pq x" + "\P Q x (h1: _) pq (h2: _). h2 \ x \ h1" + + exE: "Null" + "\P Q x (h1: _) (h2: _). h2 \ x \ h1" + + conjI (P, Q): "\P Q p q. (p, q)" + "\P Q p (h: _) q. conjI_realizer \ + (\p. realizes p P) \ p \ (\q. realizes q Q) \ q \ h" + + conjI (P): "\P Q p. p" + "\P Q p. conjI \ _ \ _" + + conjI (Q): "\P Q q. q" + "\P Q (h: _) q. conjI \ _ \ _ \ h" + + conjI: "Null" + "\P Q. conjI \ _ \ _" + + conjunct1 (P, Q): "\P Q. fst" + "\P Q pq. conjunct1 \ _ \ _" + + conjunct1 (P): "\P Q p. p" + "\P Q p. conjunct1 \ _ \ _" + + conjunct1 (Q): "Null" + "\P Q q. conjunct1 \ _ \ _" + + conjunct1: "Null" + "\P Q. conjunct1 \ _ \ _" + + conjunct2 (P, Q): "\P Q. snd" + "\P Q pq. conjunct2 \ _ \ _" + + conjunct2 (P): "Null" + "\P Q p. conjunct2 \ _ \ _" + + conjunct2 (Q): "\P Q p. p" + "\P Q p. conjunct2 \ _ \ _" + + conjunct2: "Null" + "\P Q. conjunct2 \ _ \ _" + + disjI1 (P, Q): "\P Q. Inl" + "\P Q p. iffD2 \ _ \ _ \ (sum.cases_1 \ (\p. realizes p P) \ _ \ p)" + + disjI1 (P): "\P Q. Some" + "\P Q p. iffD2 \ _ \ _ \ (option.cases_2 \ _ \ (\p. realizes p P) \ p)" + + disjI1 (Q): "\P Q. None" + "\P Q. iffD2 \ _ \ _ \ (option.cases_1 \ _ \ _)" + + disjI1: "\P Q. Left" + "\P Q. iffD2 \ _ \ _ \ (sumbool.cases_1 \ _ \ _)" + + disjI2 (P, Q): "\Q P. Inr" + "\Q P q. iffD2 \ _ \ _ \ (sum.cases_2 \ _ \ (\q. realizes q Q) \ q)" + + disjI2 (P): "\Q P. None" + "\Q P. iffD2 \ _ \ _ \ (option.cases_1 \ _ \ _)" + + disjI2 (Q): "\Q P. Some" + "\Q P q. iffD2 \ _ \ _ \ (option.cases_2 \ _ \ (\q. realizes q Q) \ q)" + + disjI2: "\Q P. Right" + "\Q P. iffD2 \ _ \ _ \ (sumbool.cases_2 \ _ \ _)" + + disjE (P, Q, R): "\P Q R pq pr qr. + (case pq of Inl p \ pr p | Inr q \ qr q)" + "\P Q R pq (h1: _) pr (h2: _) qr. + disjE_realizer \ _ \ _ \ pq \ (\r. realizes r R) \ pr \ qr \ h1 \ h2" + + disjE (Q, R): "\P Q R pq pr qr. + (case pq of None \ pr | Some q \ qr q)" + "\P Q R pq (h1: _) pr (h2: _) qr. + disjE_realizer2 \ _ \ _ \ pq \ (\r. realizes r R) \ pr \ qr \ h1 \ h2" + + disjE (P, R): "\P Q R pq pr qr. + (case pq of None \ qr | Some p \ pr p)" + "\P Q R pq (h1: _) pr (h2: _) qr (h3: _). + disjE_realizer2 \ _ \ _ \ pq \ (\r. realizes r R) \ qr \ pr \ h1 \ h3 \ h2" + + disjE (R): "\P Q R pq pr qr. + (case pq of Left \ pr | Right \ qr)" + "\P Q R pq (h1: _) pr (h2: _) qr. + disjE_realizer3 \ _ \ _ \ pq \ (\r. realizes r R) \ pr \ qr \ h1 \ h2" + + disjE (P, Q): "Null" + "\P Q R pq. disjE_realizer \ _ \ _ \ pq \ (\r. realizes Null R) \ _ \ _" + + disjE (Q): "Null" + "\P Q R pq. disjE_realizer2 \ _ \ _ \ pq \ (\r. realizes Null R) \ _ \ _" + + disjE (P): "Null" + "\P Q R pq (h1: _) (h2: _) (h3: _). + disjE_realizer2 \ _ \ _ \ pq \ (\r. realizes Null R) \ _ \ _ \ h1 \ h3 \ h2" + + disjE: "Null" + "\P Q R pq. disjE_realizer3 \ _ \ _ \ pq \ (\r. realizes Null R) \ _ \ _" + + FalseE (P): "\P. arbitrary" + "\P. FalseE \ _" + + FalseE: "Null" + "\P. FalseE \ _" + + notI (P): "Null" + "\P (h: _). allI \ _ \ (\x. notI \ _ \ (h \ x))" + + notI: "Null" + "\P. notI \ _" + + notE (P, R): "\P R p. arbitrary" + "\P R (h: _) p. notE \ _ \ _ \ (spec \ _ \ p \ h)" + + notE (P): "Null" + "\P R (h: _) p. notE \ _ \ _ \ (spec \ _ \ p \ h)" + + notE (R): "\P R. arbitrary" + "\P R. notE \ _ \ _" + + notE: "Null" + "\P R. notE \ _ \ _" + + subst (P): "\s t P ps. ps" + "\s t P (h: _) ps. subst \ s \ t \ (\x. realizes ps (P x)) \ h" + + subst: "Null" + "\s t P. subst \ s \ t \ (\x. realizes Null (P x))" + + iffD1 (P, Q): "\Q P. fst" + "\Q P pq (h: _) p. + mp \ _ \ _ \ (spec \ _ \ p \ (conjunct1 \ _ \ _ \ h))" + + iffD1 (P): "\Q P p. p" + "\Q P p (h: _). mp \ _ \ _ \ (conjunct1 \ _ \ _ \ h)" + + iffD1 (Q): "Null" + "\Q P q1 (h: _) q2. + mp \ _ \ _ \ (spec \ _ \ q2 \ (conjunct1 \ _ \ _ \ h))" + + iffD1: "Null" + "\Q P. iffD1 \ _ \ _" + + iffD2 (P, Q): "\P Q. snd" + "\P Q pq (h: _) q. + mp \ _ \ _ \ (spec \ _ \ q \ (conjunct2 \ _ \ _ \ h))" + + iffD2 (P): "\P Q p. p" + "\P Q p (h: _). mp \ _ \ _ \ (conjunct2 \ _ \ _ \ h)" + + iffD2 (Q): "Null" + "\P Q q1 (h: _) q2. + mp \ _ \ _ \ (spec \ _ \ q2 \ (conjunct2 \ _ \ _ \ h))" + + iffD2: "Null" + "\P Q. iffD2 \ _ \ _" + + iffI (P, Q): "\P Q pq qp. (pq, qp)" + "\P Q pq (h1 : _) qp (h2 : _). conjI_realizer \ + (\pq. \x. realizes x P \ realizes (pq x) Q) \ pq \ + (\qp. \x. realizes x Q \ realizes (qp x) P) \ qp \ + (allI \ _ \ (\x. impI \ _ \ _ \ (h1 \ x))) \ + (allI \ _ \ (\x. impI \ _ \ _ \ (h2 \ x)))" + + iffI (P): "\P Q p. p" + "\P Q (h1 : _) p (h2 : _). conjI \ _ \ _ \ + (allI \ _ \ (\x. impI \ _ \ _ \ (h1 \ x))) \ + (impI \ _ \ _ \ h2)" + + iffI (Q): "\P Q q. q" + "\P Q q (h1 : _) (h2 : _). conjI \ _ \ _ \ + (impI \ _ \ _ \ h1) \ + (allI \ _ \ (\x. impI \ _ \ _ \ (h2 \ x)))" + + iffI: "Null" + "\P Q. iffI \ _ \ _" + + classical: "Null" + "\P. classical \ _" + + +subsection {* Induction on natural numbers *} + +theorem nat_ind_realizer: + "R f 0 \ (\y h. R h y \ R (g y h) (Suc y)) \ + (R::'a \ nat \ bool) (nat_rec f g x) x" +proof - + assume r1: "R f 0" + assume r2: "\y h. R h y \ R (g y h) (Suc y)" + show "R (nat_rec f g x) x" + proof (induct x) + case 0 + from r1 show ?case by simp + next + case (Suc n) + from Suc have "R (g n (nat_rec f g n)) (Suc n)" by (rule r2) + thus ?case by simp + qed +qed + +realizers + NatDef.nat_induct (P): "\P n p0 ps. nat_rec p0 ps n" + "\P n p0 (h: _) ps. nat_ind_realizer \ _ \ _ \ _ \ _ \ h" + + NatDef.nat_induct: "Null" + "\P n. nat_induct \ _ \ _" + + Nat.nat.induct (P): "\P n p0 ps. nat_rec p0 ps n" + "\P n p0 (h: _) ps. nat_ind_realizer \ _ \ _ \ _ \ _ \ h" + + Nat.nat.induct: "Null" + "\P n. nat_induct \ _ \ _" + +end diff -r e6e826bb8c3c -r bc2b32ee62fd src/HOL/IsaMakefile --- a/src/HOL/IsaMakefile Sun Jul 21 15:37:04 2002 +0200 +++ b/src/HOL/IsaMakefile Sun Jul 21 15:42:30 2002 +0200 @@ -17,6 +17,7 @@ HOL-AxClasses \ HOL-Bali \ HOL-CTL \ + HOL-Extraction \ HOL-GroupTheory \ HOL-Real-HahnBanach \ HOL-Real-ex \ @@ -80,7 +81,8 @@ $(SRC)/TFL/rules.ML $(SRC)/TFL/tfl.ML $(SRC)/TFL/thms.ML $(SRC)/TFL/thry.ML \ $(SRC)/TFL/usyntax.ML $(SRC)/TFL/utils.ML \ Datatype.thy Datatype_Universe.ML Datatype_Universe.thy Divides_lemmas.ML \ - Divides.thy Finite_Set.ML Finite_Set.thy Fun.ML Fun.thy Gfp.ML Gfp.thy \ + Divides.thy Extraction.thy Finite_Set.ML Finite_Set.thy \ + Fun.ML Fun.thy Gfp.ML Gfp.thy \ Hilbert_Choice.thy Hilbert_Choice_lemmas.ML HOL.ML \ HOL.thy HOL_lemmas.ML Inductive.thy Integ/Bin.ML Integ/Bin.thy \ Integ/Equiv.ML Integ/Equiv.thy Integ/Int.ML Integ/Int.thy \ @@ -99,7 +101,8 @@ Tools/datatype_rep_proofs.ML \ Tools/inductive_package.ML Tools/inductive_codegen.ML Tools/meson.ML Tools/numeral_syntax.ML \ Tools/primrec_package.ML Tools/recdef_package.ML Tools/recfun_codegen.ML \ - Tools/record_package.ML Tools/split_rule.ML Tools/typedef_package.ML \ + Tools/record_package.ML Tools/rewrite_hol_proof.ML \ + Tools/split_rule.ML Tools/typedef_package.ML \ Transitive_Closure.thy Transitive_Closure.ML Typedef.thy \ Wellfounded_Recursion.ML Wellfounded_Recursion.thy Wellfounded_Relations.ML \ Wellfounded_Relations.thy arith_data.ML blastdata.ML cladata.ML \ @@ -537,6 +540,17 @@ @$(ISATOOL) usedir $(OUT)/HOL CTL +## HOL-Extraction + +HOL-Extraction: HOL $(LOG)/HOL-Extraction.gz + +$(LOG)/HOL-Extraction.gz: $(OUT)/HOL \ + Extraction/Higman.thy Extraction/ROOT.ML Extraction/QuotRem.thy \ + Extraction/Warshall.thy Extraction/document/root.tex \ + Extraction/document/root.bib + @$(ISATOOL) usedir $(OUT)/HOL Extraction + + ## HOL-IOA HOL-IOA: HOL $(LOG)/HOL-IOA.gz diff -r e6e826bb8c3c -r bc2b32ee62fd src/HOL/Main.thy --- a/src/HOL/Main.thy Sun Jul 21 15:37:04 2002 +0200 +++ b/src/HOL/Main.thy Sun Jul 21 15:42:30 2002 +0200 @@ -6,7 +6,7 @@ header {* Main HOL *} -theory Main = Map + Hilbert_Choice: +theory Main = Map + Hilbert_Choice + Extraction: text {* Theory @{text Main} includes everything. Note that theory @{text diff -r e6e826bb8c3c -r bc2b32ee62fd src/HOL/ROOT.ML --- a/src/HOL/ROOT.ML Sun Jul 21 15:37:04 2002 +0200 +++ b/src/HOL/ROOT.ML Sun Jul 21 15:42:30 2002 +0200 @@ -42,3 +42,5 @@ print_depth 8; Goal "True"; (*leave subgoal package empty*) + +val HOL_proofs = !proofs;