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