src/Tools/isac/Knowledge/InsSort.thy
author wneuper <walther.neuper@jku.at>
Fri, 07 May 2021 18:12:51 +0200
changeset 60278 343efa173023
parent 60154 2ab0d1523731
child 60286 31efa1b39a20
permissions -rw-r--r--
* WN: simplify const names like "is'_expanded"
walther@60077
     1
theory InsSort imports "Interpret.Interpret" Rational(*for norm_Rational?!?*)
wneuper@59229
     2
begin
neuper@37906
     3
wneuper@59472
     4
section \<open>consts\<close>
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@59472
    12
section \<open>functions\<close>
wneuper@59234
    13
primrec ins :: "int \<Rightarrow> int xlist \<Rightarrow> int xlist"
wneuper@59229
    14
where
wneuper@59244
    15
ins_Nil:  "ins i {|| ||} = {||i||}" |
wneuper@59234
    16
ins_Cons: "ins i (x @# xs) = (if i < x then (i @# x @# xs) else x @# (ins i xs))"
neuper@37906
    17
wneuper@59425
    18
consts sort :: "int xlist \<Rightarrow> int xlist"
wneuper@59425
    19
axiomatization where
wneuper@59425
    20
sort_deff [code]: "sort xs = xfoldr ins xs {|| ||}"
wneuper@59425
    21
(* this broke with 406681ebe781 ERROR "partial_function: shift respective thys to ProgLang" with
wneuper@59425
    22
   ERROR Tactic failed
wneuper@59425
    23
   The error(s) above occurred for the goal statement\<^here>:
wneuper@59425
    24
   sort xs = xfoldr ins xs {|| ||}
wneuper@59234
    25
fun sort :: "int xlist \<Rightarrow> int xlist"
wneuper@59229
    26
where
wneuper@59244
    27
sort_deff: "sort xs = xfoldr ins xs {|| ||}"
wneuper@59425
    28
*)
wneuper@59233
    29
print_theorems
wneuper@59343
    30
(* thm sort_def  InsSort.sort \<equiv> sort_sumC *)
wneuper@59233
    31
thm sort_deff (* InsSort.sort ?xs = foldr ins ?xs [] *)
neuper@37906
    32
wneuper@59229
    33
(* access to definitions from ML *)
wneuper@59472
    34
ML \<open>
wneuper@59231
    35
@{thm ins.simps(1)};
wneuper@59231
    36
@{thm ins.simps(2)};
wneuper@59472
    37
\<close>
neuper@37906
    38
wneuper@59244
    39
value "sort {||2,3,1||}"
wneuper@59425
    40
(* this broke with 406681ebe781 ERROR "Failed to apply initial proof method\<^here>:" with
wneuper@59244
    41
lemma test: "sort {||2,3,1||} = {||1,2,3||}"
wneuper@59425
    42
by auto
wneuper@59425
    43
*)
neuper@37954
    44
wneuper@59472
    45
section \<open>eval functions\<close>
wneuper@59472
    46
section \<open>term order\<close>
wneuper@59472
    47
section \<open>rulesets\<close>
wneuper@59472
    48
ML \<open>
wneuper@59229
    49
