src/HOL/Tools/res_axioms.ML
changeset 20969 341808e0b7f2
parent 20902 a0034e545c13
child 20996 197e6875d637
equal deleted inserted replaced
20968:5294baa98468 20969:341808e0b7f2
   283   | abstract_rule_list _ _ th = raise THM ("abstract_rule_list", 0, [th]);
   283   | abstract_rule_list _ _ th = raise THM ("abstract_rule_list", 0, [th]);
   284 
   284 
   285 
   285 
   286 val Envir.Envir {asol = tenv0, iTs = tyenv0, ...} = Envir.empty 0
   286 val Envir.Envir {asol = tenv0, iTs = tyenv0, ...} = Envir.empty 0
   287 
   287 
   288 (*Does an existing abstraction definition have an RHS that matches the one we need now?*)
   288 (*Does an existing abstraction definition have an RHS that matches the one we need now?
   289 fun match_rhs t th =
   289   thy is the current theory, which must extend that of theorem th.*)
   290   let val thy = theory_of_thm th
   290 fun match_rhs thy t th =
   291       val _ = if !trace_abs then warning ("match_rhs: " ^ string_of_cterm (cterm_of thy t) ^ 
   291   let val _ = if !trace_abs then warning ("match_rhs: " ^ string_of_cterm (cterm_of thy t) ^ 
   292                                           " against\n" ^ string_of_thm th) else ();
   292                                           " against\n" ^ string_of_thm th) else ();
   293       val (tyenv,tenv) = Pattern.first_order_match thy (rhs_of th, t) (tyenv0,tenv0)
   293       val (tyenv,tenv) = Pattern.first_order_match thy (rhs_of th, t) (tyenv0,tenv0)
   294       val term_insts = map Meson.term_pair_of (Vartab.dest tenv)
   294       val term_insts = map Meson.term_pair_of (Vartab.dest tenv)
   295       val ct_pairs = if forall lambda_free (map #2 term_insts) then
   295       val ct_pairs = if subthy (theory_of_thm th, thy) andalso 
   296                          map (pairself (cterm_of thy)) term_insts
   296                         forall lambda_free (map #2 term_insts) 
       
   297                      then map (pairself (cterm_of thy)) term_insts
   297                      else raise Pattern.MATCH (*Cannot allow lambdas in the instantiation*)
   298                      else raise Pattern.MATCH (*Cannot allow lambdas in the instantiation*)
   298       fun ctyp2 (ixn, (S, T)) = (ctyp_of thy (TVar (ixn, S)), ctyp_of thy T)
   299       fun ctyp2 (ixn, (S, T)) = (ctyp_of thy (TVar (ixn, S)), ctyp_of thy T)
   299       val th' = cterm_instantiate ct_pairs th
   300       val th' = cterm_instantiate ct_pairs th
   300   in  SOME (th, instantiate (map ctyp2 (Vartab.dest tyenv), []) th')  end
   301   in  SOME (th, instantiate (map ctyp2 (Vartab.dest tyenv), []) th')  end
   301   handle _ => NONE;
   302   handle _ => NONE;
   322                 val args = term_frees abs_v_u
   323                 val args = term_frees abs_v_u
   323                 val _ = if !trace_abs then warning (Int.toString (length args) ^ " arguments") else ();
   324                 val _ = if !trace_abs then warning (Int.toString (length args) ^ " arguments") else ();
   324                 val rhs = list_abs_free (map dest_Free args, abs_v_u)
   325                 val rhs = list_abs_free (map dest_Free args, abs_v_u)
   325                       (*Forms a lambda-abstraction over the formal parameters*)
   326                       (*Forms a lambda-abstraction over the formal parameters*)
   326                 val _ = if !trace_abs then warning ("Looking up " ^ string_of_cterm cu') else ();
   327                 val _ = if !trace_abs then warning ("Looking up " ^ string_of_cterm cu') else ();
       
   328                 val thy = theory_of_thm u'_th
   327                 val (ax,ax',thy) =
   329                 val (ax,ax',thy) =
   328                  case List.mapPartial (match_rhs abs_v_u) (Net.match_term (!abstraction_cache) u')
   330                  case List.mapPartial (match_rhs thy abs_v_u) 
   329                         of
   331                          (Net.match_term (!abstraction_cache) u') of
   330                      (ax,ax')::_ => 
   332                      (ax,ax')::_ => 
   331                        (if !trace_abs then warning ("Re-using axiom " ^ string_of_thm ax) else ();
   333                        (if !trace_abs then warning ("Re-using axiom " ^ string_of_thm ax) else ();
   332                         (ax,ax',thy))
   334                         (ax,ax',thy))
   333                    | [] =>
   335                    | [] =>
   334                       let val _ = if !trace_abs then warning "Lookup was empty" else ();
   336                       let val _ = if !trace_abs then warning "Lookup was empty" else ();
   335                           val Ts = map type_of args
   337                           val Ts = map type_of args
   336                           val cT = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
   338                           val cT = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
   337                           val thy = theory_of_thm u'_th
       
   338                           val c = Const (Sign.full_name thy cname, cT)
   339                           val c = Const (Sign.full_name thy cname, cT)
   339                           val thy = Sign.add_consts_authentic [(cname, cT, NoSyn)] thy
   340                           val thy = Sign.add_consts_authentic [(cname, cT, NoSyn)] thy
   340                                      (*Theory is augmented with the constant,
   341                                      (*Theory is augmented with the constant,
   341                                        then its definition*)
   342                                        then its definition*)
   342                           val cdef = cname ^ "_def"
   343                           val cdef = cname ^ "_def"
   346                                                       string_of_thm (get_axiom thy cdef))) 
   347                                                       string_of_thm (get_axiom thy cdef))) 
   347                                   else ();
   348                                   else ();
   348                           val ax = get_axiom thy cdef |> freeze_thm
   349                           val ax = get_axiom thy cdef |> freeze_thm
   349                                      |> mk_object_eq |> strip_lambdas (length args)
   350                                      |> mk_object_eq |> strip_lambdas (length args)
   350                                      |> mk_meta_eq |> Meson.generalize
   351                                      |> mk_meta_eq |> Meson.generalize
   351                           val (_,ax') = Option.valOf (match_rhs abs_v_u ax)
   352                           val (_,ax') = Option.valOf (match_rhs thy abs_v_u ax)
   352                           val _ = if !trace_abs then 
   353                           val _ = if !trace_abs then 
   353                                     (warning ("Declaring: " ^ string_of_thm ax);
   354                                     (warning ("Declaring: " ^ string_of_thm ax);
   354                                      warning ("Instance: " ^ string_of_thm ax')) 
   355                                      warning ("Instance: " ^ string_of_thm ax')) 
   355                                   else ();
   356                                   else ();
   356                           val _ = abstraction_cache := Net.insert_term eq_absdef 
   357                           val _ = abstraction_cache := Net.insert_term eq_absdef 
   398                 val args = filter (valid_name defs) (term_frees abs_v_u)
   399                 val args = filter (valid_name defs) (term_frees abs_v_u)
   399                 val crhs = list_cabs (map cterm args, cterm abs_v_u)
   400                 val crhs = list_cabs (map cterm args, cterm abs_v_u)
   400                       (*Forms a lambda-abstraction over the formal parameters*)
   401                       (*Forms a lambda-abstraction over the formal parameters*)
   401                 val rhs = term_of crhs
   402                 val rhs = term_of crhs
   402                 val (ax,ax') =
   403                 val (ax,ax') =
   403                  case List.mapPartial (match_rhs abs_v_u) 
   404                  case List.mapPartial (match_rhs thy abs_v_u) 
   404                         (Net.match_term (!abstraction_cache) u') of
   405                         (Net.match_term (!abstraction_cache) u') of
   405                      (ax,ax')::_ => 
   406                      (ax,ax')::_ => 
   406                        (if !trace_abs then warning ("Re-using axiom " ^ string_of_thm ax) else ();
   407                        (if !trace_abs then warning ("Re-using axiom " ^ string_of_thm ax) else ();
   407                         (ax,ax'))
   408                         (ax,ax'))
   408                    | [] =>
   409                    | [] =>
   410                           val const_ty = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
   411                           val const_ty = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
   411                           val c = Free (gensym "abs_", const_ty)
   412                           val c = Free (gensym "abs_", const_ty)
   412                           val ax = assume (Thm.capply (cterm (equals const_ty $ c)) crhs)
   413                           val ax = assume (Thm.capply (cterm (equals const_ty $ c)) crhs)
   413                                      |> mk_object_eq |> strip_lambdas (length args)
   414                                      |> mk_object_eq |> strip_lambdas (length args)
   414                                      |> mk_meta_eq |> Meson.generalize
   415                                      |> mk_meta_eq |> Meson.generalize
   415                           val (_,ax') = Option.valOf (match_rhs abs_v_u ax)
   416                           val (_,ax') = Option.valOf (match_rhs thy abs_v_u ax)
   416                           val _ = abstraction_cache := Net.insert_term eq_absdef (rhs,ax)
   417                           val _ = abstraction_cache := Net.insert_term eq_absdef (rhs,ax)
   417                                     (!abstraction_cache)
   418                                     (!abstraction_cache)
   418                             handle Net.INSERT =>
   419                             handle Net.INSERT =>
   419                               raise THM ("assume_absfuns: INSERT", 0, [th,u'_th,ax])
   420                               raise THM ("assume_absfuns: INSERT", 0, [th,u'_th,ax])
   420                       in (ax,ax') end
   421                       in (ax,ax') end
   640 (*Setup function: takes a theory and installs ALL known theorems into the clause cache*)
   641 (*Setup function: takes a theory and installs ALL known theorems into the clause cache*)
   641 
   642 
   642 fun skolem_cache (name,th) thy =
   643 fun skolem_cache (name,th) thy =
   643   let val prop = Thm.prop_of th
   644   let val prop = Thm.prop_of th
   644   in
   645   in
   645       if lambda_free prop 
   646       if lambda_free prop orelse (not abstract_lambdas andalso monomorphic prop)
   646          (*orelse monomorphic prop? Monomorphic theorems can be Skolemized on demand,
   647          (*Monomorphic theorems can be Skolemized on demand,
   647            but there are problems with re-use of abstraction functions if we don't
   648            but there are problems with re-use of abstraction functions if we don't
   648            do them now, even for monomorphic theorems.*)
   649            do them now, even for monomorphic theorems.*)
   649       then thy  
   650       then thy  
   650       else #2 (skolem_cache_thm (name,th) thy)
   651       else #2 (skolem_cache_thm (name,th) thy)
   651   end;
   652   end;