test/Tools/isac/Knowledge/inssort.sml
author Walther Neuper <neuper@ist.tugraz.at>
Wed, 05 Dec 2012 15:56:38 +0100
changeset 48790 98df8f6dc3f9
parent 38063 d2bdc4095e73
child 59188 c477d0f79ab9
permissions -rw-r--r--
Test_Isac.thy works until "Interpret/mstools.sml"
neuper@37906
     1
(* use"test-inssort.sml";
neuper@37906
     2
   W.N.17.6.00
neuper@37906
     3
*)
neuper@38063
     4
"--------------------------------------------------------";
neuper@38063
     5
"--------------------------------------------------------";
neuper@38063
     6
"table of contents --------------------------------------";
neuper@38063
     7
"--------------------------------------------------------";
neuper@38063
     8
"----------- inssort in SML -----------------------------";
neuper@38063
     9
"--------------------------------------------------------";
neuper@38063
    10
"--------------------------------------------------------";
neuper@38063
    11
neuper@38063
    12
neuper@38063
    13
"----------- inssort in SML -----------------------------";
neuper@38063
    14
"----------- inssort in SML -----------------------------";
neuper@38063
    15
"----------- inssort in SML -----------------------------";
neuper@38063
    16
fun foldr _ [] a = a
neuper@38063
    17
  | foldr f (x :: xs) a = foldr f xs (f a x);
neuper@38063
    18
