diff -r 143f58bb34f9 -r 0909deb8059b src/HOL/Imperative_HOL/ex/Linked_Lists.thy --- a/src/HOL/Imperative_HOL/ex/Linked_Lists.thy Thu May 26 16:57:14 2016 +0200 +++ b/src/HOL/Imperative_HOL/ex/Linked_Lists.thy Thu May 26 17:51:22 2016 +0200 @@ -2,15 +2,15 @@ Author: Lukas Bulwahn, TU Muenchen *) -section {* Linked Lists by ML references *} +section \Linked Lists by ML references\ theory Linked_Lists imports "../Imperative_HOL" "~~/src/HOL/Library/Code_Target_Int" begin -section {* Definition of Linked Lists *} +section \Definition of Linked Lists\ -setup {* Sign.add_const_constraint (@{const_name Ref}, SOME @{typ "nat \ 'a::type ref"}) *} +setup \Sign.add_const_constraint (@{const_name Ref}, SOME @{typ "nat \ 'a::type ref"})\ datatype 'a node = Empty | Node 'a "'a node ref" primrec @@ -55,9 +55,9 @@ by (simp_all add: traverse.simps[of "Empty"] traverse.simps[of "Node x r"]) -section {* Proving correctness with relational abstraction *} +section \Proving correctness with relational abstraction\ -subsection {* Definition of list_of, list_of', refs_of and refs_of' *} +subsection \Definition of list_of, list_of', refs_of and refs_of'\ primrec list_of :: "heap \ ('a::heap) node \ 'a list \ bool" where @@ -79,7 +79,7 @@ | "refs_of' h r (x#xs) = ((x = r) \ refs_of h (Ref.get h x) xs)" -subsection {* Properties of these definitions *} +subsection \Properties of these definitions\ lemma list_of_Empty[simp]: "list_of h Empty xs = (xs = [])" by (cases xs, auto) @@ -216,7 +216,7 @@ thus ?thesis by (auto simp add: refs_of'_def') qed -subsection {* More complicated properties of these predicates *} +subsection \More complicated properties of these predicates\ lemma list_of_append: "list_of h n (as @ bs) \ \m. list_of h m bs" @@ -258,7 +258,7 @@ by (fastforce simp add: refs_of_distinct refs_of_next) -subsection {* Interaction of these predicates with our heap transitions *} +subsection \Interaction of these predicates with our heap transitions\ lemma list_of_set_ref: "refs_of h q rs \ p \ set rs \ list_of (Ref.set p v h) q as = list_of h q as" proof (induct as arbitrary: q rs) @@ -378,7 +378,7 @@ with assms that show thesis by auto qed -section {* Proving make_llist and traverse correct *} +section \Proving make_llist and traverse correct\ lemma refs_of_invariant: assumes "refs_of h (r::('a::heap) node) xs" @@ -518,9 +518,9 @@ using effect_deterministic by fastforce qed -section {* Proving correctness of in-place reversal *} +section \Proving correctness of in-place reversal\ -subsection {* Definition of in-place reversal *} +subsection \Definition of in-place reversal\ partial_function (heap) rev' :: "('a::heap) node ref \ 'a node ref \ 'a node ref Heap" where @@ -541,7 +541,7 @@ "rev Empty = return Empty" | "rev (Node x n) = do { q \ ref Empty; p \ ref (Node x n); v \ rev' q p; !v }" -subsection {* Correctness Proof *} +subsection \Correctness Proof\ lemma rev'_invariant: assumes "effect (rev' q p) h h' v" @@ -649,10 +649,10 @@ qed -section {* The merge function on Linked Lists *} -text {* We also prove merge correct *} +section \The merge function on Linked Lists\ +text \We also prove merge correct\ -text{* First, we define merge on lists in a natural way. *} +text\First, we define merge on lists in a natural way.\ fun Lmerge :: "('a::ord) list \ 'a list \ 'a list" where @@ -661,7 +661,7 @@ | "Lmerge [] ys = ys" | "Lmerge xs [] = xs" -subsection {* Definition of merge function *} +subsection \Definition of merge function\ partial_function (heap) merge :: "('a::{heap, ord}) node ref \ 'a node ref \ 'a node ref Heap" where @@ -694,9 +694,9 @@ lemma sum_distrib: "case_sum fl fr (case x of Empty \ y | Node v n \ (z v n)) = (case x of Empty \ case_sum fl fr y | Node v n \ case_sum fl fr (z v n))" by (cases x) auto -subsection {* Induction refinement by applying the abstraction function to our induct rule *} +subsection \Induction refinement by applying the abstraction function to our induct rule\ -text {* From our original induction rule Lmerge.induct, we derive a new rule with our list_of' predicate *} +text \From our original induction rule Lmerge.induct, we derive a new rule with our list_of' predicate\ lemma merge_induct2: assumes "list_of' h (p::'a::{heap, ord} node ref) xs" @@ -741,7 +741,7 @@ qed -text {* secondly, we add the effect statement in the premise, and derive the effect statements for the single cases which we then eliminate with our effect elim rules. *} +text \secondly, we add the effect statement in the premise, and derive the effect statements for the single cases which we then eliminate with our effect elim rules.\ lemma merge_induct3: assumes "list_of' h p xs" @@ -790,10 +790,10 @@ qed -subsection {* Proving merge correct *} +subsection \Proving merge correct\ -text {* As many parts of the following three proofs are identical, we could actually move the -same reasoning into an extended induction rule *} +text \As many parts of the following three proofs are identical, we could actually move the +same reasoning into an extended induction rule\ lemma merge_unchanged: assumes "refs_of' h p xs" @@ -822,7 +822,7 @@ from 3(11) pnrs_def have no_inter: "set pnrs \ set ys = {}" by auto from 3(7)[OF refs_of'_pn 3(10) this p_in] 3(3) have p_is_Node: "Ref.get h1 p = Node x pn" by simp - from 3(7)[OF refs_of'_pn 3(10) no_inter r_in] 3(8) `r \ p` show ?case + from 3(7)[OF refs_of'_pn 3(10) no_inter r_in] 3(8) \r \ p\ show ?case by simp next case (4 x xs' y ys' p q pn qn h1 r1 h' xs ys r) @@ -835,7 +835,7 @@ with 4(11) 4(12) qnrs_def refs_of'_distinct[OF 4(10)] have q_in: "q \ set xs \ set qnrs" by auto from 4(11) qnrs_def have no_inter: "set xs \ set qnrs = {}" by auto from 4(7)[OF 4(9) refs_of'_qn this q_in] 4(4) have q_is_Node: "Ref.get h1 q = Node y qn" by simp - from 4(7)[OF 4(9) refs_of'_qn no_inter r_in] 4(8) `r \ q` show ?case + from 4(7)[OF 4(9) refs_of'_qn no_inter r_in] 4(8) \r \ q\ show ?case by simp qed qed @@ -937,9 +937,9 @@ show ?case by (auto simp: list_of'_set_ref) qed -section {* Code generation *} +section \Code generation\ -text {* A simple example program *} +text \A simple example program\ definition test_1 where "test_1 = (do { ll_xs \ make_llist [1..(15::int)]; xs \ traverse ll_xs; return xs })" definition test_2 where "test_2 = (do { ll_xs \ make_llist [1..(15::int)]; ll_ys \ rev ll_xs; ys \ traverse ll_ys; return ys })" @@ -958,9 +958,9 @@ code_reserved SML upto -ML_val {* @{code test_1} () *} -ML_val {* @{code test_2} () *} -ML_val {* @{code test_3} () *} +ML_val \@{code test_1} ()\ +ML_val \@{code test_2} ()\ +ML_val \@{code test_3} ()\ export_code test_1 test_2 test_3 checking SML SML_imp OCaml? OCaml_imp? Haskell? Scala Scala_imp