author | nipkow |
Wed, 27 Mar 2024 16:48:23 +0100 | |
changeset 80036 | a594d22e69d6 |
parent 79969 | 4aeb25ba90f3 |
child 80624 | 9f8034d29365 |
permissions | -rw-r--r-- |
79490 | 1 |
|
2 |
signature TIMING_FUNCTIONS = |
|
3 |
sig |
|
4 |
type 'a converter = { |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
5 |
constc : local_theory -> term list -> (term -> 'a) -> term -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
6 |
funcc : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
7 |
ifc : local_theory -> term list -> (term -> 'a) -> typ -> term -> term -> term -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
8 |
casec : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
9 |
letc : local_theory -> term list -> (term -> 'a) -> typ -> term -> string list -> typ list -> term -> 'a |
79490 | 10 |
}; |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
11 |
val walk : local_theory -> term list -> 'a converter -> term -> 'a |
79490 | 12 |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
13 |
type pfunc = { names : string list, terms : term list, typs : typ list } |
79490 | 14 |
val fun_pretty': Proof.context -> pfunc -> Pretty.T |
15 |
val fun_pretty: Proof.context -> Function.info -> Pretty.T |
|
16 |
val print_timing': Proof.context -> pfunc -> pfunc -> unit |
|
17 |
val print_timing: Proof.context -> Function.info -> Function.info -> unit |
|
18 |
||
79969 | 19 |
val reg_and_proove_time_func: theory -> term list -> term list |
79490 | 20 |
-> bool -> Function.info * theory |
79969 | 21 |
val reg_time_func: theory -> term list -> term list |
22 |
-> bool -> theory |
|
79490 | 23 |
|
24 |
val time_dom_tac: Proof.context -> thm -> thm list -> int -> tactic |
|
25 |
||
26 |
end |
|
27 |
||
28 |
structure Timing_Functions : TIMING_FUNCTIONS = |
|
29 |
struct |
|
30 |
(* Configure config variable to adjust the prefix *) |
|
31 |
val bprefix = Attrib.setup_config_string @{binding "time_prefix"} (K "T_") |
|
32 |
||
33 |
(* some default values to build terms easier *) |
|
34 |
val zero = Const (@{const_name "Groups.zero"}, HOLogic.natT) |
|
35 |
val one = Const (@{const_name "Groups.one"}, HOLogic.natT) |
|
36 |
(* Extracts terms from function info *) |
|
37 |
fun terms_of_info (info: Function.info) = |
|
38 |
let |
|
39 |
val {simps, ...} = info |
|
40 |
in |
|
41 |
map Thm.prop_of (case simps of SOME s => s | NONE => error "No terms of function found in info") |
|
42 |
end; |
|
43 |
||
44 |
type pfunc = { |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
45 |
names : string list, |
79490 | 46 |
terms : term list, |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
47 |
typs : typ list |
79490 | 48 |
} |
49 |
fun info_pfunc (info: Function.info): pfunc = |
|
50 |
let |
|
51 |
val {defname, fs, ...} = info; |
|
52 |
val T = case hd fs of (Const (_,T)) => T | _ => error "Internal error: Invalid info to print" |
|
53 |
in |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
54 |
{ names=[Binding.name_of defname], terms=terms_of_info info, typs=[T] } |
79490 | 55 |
end |
56 |
||
57 |
(* Auxiliary functions for printing functions *) |
|
58 |
fun fun_pretty' ctxt (pfunc: pfunc) = |
|
59 |
let |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
60 |
val {names, terms, typs} = pfunc; |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
61 |
val header_beg = Pretty.str "fun "; |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
62 |
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
|
63 |
val header_content = |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
64 |
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
|
65 |
val header_end = Pretty.str " where\n "; |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
66 |
val header = [header_beg] @ header_content @ [header_end]; |
79490 | 67 |
fun separate sep prts = |
68 |
flat (Library.separate [Pretty.str sep] (map single prts)); |
|
69 |
val ptrms = (separate "\n| " (map (Syntax.pretty_term ctxt) terms)); |
|
70 |
in |
|
71 |
Pretty.text_fold (header @ ptrms) |
|
72 |
end |
|
73 |
fun fun_pretty ctxt = fun_pretty' ctxt o info_pfunc |
|
74 |
fun print_timing' ctxt (opfunc: pfunc) (tpfunc: pfunc) = |
|
75 |
let |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
76 |
val {names, ...} = opfunc; |
79490 | 77 |
val poriginal = Pretty.item [Pretty.str "Original function:\n", fun_pretty' ctxt opfunc] |
78 |
val ptiming = Pretty.item [Pretty.str ("Running time function:\n"), fun_pretty' ctxt tpfunc] |
|
79 |
in |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
80 |
Pretty.writeln (Pretty.text_fold [Pretty.str ("Converting " ^ (hd names) ^ (String.concat (map (fn nm => ", " ^ nm) (tl names))) ^ "\n"), poriginal, Pretty.str "\n", ptiming]) |
79490 | 81 |
end |
82 |
fun print_timing ctxt (oinfo: Function.info) (tinfo: Function.info) = |
|
83 |
print_timing' ctxt (info_pfunc oinfo) (info_pfunc tinfo) |
|
84 |
||
85 |
val If_name = @{const_name "HOL.If"} |
|
86 |
val Let_name = @{const_name "HOL.Let"} |
|
87 |
||
88 |
(* returns true if it's an if term *) |
|
89 |
fun is_if (Const (n,_)) = (n = If_name) |
|
90 |
| is_if _ = false |
|
91 |
(* returns true if it's a case term *) |
|
92 |
fun is_case (Const (n,_)) = String.isPrefix "case_" (List.last (String.fields (fn s => s = #".") n)) |
|
93 |
| is_case _ = false |
|
94 |
(* returns true if it's a let term *) |
|
95 |
fun is_let (Const (n,_)) = (n = Let_name) |
|
96 |
| is_let _ = false |
|
97 |
(* change type of original function to new type (_ \<Rightarrow> ... \<Rightarrow> _ to _ \<Rightarrow> ... \<Rightarrow> nat) |
|
98 |
and replace all function arguments f with (t*T_f) *) |
|
99 |
fun change_typ (Type ("fun", [T1, T2])) = Type ("fun", [check_for_fun T1, change_typ T2]) |
|
100 |
| change_typ _ = HOLogic.natT |
|
101 |
and check_for_fun (f as Type ("fun", [_,_])) = HOLogic.mk_prodT (f, change_typ f) |
|
102 |
| check_for_fun (Type ("Product_Type.prod", [t1,t2])) = HOLogic.mk_prodT (check_for_fun t1, check_for_fun t2) |
|
103 |
| check_for_fun f = f |
|
104 |
(* Convert string name of function to its timing equivalent *) |
|
105 |
fun fun_name_to_time ctxt name = |
|
106 |
let |
|
107 |
val prefix = Config.get ctxt bprefix |
|
108 |
fun replace_last_name [n] = [prefix ^ n] |
|
109 |
| replace_last_name (n::ns) = n :: (replace_last_name ns) |
|
110 |
| replace_last_name _ = error "Internal error: Invalid function name to convert" |
|
111 |
val parts = String.fields (fn s => s = #".") name |
|
112 |
in |
|
113 |
String.concatWith "." (replace_last_name parts) |
|
114 |
end |
|
115 |
(* Count number of arguments of a function *) |
|
116 |
fun count_args (Type (n, [_,res])) = (if n = "fun" then 1 + count_args res else 0) |
|
117 |
| count_args _ = 0 |
|
118 |
(* Check if number of arguments matches function *) |
|
119 |
fun check_args s (Const (_,T), args) = |
|
120 |
(if length args = count_args T then () else error ("Partial applications/Lambdas not allowed (" ^ s ^ ")")) |
|
121 |
| check_args s (Free (_,T), args) = |
|
122 |
(if length args = count_args T then () else error ("Partial applications/Lambdas not allowed (" ^ s ^ ")")) |
|
123 |
| check_args s _ = error ("Partial applications/Lambdas not allowed (" ^ s ^ ")") |
|
124 |
(* Removes Abs *) |
|
125 |
fun rem_abs f (Abs (_,_,t)) = rem_abs f t |
|
126 |
| rem_abs f t = f t |
|
127 |
(* Map right side of equation *) |
|
128 |
fun map_r f (pT $ (eq $ l $ r)) = (pT $ (eq $ l $ f r)) |
|
129 |
| map_r _ _ = error "Internal error: No right side of equation found" |
|
130 |
(* Get left side of equation *) |
|
131 |
fun get_l (_ $ (_ $ l $ _)) = l |
|
132 |
| get_l _ = error "Internal error: No left side of equation found" |
|
133 |
(* Get right side of equation *) |
|
134 |
fun get_r (_ $ (_ $ _ $ r)) = r |
|
135 |
| get_r _ = error "Internal error: No right side of equation found" |
|
136 |
(* Return name of Const *) |
|
137 |
fun Const_name (Const (nm,_)) = SOME nm |
|
138 |
| Const_name _ = NONE |
|
139 |
||
140 |
fun time_term ctxt (Const (nm,T)) = |
|
141 |
let |
|
142 |
val T_nm = fun_name_to_time ctxt nm |
|
143 |
val T_T = change_typ T |
|
144 |
in |
|
145 |
(SOME (Syntax.check_term ctxt (Const (T_nm,T_T)))) |
|
146 |
handle (ERROR _) => |
|
147 |
case Syntax.read_term ctxt (Long_Name.base_name T_nm) |
|
148 |
of (Const (nm,_)) => SOME (Const (nm,T_T)) |
|
149 |
| _ => error ("Timing function of " ^ nm ^ " is not defined") |
|
150 |
end |
|
151 |
| time_term _ _ = error "Internal error: No valid function given" |
|
152 |
||
153 |
type 'a converter = { |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
154 |
constc : local_theory -> term list -> (term -> 'a) -> term -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
155 |
funcc : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
156 |
ifc : local_theory -> term list -> (term -> 'a) -> typ -> term -> term -> term -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
157 |
casec : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a, |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
158 |
letc : local_theory -> term list -> (term -> 'a) -> typ -> term -> string list -> typ list -> term -> 'a |
79490 | 159 |
}; |
160 |
||
161 |
(* Walks over term and calls given converter *) |
|
162 |
fun walk_func (t1 $ t2) ts = walk_func t1 (t2::ts) |
|
163 |
| walk_func t ts = (t, ts) |
|
164 |
fun build_func (f, []) = f |
|
165 |
| build_func (f, (t::ts)) = build_func (f$t, ts) |
|
166 |
fun walk_abs (Abs (nm,T,t)) nms Ts = walk_abs t (nm::nms) (T::Ts) |
|
167 |
| walk_abs t nms Ts = (t, nms, Ts) |
|
168 |
fun build_abs t (nm::nms) (T::Ts) = build_abs (Abs (nm,T,t)) nms Ts |
|
169 |
| build_abs t [] [] = t |
|
170 |
| build_abs _ _ _ = error "Internal error: Invalid terms to build abs" |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
171 |
fun walk ctxt (origin: term list) (conv as {ifc, casec, funcc, letc, ...} : 'a converter) (t as _ $ _) = |
79490 | 172 |
let |
173 |
val (f, args) = walk_func t [] |
|
174 |
val this = (walk ctxt origin conv) |
|
175 |
val _ = (case f of Abs _ => error "Lambdas not supported" | _ => ()) |
|
176 |
in |
|
177 |
(if is_if f then |
|
178 |
(case f of (Const (_,T)) => |
|
179 |
(case args of [cond, t, f] => ifc ctxt origin this T cond t f |
|
180 |
| _ => error "Partial applications not supported (if)") |
|
181 |
| _ => error "Internal error: invalid if term") |
|
182 |
else if is_case f then casec ctxt origin this f args |
|
183 |
else if is_let f then |
|
184 |
(case f of (Const (_,lT)) => |
|
185 |
(case args of [exp, t] => |
|
186 |
let val (t,nms,Ts) = walk_abs t [] [] in letc ctxt origin this lT exp nms Ts t end |
|
187 |
| _ => error "Partial applications not allowed (let)") |
|
188 |
| _ => error "Internal error: invalid let term") |
|
189 |
else funcc ctxt origin this f args) |
|
190 |
end |
|
191 |
| walk ctxt origin (conv as {constc, ...}) c = |
|
192 |
constc ctxt origin (walk ctxt origin conv) c |
|
193 |
||
194 |
(* 1. Fix all terms *) |
|
195 |
(* Exchange Var in types and terms to Free *) |
|
196 |
fun fixTerms (Var(ixn,T)) = Free (fst ixn, T) |
|
197 |
| fixTerms t = t |
|
198 |
fun fixTypes (TVar ((t, _), T)) = TFree (t, T) |
|
199 |
| fixTypes t = t |
|
79542 | 200 |
|
201 |
fun noFun (Type ("fun",_)) = error "Functions in datatypes are not allowed in case constructions" |
|
202 |
| noFun _ = () |
|
79490 | 203 |
fun casecBuildBounds n t = if n > 0 then casecBuildBounds (n-1) (t $ (Bound (n-1))) else t |
79542 | 204 |
fun casecAbs ctxt f n (Type (_,[T,Tr])) (Abs (v,Ta,t)) = (noFun T; Abs (v,Ta,casecAbs ctxt f n Tr t)) |
79490 | 205 |
| casecAbs ctxt f n (Type (Tn,[T,Tr])) t = |
79542 | 206 |
(noFun T; case Variable.variant_fixes ["x"] ctxt of ([v],ctxt) => |
79714 | 207 |
(if Tn = "fun" then Abs (v,T,casecAbs ctxt f (n + 1) Tr t) else f t) |
79490 | 208 |
| _ => error "Internal error: could not fix variable") |
79714 | 209 |
| casecAbs _ f n _ t = f (casecBuildBounds n (Term.incr_bv n 0 t)) |
79490 | 210 |
fun fixCasecCases _ _ _ [t] = [t] |
211 |
| fixCasecCases ctxt f (Type (_,[T,Tr])) (t::ts) = casecAbs ctxt f 0 T t :: fixCasecCases ctxt f Tr ts |
|
212 |
| fixCasecCases _ _ _ _ = error "Internal error: invalid case types/terms" |
|
79542 | 213 |
fun fixCasec ctxt _ f (t as Const (_,T)) args = |
214 |
(check_args "cases" (t,args); build_func (t,fixCasecCases ctxt f T args)) |
|
79490 | 215 |
| fixCasec _ _ _ _ _ = error "Internal error: invalid case term" |
216 |
||
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
217 |
fun fixPartTerms ctxt (term: term list) t = |
79490 | 218 |
let |
219 |
val _ = check_args "args" (walk_func (get_l t) []) |
|
220 |
in |
|
221 |
map_r (walk ctxt term { |
|
222 |
funcc = (fn _ => fn _ => fn f => fn t => fn args => |
|
223 |
(check_args "func" (t,args); build_func (t, map f args))), |
|
224 |
constc = (fn _ => fn _ => fn _ => fn c => (case c of Abs _ => error "Lambdas not supported" | _ => c)), |
|
225 |
ifc = (fn _ => fn _ => fn f => fn T => fn cond => fn tt => fn tf => |
|
226 |
((Const (If_name, T)) $ f cond $ (f tt) $ (f tf))), |
|
227 |
casec = fixCasec, |
|
228 |
letc = (fn _ => fn _ => fn f => fn expT => fn exp => fn nms => fn Ts => fn t => |
|
229 |
let |
|
230 |
val f' = if length nms = 0 then |
|
231 |
(case f (t$exp) of t$_ => t | _ => error "Internal error: case could not be fixed (let)") |
|
232 |
else f t |
|
79542 | 233 |
in (Const (Let_name,expT) $ (f exp) $ build_abs f' nms Ts) end) |
79490 | 234 |
}) t |
235 |
end |
|
236 |
||
237 |
(* 2. Check if function is recursive *) |
|
238 |
fun or f (a,b) = f a orelse b |
|
239 |
fun find_rec ctxt term = (walk ctxt term { |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
240 |
funcc = (fn _ => fn _ => fn f => fn t => fn args => List.exists (fn term => Const_name t = Const_name term) term orelse List.foldr (or f) false args), |
79490 | 241 |
constc = (K o K o K o K) false, |
242 |
ifc = (fn _ => fn _ => fn f => fn _ => fn cond => fn tt => fn tf => f cond orelse f tt orelse f tf), |
|
243 |
casec = (fn _ => fn _ => fn f => fn t => fn cs => f t orelse List.foldr (or (rem_abs f)) false cs), |
|
244 |
letc = (fn _ => fn _ => fn f => fn _ => fn exp => fn _ => fn _ => fn t => f exp orelse f t) |
|
245 |
}) o get_r |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
246 |
fun is_rec ctxt (term: term list) = List.foldr (or (find_rec ctxt term)) false |
79490 | 247 |
|
248 |
(* 3. Convert equations *) |
|
249 |
(* Some Helper *) |
|
250 |
val plusTyp = @{typ "nat => nat => nat"} |
|
251 |
fun plus (SOME a) (SOME b) = SOME (Const (@{const_name "Groups.plus"}, plusTyp) $ a $ b) |
|
252 |
| plus (SOME a) NONE = SOME a |
|
253 |
| plus NONE (SOME b) = SOME b |
|
254 |
| plus NONE NONE = NONE |
|
255 |
fun opt_term NONE = HOLogic.zero |
|
256 |
| opt_term (SOME t) = t |
|
257 |
||
258 |
(* Converting of function term *) |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
259 |
fun fun_to_time ctxt (origin: term list) (func as Const (nm,T)) = |
79490 | 260 |
let |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
261 |
val full_name_origin = map (fst o dest_Const) origin |
79490 | 262 |
val prefix = Config.get ctxt bprefix |
263 |
in |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
264 |
if List.exists (fn nm_orig => nm = nm_orig) full_name_origin then SOME (Free (prefix ^ Term.term_name func, change_typ T)) else |
79490 | 265 |
if Zero_Funcs.is_zero (Proof_Context.theory_of ctxt) (nm,T) then NONE else |
266 |
time_term ctxt func |
|
267 |
end |
|
268 |
| fun_to_time _ _ (Free (nm,T)) = SOME (HOLogic.mk_snd (Free (nm,HOLogic.mk_prodT (T,change_typ T)))) |
|
269 |
| fun_to_time _ _ _ = error "Internal error: invalid function to convert" |
|
270 |
||
271 |
(* Convert arguments of left side of a term *) |
|
272 |
fun conv_arg _ _ (Free (nm,T as Type("fun",_))) = Free (nm, HOLogic.mk_prodT (T, change_typ T)) |
|
273 |
| conv_arg ctxt origin (f as Const (_,T as Type("fun",_))) = |
|
274 |
(Const (@{const_name "Product_Type.Pair"}, |
|
275 |
Type ("fun", [T,Type ("fun",[change_typ T, HOLogic.mk_prodT (T,change_typ T)])])) |
|
276 |
$ f $ (fun_to_time ctxt origin f |> Option.valOf)) |
|
277 |
| conv_arg ctxt origin ((Const ("Product_Type.Pair", _)) $ l $ r) = HOLogic.mk_prod (conv_arg ctxt origin l, conv_arg ctxt origin r) |
|
278 |
| conv_arg _ _ x = x |
|
279 |
fun conv_args ctxt origin = map (conv_arg ctxt origin) |
|
280 |
||
281 |
(* Handle function calls *) |
|
282 |
fun build_zero (Type ("fun", [T, R])) = Abs ("x", T, build_zero R) |
|
283 |
| build_zero _ = zero |
|
284 |
fun funcc_use_origin _ _ (Free (nm, T as Type ("fun",_))) = HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T))) |
|
285 |
| funcc_use_origin _ _ t = t |
|
286 |
fun funcc_conv_arg ctxt origin (t as (_ $ _)) = map_aterms (funcc_use_origin ctxt origin) t |
|
287 |
| funcc_conv_arg _ _ (Free (nm, T as Type ("fun",_))) = (Free (nm, HOLogic.mk_prodT (T, change_typ T))) |
|
288 |
| funcc_conv_arg ctxt origin (f as Const (_,T as Type ("fun",_))) = |
|
289 |
(Const (@{const_name "Product_Type.Pair"}, |
|
290 |
Type ("fun", [T,Type ("fun",[change_typ T, HOLogic.mk_prodT (T,change_typ T)])])) |
|
291 |
$ f $ (Option.getOpt (fun_to_time ctxt origin f, build_zero T))) |
|
292 |
| funcc_conv_arg _ _ t = t |
|
293 |
fun funcc_conv_args ctxt origin = map (funcc_conv_arg ctxt origin) |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
294 |
fun funcc ctxt (origin: term list) f func args = List.foldr (I #-> plus) |
79490 | 295 |
(case fun_to_time ctxt origin func of SOME t => SOME (build_func (t,funcc_conv_args ctxt origin args)) |
296 |
| NONE => NONE) |
|
297 |
(map f args) |
|
298 |
||
299 |
(* Handle case terms *) |
|
300 |
fun casecIsCase (Type (n1, [_,Type (n2, _)])) = (n1 = "fun" andalso n2 = "fun") |
|
301 |
| casecIsCase _ = false |
|
302 |
fun casecLastTyp (Type (n, [T1,T2])) = Type (n, [T1, change_typ T2]) |
|
303 |
| casecLastTyp _ = error "Internal error: invalid case type" |
|
304 |
fun casecTyp (Type (n, [T1, T2])) = |
|
305 |
Type (n, [change_typ T1, (if casecIsCase T2 then casecTyp else casecLastTyp) T2]) |
|
306 |
| casecTyp _ = error "Internal error: invalid case type" |
|
307 |
fun casecAbs f (Abs (v,Ta,t)) = (case casecAbs f t of (nconst,t) => (nconst,Abs (v,Ta,t))) |
|
308 |
| casecAbs f t = (case f t of NONE => (false,HOLogic.zero) | SOME t => (true,t)) |
|
309 |
fun casecArgs _ [t] = (false, [t]) |
|
310 |
| casecArgs f (t::ar) = |
|
311 |
(case casecAbs f t of (nconst, tt) => |
|
312 |
casecArgs f ar ||> (fn ar => tt :: ar) |>> (if nconst then K true else I)) |
|
313 |
| casecArgs _ _ = error "Internal error: invalid case term" |
|
314 |
fun casec _ _ f (Const (t,T)) args = |
|
315 |
if not (casecIsCase T) then error "Internal error: invalid case type" else |
|
316 |
let val (nconst, args') = casecArgs f args in |
|
317 |
plus |
|
79969 | 318 |
(f (List.last args)) |
79490 | 319 |
(if nconst then |
320 |
SOME (build_func (Const (t,casecTyp T), args')) |
|
321 |
else NONE) |
|
322 |
end |
|
323 |
| casec _ _ _ _ _ = error "Internal error: invalid case term" |
|
324 |
||
325 |
(* Handle if terms -> drop the term if true and false terms are zero *) |
|
326 |
fun ifc ctxt origin f _ cond tt ft = |
|
327 |
let |
|
328 |
fun use_origin _ _ (Free (nm, T as Type ("fun",_))) = HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T))) |
|
329 |
| use_origin _ _ t = t |
|
330 |
val rcond = map_aterms (use_origin ctxt origin) cond |
|
331 |
val tt = f tt |
|
332 |
val ft = f ft |
|
333 |
in |
|
79969 | 334 |
plus (f cond) (case (tt,ft) of (NONE, NONE) => NONE | _ => |
335 |
(SOME ((Const (If_name, @{typ "bool \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"})) $ rcond $ (opt_term tt) $ (opt_term ft)))) |
|
79490 | 336 |
end |
337 |
||
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
338 |
fun letc_change_typ (Type ("fun", [T1, Type ("fun", [T2, _])])) = (Type ("fun", [T1, Type ("fun", [change_typ T2, HOLogic.natT])])) |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
339 |
| letc_change_typ _ = error "Internal error: invalid let type" |
79490 | 340 |
fun letc _ _ f expT exp nms Ts t = |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
341 |
plus (f exp) |
79542 | 342 |
(if length nms = 0 (* In case of "length nms = 0" the expression got reducted |
343 |
Here we need Bound 0 to gain non-partial application *) |
|
344 |
then (case f (t $ Bound 0) of SOME (t' $ Bound 0) => |
|
345 |
(SOME (Const (Let_name, letc_change_typ expT) $ exp $ t')) |
|
346 |
(* Expression is not used and can therefore let be dropped *) |
|
347 |
| SOME t' => SOME t' |
|
348 |
| NONE => NONE) |
|
349 |
else (case f t of SOME t' => |
|
350 |
SOME (if Term.is_dependent t' then Const (Let_name, letc_change_typ expT) $ exp $ build_abs t' nms Ts |
|
351 |
else Term.subst_bounds([exp],t')) |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
352 |
| NONE => NONE)) |
79490 | 353 |
|
354 |
(* The converter for timing functions given to the walker *) |
|
355 |
val converter : term option converter = { |
|
80036 | 356 |
constc = fn _ => fn _ => fn _ => fn t => |
357 |
(case t of Const ("HOL.undefined", _) => SOME (Const ("HOL.undefined", @{typ "nat"})) |
|
358 |
| _ => NONE), |
|
79490 | 359 |
funcc = funcc, |
360 |
ifc = ifc, |
|
361 |
casec = casec, |
|
362 |
letc = letc |
|
363 |
} |
|
79665 | 364 |
fun top_converter is_rec _ _ = opt_term o (fn exp => plus exp (if is_rec then SOME one else NONE)) |
79490 | 365 |
|
366 |
(* Use converter to convert right side of a term *) |
|
79969 | 367 |
fun to_time ctxt origin is_rec term = |
368 |
top_converter is_rec ctxt origin (walk ctxt origin converter term) |
|
79490 | 369 |
|
370 |
(* Converts a term to its running time version *) |
|
79969 | 371 |
fun convert_term ctxt (origin: term list) is_rec (pT $ (Const (eqN, _) $ l $ r)) = |
79490 | 372 |
pT |
373 |
$ (Const (eqN, @{typ "nat \<Rightarrow> nat \<Rightarrow> bool"}) |
|
374 |
$ (build_func ((walk_func l []) |>> (fun_to_time ctxt origin) |>> Option.valOf ||> conv_args ctxt origin)) |
|
79969 | 375 |
$ (to_time ctxt origin is_rec r)) |
376 |
| convert_term _ _ _ _ = error "Internal error: invalid term to convert" |
|
79490 | 377 |
|
378 |
(* 4. Tactic to prove "f_dom n" *) |
|
379 |
fun time_dom_tac ctxt induct_rule domintros = |
|
380 |
(Induction.induction_tac ctxt true [] [[]] [] (SOME [induct_rule]) [] |
|
381 |
THEN_ALL_NEW ((K (auto_tac ctxt)) THEN' (fn i => FIRST' ( |
|
382 |
(if i <= length domintros then [Metis_Tactic.metis_tac [] ATP_Problem_Generate.combsN ctxt [List.nth (domintros, i-1)]] else []) @ |
|
383 |
[Metis_Tactic.metis_tac [] ATP_Problem_Generate.combsN ctxt domintros]) i))) |
|
384 |
||
385 |
||
386 |
fun get_terms theory (term: term) = |
|
387 |
Spec_Rules.retrieve_global theory term |
|
388 |
|> hd |> #rules |
|
389 |
|> map Thm.prop_of |
|
390 |
handle Empty => error "Function or terms of function not found" |
|
391 |
||
392 |
(* Register timing function of a given function *) |
|
79969 | 393 |
fun reg_and_proove_time_func (theory: theory) (term: term list) (terms: term list) print = |
394 |
reg_time_func theory term terms false |
|
79490 | 395 |
|> proove_termination term terms print |
79969 | 396 |
and reg_time_func (theory: theory) (term: term list) (terms: term list) print = |
79490 | 397 |
let |
398 |
val lthy = Named_Target.theory_init theory |
|
399 |
val _ = |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
400 |
case time_term lthy (hd term) |
79490 | 401 |
handle (ERROR _) => NONE |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
402 |
of SOME _ => error ("Timing function already declared: " ^ (Term.term_name (hd term))) |
79490 | 403 |
| NONE => () |
404 |
||
405 |
(* 1. Fix all terms *) |
|
406 |
(* Exchange Var in types and terms to Free and check constraints *) |
|
79969 | 407 |
val terms = map |
79542 | 408 |
(map_aterms fixTerms |
409 |
#> map_types (map_atyps fixTypes) |
|
410 |
#> fixPartTerms lthy term) |
|
411 |
terms |
|
79490 | 412 |
|
413 |
(* 2. Check if function is recursive *) |
|
414 |
val is_rec = is_rec lthy term terms |
|
415 |
||
416 |
(* 3. Convert every equation |
|
417 |
- Change type of toplevel equation from _ \<Rightarrow> _ \<Rightarrow> bool to nat \<Rightarrow> nat \<Rightarrow> bool |
|
418 |
- On left side change name of function to timing function |
|
419 |
- Convert right side of equation with conversion schema |
|
420 |
*) |
|
79969 | 421 |
val timing_terms = map (convert_term lthy term is_rec) terms |
79490 | 422 |
|
423 |
(* 4. Register function and prove termination *) |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
424 |
val names = map Term.term_name term |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
425 |
val timing_names = map (fun_name_to_time lthy) names |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
426 |
val bindings = map (fn nm => (Binding.name nm, NONE, NoSyn)) timing_names |
79490 | 427 |
fun pat_completeness_auto ctxt = |
428 |
Pat_Completeness.pat_completeness_tac ctxt 1 THEN auto_tac ctxt |
|
429 |
val specs = map (fn eq => (((Binding.empty, []), eq), [], [])) timing_terms |
|
430 |
||
431 |
(* For partial functions sequential=true is needed in order to support them |
|
432 |
We need sequential=false to support the automatic proof of termination over dom |
|
433 |
*) |
|
434 |
fun register seq = |
|
435 |
let |
|
436 |
val _ = (if seq then warning "Falling back on sequential function..." else ()) |
|
437 |
val fun_config = Function_Common.FunctionConfig |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
438 |
{sequential=seq, default=NONE, domintros=true, partials=false} |
79490 | 439 |
in |
440 |
Function.add_function bindings specs fun_config pat_completeness_auto lthy |
|
441 |
end |
|
442 |
||
443 |
(* Context for printing without showing question marks *) |
|
444 |
val print_ctxt = lthy |
|
445 |
|> Config.put show_question_marks false |
|
446 |
|> Config.put show_sorts false (* Change it for debugging *) |
|
79542 | 447 |
val print_ctxt = List.foldl (fn (term, ctxt) => Variable.add_fixes_implicit term ctxt) print_ctxt (term@timing_terms) |
79490 | 448 |
(* Print result if print *) |
449 |
val _ = if not print then () else |
|
450 |
let |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
451 |
val nms = map (fst o dest_Const) term |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
452 |
val typs = map (snd o dest_Const) term |
79490 | 453 |
in |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
454 |
print_timing' print_ctxt { names=nms, terms=terms, typs=typs } { names=timing_names, terms=timing_terms, typs=map change_typ typs } |
79490 | 455 |
end |
456 |
||
457 |
(* Register function *) |
|
458 |
val (_, lthy) = |
|
459 |
register false |
|
460 |
handle (ERROR _) => |
|
461 |
register true |
|
462 |
| Match => |
|
463 |
register true |
|
464 |
in |
|
465 |
Local_Theory.exit_global lthy |
|
466 |
end |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
467 |
and proove_termination (term: term list) terms print (theory: theory) = |
79490 | 468 |
let |
469 |
val lthy = Named_Target.theory_init theory |
|
470 |
||
471 |
(* Start proving the termination *) |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
472 |
val infos = SOME (map (Function.get_info lthy) term) handle Empty => NONE |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
473 |
val timing_names = map (fun_name_to_time lthy o Term.term_name) term |
79490 | 474 |
|
475 |
(* Proof by lexicographic_order_tac *) |
|
476 |
val (time_info, lthy') = |
|
477 |
(Function.prove_termination NONE |
|
478 |
(Lexicographic_Order.lexicographic_order_tac false lthy) lthy) |
|
479 |
handle (ERROR _) => |
|
480 |
let |
|
481 |
val _ = warning "Falling back on proof over dom..." |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
482 |
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
|
483 |
|
79490 | 484 |
fun args (a$(Var ((nm,_),T))) = args a |> (fn ar => (nm,T)::ar) |
485 |
| args (a$(Const (_,T))) = args a |> (fn ar => ("x",T)::ar) |
|
486 |
| args _ = [] |
|
487 |
val dom_args = |
|
488 |
terms |> hd |> get_l |> args |
|
489 |
|> Variable.variant_frees lthy [] |
|
490 |
|> map fst |
|
491 |
||
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
492 |
val {inducts, ...} = case infos of SOME [i] => i | _ => error "Proof over dom failed as no induct rule was found" |
79490 | 493 |
val induct = (Option.valOf inducts |> hd) |
494 |
||
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
495 |
val domintros = Proof_Context.get_fact lthy (Facts.named (hd timing_names ^ ".domintros")) |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
496 |
val prop = (hd timing_names ^ "_dom (" ^ (String.concatWith "," dom_args) ^ ")") |
79490 | 497 |
|> Syntax.read_prop lthy |
498 |
||
499 |
(* Prove a helper lemma *) |
|
500 |
val dom_lemma = Goal.prove lthy dom_args [] prop |
|
501 |
(fn {context, ...} => HEADGOAL (time_dom_tac context induct domintros)) |
|
502 |
(* Add dom_lemma to simplification set *) |
|
503 |
val simp_lthy = Simplifier.add_simp dom_lemma lthy |
|
504 |
in |
|
505 |
(* Use lemma to prove termination *) |
|
506 |
Function.prove_termination NONE |
|
507 |
(auto_tac simp_lthy) lthy |
|
508 |
end |
|
509 |
||
510 |
(* Context for printing without showing question marks *) |
|
511 |
val print_ctxt = lthy' |
|
512 |
|> Config.put show_question_marks false |
|
513 |
|> Config.put show_sorts false (* Change it for debugging *) |
|
514 |
(* Print result if print *) |
|
515 |
val _ = if not print then () else |
|
516 |
let |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
517 |
val nms = map (fst o dest_Const) term |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
518 |
val typs = map (snd o dest_Const) term |
79490 | 519 |
in |
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
520 |
print_timing' print_ctxt { names=nms, terms=terms, typs=typs } (info_pfunc time_info) |
79490 | 521 |
end |
522 |
in |
|
523 |
(time_info, Local_Theory.exit_global lthy') |
|
524 |
end |
|
525 |
||
79969 | 526 |
fun fix_definition (Const ("Pure.eq", _) $ l $ r) = Const ("HOL.Trueprop", @{typ "bool \<Rightarrow> prop"}) |
527 |
$ (Const ("HOL.eq", @{typ "bool \<Rightarrow> bool \<Rightarrow> bool"}) $ l $ r) |
|
528 |
| fix_definition t = t |
|
529 |
fun check_definition [t] = [t] |
|
530 |
| check_definition _ = error "Only a single defnition is allowed" |
|
531 |
||
79490 | 532 |
(* Convert function into its timing function (called by command) *) |
79969 | 533 |
fun reg_time_fun_cmd (funcs, thms) (theory: theory) = |
79490 | 534 |
let |
535 |
val ctxt = Proof_Context.init_global theory |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
536 |
val fterms = map (Syntax.read_term ctxt) funcs |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
537 |
val (_, lthy') = reg_and_proove_time_func theory fterms |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
538 |
(case thms of NONE => get_terms theory (hd fterms) |
79490 | 539 |
| SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of) |
79969 | 540 |
true |
79490 | 541 |
in lthy' |
542 |
end |
|
543 |
||
544 |
(* Convert function into its timing function (called by command) with termination proof provided by user*) |
|
79969 | 545 |
fun reg_time_function_cmd (funcs, thms) (theory: theory) = |
79490 | 546 |
let |
547 |
val ctxt = Proof_Context.init_global theory |
|
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
548 |
val fterms = map (Syntax.read_term ctxt) funcs |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
549 |
val theory = reg_time_func theory fterms |
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
550 |
(case thms of NONE => get_terms theory (hd fterms) |
79490 | 551 |
| SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of) |
79969 | 552 |
true |
79490 | 553 |
in theory |
554 |
end |
|
555 |
||
79969 | 556 |
(* Convert function into its timing function (called by command) *) |
557 |
fun reg_time_definition_cmd (funcs, thms) (theory: theory) = |
|
558 |
let |
|
559 |
val ctxt = Proof_Context.init_global theory |
|
560 |
val fterms = map (Syntax.read_term ctxt) funcs |
|
561 |
val (_, lthy') = reg_and_proove_time_func theory fterms |
|
562 |
(case thms of NONE => get_terms theory (hd fterms) |> check_definition |> map fix_definition |
|
563 |
| SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of) |
|
564 |
true |
|
565 |
in lthy' |
|
566 |
end |
|
567 |
||
79494
c7536609bb9b
translation to time functions now with canonical let.
nipkow
parents:
79490
diff
changeset
|
568 |
val parser = (Scan.repeat1 Parse.prop) -- (Scan.option (Parse.keyword_improper "equations" -- Parse.thms1 >> snd)) |
79490 | 569 |
|
79969 | 570 |
val _ = Outer_Syntax.command @{command_keyword "time_fun"} |
79490 | 571 |
"Defines runtime function of a function" |
79969 | 572 |
(parser >> (fn p => Toplevel.theory (reg_time_fun_cmd p))) |
79490 | 573 |
|
79969 | 574 |
val _ = Outer_Syntax.command @{command_keyword "time_function"} |
79490 | 575 |
"Defines runtime function of a function" |
79969 | 576 |
(parser >> (fn p => Toplevel.theory (reg_time_function_cmd p))) |
577 |
||
578 |
val _ = Outer_Syntax.command @{command_keyword "time_definition"} |
|
579 |
"Defines runtime function of a definition" |
|
580 |
(parser >> (fn p => Toplevel.theory (reg_time_definition_cmd p))) |
|
79490 | 581 |
|
582 |
end |