val ins_sort = 
walther@59851
    50
  Rule_Def.Repeat {
walther@59852
    51
    id = "ins_sort", preconds = [], rew_ord = ("tless_true", tless_true), erls = Rule_Set.empty,
walther@59852
    52
    srls = Rule_Set.empty, calc = [], rules = [
wneuper@59416
    53
      Rule.Thm ("xfoldr_Nil",(*num_str*) @{thm xfoldr_Nil} (* foldr ?f [] = id *)),
wneuper@59416
    54
	    Rule.Thm ("xfoldr_Cons", @{thm xfoldr_Cons} (* foldr ?f (?x # ?xs) = ?f ?x \<circ> foldr ?f ?xs *)),
wneuper@59232
    55
wneuper@59416
    56
	    Rule.Thm ("ins_Nil", @{thm ins_Nil} (* ins ?i [] = [?i] *)),
wneuper@59416
    57
	    Rule.Thm ("ins_Cons", @{thm ins_Cons} (* ins ?i (?x # ?xs) = (if ?i < ?x then ?i # ?x # ?xs else ?x # ins ?i ?xs) *)),
wneuper@59416
    58
	    Rule.Thm ("sort_deff", @{thm sort_deff} (* InsSort.sort ?xs = foldr ins ?xs [] *)),
wneuper@59232
    59
wneuper@59416
    60
	    Rule.Thm ("o_id", @{thm o_id} (* ?f \<circ> id = ?f *)),
wneuper@59416
    61
	    Rule.Thm ("o_assoc", @{thm o_assoc} (* ?f \<circ> (?g \<circ> ?h) = ?f \<circ> ?g \<circ> ?h *)),
wneuper@59416
    62
	    Rule.Thm ("o_apply", @{thm o_apply} (* (?f \<circ> ?g) ?x = ?f (?g ?x) *)),
wneuper@59416
    63
	    Rule.Thm ("id_apply", @{thm id_apply} (* id ?x = ?x *)),
wneuper@59229
    64
      
walther@59878
    65
	    Rule.Eval ("Orderings.ord_class.less", Prog_Expr.eval_equ "#less_"),
wneuper@59416
    66
	    Rule.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@59416
    67
	    Rule.Thm ("if_True", @{thm if_True} (* "(if True then x else y) = x" *)),
wneuper@59416
    68
	    Rule.Thm ("if_False", @{thm if_False} (* "(if False then x else y) = y" *))],
walther@59878
    69
	  errpatts = [], scr = Rule.Empty_Prog};      
wneuper@59472
    70
\<close>
wneuper@59472
    71
setup \<open>KEStore_Elems.add_rlss [("ins_sort", (Context.theory_name @{theory}, ins_sort))]\<close>
neuper@37954
    72
wneuper@59472
    73
section \<open>problems\<close>
wneuper@59472
    74
setup \<open>KEStore_Elems.add_pbts
walther@60022
    75
  [ Problem.prep_input @{theory} "pbl_Programming" [] Problem.id_empty 
walther@60022
    76
     (["Programming"], [], Rule_Set.empty, NONE, []),
walther@60022
    77
    Problem.prep_input @{theory} "pbl_Prog_sort" [] Problem.id_empty 
walther@60022
    78
     (["SORT", "Programming"], [], Rule_Set.empty, NONE, []),
walther@60022
    79
    Problem.prep_input @{theory} "pbl_Prog_sort_ins" [] Problem.id_empty
walther@59997
    80
     (["insertion", "SORT", "Programming"], 
wneuper@59229
    81
     [("#Given", ["unsorted u_u"]), ("#Find", ["sorted s_s"])], 
walther@59852
    82
     Rule_Set.empty, 
walther@60022
    83
     SOME "Sort u_u", [["Programming", "SORT", "insertion_steps"]])]\<close>
wneuper@59229
    84
wneuper@59472
    85
section \<open>methods\<close>
wneuper@59229
    86
(* TODO: implementation needs extra object-level lists ?!?*)
wneuper@59472
    87
setup \<open>KEStore_Elems.add_mets
walther@60154
    88
  [ MethodC.prep_input @{theory} "met_Programming" [] MethodC.id_empty
wneuper@59229
    89
      (["Programming"], [],
walther@59852
    90
        {rew_ord'="tless_true",rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty,
walther@59852
    91
          crls = Rule_Set.empty, errpats = [], nrls = Rule_Set.empty}, @{thm refl}),
walther@60154
    92
    MethodC.prep_input @{theory} "met_Prog_sort" [] MethodC.id_empty
walther@59997
    93
      (["Programming", "SORT"], [],
walther@59852
    94
        {rew_ord'="tless_true",rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty,
walther@59852
    95
          crls = Rule_Set.empty, errpats = [], nrls = Rule_Set.empty},
wneuper@59545
    96
        @{thm refl})]
wneuper@59473
    97
\<close>
wneuper@59545
    98
wneuper@59504
    99
partial_function (tailrec) sort_program :: "int xlist => int xlist"
wneuper@59504
   100
  where
walther@59635
   101
"sort_program unso = (
walther@59635
   102
  let
wneuper@59504
   103
    uns = Take (sort unso)
wneuper@59504
   104
  in
walther@59635
   105
    (Rewrite_Set ''ins_sort'') uns)"
wneuper@59473
   106
setup \<open>KEStore_Elems.add_mets
walther@60154
   107
   [MethodC.prep_input @{theory} "met_Prog_sort_ins" [] MethodC.id_empty
walther@59997
   108
      (["Programming", "SORT", "insertion"], 
wneuper@59240
   109
      [("#Given", ["unsorted u_u"]), ("#Find", ["sorted s_s"])],
walther@59852
   110
        {rew_ord'="tless_true",rls' = Atools_erls, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty,
wneuper@59229
   111
          crls = Atools_crls, errpats = [], nrls = norm_Rational},
wneuper@59551
   112
        @{thm sort_program.simps})]
wneuper@59473
   113
\<close>
wneuper@59545
   114
wneuper@59504
   115
partial_function (tailrec) sort_program2 :: "int xlist => int xlist"
wneuper@59504
   116
  where 
walther@59635
   117
"sort_program2 unso = (
walther@59635
   118
  let
walther@59635
   119
    uns = Take (sort unso) 
walther@59635
   120
  in (
walther@59635
   121
    Repeat (
walther@59635
   122
      (Repeat (Rewrite ''xfoldr_Nil'')) Or
walther@59635
   123
      (Repeat (Rewrite ''xfoldr_Cons'')) Or
walther@59635
   124
      (Repeat (Rewrite ''ins_Nil'')) Or
walther@59635
   125
      (Repeat (Rewrite ''ins_Cons'')) Or
walther@59635
   126
      (Repeat (Rewrite ''sort_deff'')) Or
walther@59635
   127
      (Repeat (Rewrite ''o_id'')) Or
walther@59635
   128
      (Repeat (Rewrite ''o_assoc'')) Or
walther@59635
   129
      (Repeat (Rewrite ''o_apply'')) Or
walther@59635
   130
      (Repeat (Rewrite ''id_apply'')) Or
walther@59635
   131
      (Repeat (Calculate ''le'')) Or
walther@59635
   132
      (Repeat (Rewrite ''If_def'')) Or
walther@59635
   133
      (Repeat (Rewrite ''if_True'')) Or
walther@59635
   134
      (Repeat (Rewrite ''if_False'')))
walther@59635
   135
    ) uns)"
wneuper@59473
   136
setup \<open>KEStore_Elems.add_mets
walther@60154
   137
   [MethodC.prep_input @{theory} "met_Prog_sort_ins_steps" [] MethodC.id_empty
walther@59997
   138
      (["Programming", "SORT", "insertion_steps"], 
wneuper@59245
   139
      [("#Given", ["unsorted u_u"]), ("#Find", ["sorted s_s"])],
walther@59852
   140
        {rew_ord'="tless_true",rls' = Atools_erls, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty,
wneuper@59245
   141
          crls = Atools_crls, errpats = [], nrls = norm_Rational},
wneuper@59551
   142
        @{thm sort_program2.simps})]
wneuper@59472
   143
\<close>
wneuper@59243
   144
wneuper@59472
   145
subsection \<open>CAS-commands\<close>
wneuper@59472
   146
ML \<open>
wneuper@59244
   147
(* for starting a new example, e.g. "Sort {|| 1, 3, 2 ||}" after <NEW> on WorkSheet;
walther@59799
   148
   combine this input with dataformat of pbt (without "#Given", etc):
wneuper@59243
   149
*)
wneuper@59243
   150
fun argl2dtss [t] =
wneuper@59243
   151
    [(@{term "unsorted"}, [t]),
wneuper@59243
   152
     (@{term "sorted"},   [@{term "DUMMY::int xlist"}])
wneuper@59243
   153
     ]
walther@59962
   154
  | argl2dtss _ = raise ERROR "InsSort.thy: wrong argument for argl2dtss"
wneuper@59472
   155
\<close>
walther@59799
   156
(* combine input with other data required for formal specification *)
wneuper@59472
   157
setup \<open>KEStore_Elems.add_cas
wneuper@59243
   158
  [ ( @{term "Sort"},  
walther@59997
   159
      (("InsSort", ["insertion", "SORT", "Programming"], ["no_met"]), argl2dtss)
wneuper@59243
   160
    )
wneuper@59243
   161
  ]
walther@60278
   162
\<close> ML \<open>
walther@60278
   163
\<close> ML \<open>
wneuper@59472
   164
\<close>
neuper@37906
   165
end