1 (* Title: functions on lists for Scripts
2 Author: Walther Neuper 0108
3 (c) due to copyright terms
6 theory ListC imports Complex_Main
7 uses ("library.sml")("calcelems.sml")
8 ("ProgLang/term.sml")("ProgLang/calculate.sml")
9 ("ProgLang/rewrite.sml")
11 use "library.sml" (*indent,...*)
12 use "calcelems.sml" (*str_of_type, Thm,...*)
13 use "ProgLang/term.sml" (*num_str,...*)
14 use "ProgLang/calculate.sml" (*???*)
15 use "ProgLang/rewrite.sml" (*?*** At command "end" (line 205../ListC.thy*)
17 text {* 'nat' in List.thy replaced by 'real' *}
19 primrec length_' :: "'a list => real"
21 LENGTH_NIL: "length_' [] = 0" (*length: 'a list => nat*)
22 | LENGTH_CONS: "length_' (x#xs) = 1 + length_' xs"
24 primrec del :: "['a list, 'a] => 'a list"
26 del_base: "del [] x = []"
27 | del_rec: "del (y#ys) x = (if x = y then ys else y#(del ys x))"
30 list_diff :: "['a list, 'a list] => 'a list" (* as -- bs *)
31 ("(_ --/ _)" [66, 66] 65)
32 where "a -- b == foldl del a b"
34 consts nth_' :: "[real, 'a list] => 'a"
36 (*** more than one non-variable in pattern in "nth_ 1 [x] = x"--*)
37 NTH_NIL: "nth_' 1 (x#xs) = x"
38 (* NTH_CONS: "nth_' n (x#xs) = nth_' (n+ -1) xs" *)
40 (*rewriter does not reach base case ...... ;
41 the condition involves another rule set (erls, eval_binop in Atools):*)
42 NTH_CONS: "1 < n ==> nth_' n (x#xs) = nth_' (n+ - 1) xs"
44 (*primrec from Isabelle/src/HOL/List.thy -- def.twice not allowed*)
46 hd_thm: "hd(x#xs) = x"
49 tl_Cons: "tl(x#xs) = xs"
51 null_Nil: "null([]) = True"
52 null_Cons: "null(x#xs) = False"
54 LAST: "last(x#xs) = (if xs=[] then x else last xs)"
56 butlast_Nil: "butlast [] = []"
57 butlast_Cons: "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
59 mem_Nil: "x mem [] = False"
60 mem_Cons: "x mem (y#ys) = (if y=x then True else x mem ys)"
61 (*primrec-------already named---
63 "set (x#xs) = insert x (set xs)"
65 list_all_Nil "list_all P [] = True"
66 list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
69 map_Nil: "map f [] = []"
70 map_Cons: "map f (x#xs) = f(x)#map f xs"
72 append_Nil: "[] @ys = ys"
73 append_Cons: "(x#xs)@ys = x#(xs@ys)"
75 rev_Nil: "rev([]) = []"
76 rev_Cons: "rev(x#xs) = rev(xs) @ [x]"
78 filter_Nil: "filter P [] = []"
79 filter_Cons: "filter P (x#xs) =(if P x then x#filter P xs else filter P xs)"
80 (*primrec-------already named---
81 foldl_Nil "foldl f a [] = a"
82 foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs"
85 foldr_Nil: "foldr f [] a = a"
86 foldr_Cons: "foldr f (x#xs) a = f x (foldr f xs a)"
88 concat_Nil: "concat([]) = []"
89 concat_Cons: "concat(x#xs) = x @ concat(xs)"
90 (*primrec-------already named---
91 drop_Nil "drop n [] = []"
92 drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
93 (* Warning: simpset does not contain this definition but separate theorems
96 take_Nil "take n [] = []"
97 take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
98 (* Warning: simpset does not contain this definition but separate theorems
101 nth_Cons "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
102 (* Warning: simpset does not contain this definition but separate theorems
106 "(x#xs)[i:=v] = (case i of 0 => v # xs
107 | Suc j => x # xs[j:=v])"
110 takeWhile_Nil: "takeWhile P [] = []"
112 "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
114 dropWhile_Nil: "dropWhile P [] = []"
116 "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
118 zip_Nil: "zip xs [] = []"
119 zip_Cons: "zip xs (y#ys) =(case xs of [] => [] | z#zs =>(z,y)#zip zs ys)"
120 (* Warning: simpset does not contain this definition but separate theorems
121 for xs=[] / xs=z#zs *)
124 upt_Suc "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
127 distinct_Nil: "distinct [] = True"
128 distinct_Cons: "distinct (x#xs) = (x ~: set xs & distinct xs)"
130 remdups_Nil: "remdups [] = []"
131 remdups_Cons: "remdups (x#xs) =
132 (if x : set xs then remdups xs else x # remdups xs)"
133 (*primrec-------already named---
134 replicate_0 "replicate 0 x = []"
135 replicate_Suc "replicate (Suc n) x = x # replicate n x"
138 (** Lexicographic orderings on lists ...!!!**)
140 ML{* (*the former ListC.ML*)
141 (** rule set for evaluating listexpr in scripts **)
143 Rls{id="list_rls",preconds = [], rew_ord = ("dummy_ord",dummy_ord),
144 erls = e_rls, srls = Erls, calc = [], (*asm_thm=[],*)
145 rules = (*8.01: copied from*)
146 [Thm ("refl", num_str @{thm refl}), (*'a<>b -> FALSE' by fun eval_equal*)
147 Thm ("o_apply", num_str @{thm o_apply}),
149 Thm ("NTH_CONS",num_str @{thm NTH_CONS}),(*erls for cond. in Atools.ML*)
150 Thm ("NTH_NIL",num_str @{thm NTH_NIL}),
151 Thm ("append_Cons",num_str @{thm append_Cons}),
152 Thm ("append_Nil",num_str @{thm append_Nil}),
153 Thm ("butlast_Cons",num_str @{thm butlast_Cons}),
154 Thm ("butlast_Nil",num_str @{thm butlast_Nil}),
155 Thm ("concat_Cons",num_str @{thm concat_Cons}),
156 Thm ("concat_Nil",num_str @{thm concat_Nil}),
157 Thm ("del_base",num_str @{thm del_base}),
158 Thm ("del_rec",num_str @{thm del_rec}),
160 Thm ("distinct_Cons",num_str @{thm distinct_Cons}),
161 Thm ("distinct_Nil",num_str @{thm distinct_Nil}),
162 Thm ("dropWhile_Cons",num_str @{thm dropWhile_Cons}),
163 Thm ("dropWhile_Nil",num_str @{thm dropWhile_Nil}),
164 Thm ("filter_Cons",num_str @{thm filter_Cons}),
165 Thm ("filter_Nil",num_str @{thm filter_Nil}),
166 Thm ("foldr_Cons",num_str @{thm foldr_Cons}),
167 Thm ("foldr_Nil",num_str @{thm foldr_Nil}),
168 Thm ("hd_thm",num_str @{thm hd_thm}),
169 Thm ("LAST",num_str @{thm LAST}),
170 Thm ("LENGTH_CONS",num_str @{thm LENGTH_CONS}),
171 Thm ("LENGTH_NIL",num_str @{thm LENGTH_NIL}),
172 Thm ("list_diff_def",num_str @{thm list_diff_def}),
173 Thm ("map_Cons",num_str @{thm map_Cons}),
174 Thm ("map_Nil",num_str @{thm map_Cons}),
175 Thm ("mem_Cons",num_str @{thm mem_Cons}),
176 Thm ("mem_Nil",num_str @{thm mem_Nil}),
177 Thm ("null_Cons",num_str @{thm null_Cons}),
178 Thm ("null_Nil",num_str @{thm null_Nil}),
179 Thm ("remdups_Cons",num_str @{thm remdups_Cons}),
180 Thm ("remdups_Nil",num_str @{thm remdups_Nil}),
181 Thm ("rev_Cons",num_str @{thm rev_Cons}),
182 Thm ("rev_Nil",num_str @{thm rev_Nil}),
183 Thm ("take_Nil",num_str @{thm take_Nil}),
184 Thm ("take_Cons",num_str @{thm take_Cons}),
185 Thm ("tl_Cons",num_str @{thm tl_Cons}),
186 Thm ("tl_Nil",num_str @{thm tl_Nil}),
187 Thm ("zip_Cons",num_str @{thm zip_Cons}),
188 Thm ("zip_Nil",num_str @{thm zip_Nil})
189 ], scr = EmptyScr}:rls;
193 ruleset' := overwritelthy @{theory} (!ruleset',
194 [("list_rls",list_rls)