(*val foldr = fn : ('a -> 'b -> 'a) -> 'b list -> 'a -> 'a*)
neuper@38063
    19
fun ins [] a = [a]
neuper@38063
    20
  | ins (x :: xs) a = if x < a then x :: (ins xs a) else a :: (x :: xs);
neuper@38063
    21
(*val ins = fn : int list -> int -> int list*)
neuper@38063
    22
fun sort xs = foldr ins xs [];
neuper@38063
    23
(*val sort = fn : int list -> int list*)
neuper@38063
    24
neuper@38063
    25
(*========== inhibit exn =======================================================
neuper@37906
    26
neuper@37906
    27
(* insertion sort, would need lists different from script-lists WN.11.00
neuper@37906
    28
WN.7.6.03: -"- started with someList :: 'a list => unl, fun dest_list *)
neuper@37906
    29
neuper@37906
    30
"--------------- sort [1,4,3,2] by rewrite_set ----------------";
neuper@37991
    31
val thy' = "InsSort";
neuper@37906
    32
val ct = "sort [1,4,3,2]";
neuper@37906
    33
"--- 1 ---";
neuper@37906
    34
val rls = "ins_sort";
neuper@37906
    35
val (ct,_) = the (rewrite_set thy' "eval_rls" false rls ct);
neuper@37906
    36
if ct="[1, 2, 3, 4]" then "sort [1,4,3,2] OK"
neuper@38031
    37
else error "sort [1,4,3,2] didn't work";
neuper@37906
    38
neuper@37906
    39
neuper@37906
    40
"---------------- sort [1,3,2] by rewrite stepwise ----------------";
neuper@37991
    41
val thy' = "InsSort";
neuper@37906
    42
val ct = "sort [1,3,2]";
neuper@37906
    43
"--- 1 ---";
neuper@37906
    44
val thm = ("sort_def","");
neuper@37906
    45
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    46
(*val ct = "foldr ins [#1::real, #3::real, #2::real] []"*)
neuper@37906
    47
"--- 2 ---";
neuper@37906
    48
val thm = ("foldr_rec","");
neuper@37906
    49
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    50
(*val ct = "foldr ins [#3, #2] (ins [] #1)"*)
neuper@37906
    51
"--- 3 ---";
neuper@37906
    52
val thm = ("ins_base","");
neuper@37906
    53
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    54
(*val ct = "foldr ins [#3, #2] [#1]"*)
neuper@37906
    55
"--- 4 ---";
neuper@37906
    56
val thm = ("foldr_rec","");
neuper@37906
    57
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    58
(*val ct = "foldr ins [#2] (ins [#1] #3)"*)
neuper@37906
    59
"--- 5 ---";
neuper@37906
    60
val thm = ("ins_rec","");
neuper@37906
    61
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    62
(*val ct = "foldr ins [#2] (if #1 < #3 then #1 # ins [] #3 else [#3, #1])"*)
neuper@37906
    63
"--- 6 ---";
neuper@37906
    64
val op_ = "le";
neuper@37906
    65
val (ct,_) = the (calculate thy' op_ ct);
neuper@37906
    66
(*val ct = "foldr ins [#2] (if True then #1 # ins [] #3 else [#3, #1])"*)
neuper@37906
    67
"--- 7 ---";
neuper@37906
    68
val thm = ("if_True","");
neuper@37906
    69
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    70
(*val ct = "foldr ins [#2] (#1 # ins [] #3)"*)
neuper@37906
    71
"--- 8 ---";
neuper@37906
    72
val thm = ("ins_base","");
neuper@37906
    73
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    74
(*val ct = "foldr ins [#2] [#1, #3]"*)
neuper@37906
    75
"--- 9 ---";
neuper@37906
    76
val thm = ("foldr_rec","");
neuper@37906
    77
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    78
(*val ct = "foldr ins [] (ins [#1, #3] #2)"*)
neuper@37906
    79
"--- 10 ---";
neuper@37906
    80
val thm = ("ins_rec","");
neuper@37906
    81
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    82
(*val ct = "foldr ins [] (if #1 < #2 then #1 # ins [#3] #2 else [#2, #1, #3])"*)
neuper@37906
    83
"--- 11 ---";
neuper@37906
    84
val op_ = "le";
neuper@37906
    85
val (ct,_) = the (calculate thy' op_ ct);
neuper@37906
    86
(*val ct = "foldr ins [] (if True then #1 # ins [#3] #2 else [#2, #1, #3])"*)
neuper@37906
    87
"--- 12 ---";
neuper@37906
    88
val thm = ("if_True","");
neuper@37906
    89
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    90
(*"foldr ins [] (#1 # ins [#3] #2)"*)
neuper@37906
    91
"--- 13 ---";
neuper@37906
    92
val thm = ("ins_rec","");
neuper@37906
    93
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
    94
(*"foldr ins [] (#1 # (if #3 < #2 then #3 # ins [] #2 else [#2, #3]))"*)
neuper@37906
    95
"--- 14 ---";
neuper@37906
    96
val op_ = "le";
neuper@37906
    97
val (ct,_) = the (calculate thy' op_ ct);
neuper@37906
    98
(*val ct = "foldr ins [] (#1 # (if False then #3 # ins [] #2 else [#2, #3]))"*)
neuper@37906
    99
"--- 15 ---";
neuper@37906
   100
val thm = ("if_False","");
neuper@37906
   101
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
   102
(*val ct = "foldr ins [] [#1, #2, #3]"*)
neuper@37906
   103
"--- 16 ---";
neuper@37906
   104
val thm = ("foldr_base","");
neuper@37906
   105
val (ct,_) = the (rewrite thy' "tless_true" "eval_rls" false thm ct);
neuper@37906
   106
(*val ct = "[#1, #2, #3]"*)
neuper@37906
   107
if ct="[1, 2, 3]" then "sort [1,3,2] OK"
neuper@38031
   108
else error "sort [1,3,2] didn't work";
neuper@37906
   109
neuper@37906
   110
neuper@37906
   111
"---------------- sort [1,3,2] from script ----------------";
neuper@37906
   112
val fmz = ["unsorted [1,3,2]", "sorted S"];
neuper@37906
   113
val (dI',pI',mI') = 
neuper@37991
   114
  ("InsSort", ["inssort","functional"], ("InsSort","inssort"));
neuper@37906
   115
val p = e_pos'; val c = []; 
neuper@37906
   116
neuper@37906
   117
neuper@37906
   118
neuper@37906
   119
(* ------- 17.6.00: mit kleinen problemen aufgegeben
neuper@48790
   120
val scr=Prog ((term_of o the o (parse thy))
neuper@37906
   121
	       "Script Sort (u_::'a list) =   \
neuper@37906
   122
		\ Rewrite_Set ins_sort False u_");
neuper@37906
   123
neuper@48790
   124
val scr=Prog ((term_of o the o (parse thy))
neuper@37906
   125
      "Script Ins_sort (u_::real list) =          \
neuper@37906
   126
       \ (let u_ = Rewrite sort_def   False u_; \
neuper@37906
   127
       \      u_ = Rewrite foldr_rec  False u_; \
neuper@37906
   128
       \      u_ = Rewrite ins_base   False u_; \
neuper@37906
   129
       \      u_ = Rewrite foldr_rec  False u_; \
neuper@37906
   130
       \      u_ = Rewrite ins_rec    False u_; \
neuper@37906
   131
       \      u_ = Calculate le u_;             \
neuper@37906
   132
       \      u_ = Rewrite if_True    False u_; \
neuper@37906
   133
       \      u_ = Rewrite ins_base   False u_; \
neuper@37906
   134
       \      u_ = Rewrite foldr_rec  False u_; \
neuper@37906
   135
       \      u_ = Rewrite ins_rec    False u_; \
neuper@37906
   136
       \      u_ = Calculate le u_;             \
neuper@37906
   137
       \      u_ = Rewrite if_True    False u_; \
neuper@37906
   138
       \      u_ = Rewrite ins_rec    False u_; \
neuper@37906
   139
       \      u_ = Calculate le u_;             \
neuper@37906
   140
       \      u_ = Rewrite if_False   False u_; \
neuper@37906
   141
       \      u_ = Rewrite foldr_base False u_  \
neuper@37906
   142
       \  in u_)");
neuper@37906
   143
val scr=parse thy
neuper@37906
   144
      "Script Ins_sort (u_::real list) =          \
neuper@37906
   145
       \ (let u_ = Rewrite sort_def   False u_; \
neuper@37906
   146
       \      u_ = Rewrite foldr_rec  False u_; \
neuper@37906
   147
       \      u_ = Rewrite ins_base   False u_; \
neuper@37906
   148
       \      u_ = Rewrite foldr_rec  False u_; \
neuper@37906
   149
       \      u_ = Rewrite ins_rec    False u_; \
neuper@37906
   150
       \      u_ = Calculate le u_;             \
neuper@37906
   151
       \      u_ = Rewrite if_True    False u_; \
neuper@37906
   152
       \      u_ = Rewrite ins_base   False u_; \
neuper@37906
   153
       \      u_ = Rewrite foldr_rec  False u_; \
neuper@37906
   154
       \      u_ = Rewrite ins_rec    False u_; \
neuper@37906
   155
       \      u_ = u_   \
neuper@37906
   156
       \  in u_)";
neuper@37906
   157
neuper@37906
   158
atomty (term_of (the scr));
neuper@37906
   159
neuper@38063
   160
------- *)
neuper@38063
   161
============ inhibit exn =====================================================*)