266 | lambda_free _ = true; |
266 | lambda_free _ = true; |
267 |
267 |
268 fun monomorphic t = |
268 fun monomorphic t = |
269 Term.fold_types (Term.fold_atyps (fn TVar _ => K false | _ => I)) t true; |
269 Term.fold_types (Term.fold_atyps (fn TVar _ => K false | _ => I)) t true; |
270 |
270 |
|
271 fun dest_abs_list ct = |
|
272 let val (cv,ct') = Thm.dest_abs NONE ct |
|
273 val (cvs,cu) = dest_abs_list ct' |
|
274 in (cv::cvs, cu) end |
|
275 handle CTERM _ => ([],ct); |
|
276 |
|
277 fun lambda_list [] u = u |
|
278 | lambda_list (v::vs) u = lambda v (lambda_list vs u); |
|
279 |
|
280 fun abstract_rule_list [] [] th = th |
|
281 | abstract_rule_list (v::vs) (ct::cts) th = abstract_rule v ct (abstract_rule_list vs cts th) |
|
282 | abstract_rule_list _ _ th = raise THM ("abstract_rule_list", 0, [th]); |
|
283 |
271 (*Traverse a theorem, declaring abstraction function definitions. String s is the suggested |
284 (*Traverse a theorem, declaring abstraction function definitions. String s is the suggested |
272 prefix for the constants. Resulting theory is returned in the first theorem. *) |
285 prefix for the constants. Resulting theory is returned in the first theorem. *) |
273 fun declare_absfuns th = |
286 fun declare_absfuns th = |
274 let fun abstract thy ct = |
287 let fun abstract thy ct = |
275 if lambda_free (term_of ct) then (transfer thy (reflexive ct), []) |
288 if lambda_free (term_of ct) then (transfer thy (reflexive ct), []) |
276 else |
289 else |
277 case term_of ct of |
290 case term_of ct of |
278 Abs (_,T,u) => |
291 Abs _ => |
279 let val cname = Name.internal (gensym "abs_"); |
292 let val cname = Name.internal (gensym "abs_"); |
280 val _ = assert_eta_free ct; |
293 val _ = assert_eta_free ct; |
281 val (cv,cta) = Thm.dest_abs NONE ct |
294 val (cvs,cta) = dest_abs_list ct |
282 val v = (#1 o dest_Free o term_of) cv |
295 val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs) |
283 val (u'_th,defs) = abstract thy cta |
296 val (u'_th,defs) = abstract thy cta |
284 val cu' = crhs_of u'_th |
297 val cu' = crhs_of u'_th |
285 val abs_v_u = lambda (term_of cv) (term_of cu') |
298 val abs_v_u = lambda_list (map term_of cvs) (term_of cu') |
286 (*get the formal parameters: ALL variables free in the term*) |
299 (*get the formal parameters: ALL variables free in the term*) |
287 val args = term_frees abs_v_u |
300 val args = term_frees abs_v_u |
288 val rhs = list_abs_free (map dest_Free args, abs_v_u) |
301 val rhs = list_abs_free (map dest_Free args, abs_v_u) |
289 (*Forms a lambda-abstraction over the formal parameters*) |
302 (*Forms a lambda-abstraction over the formal parameters*) |
290 val v_rhs = Logic.varify rhs |
303 val v_rhs = Logic.varify rhs |
292 case List.find (fn ax => v_rhs aconv rhs_of ax) |
305 case List.find (fn ax => v_rhs aconv rhs_of ax) |
293 (Net.match_term (!abstraction_cache) v_rhs) of |
306 (Net.match_term (!abstraction_cache) v_rhs) of |
294 SOME ax => (ax,thy) (*cached axiom, current theory*) |
307 SOME ax => (ax,thy) (*cached axiom, current theory*) |
295 | NONE => |
308 | NONE => |
296 let val Ts = map type_of args |
309 let val Ts = map type_of args |
297 val cT = Ts ---> (T --> typ_of (ctyp_of_term cu')) |
310 val cT = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu')) |
298 val thy = theory_of_thm u'_th |
311 val thy = theory_of_thm u'_th |
299 val c = Const (Sign.full_name thy cname, cT) |
312 val c = Const (Sign.full_name thy cname, cT) |
300 val thy = Theory.add_consts_i [(cname, cT, NoSyn)] thy |
313 val thy = Theory.add_consts_i [(cname, cT, NoSyn)] thy |
301 (*Theory is augmented with the constant, |
314 (*Theory is augmented with the constant, |
302 then its definition*) |
315 then its definition*) |
310 raise THM ("declare_absfuns: INSERT", 0, [th,u'_th,ax]) |
323 raise THM ("declare_absfuns: INSERT", 0, [th,u'_th,ax]) |
311 in (ax,thy) end |
324 in (ax,thy) end |
312 val _ = assert (v_rhs aconv rhs_of ax) "declare_absfuns: rhs mismatch" |
325 val _ = assert (v_rhs aconv rhs_of ax) "declare_absfuns: rhs mismatch" |
313 val def = #1 (Drule.freeze_thaw ax) |
326 val def = #1 (Drule.freeze_thaw ax) |
314 val def_args = list_combination def (map (cterm_of thy) args) |
327 val def_args = list_combination def (map (cterm_of thy) args) |
315 in (transitive (abstract_rule v cv u'_th) (symmetric def_args), |
328 in (transitive (abstract_rule_list vs cvs u'_th) (symmetric def_args), |
316 def :: defs) end |
329 def :: defs) end |
317 | (t1$t2) => |
330 | (t1$t2) => |
318 let val (ct1,ct2) = Thm.dest_comb ct |
331 let val (ct1,ct2) = Thm.dest_comb ct |
319 val (th1,defs1) = abstract thy ct1 |
332 val (th1,defs1) = abstract thy ct1 |
320 val (th2,defs2) = abstract (theory_of_thm th1) ct2 |
333 val (th2,defs2) = abstract (theory_of_thm th1) ct2 |
337 fun abstract ct = |
350 fun abstract ct = |
338 if lambda_free (term_of ct) then (reflexive ct, []) |
351 if lambda_free (term_of ct) then (reflexive ct, []) |
339 else |
352 else |
340 case term_of ct of |
353 case term_of ct of |
341 Abs (_,T,u) => |
354 Abs (_,T,u) => |
342 let val (cv,cta) = Thm.dest_abs NONE ct |
355 let val _ = assert_eta_free ct; |
343 val _ = assert_eta_free ct; |
356 val (cvs,cta) = dest_abs_list ct |
344 val v = (#1 o dest_Free o term_of) cv |
357 val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs) |
345 val (u'_th,defs) = abstract cta |
358 val (u'_th,defs) = abstract cta |
346 val cu' = crhs_of u'_th |
359 val cu' = crhs_of u'_th |
347 val abs_v_u = Thm.cabs cv cu' |
360 (*Could use Thm.cabs instead of lambda to work at level of cterms*) |
|
361 val abs_v_u = lambda_list (map term_of cvs) (term_of cu') |
348 (*get the formal parameters: free variables not present in the defs |
362 (*get the formal parameters: free variables not present in the defs |
349 (to avoid taking abstraction function names as parameters) *) |
363 (to avoid taking abstraction function names as parameters) *) |
350 val args = filter (valid_name defs) (term_frees (term_of abs_v_u)) |
364 val args = filter (valid_name defs) (term_frees abs_v_u) |
351 val crhs = list_cabs (map cterm args, abs_v_u) |
365 val crhs = list_cabs (map cterm args, cterm abs_v_u) |
352 (*Forms a lambda-abstraction over the formal parameters*) |
366 (*Forms a lambda-abstraction over the formal parameters*) |
353 val rhs = term_of crhs |
367 val rhs = term_of crhs |
354 val def = (*FIXME: can we also reuse the const-abstractions?*) |
368 val def = (*FIXME: can we also reuse the const-abstractions?*) |
355 case List.find (fn ax => rhs aconv rhs_of ax andalso |
369 case List.find (fn ax => rhs aconv rhs_of ax andalso |
356 Context.joinable (thy, theory_of_thm ax)) |
370 Context.joinable (thy, theory_of_thm ax)) |
357 (Net.match_term (!abstraction_cache) rhs) of |
371 (Net.match_term (!abstraction_cache) rhs) of |
358 SOME ax => ax |
372 SOME ax => ax |
359 | NONE => |
373 | NONE => |
360 let val Ts = map type_of args |
374 let val Ts = map type_of args |
361 val const_ty = Ts ---> (T --> typ_of (ctyp_of_term cu')) |
375 val const_ty = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu')) |
362 val c = Free (gensym "abs_", const_ty) |
376 val c = Free (gensym "abs_", const_ty) |
363 val ax = assume (Thm.capply (cterm (equals const_ty $ c)) crhs) |
377 val ax = assume (Thm.capply (cterm (equals const_ty $ c)) crhs) |
364 val _ = abstraction_cache := Net.insert_term eq_absdef (rhs,ax) |
378 val _ = abstraction_cache := Net.insert_term eq_absdef (rhs,ax) |
365 (!abstraction_cache) |
379 (!abstraction_cache) |
366 handle Net.INSERT => |
380 handle Net.INSERT => |
367 raise THM ("assume_absfuns: INSERT", 0, [th,u'_th,ax]) |
381 raise THM ("assume_absfuns: INSERT", 0, [th,u'_th,ax]) |
368 in ax end |
382 in ax end |
369 val _ = assert (rhs aconv rhs_of def) "assume_absfuns: rhs mismatch" |
383 val _ = assert (rhs aconv rhs_of def) "assume_absfuns: rhs mismatch" |
370 val def_args = list_combination def (map cterm args) |
384 val def_args = list_combination def (map cterm args) |
371 in (transitive (abstract_rule v cv u'_th) (symmetric def_args), |
385 in (transitive (abstract_rule_list vs cvs u'_th) (symmetric def_args), |
372 def :: defs) end |
386 def :: defs) end |
373 | (t1$t2) => |
387 | (t1$t2) => |
374 let val (ct1,ct2) = Thm.dest_comb ct |
388 let val (ct1,ct2) = Thm.dest_comb ct |
375 val (t1',defs1) = abstract ct1 |
389 val (t1',defs1) = abstract ct1 |
376 val (t2',defs2) = abstract ct2 |
390 val (t2',defs2) = abstract ct2 |
417 (*Converts an Isabelle theorem (intro, elim or simp format) into nnf.*) |
431 (*Converts an Isabelle theorem (intro, elim or simp format) into nnf.*) |
418 (*It now works for HOL too. *) |
432 (*It now works for HOL too. *) |
419 fun to_nnf th = |
433 fun to_nnf th = |
420 th |> transfer_to_Reconstruction |
434 th |> transfer_to_Reconstruction |
421 |> transform_elim |> zero_var_indexes |> Drule.freeze_thaw |> #1 |
435 |> transform_elim |> zero_var_indexes |> Drule.freeze_thaw |> #1 |
422 |> ObjectLogic.atomize_thm |> make_nnf; |
436 |> ObjectLogic.atomize_thm |> make_nnf |> strip_lambdas; |
423 |
437 |
424 (*The cache prevents repeated clausification of a theorem, |
438 (*The cache prevents repeated clausification of a theorem, |
425 and also repeated declaration of Skolem functions*) |
439 and also repeated declaration of Skolem functions*) |
426 (* FIXME better use Termtab!? No, we MUST use theory data!!*) |
440 (* FIXME better use Termtab!? No, we MUST use theory data!!*) |
427 val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table) |
441 val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table) |