author | haftmann |
Thu, 19 Jun 2025 17:15:40 +0200 | |
changeset 82734 | 89347c0cc6a3 |
parent 82081 | 50dd4fc40fcb |
permissions | -rw-r--r-- |
79490 | 1 |
|
2 |
signature TIMING_FUNCTIONS = |
|
3 |
sig |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
4 |
type 'a wctxt = { |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
5 |
ctxt: local_theory, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
6 |
origins: term list, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
7 |
f: term -> 'a |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
8 |
} |
79490 | 9 |
type 'a converter = { |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
10 |
constc : 'a wctxt -> term -> 'a, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
11 |
funcc : 'a wctxt -> term -> term list -> 'a, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
12 |
ifc : 'a wctxt -> typ -> term -> term -> term -> 'a, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
13 |
casec : 'a wctxt -> term -> term list -> 'a, |
81147 | 14 |
letc : 'a wctxt -> typ -> term -> (string * typ) list -> term -> 'a |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
15 |
} |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
16 |
val walk : local_theory -> term list -> 'a converter -> term -> 'a |
81147 | 17 |
val Iconst : term wctxt -> term -> term |
18 |
val Ifunc : term wctxt -> term -> term list -> term |
|
19 |
val Iif : term wctxt -> typ -> term -> term -> term -> term |
|
20 |
val Icase : term wctxt -> term -> term list -> term |
|
21 |
val Ilet : term wctxt -> typ -> term -> (string * typ) list -> term -> term |
|
79490 | 22 |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
23 |
type pfunc = { names : string list, terms : term list, typs : typ list } |
79490 | 24 |
val fun_pretty': Proof.context -> pfunc -> Pretty.T |
25 |
val fun_pretty: Proof.context -> Function.info -> Pretty.T |
|
26 |
val print_timing': Proof.context -> pfunc -> pfunc -> unit |
|
27 |
val print_timing: Proof.context -> Function.info -> Function.info -> unit |
|
28 |
||
82081 | 29 |
type time_config = { |
30 |
print: bool, |
|
31 |
simp: bool, |
|
32 |
partial: bool |
|
33 |
} |
|
34 |
datatype result = Function of Function.info | PartialFunction of thm |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
35 |
val reg_and_proove_time_func: local_theory -> term list -> term list |
82081 | 36 |
-> time_config -> result * local_theory |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
37 |
val reg_time_func: local_theory -> term list -> term list |
82081 | 38 |
-> time_config -> result * local_theory |
39 |
||
79490 | 40 |
|
41 |
val time_dom_tac: Proof.context -> thm -> thm list -> int -> tactic |
|
42 |
||
82081 | 43 |
|
79490 | 44 |
end |
45 |
||
46 |
structure Timing_Functions : TIMING_FUNCTIONS = |
|
47 |
struct |
|
48 |
(* Configure config variable to adjust the prefix *) |
|
49 |
val bprefix = Attrib.setup_config_string @{binding "time_prefix"} (K "T_") |
|
81147 | 50 |
val bprefix_snd = Attrib.setup_config_string @{binding "time_prefix_snd"} (K "T2_") |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
51 |
(* Configure config variable to adjust the suffix *) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
52 |
val bsuffix = Attrib.setup_config_string @{binding "time_suffix"} (K "") |
79490 | 53 |
|
54 |
(* Extracts terms from function info *) |
|
55 |
fun terms_of_info (info: Function.info) = |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
56 |
map Thm.prop_of (case #simps info of SOME s => s |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
57 |
| NONE => error "No terms of function found in info") |
79490 | 58 |
|
59 |
type pfunc = { |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
60 |
names : string list, |
79490 | 61 |
terms : term list, |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
62 |
typs : typ list |
79490 | 63 |
} |
64 |
fun info_pfunc (info: Function.info): pfunc = |
|
65 |
let |
|
66 |
val {defname, fs, ...} = info; |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
67 |
val T = case hd fs of (Const (_,T)) => T |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
68 |
| (Free (_,T)) => T |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
69 |
| _ => error "Internal error: Invalid info to print" |
79490 | 70 |
in |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
71 |
{ names=[Binding.name_of defname], terms=terms_of_info info, typs=[T] } |
79490 | 72 |
end |
73 |
||
74 |
(* Auxiliary functions for printing functions *) |
|
75 |
fun fun_pretty' ctxt (pfunc: pfunc) = |
|
76 |
let |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
77 |
val {names, terms, typs} = pfunc; |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
78 |
val header_beg = Pretty.str "fun "; |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
79 |
fun prepHeadCont (nm,T) = [Pretty.str (nm ^ " :: "), (Pretty.quote (Syntax.pretty_typ ctxt T))] |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
80 |
val header_content = |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
81 |
List.concat (prepHeadCont (hd names,hd typs) :: map ((fn l => Pretty.str "\nand " :: l) o prepHeadCont) (ListPair.zip (tl names, tl typs))); |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
82 |
val header_end = Pretty.str " where\n "; |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
83 |
val header = [header_beg] @ header_content @ [header_end]; |
79490 | 84 |
fun separate sep prts = |
85 |
flat (Library.separate [Pretty.str sep] (map single prts)); |
|
86 |
val ptrms = (separate "\n| " (map (Syntax.pretty_term ctxt) terms)); |
|
87 |
in |
|
88 |
Pretty.text_fold (header @ ptrms) |
|
89 |
end |
|
90 |
fun fun_pretty ctxt = fun_pretty' ctxt o info_pfunc |
|
91 |
fun print_timing' ctxt (opfunc: pfunc) (tpfunc: pfunc) = |
|
92 |
let |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
93 |
val {names, ...} = opfunc; |
79490 | 94 |
val poriginal = Pretty.item [Pretty.str "Original function:\n", fun_pretty' ctxt opfunc] |
95 |
val ptiming = Pretty.item [Pretty.str ("Running time function:\n"), fun_pretty' ctxt tpfunc] |
|
96 |
in |
|
81147 | 97 |
Pretty.writeln (Pretty.text_fold [ |
98 |
Pretty.str ("Converting " ^ (hd names) ^ (String.concat (map (fn nm => ", " ^ nm) (tl names))) ^ "\n"), |
|
99 |
poriginal, Pretty.str "\n", ptiming]) |
|
79490 | 100 |
end |
101 |
fun print_timing ctxt (oinfo: Function.info) (tinfo: Function.info) = |
|
102 |
print_timing' ctxt (info_pfunc oinfo) (info_pfunc tinfo) |
|
103 |
||
81147 | 104 |
fun print_lemma ctxt defs (T_terms: term list) = |
105 |
let |
|
106 |
val names = |
|
107 |
defs |
|
108 |
|> map snd |
|
109 |
|> map (fn s => "_" ^ s) |
|
110 |
|> List.foldr (op ^) "" |
|
111 |
val begin = "lemma T" ^ names ^ "_simps [simp,code]:\n" |
|
112 |
fun convLine T_term = |
|
113 |
" \"" ^ Syntax.string_of_term ctxt T_term ^ "\"\n" |
|
114 |
val lines = map convLine T_terms |
|
115 |
fun convDefs def = " " ^ (fst def) |
|
116 |
val proof = " by (simp_all add:" :: (map convDefs defs) @ [")"] |
|
117 |
val _ = Pretty.writeln (Pretty.str "Characteristic recursion equations can be derived:") |
|
118 |
in |
|
119 |
(begin :: lines @ proof) |
|
120 |
|> String.concat |
|
121 |
(* |> Active.sendback_markup_properties [Markup.padding_fun] *) |
|
122 |
|> Pretty.str |
|
123 |
|> Pretty.writeln |
|
124 |
end |
|
125 |
||
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
126 |
fun contains l e = exists (fn e' => e' = e) l |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
127 |
fun contains' comp l e = exists (comp e) l |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
128 |
(* Split name by . *) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
129 |
val split_name = String.fields (fn s => s = #".") |
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
130 |
|
79490 | 131 |
(* returns true if it's an if term *) |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
132 |
fun is_if (Const (@{const_name "HOL.If"},_)) = true |
79490 | 133 |
| is_if _ = false |
134 |
(* returns true if it's a case term *) |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
135 |
fun is_case (Const (n,_)) = n |> split_name |> List.last |> String.isPrefix "case_" |
79490 | 136 |
| is_case _ = false |
137 |
(* returns true if it's a let term *) |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
138 |
fun is_let (Const (@{const_name "HOL.Let"},_)) = true |
79490 | 139 |
| is_let _ = false |
140 |
(* Convert string name of function to its timing equivalent *) |
|
81147 | 141 |
fun fun_name_to_time' ctxt s second name = |
79490 | 142 |
let |
81147 | 143 |
val prefix = Config.get ctxt (if second then bprefix_snd else bprefix) |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
144 |
val suffix = (if s then Config.get ctxt bsuffix else "") |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
145 |
fun replace_last_name [n] = [prefix ^ n ^ suffix] |
79490 | 146 |
| replace_last_name (n::ns) = n :: (replace_last_name ns) |
147 |
| replace_last_name _ = error "Internal error: Invalid function name to convert" |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
148 |
val parts = split_name name |
79490 | 149 |
in |
150 |
String.concatWith "." (replace_last_name parts) |
|
151 |
end |
|
81147 | 152 |
fun fun_name_to_time ctxt s name = fun_name_to_time' ctxt s false name |
79490 | 153 |
(* Count number of arguments of a function *) |
154 |
fun count_args (Type (n, [_,res])) = (if n = "fun" then 1 + count_args res else 0) |
|
155 |
| count_args _ = 0 |
|
156 |
(* Check if number of arguments matches function *) |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
157 |
fun check_args s (t, args) = |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
158 |
(if length args = count_args (type_of t) then () |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
159 |
else error ("Partial applications/Lambdas not allowed (" ^ s ^ ")")) |
79490 | 160 |
(* Removes Abs *) |
161 |
fun rem_abs f (Abs (_,_,t)) = rem_abs f t |
|
162 |
| rem_abs f t = f t |
|
163 |
(* Map right side of equation *) |
|
164 |
fun map_r f (pT $ (eq $ l $ r)) = (pT $ (eq $ l $ f r)) |
|
165 |
| map_r _ _ = error "Internal error: No right side of equation found" |
|
166 |
(* Get left side of equation *) |
|
167 |
fun get_l (_ $ (_ $ l $ _)) = l |
|
168 |
| get_l _ = error "Internal error: No left side of equation found" |
|
169 |
(* Get right side of equation *) |
|
170 |
fun get_r (_ $ (_ $ _ $ r)) = r |
|
171 |
| get_r _ = error "Internal error: No right side of equation found" |
|
172 |
(* Return name of Const *) |
|
173 |
fun Const_name (Const (nm,_)) = SOME nm |
|
174 |
| Const_name _ = NONE |
|
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
175 |
fun is_Used (Type ("Product_Type.prod", _)) = true |
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
176 |
| is_Used _ = false |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
177 |
(* Custom compare function for types ignoring variable names *) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
178 |
fun typ_comp (Type (A,a)) (Type (B,b)) = (A = B) andalso List.foldl (fn ((c,i),s) => typ_comp c i andalso s) true (ListPair.zip (a, b)) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
179 |
| typ_comp (Type _) _ = false |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
180 |
| typ_comp _ (Type _) = false |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
181 |
| typ_comp _ _ = true |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
182 |
fun const_comp (Const (nm,T)) (Const (nm',T')) = nm = nm' andalso typ_comp T T' |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
183 |
| const_comp _ _ = false |
79490 | 184 |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
185 |
type 'a wctxt = { |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
186 |
ctxt: local_theory, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
187 |
origins: term list, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
188 |
f: term -> 'a |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
189 |
} |
79490 | 190 |
type 'a converter = { |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
191 |
constc : 'a wctxt -> term -> 'a, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
192 |
funcc : 'a wctxt -> term -> term list -> 'a, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
193 |
ifc : 'a wctxt -> typ -> term -> term -> term -> 'a, |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
194 |
casec : 'a wctxt -> term -> term list -> 'a, |
81147 | 195 |
letc : 'a wctxt -> typ -> term -> (string * typ) list -> term -> 'a |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
196 |
} |
79490 | 197 |
|
198 |
(* Walks over term and calls given converter *) |
|
81255 | 199 |
fun list_abs ([], t) = t |
200 |
| list_abs (a::abs,t) = list_abs (abs,t) |> absfree a |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
201 |
fun walk ctxt (origin: term list) (conv as {ifc, casec, funcc, letc, ...} : 'a converter) (t as _ $ _) = |
79490 | 202 |
let |
81147 | 203 |
val (f, args) = strip_comb t |
79490 | 204 |
val this = (walk ctxt origin conv) |
205 |
val _ = (case f of Abs _ => error "Lambdas not supported" | _ => ()) |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
206 |
val wctxt = {ctxt = ctxt, origins = origin, f = this} |
79490 | 207 |
in |
208 |
(if is_if f then |
|
209 |
(case f of (Const (_,T)) => |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
210 |
(case args of [cond, t, f] => ifc wctxt T cond t f |
79490 | 211 |
| _ => error "Partial applications not supported (if)") |
212 |
| _ => error "Internal error: invalid if term") |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
213 |
else if is_case f then casec wctxt f args |
79490 | 214 |
else if is_let f then |
215 |
(case f of (Const (_,lT)) => |
|
81147 | 216 |
(case args of [exp, t] => |
81255 | 217 |
let val (abs,t) = Term.strip_abs_eta 1 t in letc wctxt lT exp abs t end |
79490 | 218 |
| _ => error "Partial applications not allowed (let)") |
219 |
| _ => error "Internal error: invalid let term") |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
220 |
else funcc wctxt f args) |
79490 | 221 |
end |
222 |
| walk ctxt origin (conv as {constc, ...}) c = |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
223 |
constc {ctxt = ctxt, origins = origin, f = walk ctxt origin conv} c |
81147 | 224 |
fun Ifunc (wctxt: term wctxt) t args = list_comb (#f wctxt t,map (#f wctxt) args) |
225 |
val Iconst = K I |
|
226 |
fun Iif (wctxt: term wctxt) T cond tt tf = |
|
227 |
Const (@{const_name "HOL.If"}, T) $ (#f wctxt cond) $ (#f wctxt tt) $ (#f wctxt tf) |
|
81358 | 228 |
fun Icase (wctxt: term wctxt) t cs = list_comb |
229 |
(#f wctxt t,map (fn c => c |> Term.strip_abs_eta (c |> fastype_of |> strip_type |> fst |> length) ||> #f wctxt |> list_abs) cs) |
|
81147 | 230 |
fun Ilet (wctxt: term wctxt) lT exp abs t = |
81358 | 231 |
Const (@{const_name "HOL.Let"}, lT) $ (#f wctxt exp) $ list_abs (abs, #f wctxt t) |
79490 | 232 |
|
233 |
(* 1. Fix all terms *) |
|
234 |
(* Exchange Var in types and terms to Free *) |
|
81147 | 235 |
fun freeTerms (Var(ixn,T)) = Free (fst ixn, T) |
236 |
| freeTerms t = t |
|
237 |
fun freeTypes (TVar ((t, _), T)) = TFree (t, T) |
|
238 |
| freeTypes t = t |
|
82081 | 239 |
fun fix_definition (Const ("Pure.eq", _) $ l $ r) = HOLogic.mk_Trueprop (HOLogic.mk_eq (l,r)) |
240 |
| fix_definition t = t |
|
241 |
fun check_definition [t] = [t] |
|
242 |
| check_definition _ = error "Only a single definition is allowed" |
|
243 |
fun get_terms theory (term: term) = |
|
244 |
let |
|
245 |
val equations = Spec_Rules.retrieve theory term |
|
246 |
|> map #rules |
|
247 |
|> map (map Thm.prop_of) |
|
248 |
handle Empty => error "Function or terms of function not found" |
|
249 |
in |
|
250 |
equations |
|
251 |
|> map (map fix_definition) |
|
252 |
|> filter (List.exists |
|
253 |
(fn t => typ_comp (t |> get_l |> strip_comb |> fst |> dest_Const |> snd) (term |> strip_comb |> fst |> dest_Const |> snd))) |
|
254 |
|> hd |
|
255 |
end |
|
79542 | 256 |
|
81289 | 257 |
fun fixCasecCases _ [t] = [t] |
258 |
| fixCasecCases wctxt (t::ts) = |
|
259 |
let |
|
260 |
val num = fastype_of t |> strip_type |> fst |> length |
|
261 |
val c' = Term.strip_abs_eta num t |> list_abs |
|
262 |
in |
|
263 |
c' :: fixCasecCases wctxt ts |
|
264 |
end |
|
265 |
| fixCasecCases _ _ = error "Internal error: invalid case types/terms" |
|
266 |
fun fixCasec wctxt t args = |
|
267 |
(check_args "cases" (t,args); list_comb (t,fixCasecCases wctxt args)) |
|
79490 | 268 |
|
81147 | 269 |
fun shortFunc fixedNum (Const (nm,T)) = |
270 |
Const (nm,T |> strip_type |>> drop fixedNum |> (op --->)) |
|
271 |
| shortFunc _ _ = error "Internal error: Invalid term" |
|
272 |
fun shortApp fixedNum (c, args) = |
|
273 |
(shortFunc fixedNum c, drop fixedNum args) |
|
274 |
fun shortOriginFunc (term: term list) fixedNum (f as (c as Const (_,_), _)) = |
|
275 |
if contains' const_comp term c then shortApp fixedNum f else f |
|
276 |
| shortOriginFunc _ _ t = t |
|
81255 | 277 |
fun map_abs f (t as Abs _) = t |> strip_abs ||> f |> list_abs |
278 |
| map_abs _ t = t |
|
81147 | 279 |
fun fixTerms ctxt (term: term list) (fixedNum: int) (t as pT $ (eq $ l $ r)) = |
79490 | 280 |
let |
81147 | 281 |
val _ = check_args "args" (strip_comb (get_l t)) |
282 |
val l' = shortApp fixedNum (strip_comb l) |> list_comb |
|
283 |
val shortOriginFunc' = shortOriginFunc (term |> map (fst o strip_comb)) fixedNum |
|
81358 | 284 |
val consts = Proof_Context.consts_of ctxt |
285 |
val net = Consts.revert_abbrevs consts ["internal"] |> hd |> Item_Net.content |
|
286 |
(* filter out consts *) |
|
287 |
|> filter (is_Const o fst o strip_comb o fst) |
|
288 |
(* filter out abbreviations for locales *) |
|
289 |
|> filter (fn n => "local" |
|
290 |
= (n |> snd |> strip_comb |> fst |> dest_Const_name |> split_name |> hd)) |
|
291 |
|> filter (fn n => (n |> fst |> strip_comb |> fst |> dest_Const_name |> split_name |> List.last) = |
|
292 |
(n |> snd |> strip_comb |> fst |> dest_Const_name |> split_name |> List.last)) |
|
293 |
|> map (fst #> strip_comb #>> dest_Const_name ##> length) |
|
294 |
fun n_abbrev (Const (nm,_)) = |
|
295 |
let |
|
296 |
val f = filter (fn n => fst n = nm) net |
|
297 |
in |
|
298 |
if length f >= 1 then f |> hd |> snd else 0 |
|
299 |
end |
|
300 |
| n_abbrev _ = 0 |
|
81147 | 301 |
val r' = walk ctxt term { |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
302 |
funcc = (fn wctxt => fn t => fn args => |
81358 | 303 |
let |
304 |
val n_abb = n_abbrev t |
|
305 |
val t = case t of Const (nm,T) => Const (nm, T |> strip_type |>> drop n_abb |> (op --->)) |
|
306 |
| t => t |
|
307 |
val args = drop n_abb args |
|
308 |
in |
|
309 |
(check_args "func" (t,args); |
|
310 |
(#f wctxt t, map (#f wctxt) args) |> shortOriginFunc' |> list_comb) |
|
311 |
end), |
|
81255 | 312 |
constc = fn wctxt => map_abs (#f wctxt), |
81147 | 313 |
ifc = Iif, |
79490 | 314 |
casec = fixCasec, |
81147 | 315 |
letc = (fn wctxt => fn expT => fn exp => fn abs => fn t => |
81255 | 316 |
(Const (@{const_name "HOL.Let"},expT) $ (#f wctxt exp) $ list_abs (abs, #f wctxt t))) |
81147 | 317 |
} r |
318 |
in |
|
319 |
pT $ (eq $ l' $ r') |
|
79490 | 320 |
end |
81147 | 321 |
| fixTerms _ _ _ _ = error "Internal error: invalid term" |
79490 | 322 |
|
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
323 |
(* 2. Check for properties about the function *) |
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
324 |
(* 2.1 Check if function is recursive *) |
79490 | 325 |
fun or f (a,b) = f a orelse b |
326 |
fun find_rec ctxt term = (walk ctxt term { |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
327 |
funcc = (fn wctxt => fn t => fn args => |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
328 |
List.exists (fn term => (Const_name t) = (Const_name term)) term |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
329 |
orelse List.foldr (or (#f wctxt)) false args), |
81255 | 330 |
constc = fn wctxt => fn t => case t of |
331 |
Abs _ => t |> strip_abs |> snd |> (#f wctxt) |
|
332 |
| _ => false, |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
333 |
ifc = (fn wctxt => fn _ => fn cond => fn tt => fn tf => |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
334 |
(#f wctxt) cond orelse (#f wctxt) tt orelse (#f wctxt) tf), |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
335 |
casec = (fn wctxt => fn t => fn cs => |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
336 |
(#f wctxt) t orelse List.foldr (or (rem_abs (#f wctxt))) false cs), |
81147 | 337 |
letc = (fn wctxt => fn _ => fn exp => fn _ => fn t => |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
338 |
(#f wctxt) exp orelse (#f wctxt) t) |
79490 | 339 |
}) o get_r |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
340 |
fun is_rec ctxt (term: term list) = List.foldr (or (find_rec ctxt term)) false |
79490 | 341 |
|
342 |
(* 4. Tactic to prove "f_dom n" *) |
|
343 |
fun time_dom_tac ctxt induct_rule domintros = |
|
344 |
(Induction.induction_tac ctxt true [] [[]] [] (SOME [induct_rule]) [] |
|
345 |
THEN_ALL_NEW ((K (auto_tac ctxt)) THEN' (fn i => FIRST' ( |
|
346 |
(if i <= length domintros then [Metis_Tactic.metis_tac [] ATP_Problem_Generate.combsN ctxt [List.nth (domintros, i-1)]] else []) @ |
|
347 |
[Metis_Tactic.metis_tac [] ATP_Problem_Generate.combsN ctxt domintros]) i))) |
|
348 |
||
82081 | 349 |
(* Register timing function of a given function *) |
350 |
type time_config = { |
|
351 |
print: bool, |
|
352 |
simp: bool, |
|
353 |
partial: bool |
|
354 |
} |
|
355 |
datatype result = Function of Function.info | PartialFunction of thm |
|
356 |
fun reg_time_func (lthy: local_theory) (term: term list) (terms: term list) (config: time_config) = |
|
357 |
let |
|
358 |
(* some default values to build terms easier *) |
|
359 |
(* Const (@{const_name "Groups.zero"}, HOLogic.natT) *) |
|
360 |
val zero = if #partial config then @{term "Some (0::nat)"} else HOLogic.zero |
|
361 |
val one = Const (@{const_name "Groups.one"}, HOLogic.natT) |
|
362 |
val natOptT = @{typ "nat option"} |
|
363 |
val finT = if #partial config then natOptT else HOLogic.natT |
|
364 |
val some = @{term "Some::nat \<Rightarrow> nat option"} |
|
81147 | 365 |
|
82081 | 366 |
(* change type of original function to new type (_ \<Rightarrow> ... \<Rightarrow> _ to _ \<Rightarrow> ... \<Rightarrow> nat) |
367 |
and replace all function arguments f with (t*T_f) if used *) |
|
368 |
fun change_typ' used (Type ("fun", [T1, T2])) = |
|
369 |
Type ("fun", [check_for_fun' (used 0) T1, change_typ' (fn i => used (i+1)) T2]) |
|
370 |
| change_typ' _ _ = finT |
|
371 |
and check_for_fun' true (f as Type ("fun", [_,_])) = HOLogic.mk_prodT (f, change_typ' (K false) f) |
|
372 |
| check_for_fun' false (f as Type ("fun", [_,_])) = change_typ' (K true) f |
|
373 |
| check_for_fun' _ t = t |
|
374 |
val change_typ = change_typ' (K true) |
|
375 |
fun time_term ctxt s (Const (nm,T)) = |
|
376 |
let |
|
377 |
val T_nm = fun_name_to_time ctxt s nm |
|
378 |
val T_T = change_typ T |
|
379 |
in |
|
380 |
(SOME (Syntax.check_term ctxt (Const (T_nm,T_T)))) |
|
381 |
handle (ERROR _) => |
|
382 |
case Syntax.read_term ctxt (Long_Name.base_name T_nm) |
|
383 |
of (Const (T_nm,T_T)) => |
|
384 |
let |
|
385 |
fun col_Used i (Type ("fun", [Type ("fun", _), Ts])) (Type ("fun", [T', Ts'])) = |
|
386 |
(if is_Used T' then [i] else []) @ col_Used (i+1) Ts Ts' |
|
387 |
| col_Used i (Type ("fun", [_, Ts])) (Type ("fun", [_, Ts'])) = col_Used (i+1) Ts Ts' |
|
388 |
| col_Used _ _ _ = [] |
|
389 |
val binderT = change_typ' (contains (col_Used 0 T T_T)) T |> Term.binder_types |
|
390 |
val finT = Term.body_type T_T |
|
391 |
in |
|
392 |
SOME (Const (T_nm, binderT ---> finT)) |
|
393 |
end |
|
394 |
| _ => error ("Timing function of " ^ nm ^ " is not defined") |
|
395 |
end |
|
396 |
| time_term _ _ _ = error "Internal error: No valid function given" |
|
81147 | 397 |
|
82081 | 398 |
fun opt_term NONE = zero |
399 |
| opt_term (SOME t) = t |
|
400 |
fun use_origin (Free (nm, T as Type ("fun",_))) = HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T))) |
|
401 |
| use_origin t = t |
|
402 |
||
403 |
(* Conversion of function term *) |
|
404 |
fun fun_to_time' ctxt (origin: term list) second (func as Const (nm,T)) = |
|
405 |
let |
|
406 |
val origin' = map (fst o strip_comb) origin |
|
407 |
in |
|
408 |
if contains' const_comp origin' func then SOME (Free (func |> Term.term_name |> fun_name_to_time' ctxt true second, change_typ T)) else |
|
409 |
if Zero_Funcs.is_zero (Proof_Context.theory_of ctxt) (nm,T) then NONE else |
|
410 |
time_term ctxt false func |
|
411 |
end |
|
412 |
| fun_to_time' _ _ _ (Free (nm,T)) = |
|
413 |
SOME (HOLogic.mk_snd (Free (nm,HOLogic.mk_prodT (T,change_typ' (K true) T)))) |
|
414 |
| fun_to_time' _ _ _ _ = error "Internal error: invalid function to convert" |
|
415 |
fun fun_to_time context origin func = fun_to_time' context origin false func |
|
416 |
||
417 |
(* Convert arguments of left side of a term *) |
|
418 |
fun conv_arg _ (Free (nm,T as Type("fun",_))) = |
|
419 |
Free (nm, HOLogic.mk_prodT (T, change_typ' (K false) T)) |
|
420 |
| conv_arg _ x = x |
|
421 |
fun conv_args ctxt = map (conv_arg ctxt) |
|
422 |
||
423 |
(* 3. Convert equations *) |
|
424 |
(* Some Helper *) |
|
425 |
val plusTyp = @{typ "nat => nat => nat"} |
|
426 |
fun plus (SOME a) (SOME b) = SOME ((if #partial config then @{term part_add} else Const (@{const_name "Groups.plus"}, plusTyp)) $ a $ b) |
|
427 |
| plus (SOME a) NONE = SOME a |
|
428 |
| plus NONE (SOME b) = SOME b |
|
429 |
| plus NONE NONE = NONE |
|
430 |
(* Partial helper *) |
|
431 |
val OPTION_BIND = @{term "Option.bind::nat option \<Rightarrow> (nat \<Rightarrow> nat option) \<Rightarrow> nat option"} |
|
432 |
fun OPTION_ABS_SUC args = Term.absfree ("_uu", @{typ nat}) |
|
433 |
(List.foldr (uncurry plus) |
|
434 |
(SOME (some $ HOLogic.mk_Suc (Free ("_uu", @{typ nat})))) args |> Option.valOf) |
|
435 |
fun build_option_bind term args = |
|
436 |
OPTION_BIND $ term $ OPTION_ABS_SUC args |
|
437 |
fun WRAP_FUNCTION t = |
|
438 |
if (Term.head_of t |> Term.fastype_of |> Term.body_type) = finT |
|
439 |
then t |
|
440 |
else if #partial config |
|
441 |
then some $ t |
|
442 |
else @{term "the::nat option \<Rightarrow> nat"} $ t |
|
81147 | 443 |
|
82081 | 444 |
(* Handle function calls *) |
445 |
fun build_zero (Type ("fun", [T, R])) = Abs ("uu", T, build_zero R) |
|
446 |
| build_zero _ = zero |
|
447 |
fun funcc_use_origin (Free (nm, T as Type ("fun",_))) = |
|
448 |
HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T))) |
|
449 |
| funcc_use_origin t = t |
|
450 |
fun funcc_conv_arg _ _ (t as (_ $ _)) = map_aterms funcc_use_origin t |
|
451 |
| funcc_conv_arg _ u (Free (nm, T as Type ("fun",_))) = |
|
452 |
if u then Free (nm, HOLogic.mk_prodT (T, change_typ T)) |
|
453 |
else HOLogic.mk_snd (Free (nm,HOLogic.mk_prodT (T,change_typ T))) |
|
454 |
| funcc_conv_arg wctxt true (f as Const (_,Type ("fun",_))) = |
|
455 |
HOLogic.mk_prod (f, funcc_conv_arg wctxt false f) |
|
456 |
| funcc_conv_arg wctxt false (f as Const (_,T as Type ("fun",_))) = |
|
457 |
Option.getOpt (fun_to_time (#ctxt wctxt) (#origins wctxt) f, build_zero T) |
|
458 |
| funcc_conv_arg wctxt false (f as Abs _) = |
|
459 |
f |
|
460 |
|> Term.strip_abs_eta ((length o fst o strip_type o type_of) f) |
|
461 |
||> #f wctxt ||> opt_term |
|
462 |
|> list_abs |
|
463 |
| funcc_conv_arg wctxt true (f as Abs _) = |
|
464 |
let |
|
465 |
val f' = f |
|
466 |
|> Term.strip_abs_eta ((length o fst o strip_type o type_of) f) |
|
467 |
||> map_aterms funcc_use_origin |
|
468 |
|> list_abs |
|
469 |
in |
|
470 |
HOLogic.mk_prod (f', funcc_conv_arg wctxt false f) |
|
471 |
end |
|
472 |
| funcc_conv_arg _ _ t = t |
|
473 |
||
474 |
fun funcc_conv_args _ _ [] = [] |
|
475 |
| funcc_conv_args wctxt (Type ("fun", [t, ts])) (a::args) = |
|
476 |
funcc_conv_arg wctxt (is_Used t) a :: funcc_conv_args wctxt ts args |
|
477 |
| funcc_conv_args _ _ _ = error "Internal error: Non matching type" |
|
478 |
fun funcc wctxt func args = |
|
479 |
let |
|
480 |
fun get_T (Free (_,T)) = T |
|
481 |
| get_T (Const (_,T)) = T |
|
482 |
| get_T (_ $ (Free (_,Type (_, [_, T])))) = T (* Case of snd was constructed *) |
|
483 |
| get_T _ = error "Internal error: Forgotten type" |
|
484 |
val func = (case fun_to_time (#ctxt wctxt) (#origins wctxt) func |
|
485 |
of SOME t => SOME (WRAP_FUNCTION (list_comb (t, funcc_conv_args wctxt (get_T t) args))) |
|
486 |
| NONE => NONE) |
|
487 |
val args = (map (#f wctxt) args) |
|
488 |
in |
|
489 |
(if not (#partial config) orelse func = NONE |
|
490 |
then List.foldr (uncurry plus) func args |
|
491 |
else build_option_bind (Option.valOf func) args |> SOME) |
|
492 |
end |
|
493 |
||
494 |
(* Handle case terms *) |
|
495 |
fun casecIsCase (Type (n1, [_,Type (n2, _)])) = (n1 = "fun" andalso n2 = "fun") |
|
496 |
| casecIsCase _ = false |
|
497 |
fun casecLastTyp (Type (n, [T1,T2])) = Type (n, [T1, change_typ T2]) |
|
498 |
| casecLastTyp _ = error "Internal error: Invalid case type" |
|
499 |
fun casecTyp (Type (n, [T1, T2])) = |
|
500 |
Type (n, [change_typ T1, (if casecIsCase T2 then casecTyp else casecLastTyp) T2]) |
|
501 |
| casecTyp _ = error "Internal error: Invalid case type" |
|
502 |
fun casecAbs f (Abs (v,Ta,t)) = (case casecAbs f (subst_bound (Free (v,Ta), t)) |
|
503 |
of (nconst,t) => (nconst,absfree (v,Ta) t)) |
|
504 |
| casecAbs f t = (case f t of NONE => (false, opt_term NONE) | SOME t => (true,t)) |
|
505 |
fun casecArgs _ [t] = (false, [map_aterms use_origin t]) |
|
506 |
| casecArgs f (t::ar) = |
|
507 |
(case casecAbs f t of (nconst, tt) => |
|
508 |
casecArgs f ar ||> (fn ar => tt :: ar) |>> (if nconst then K true else I)) |
|
509 |
| casecArgs _ _ = error "Internal error: Invalid case term" |
|
510 |
fun casec wctxt (Const (t,T)) args = |
|
511 |
if not (casecIsCase T) then error "Internal error: Invalid case type" else |
|
512 |
let val (nconst, args') = casecArgs (#f wctxt) args in |
|
513 |
plus |
|
514 |
((#f wctxt) (List.last args)) |
|
515 |
(if nconst then |
|
516 |
SOME (list_comb (Const (t,casecTyp T), args')) |
|
517 |
else NONE) |
|
518 |
end |
|
519 |
| casec _ _ _ = error "Internal error: Invalid case term" |
|
520 |
||
521 |
(* Handle if terms -> drop the term if true and false terms are zero *) |
|
522 |
fun ifc wctxt _ cond tt ft = |
|
523 |
let |
|
524 |
val f = #f wctxt |
|
525 |
val rcond = map_aterms use_origin cond |
|
526 |
val tt = f tt |
|
527 |
val ft = f ft |
|
528 |
in |
|
529 |
plus (f cond) (case (tt,ft) of (NONE, NONE) => NONE | _ => |
|
530 |
if tt = ft then tt else |
|
531 |
(SOME ((Const (@{const_name "HOL.If"}, @{typ "bool"} --> finT --> finT --> finT)) $ rcond |
|
532 |
$ (opt_term tt) $ (opt_term ft)))) |
|
533 |
end |
|
534 |
||
535 |
fun letc_lambda wctxt T (t as Abs _) = |
|
536 |
HOLogic.mk_prod (map_aterms use_origin t, |
|
537 |
Term.strip_abs_eta (strip_type T |> fst |> length) t ||> #f wctxt ||> opt_term ||> map_types change_typ |> list_abs) |
|
538 |
| letc_lambda _ _ t = map_aterms use_origin t |
|
539 |
fun letc wctxt expT exp ([(nm,_)]) t = |
|
540 |
plus (#f wctxt exp) |
|
541 |
(case #f wctxt t of SOME t' => |
|
542 |
(if Term.used_free nm t' |
|
543 |
then |
|
544 |
let |
|
545 |
val exp' = letc_lambda wctxt expT exp |
|
546 |
val t' = list_abs ([(nm,fastype_of exp')], t') |
|
547 |
in |
|
548 |
Const (@{const_name "HOL.Let"}, [fastype_of exp', fastype_of t'] ---> finT) $ exp' $ t' |
|
549 |
end |
|
550 |
else t') |> SOME |
|
551 |
| NONE => NONE) |
|
552 |
| letc _ _ _ _ _ = error "Unknown let state" |
|
553 |
||
554 |
fun constc _ (Const ("HOL.undefined", _)) = SOME (Const ("HOL.undefined", finT)) |
|
555 |
| constc _ _ = NONE |
|
556 |
||
557 |
(* The converter for timing functions given to the walker *) |
|
558 |
val converter : term option converter = { |
|
559 |
constc = constc, |
|
560 |
funcc = funcc, |
|
561 |
ifc = ifc, |
|
562 |
casec = casec, |
|
563 |
letc = letc |
|
564 |
} |
|
565 |
fun top_converter is_rec _ _ = |
|
566 |
if #partial config |
|
567 |
then (fn t => Option.getOpt (t, zero)) |
|
568 |
else (opt_term o (fn exp => plus exp (if is_rec then SOME one else NONE))) |
|
569 |
||
570 |
(* Use converter to convert right side of a term *) |
|
571 |
fun to_time ctxt origin is_rec term = |
|
572 |
top_converter is_rec ctxt origin (walk ctxt origin converter term) |
|
573 |
||
574 |
(* Converts a term to its running time version *) |
|
575 |
fun convert_term ctxt (origin: term list) is_rec (pT $ (Const (eqN, _) $ l $ r)) = |
|
576 |
let |
|
577 |
val (l_const, l_params) = strip_comb l |
|
578 |
in |
|
579 |
pT |
|
580 |
$ (Const (eqN, finT --> finT --> @{typ "bool"}) |
|
581 |
$ (list_comb (l_const |> fun_to_time ctxt origin |> Option.valOf, l_params |> conv_args ctxt)) |
|
582 |
$ (to_time ctxt origin is_rec r)) |
|
583 |
end |
|
584 |
| convert_term _ _ _ _ = error "Internal error: invalid term to convert" |
|
585 |
||
586 |
(* 3.5 Support for locales *) |
|
587 |
fun replaceFstSndFree ctxt (origin: term list) (rfst: term -> term) (rsnd: term -> term) = |
|
588 |
(walk ctxt origin { |
|
589 |
funcc = fn wctxt => fn t => fn args => |
|
590 |
case args of |
|
591 |
(f as Free _)::args => |
|
592 |
(case t of |
|
593 |
Const ("Product_Type.prod.fst", _) => |
|
594 |
list_comb (rfst (t $ f), map (#f wctxt) args) |
|
595 |
| Const ("Product_Type.prod.snd", _) => |
|
596 |
list_comb (rsnd (t $ f), map (#f wctxt) args) |
|
597 |
| t => list_comb (t, map (#f wctxt) (f :: args))) |
|
598 |
| args => list_comb (t, map (#f wctxt) args), |
|
599 |
constc = Iconst, |
|
600 |
ifc = Iif, |
|
601 |
casec = Icase, |
|
602 |
letc = Ilet |
|
603 |
}) |
|
604 |
||
605 |
(* 5. Check for higher-order function if original function is used \<rightarrow> find simplifications *) |
|
606 |
fun find_used' T_t = |
|
607 |
let |
|
608 |
val (T_ident, T_args) = strip_comb (get_l T_t) |
|
609 |
||
610 |
fun filter_passed [] = [] |
|
611 |
| filter_passed ((f as Free (_, Type ("Product_Type.prod",[Type ("fun",_), Type ("fun", _)])))::args) = |
|
612 |
f :: filter_passed args |
|
613 |
| filter_passed (_::args) = filter_passed args |
|
614 |
val frees = (walk lthy [] { |
|
615 |
funcc = (fn wctxt => fn t => fn args => |
|
616 |
(case t of (Const ("Product_Type.prod.snd", _)) => [] |
|
617 |
| _ => (if t = T_ident then [] else filter_passed args) |
|
618 |
@ List.foldr (fn (l,r) => (#f wctxt) l @ r) [] args)), |
|
619 |
constc = (K o K) [], |
|
620 |
ifc = (fn wctxt => fn _ => fn cond => fn tt => fn tf => (#f wctxt) cond @ (#f wctxt) tt @ (#f wctxt) tf), |
|
621 |
casec = (fn wctxt => fn _ => fn cs => List.foldr (fn (l,r) => (#f wctxt) l @ r) [] cs), |
|
622 |
letc = (fn wctxt => fn _ => fn exp => fn _ => fn t => (#f wctxt) exp @ (#f wctxt) t) |
|
623 |
}) (get_r T_t) |
|
624 |
fun build _ [] = [] |
|
625 |
| build i (a::args) = |
|
626 |
(if contains frees a then [(T_ident,i)] else []) @ build (i+1) args |
|
627 |
in |
|
628 |
build 0 T_args |
|
629 |
end |
|
630 |
fun find_simplifyble ctxt term terms = |
|
631 |
let |
|
632 |
val used = |
|
633 |
terms |
|
634 |
|> List.map find_used' |
|
635 |
|> List.foldr (op @) [] |
|
636 |
val change = |
|
637 |
Option.valOf o fun_to_time ctxt term |
|
638 |
fun detect t i (Type ("fun",_)::args) = |
|
639 |
(if contains used (change t,i) then [] else [i]) @ detect t (i+1) args |
|
640 |
| detect t i (_::args) = detect t (i+1) args |
|
641 |
| detect _ _ [] = [] |
|
642 |
in |
|
643 |
map (fn t => t |> type_of |> strip_type |> fst |> detect t 0) term |
|
644 |
end |
|
645 |
||
646 |
fun define_simp' term simplifyable ctxt = |
|
647 |
let |
|
648 |
val base_name = case Named_Target.locale_of ctxt of |
|
649 |
NONE => ctxt |> Proof_Context.theory_of |> Context.theory_base_name |
|
650 |
| SOME nm => nm |
|
651 |
||
652 |
val orig_name = term |> dest_Const_name |> split_name |> List.last |
|
653 |
val red_name = fun_name_to_time ctxt false orig_name |
|
654 |
val name = fun_name_to_time' ctxt true true orig_name |
|
655 |
val full_name = base_name ^ "." ^ name |
|
656 |
val def_name = red_name ^ "_def" |
|
657 |
val def = Binding.name def_name |
|
658 |
||
659 |
val canon = Syntax.read_term (Local_Theory.exit ctxt) name |> strip_comb |
|
660 |
val canonFrees = canon |> snd |
|
661 |
val canonType = canon |> fst |> dest_Const_type |> strip_type |> fst |> take (length canonFrees) |
|
662 |
||
663 |
val types = term |> dest_Const_type |> strip_type |> fst |
|
664 |
val vars = Variable.variant_fixes (map (K "") types) ctxt |> fst |
|
665 |
fun l_typs' i ((T as (Type ("fun",_)))::types) = |
|
666 |
(if contains simplifyable i |
|
667 |
then change_typ T |
|
668 |
else HOLogic.mk_prodT (T,change_typ T)) |
|
669 |
:: l_typs' (i+1) types |
|
670 |
| l_typs' i (T::types) = T :: l_typs' (i+1) types |
|
671 |
| l_typs' _ [] = [] |
|
672 |
val l_typs = l_typs' 0 types |
|
673 |
val lhs = |
|
674 |
List.foldl (fn ((v,T),t) => t $ Free (v,T)) (Free (red_name,l_typs ---> HOLogic.natT)) (ListPair.zip (vars,l_typs)) |
|
675 |
fun fixType (TFree _) = HOLogic.natT |
|
676 |
| fixType T = T |
|
677 |
fun fixUnspecified T = T |> strip_type ||> fixType |> (op --->) |
|
678 |
fun r_terms' i (v::vars) ((T as (Type ("fun",_)))::types) = |
|
679 |
(if contains simplifyable i |
|
680 |
then HOLogic.mk_prod (Const ("HOL.undefined", fixUnspecified T), Free (v,change_typ T)) |
|
681 |
else Free (v,HOLogic.mk_prodT (T,change_typ T))) |
|
682 |
:: r_terms' (i+1) vars types |
|
683 |
| r_terms' i (v::vars) (T::types) = Free (v,T) :: r_terms' (i+1) vars types |
|
684 |
| r_terms' _ _ _ = [] |
|
685 |
val r_terms = r_terms' 0 vars types |
|
686 |
val full_type = (r_terms |> map (type_of) ---> HOLogic.natT) |
|
687 |
val full = list_comb (Const (full_name,canonType ---> full_type), canonFrees) |
|
688 |
val rhs = list_comb (full, r_terms) |
|
689 |
val eq = (lhs, rhs) |> HOLogic.mk_eq |> HOLogic.mk_Trueprop |
|
690 |
val _ = Pretty.writeln (Pretty.block [Pretty.str "Defining simplified version:\n", |
|
691 |
Syntax.pretty_term ctxt eq]) |
|
692 |
||
693 |
val (_, ctxt') = Specification.definition NONE [] [] ((def, []), eq) ctxt |
|
694 |
||
695 |
in |
|
696 |
((def_name, orig_name), ctxt') |
|
697 |
end |
|
698 |
fun define_simp simpables ctxt = |
|
699 |
let |
|
700 |
fun cond ((term,simplifyable),(defs,ctxt)) = |
|
701 |
define_simp' term simplifyable ctxt |>> (fn def => def :: defs) |
|
702 |
in |
|
703 |
List.foldr cond ([], ctxt) simpables |
|
704 |
end |
|
705 |
||
706 |
||
707 |
fun replace from to = |
|
708 |
map (map_aterms (fn t => if t = from then to else t)) |
|
709 |
fun replaceAll [] = I |
|
710 |
| replaceAll ((from,to)::xs) = replaceAll xs o replace from to |
|
711 |
fun calculateSimplifications ctxt T_terms term simpables = |
|
712 |
let |
|
713 |
(* Show where a simplification can take place *) |
|
714 |
fun reportReductions (t,(i::is)) = |
|
715 |
(Pretty.writeln (Pretty.str |
|
716 |
((Term.term_name t |> fun_name_to_time ctxt true) |
|
717 |
^ " can be simplified because only the time-function component of parameter " |
|
718 |
^ (Int.toString (i + 1)) ^ " is used. ")); |
|
719 |
reportReductions (t,is)) |
|
720 |
| reportReductions (_,[]) = () |
|
721 |
val _ = simpables |
|
722 |
|> map reportReductions |
|
723 |
||
724 |
(* Register definitions for simplified function *) |
|
725 |
val (reds, ctxt) = define_simp simpables ctxt |
|
726 |
||
727 |
fun genRetype (Const (nm,T),is) = |
|
728 |
let |
|
729 |
val T_name = fun_name_to_time ctxt true nm |> split_name |> List.last |
|
730 |
val from = Free (T_name,change_typ T) |
|
731 |
val to = Free (T_name,change_typ' (not o contains is) T) |
|
732 |
in |
|
733 |
(from,to) |
|
734 |
end |
|
735 |
| genRetype _ = error "Internal error: invalid term" |
|
736 |
val retyping = map genRetype simpables |
|
737 |
||
738 |
fun replaceArgs (pT $ (eq $ l $ r)) = |
|
739 |
let |
|
740 |
val (t,params) = strip_comb l |
|
741 |
fun match (Const (f_nm,_),_) = |
|
742 |
(fun_name_to_time ctxt true f_nm |> Long_Name.base_name) = (dest_Free t |> fst) |
|
743 |
| match _ = false |
|
744 |
val simps = List.find match simpables |> Option.valOf |> snd |
|
745 |
||
746 |
fun dest_Prod_snd (Free (nm, Type (_, [_, T2]))) = |
|
747 |
Free (fun_name_to_time ctxt false nm, T2) |
|
748 |
| dest_Prod_snd _ = error "Internal error: Argument is not a pair" |
|
749 |
fun rep _ [] = ([],[]) |
|
750 |
| rep i (x::xs) = |
|
751 |
let |
|
752 |
val (rs,args) = rep (i+1) xs |
|
753 |
in |
|
754 |
if contains simps i |
|
755 |
then (x::rs,dest_Prod_snd x::args) |
|
756 |
else (rs,x::args) |
|
757 |
end |
|
758 |
val (rs,params) = rep 0 params |
|
759 |
fun fFst _ = error "Internal error: Invalid term to simplify" |
|
760 |
fun fSnd (t as (Const _ $ f)) = |
|
761 |
(if contains rs f |
|
762 |
then dest_Prod_snd f |
|
763 |
else t) |
|
764 |
| fSnd t = t |
|
765 |
in |
|
766 |
(pT $ (eq |
|
767 |
$ (list_comb (t,params)) |
|
768 |
$ (replaceFstSndFree ctxt term fFst fSnd r |
|
769 |
|> (fn t => replaceAll (map (fn t => (t,dest_Prod_snd t)) rs) [t]) |
|
770 |
|> hd |
|
771 |
) |
|
772 |
)) |
|
773 |
end |
|
774 |
| replaceArgs _ = error "Internal error: Invalid term" |
|
775 |
||
776 |
(* Calculate reduced terms *) |
|
777 |
val T_terms_red = T_terms |
|
778 |
|> replaceAll retyping |
|
779 |
|> map replaceArgs |
|
780 |
||
781 |
val _ = print_lemma ctxt reds T_terms_red |
|
782 |
val _ = |
|
783 |
Pretty.writeln (Pretty.str "If you do not want the simplified T function, use \"time_fun [no_simp]\"") |
|
784 |
in |
|
785 |
ctxt |
|
786 |
end |
|
81147 | 787 |
|
79490 | 788 |
val _ = |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
789 |
case time_term lthy true (hd term) |
79490 | 790 |
handle (ERROR _) => NONE |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
791 |
of SOME _ => error ("Timing function already declared: " ^ (Term.term_name (hd term))) |
79490 | 792 |
| NONE => () |
793 |
||
81147 | 794 |
(* Number of terms fixed by locale *) |
81358 | 795 |
val fixedNum = term |> hd |
81147 | 796 |
|> strip_comb |> snd |
797 |
|> length |
|
798 |
||
82081 | 799 |
(********************* BEGIN OF CONVERSION *********************) |
79490 | 800 |
(* 1. Fix all terms *) |
801 |
(* Exchange Var in types and terms to Free and check constraints *) |
|
79969 | 802 |
val terms = map |
81147 | 803 |
(map_aterms freeTerms |
804 |
#> map_types (map_atyps freeTypes) |
|
805 |
#> fixTerms lthy term fixedNum) |
|
79542 | 806 |
terms |
81147 | 807 |
val fixedFrees = (hd term) |> strip_comb |> snd |> take fixedNum |
808 |
val fixedFreesNames = map (fst o dest_Free) fixedFrees |
|
809 |
val term = map (shortFunc fixedNum o fst o strip_comb) term |
|
81255 | 810 |
fun correctTerm term = |
811 |
let |
|
812 |
val get_f = fst o strip_comb o get_l |
|
813 |
in |
|
814 |
List.find (fn t => (dest_Const_name o get_f) t = dest_Const_name term) terms |
|
815 |
|> Option.valOf |> get_f |
|
816 |
end |
|
817 |
val term = map correctTerm term |
|
79490 | 818 |
|
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
819 |
(* 2. Find properties about the function *) |
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
820 |
(* 2.1 Check if function is recursive *) |
79490 | 821 |
val is_rec = is_rec lthy term terms |
822 |
||
823 |
(* 3. Convert every equation |
|
824 |
- Change type of toplevel equation from _ \<Rightarrow> _ \<Rightarrow> bool to nat \<Rightarrow> nat \<Rightarrow> bool |
|
825 |
- On left side change name of function to timing function |
|
826 |
- Convert right side of equation with conversion schema |
|
827 |
*) |
|
81147 | 828 |
fun fFst (t as (Const (_,T) $ Free (nm,_))) = |
829 |
(if contains fixedFreesNames nm |
|
830 |
then Free (nm,strip_type T |>> tl |> (op --->)) |
|
831 |
else t) |
|
832 |
| fFst t = t |
|
833 |
fun fSnd (t as (Const (_,T) $ Free (nm,_))) = |
|
834 |
(if contains fixedFreesNames nm |
|
835 |
then Free (fun_name_to_time lthy false nm,strip_type T |>> tl |> (op --->)) |
|
836 |
else t) |
|
837 |
| fSnd t = t |
|
838 |
val T_terms = map (convert_term lthy term is_rec) terms |
|
839 |
|> map (map_r (replaceFstSndFree lthy term fFst fSnd)) |
|
840 |
||
82081 | 841 |
val simpables = (if #simp config |
81147 | 842 |
then find_simplifyble lthy term T_terms |
843 |
else map (K []) term) |
|
844 |
|> (fn s => ListPair.zip (term,s)) |
|
845 |
(* Determine if something is simpable, if so rename everything *) |
|
846 |
val simpable = simpables |> map snd |> exists (not o null) |
|
847 |
(* Rename to secondary if simpable *) |
|
848 |
fun genRename (t,_) = |
|
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
849 |
let |
82081 | 850 |
val old = fun_to_time' lthy term false t |> Option.valOf |
81147 | 851 |
val new = fun_to_time' lthy term true t |> Option.valOf |
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
852 |
in |
81147 | 853 |
(old,new) |
80624
9f8034d29365
time_function T_map can now be generated automatically.
nipkow
parents:
80036
diff
changeset
|
854 |
end |
81147 | 855 |
val can_T_terms = if simpable |
856 |
then replaceAll (map genRename simpables) T_terms |
|
857 |
else T_terms |
|
79490 | 858 |
|
81147 | 859 |
(* 4. Register function and prove completeness *) |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
860 |
val names = map Term.term_name term |
81147 | 861 |
val timing_names = map (fun_name_to_time' lthy true simpable) names |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
862 |
val bindings = map (fn nm => (Binding.name nm, NONE, NoSyn)) timing_names |
79490 | 863 |
fun pat_completeness_auto ctxt = |
864 |
Pat_Completeness.pat_completeness_tac ctxt 1 THEN auto_tac ctxt |
|
81147 | 865 |
val specs = map (fn eq => (((Binding.empty, []), eq), [], [])) can_T_terms |
82081 | 866 |
val part_specs = (Binding.empty_atts, hd can_T_terms) |
79490 | 867 |
|
81147 | 868 |
(* Context for printing without showing question marks *) |
869 |
val print_ctxt = lthy |
|
870 |
|> Config.put show_question_marks false |
|
871 |
|> Config.put show_sorts false (* Change it for debugging *) |
|
872 |
val print_ctxt = List.foldl (fn (term, ctxt) => Variable.add_fixes_implicit term ctxt) print_ctxt (term @ T_terms) |
|
873 |
(* Print result if print *) |
|
82081 | 874 |
val _ = if not (#print config) then () else |
81147 | 875 |
let |
876 |
val nms = map (dest_Const_name) term |
|
877 |
val typs = map (dest_Const_type) term |
|
878 |
in |
|
879 |
print_timing' print_ctxt { names=nms, terms=terms, typs=typs } |
|
82081 | 880 |
{ names=timing_names, terms=can_T_terms, typs=map change_typ typs } |
81147 | 881 |
end |
882 |
||
79490 | 883 |
(* For partial functions sequential=true is needed in order to support them |
884 |
We need sequential=false to support the automatic proof of termination over dom |
|
885 |
*) |
|
886 |
fun register seq = |
|
887 |
let |
|
888 |
val _ = (if seq then warning "Falling back on sequential function..." else ()) |
|
889 |
val fun_config = Function_Common.FunctionConfig |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
890 |
{sequential=seq, default=NONE, domintros=true, partials=false} |
79490 | 891 |
in |
82081 | 892 |
if #partial config |
893 |
then Partial_Function.add_partial_function "option" bindings part_specs lthy |>> PartialFunction o snd |
|
894 |
else Function.add_function bindings specs fun_config pat_completeness_auto lthy |>> Function |
|
79490 | 895 |
end |
896 |
||
81147 | 897 |
val (info,ctxt) = |
898 |
register false |
|
899 |
handle (ERROR _) => |
|
900 |
register true |
|
901 |
| Match => |
|
902 |
register true |
|
79490 | 903 |
|
81147 | 904 |
val ctxt = if simpable then calculateSimplifications ctxt T_terms term simpables else ctxt |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
905 |
in |
82081 | 906 |
(info, ctxt) |
79490 | 907 |
end |
81147 | 908 |
fun proove_termination (term: term list) terms (T_info: Function.info, lthy: local_theory) = |
79490 | 909 |
let |
910 |
(* Start proving the termination *) |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
911 |
val infos = SOME (map (Function.get_info lthy) term) handle Empty => NONE |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
912 |
val timing_names = map (fun_name_to_time lthy true o Term.term_name) term |
79490 | 913 |
|
914 |
(* Proof by lexicographic_order_tac *) |
|
915 |
val (time_info, lthy') = |
|
916 |
(Function.prove_termination NONE |
|
917 |
(Lexicographic_Order.lexicographic_order_tac false lthy) lthy) |
|
918 |
handle (ERROR _) => |
|
919 |
let |
|
920 |
val _ = warning "Falling back on proof over dom..." |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
921 |
val _ = (if length term > 1 then error "Proof over dom not supported for mutual recursive functions" else ()) |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
922 |
|
79490 | 923 |
fun args (a$(Var ((nm,_),T))) = args a |> (fn ar => (nm,T)::ar) |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
924 |
| args (a$(Const (_,T))) = args a |> (fn ar => ("uu",T)::ar) |
79490 | 925 |
| args _ = [] |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
926 |
val dom_vars = |
81147 | 927 |
terms |> hd |> get_l |> map_types (map_atyps freeTypes) |
81519 | 928 |
|> args |> Variable.variant_names lthy |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
929 |
val dom_args = |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
930 |
List.foldl (fn (t,p) => HOLogic.mk_prod ((Free t),p)) (Free (hd dom_vars)) (tl dom_vars) |
79490 | 931 |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
932 |
val {inducts, ...} = case infos of SOME [i] => i | _ => error "Proof over dom failed as no induct rule was found" |
79490 | 933 |
val induct = (Option.valOf inducts |> hd) |
934 |
||
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
935 |
val domintros = Proof_Context.get_fact lthy (Facts.named (hd timing_names ^ ".domintros")) |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
936 |
val prop = HOLogic.mk_Trueprop (#dom T_info $ dom_args) |
79490 | 937 |
|
938 |
(* Prove a helper lemma *) |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
939 |
val dom_lemma = Goal.prove lthy (map fst dom_vars) [] prop |
79490 | 940 |
(fn {context, ...} => HEADGOAL (time_dom_tac context induct domintros)) |
941 |
(* Add dom_lemma to simplification set *) |
|
942 |
val simp_lthy = Simplifier.add_simp dom_lemma lthy |
|
943 |
in |
|
944 |
(* Use lemma to prove termination *) |
|
945 |
Function.prove_termination NONE |
|
946 |
(auto_tac simp_lthy) lthy |
|
947 |
end |
|
948 |
in |
|
82081 | 949 |
(Function time_info, lthy') |
79490 | 950 |
end |
82081 | 951 |
fun reg_and_proove_time_func (lthy: local_theory) (term: term list) (terms: term list) (config: time_config) = |
952 |
case reg_time_func lthy term terms config |
|
953 |
of (Function info, lthy') => proove_termination term terms (info, lthy') |
|
954 |
| r => r |
|
79490 | 955 |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
956 |
fun isTypeClass' (Const (nm,_)) = |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
957 |
(case split_name nm |> rev |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
958 |
of (_::nm::_) => String.isSuffix "_class" nm |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
959 |
| _ => false) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
960 |
| isTypeClass' _ = false |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
961 |
val isTypeClass = |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
962 |
(List.foldr (fn (a,b) => a orelse b) false) o (map isTypeClass') |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
963 |
|
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
964 |
fun detect_typ (ctxt: local_theory) (term: term) = |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
965 |
let |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
966 |
val class_term = (case term of Const (nm,_) => Syntax.read_term ctxt nm |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
967 |
| _ => error "Could not find term of class") |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
968 |
fun find_free (Type (_,class)) (Type (_,inst)) = |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
969 |
List.foldl (fn ((c,i),s) => (case s of NONE => find_free c i | t => t)) (NONE) (ListPair.zip (class, inst)) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
970 |
| find_free (TFree _) (TFree _) = NONE |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
971 |
| find_free (TFree _) (Type (nm,_)) = SOME nm |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
972 |
| find_free _ _ = error "Unhandled case in detecting type" |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
973 |
in |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
974 |
find_free (type_of class_term) (type_of term) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
975 |
|> Option.map (hd o rev o split_name) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
976 |
end |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
977 |
|
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
978 |
fun set_suffix (fterms: term list) ctxt = |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
979 |
let |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
980 |
val isTypeClass = isTypeClass fterms |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
981 |
val _ = (if length fterms > 1 andalso isTypeClass then error "No mutual recursion inside instantiation allowed" else ()) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
982 |
val suffix = (if isTypeClass then detect_typ ctxt (hd fterms) else NONE) |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
983 |
in |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
984 |
(case suffix of NONE => I | SOME s => Config.put bsuffix ("_" ^ s)) ctxt |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
985 |
end |
79969 | 986 |
|
81147 | 987 |
fun check_opts [] = false |
988 |
| check_opts ["no_simp"] = true |
|
989 |
| check_opts (a::_) = error ("Option " ^ a ^ " is not defined") |
|
990 |
||
82081 | 991 |
(* Converts a function into its timing function using fun *) |
81147 | 992 |
fun reg_time_fun_cmd ((opts, funcs), thms) (ctxt: local_theory) = |
79490 | 993 |
let |
81147 | 994 |
val no_simp = check_opts opts |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
995 |
val fterms = map (Syntax.read_term ctxt) funcs |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
996 |
val ctxt = set_suffix fterms ctxt |
82081 | 997 |
val config = { print = true, simp = not no_simp, partial = false } |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
998 |
val (_, ctxt') = reg_and_proove_time_func ctxt fterms |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
999 |
(case thms of NONE => get_terms ctxt (hd fterms) |
79490 | 1000 |
| SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of) |
82081 | 1001 |
config |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1002 |
in ctxt' |
79490 | 1003 |
end |
1004 |
||
82081 | 1005 |
(* Converts a function into its timing function using function with termination proof provided by user*) |
81147 | 1006 |
fun reg_time_function_cmd ((opts, funcs), thms) (ctxt: local_theory) = |
79490 | 1007 |
let |
81147 | 1008 |
val no_simp = check_opts opts |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
1009 |
val fterms = map (Syntax.read_term ctxt) funcs |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1010 |
val ctxt = set_suffix fterms ctxt |
82081 | 1011 |
val config = { print = true, simp = not no_simp, partial = false } |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1012 |
val ctxt' = reg_time_func ctxt fterms |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1013 |
(case thms of NONE => get_terms ctxt (hd fterms) |
79490 | 1014 |
| SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of) |
82081 | 1015 |
config |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1016 |
|> snd |
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1017 |
in ctxt' |
79490 | 1018 |
end |
1019 |
||
82081 | 1020 |
(* Converts a function definition into its timing function using definition *) |
81147 | 1021 |
fun reg_time_definition_cmd ((opts, funcs), thms) (ctxt: local_theory) = |
79969 | 1022 |
let |
81147 | 1023 |
val no_simp = check_opts opts |
79969 | 1024 |
val fterms = map (Syntax.read_term ctxt) funcs |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1025 |
val ctxt = set_suffix fterms ctxt |
82081 | 1026 |
val config = { print = true, simp = not no_simp, partial = false } |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1027 |
val (_, ctxt') = reg_and_proove_time_func ctxt fterms |
81255 | 1028 |
(case thms of NONE => get_terms ctxt (hd fterms) |> check_definition |
79969 | 1029 |
| SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of) |
82081 | 1030 |
config |
1031 |
in ctxt' |
|
1032 |
end |
|
1033 |
||
1034 |
(* Converts a a partial function into its timing function using partial_function *) |
|
1035 |
fun reg_time_partial_function_cmd ((opts, funcs), thms) (ctxt: local_theory) = |
|
1036 |
let |
|
1037 |
val no_simp = check_opts opts |
|
1038 |
val fterms = map (Syntax.read_term ctxt) funcs |
|
1039 |
val ctxt = set_suffix fterms ctxt |
|
1040 |
val config = { print = true, simp = not no_simp, partial = true } |
|
1041 |
val (_, ctxt') = reg_and_proove_time_func ctxt fterms |
|
1042 |
(case thms of NONE => get_terms ctxt (hd fterms) |> check_definition |
|
1043 |
| SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of) |
|
1044 |
config |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1045 |
in ctxt' |
79969 | 1046 |
end |
1047 |
||
81147 | 1048 |
val parser = (Parse.opt_attribs >> map (fst o Token.name_of_src)) |
1049 |
-- Scan.repeat1 Parse.prop |
|
1050 |
-- Scan.option (Parse.keyword_improper "equations" -- Parse.thms1 >> snd) |
|
1051 |
val _ = Toplevel.local_theory |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1052 |
val _ = Outer_Syntax.local_theory @{command_keyword "time_fun"} |
79490 | 1053 |
"Defines runtime function of a function" |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1054 |
(parser >> reg_time_fun_cmd) |
79490 | 1055 |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1056 |
val _ = Outer_Syntax.local_theory @{command_keyword "time_function"} |
79490 | 1057 |
"Defines runtime function of a function" |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1058 |
(parser >> reg_time_function_cmd) |
79969 | 1059 |
|
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1060 |
val _ = Outer_Syntax.local_theory @{command_keyword "time_definition"} |
79969 | 1061 |
"Defines runtime function of a definition" |
80734
7054a1bc8347
new version of time_fun that works for classes; define T_length automatically now
nipkow
parents:
80657
diff
changeset
|
1062 |
(parser >> reg_time_definition_cmd) |
79490 | 1063 |
|
82081 | 1064 |
val _ = Outer_Syntax.local_theory @{command_keyword "time_partial_function"} |
1065 |
"Defines runtime function of a definition" |
|
1066 |
(parser >> reg_time_partial_function_cmd) |
|
1067 |
||
79490 | 1068 |
end |