wneuper@59229
|
1 |
theory InsSort imports "../Interpret/Interpret" Rational(*for norm_Rational?!?*)
|
wneuper@59229
|
2 |
begin
|
neuper@37906
|
3 |
|
wneuper@59229
|
4 |
subsection {* consts *}
|
wneuper@59229
|
5 |
consts
|
wneuper@59235
|
6 |
unsorted :: "int xlist => unl"
|
wneuper@59235
|
7 |
sorted :: "int xlist => unl"
|
neuper@37906
|
8 |
|
wneuper@59243
|
9 |
(* CAS-command *)
|
wneuper@59243
|
10 |
Sort :: "int xlist \<Rightarrow> int xlist"
|
wneuper@59243
|
11 |
|
wneuper@59243
|
12 |
(* subproblem and script-name *)
|
wneuper@59235
|
13 |
Ins'_sort :: "[int xlist,
|
wneuper@59235
|
14 |
int xlist] => int xlist"
|
neuper@37954
|
15 |
("((Script Ins'_sort (_ =))//
|
neuper@37954
|
16 |
(_))" 9)
|
wneuper@59235
|
17 |
Sortprog :: "[int xlist,
|
wneuper@59235
|
18 |
int xlist] => int xlist"
|
wneuper@59235
|
19 |
("((Script Sortprog (_ =))//
|
neuper@37954
|
20 |
(_))" 9)
|
neuper@37906
|
21 |
|
wneuper@59229
|
22 |
subsection {* functions *}
|
wneuper@59234
|
23 |
primrec ins :: "int \<Rightarrow> int xlist \<Rightarrow> int xlist"
|
wneuper@59229
|
24 |
where
|
wneuper@59244
|
25 |
ins_Nil: "ins i {|| ||} = {||i||}" |
|
wneuper@59234
|
26 |
ins_Cons: "ins i (x @# xs) = (if i < x then (i @# x @# xs) else x @# (ins i xs))"
|
neuper@37906
|
27 |
|
wneuper@59234
|
28 |
fun sort :: "int xlist \<Rightarrow> int xlist"
|
wneuper@59229
|
29 |
where
|
wneuper@59244
|
30 |
sort_deff: "sort xs = xfoldr ins xs {|| ||}"
|
wneuper@59233
|
31 |
print_theorems
|
wneuper@59233
|
32 |
thm sort_def (* InsSort.sort \<equiv> sort_sumC *)
|
wneuper@59233
|
33 |
thm sort_deff (* InsSort.sort ?xs = foldr ins ?xs [] *)
|
neuper@37906
|
34 |
|
wneuper@59229
|
35 |
(* access to definitions from ML *)
|
wneuper@59229
|
36 |
ML {*
|
wneuper@59231
|
37 |
@{thm ins.simps(1)};
|
wneuper@59231
|
38 |
@{thm ins.simps(2)};
|
wneuper@59229
|
39 |
*}
|
neuper@37906
|
40 |
|
wneuper@59244
|
41 |
value "sort {||2,3,1||}"
|
wneuper@59244
|
42 |
lemma test: "sort {||2,3,1||} = {||1,2,3||}"
|
wneuper@59229
|
43 |
by simp
|
neuper@37954
|
44 |
|
wneuper@59229
|
45 |
subsection {* eval functions *}
|
wneuper@59229
|
46 |
subsection {* rulesets *}
|
wneuper@59229
|
47 |
ML {*
|
wneuper@59229
|
48 |
val ins_sort =
|
wneuper@59229
|
49 |
Rls {
|
wneuper@59229
|
50 |
id = "ins_sort", preconds = [], rew_ord = ("tless_true", tless_true), erls = e_rls,
|
wneuper@59229
|
51 |
srls = e_rls, calc = [], rules = [
|
wneuper@59234
|
52 |
Thm ("xfoldr_Nil",(*num_str*) @{thm xfoldr_Nil} (* foldr ?f [] = id *)),
|
wneuper@59234
|
53 |
Thm ("xfoldr_Cons", @{thm xfoldr_Cons} (* foldr ?f (?x # ?xs) = ?f ?x \<circ> foldr ?f ?xs *)),
|
wneuper@59232
|
54 |
|
wneuper@59232
|
55 |
Thm ("ins_Nil", @{thm ins_Nil} (* ins ?i [] = [?i] *)),
|
wneuper@59232
|
56 |
Thm ("ins_Cons", @{thm ins_Cons} (* ins ?i (?x # ?xs) = (if ?i < ?x then ?i # ?x # ?xs else ?x # ins ?i ?xs) *)),
|
wneuper@59233
|
57 |
Thm ("sort_deff", @{thm sort_deff} (* InsSort.sort ?xs = foldr ins ?xs [] *)),
|
wneuper@59232
|
58 |
|
wneuper@59232
|
59 |
Thm ("o_id", @{thm o_id} (* ?f \<circ> id = ?f *)),
|
wneuper@59232
|
60 |
Thm ("o_assoc", @{thm o_assoc} (* ?f \<circ> (?g \<circ> ?h) = ?f \<circ> ?g \<circ> ?h *)),
|
wneuper@59232
|
61 |
Thm ("o_apply", @{thm o_apply} (* (?f \<circ> ?g) ?x = ?f (?g ?x) *)),
|
wneuper@59232
|
62 |
Thm ("id_apply", @{thm id_apply} (* id ?x = ?x *)),
|
wneuper@59229
|
63 |
|
wneuper@59229
|
64 |
Calc ("Orderings.ord_class.less", eval_equ "#less_"),
|
wneuper@59232
|
65 |
Thm ("If_def", @{thm If_def} (* if ?P then ?x else ?y \<equiv> THE z. (?P = True \<longrightarrow> z = ?x) \<and> (?P = False \<longrightarrow> z = ?y) *)),
|
wneuper@59247
|
66 |
Thm ("if_True", @{thm if_True} (* "(if True then x else y) = x" *)),
|
wneuper@59247
|
67 |
Thm ("if_False", @{thm if_False} (* "(if False then x else y) = y" *))],
|
wneuper@59229
|
68 |
errpatts = [], scr = Prog ((Thm.term_of o the o (parse thy)) "empty_script")};
|
s1210629013@55373
|
69 |
*}
|
neuper@52125
|
70 |
setup {* KEStore_Elems.add_rlss [("ins_sort", (Context.theory_name @{theory}, ins_sort))] *}
|
neuper@37954
|
71 |
|
wneuper@59229
|
72 |
subsection {* problems *}
|
wneuper@59229
|
73 |
setup {* KEStore_Elems.add_pbts
|
wneuper@59269
|
74 |
[(Specify.prep_pbt @{theory} "pbl_Programming" [] e_pblID
|
wneuper@59229
|
75 |
(["Programming"], [], e_rls, NONE, [])),
|
wneuper@59269
|
76 |
(Specify.prep_pbt @{theory} "pbl_Prog_sort" [] e_pblID
|
wneuper@59246
|
77 |
(["SORT","Programming"], [], e_rls, NONE, [])),
|
wneuper@59269
|
78 |
(Specify.prep_pbt @{theory} "pbl_Prog_sort_ins" [] e_pblID
|
wneuper@59246
|
79 |
(["insertion","SORT","Programming"],
|
wneuper@59229
|
80 |
[("#Given", ["unsorted u_u"]), ("#Find", ["sorted s_s"])],
|
wneuper@59229
|
81 |
e_rls,
|
wneuper@59246
|
82 |
SOME "Sort u_u", [["Programming","SORT","insertion_steps"]]))] *}
|
wneuper@59229
|
83 |
|
wneuper@59229
|
84 |
subsection {* methods *}
|
wneuper@59229
|
85 |
(* TODO: implementation needs extra object-level lists ?!?*)
|
wneuper@59229
|
86 |
setup {* KEStore_Elems.add_mets
|
wneuper@59269
|
87 |
[ Specify.prep_met @{theory} "met_Programming" [] e_metID
|
wneuper@59229
|
88 |
(["Programming"], [],
|
wneuper@59229
|
89 |
{rew_ord'="tless_true",rls' = e_rls, calc = [], srls = e_rls, prls = e_rls,
|
wneuper@59229
|
90 |
crls = e_rls, errpats = [], nrls = e_rls}, "empty_script"),
|
wneuper@59269
|
91 |
Specify.prep_met @{theory} "met_Prog_sort" [] e_metID
|
wneuper@59246
|
92 |
(["Programming","SORT"], [],
|
wneuper@59229
|
93 |
{rew_ord'="tless_true",rls' = e_rls, calc = [], srls = e_rls, prls = e_rls,
|
wneuper@59229
|
94 |
crls = e_rls, errpats = [], nrls = e_rls},
|
wneuper@59229
|
95 |
"empty_script"),
|
wneuper@59269
|
96 |
Specify.prep_met @{theory} "met_Prog_sort_ins" [] e_metID
|
wneuper@59246
|
97 |
(["Programming","SORT","insertion"],
|
wneuper@59240
|
98 |
[("#Given", ["unsorted u_u"]), ("#Find", ["sorted s_s"])],
|
wneuper@59229
|
99 |
{rew_ord'="tless_true",rls' = Atools_erls, calc = [], srls = e_rls, prls = e_rls,
|
wneuper@59229
|
100 |
crls = Atools_crls, errpats = [], nrls = norm_Rational},
|
wneuper@59237
|
101 |
"Script Sortprog (unso :: int xlist) = " ^
|
wneuper@59237
|
102 |
" (let uns = Take (sort unso) " ^
|
wneuper@59237
|
103 |
" in " ^
|
wneuper@59237
|
104 |
" (Rewrite_Set ins_sort False) uns" ^
|
wneuper@59245
|
105 |
" )"),
|
wneuper@59269
|
106 |
Specify.prep_met @{theory} "met_Prog_sort_ins_steps" [] e_metID
|
wneuper@59246
|
107 |
(["Programming","SORT","insertion_steps"],
|
wneuper@59245
|
108 |
[("#Given", ["unsorted u_u"]), ("#Find", ["sorted s_s"])],
|
wneuper@59245
|
109 |
{rew_ord'="tless_true",rls' = Atools_erls, calc = [], srls = e_rls, prls = e_rls,
|
wneuper@59245
|
110 |
crls = Atools_crls, errpats = [], nrls = norm_Rational},
|
wneuper@59245
|
111 |
"Script Sortprog (unso :: int xlist) = " ^
|
wneuper@59245
|
112 |
" (let uns = Take (sort unso) " ^
|
wneuper@59245
|
113 |
" in " ^
|
wneuper@59245
|
114 |
" (Repeat " ^
|
wneuper@59245
|
115 |
" ((Repeat (Rewrite xfoldr_Nil False)) Or " ^
|
wneuper@59245
|
116 |
" (Repeat (Rewrite xfoldr_Cons False)) Or " ^
|
wneuper@59245
|
117 |
" (Repeat (Rewrite ins_Nil False)) Or " ^
|
wneuper@59245
|
118 |
" (Repeat (Rewrite ins_Cons False)) Or " ^
|
wneuper@59245
|
119 |
" (Repeat (Rewrite sort_deff False)) Or " ^
|
wneuper@59245
|
120 |
" (Repeat (Rewrite o_id False)) Or " ^
|
wneuper@59245
|
121 |
" (Repeat (Rewrite o_assoc False)) Or " ^
|
wneuper@59245
|
122 |
" (Repeat (Rewrite o_apply False)) Or " ^
|
wneuper@59245
|
123 |
" (Repeat (Rewrite id_apply False)) Or " ^
|
wneuper@59245
|
124 |
" (Repeat (Calculate le )) Or " ^
|
wneuper@59245
|
125 |
" (Repeat (Rewrite If_def False)) Or " ^
|
wneuper@59245
|
126 |
" (Repeat (Rewrite if_True False)) Or " ^
|
wneuper@59245
|
127 |
" (Repeat (Rewrite if_False False)))) uns" ^
|
wneuper@59237
|
128 |
" )")
|
wneuper@59229
|
129 |
]
|
wneuper@59229
|
130 |
*}
|
wneuper@59243
|
131 |
|
wneuper@59243
|
132 |
subsection {* CAS-commands *}
|
wneuper@59243
|
133 |
ML {*
|
wneuper@59244
|
134 |
(* for starting a new example, e.g. "Sort {|| 1, 3, 2 ||}" after <NEW> on WorkSheet;
|
wneuper@59243
|
135 |
associate above input with dataformat of pbt (without "#Given", etc):
|
wneuper@59243
|
136 |
*)
|
wneuper@59243
|
137 |
fun argl2dtss [t] =
|
wneuper@59243
|
138 |
[(@{term "unsorted"}, [t]),
|
wneuper@59243
|
139 |
(@{term "sorted"}, [@{term "DUMMY::int xlist"}])
|
wneuper@59243
|
140 |
]
|
wneuper@59243
|
141 |
| argl2dtss _ = error "InsSort.thy: wrong argument for argl2dtss"
|
wneuper@59243
|
142 |
*}
|
wneuper@59243
|
143 |
(* associate input with other data required for formal specification *)
|
wneuper@59243
|
144 |
setup {* KEStore_Elems.add_cas
|
wneuper@59243
|
145 |
[ ( @{term "Sort"},
|
wneuper@59246
|
146 |
(("InsSort", ["insertion","SORT","Programming"], ["no_met"]), argl2dtss)
|
wneuper@59243
|
147 |
)
|
wneuper@59243
|
148 |
]
|
wneuper@59243
|
149 |
*}
|
wneuper@59243
|
150 |
|
neuper@37906
|
151 |
end
|