src/HOL/Tools/Sledgehammer/metis_tactics.ML
changeset 38695 e85ce10cef1a
parent 38652 e063be321438
child 38748 69fea359d3f8
equal deleted inserted replaced
38693:a99fc8d1da80 38695:e85ce10cef1a
    19 
    19 
    20 structure Metis_Tactics : METIS_TACTICS =
    20 structure Metis_Tactics : METIS_TACTICS =
    21 struct
    21 struct
    22 
    22 
    23 open Metis_Clauses
    23 open Metis_Clauses
    24 
       
    25 exception METIS of string * string
       
    26 
    24 
    27 val trace = Unsynchronized.ref false;
    25 val trace = Unsynchronized.ref false;
    28 fun trace_msg msg = if !trace then tracing (msg ()) else ();
    26 fun trace_msg msg = if !trace then tracing (msg ()) else ();
    29 
    27 
    30 val (type_lits, type_lits_setup) = Attrib.config_bool "metis_type_lits" (K true);
    28 val (type_lits, type_lits_setup) = Attrib.config_bool "metis_type_lits" (K true);
    86       (CombConst ((c, _), _, tys), tms) =>
    84       (CombConst ((c, _), _, tys), tms) =>
    87         let val tyargs = map metis_term_from_combtyp tys
    85         let val tyargs = map metis_term_from_combtyp tys
    88             val args   = map hol_term_to_fol_FO tms
    86             val args   = map hol_term_to_fol_FO tms
    89         in Metis.Term.Fn (c, tyargs @ args) end
    87         in Metis.Term.Fn (c, tyargs @ args) end
    90     | (CombVar ((v, _), _), []) => Metis.Term.Var v
    88     | (CombVar ((v, _), _), []) => Metis.Term.Var v
    91     | _ => raise METIS ("hol_term_to_fol_FO", "non first-order combterm")
    89     | _ => raise Fail "non-first-order combterm"
    92 
    90 
    93 fun hol_term_to_fol_HO (CombConst ((a, _), _, tylist)) =
    91 fun hol_term_to_fol_HO (CombConst ((a, _), _, tylist)) =
    94       Metis.Term.Fn (fn_isa_to_met_sublevel a, map metis_term_from_combtyp tylist)
    92       Metis.Term.Fn (fn_isa_to_met_sublevel a, map metis_term_from_combtyp tylist)
    95   | hol_term_to_fol_HO (CombVar ((s, _), _)) = Metis.Term.Var s
    93   | hol_term_to_fol_HO (CombVar ((s, _), _)) = Metis.Term.Var s
    96   | hol_term_to_fol_HO (CombApp (tm1, tm2)) =
    94   | hol_term_to_fol_HO (CombApp (tm1, tm2)) =
   427         cat_lines ("subst_translations:" ::
   425         cat_lines ("subst_translations:" ::
   428           (substs' |> map (fn (x, y) =>
   426           (substs' |> map (fn (x, y) =>
   429             Syntax.string_of_term ctxt (term_of x) ^ " |-> " ^
   427             Syntax.string_of_term ctxt (term_of x) ^ " |-> " ^
   430             Syntax.string_of_term ctxt (term_of y)))));
   428             Syntax.string_of_term ctxt (term_of y)))));
   431   in cterm_instantiate substs' i_th end
   429   in cterm_instantiate substs' i_th end
   432   handle THM (msg, _, _) => raise METIS ("inst_inf", msg)
   430   handle THM (msg, _, _) =>
   433        | ERROR msg => raise METIS ("inst_inf", msg)
   431          error ("Cannot replay Metis proof in Isabelle:\n" ^ msg)
   434 
   432 
   435 (* INFERENCE RULE: RESOLVE *)
   433 (* INFERENCE RULE: RESOLVE *)
   436 
   434 
   437 (*Like RSN, but we rename apart only the type variables. Vars here typically have an index
   435 (*Like RSN, but we rename apart only the type variables. Vars here typically have an index
   438   of 1, and the use of RSN would increase this typically to 3. Instantiations of those Vars
   436   of 1, and the use of RSN would increase this typically to 3. Instantiations of those Vars
   444   in
   442   in
   445       case distinct Thm.eq_thm ths of
   443       case distinct Thm.eq_thm ths of
   446         [th] => th
   444         [th] => th
   447       | _ => raise THM ("resolve_inc_tyvars: unique result expected", i, [tha,thb])
   445       | _ => raise THM ("resolve_inc_tyvars: unique result expected", i, [tha,thb])
   448   end
   446   end
   449   handle TERM (msg, _) => raise METIS ("resolve_inc_tyvars", msg)
       
   450 
   447 
   451 fun resolve_inf ctxt mode skolems thpairs atm th1 th2 =
   448 fun resolve_inf ctxt mode skolems thpairs atm th1 th2 =
   452   let
   449   let
   453     val i_th1 = lookth thpairs th1 and i_th2 = lookth thpairs th2
   450     val i_th1 = lookth thpairs th1 and i_th2 = lookth thpairs th2
   454     val _ = trace_msg (fn () => "  isa th1 (pos): " ^ Display.string_of_thm ctxt i_th1)
   451     val _ = trace_msg (fn () => "  isa th1 (pos): " ^ Display.string_of_thm ctxt i_th1)
   499         | path_finder_FO tm (p::ps) =
   496         | path_finder_FO tm (p::ps) =
   500             let val (tm1,args) = strip_comb tm
   497             let val (tm1,args) = strip_comb tm
   501                 val adjustment = get_ty_arg_size thy tm1
   498                 val adjustment = get_ty_arg_size thy tm1
   502                 val p' = if adjustment > p then p else p-adjustment
   499                 val p' = if adjustment > p then p else p-adjustment
   503                 val tm_p = List.nth(args,p')
   500                 val tm_p = List.nth(args,p')
   504                   handle Subscript => raise METIS ("equality_inf", Int.toString p ^ " adj " ^
   501                   handle Subscript =>
   505                     Int.toString adjustment  ^ " term " ^  Syntax.string_of_term ctxt tm)
   502                          error ("Cannot replay Metis proof in Isabelle:\n" ^
       
   503                                 "equality_inf: " ^ Int.toString p ^ " adj " ^
       
   504                                 Int.toString adjustment ^ " term " ^
       
   505                                 Syntax.string_of_term ctxt tm)
   506                 val _ = trace_msg (fn () => "path_finder: " ^ Int.toString p ^
   506                 val _ = trace_msg (fn () => "path_finder: " ^ Int.toString p ^
   507                                       "  " ^ Syntax.string_of_term ctxt tm_p)
   507                                       "  " ^ Syntax.string_of_term ctxt tm_p)
   508                 val (r,t) = path_finder_FO tm_p ps
   508                 val (r,t) = path_finder_FO tm_p ps
   509             in
   509             in
   510                 (r, list_comb (tm1, replace_item_list t p' args))
   510                 (r, list_comb (tm1, replace_item_list t p' args))
   588                                               thpairs (fol_th, inf))
   588                                               thpairs (fol_th, inf))
   589     val _ = trace_msg (fn () => "ISABELLE THM: " ^ Display.string_of_thm ctxt th)
   589     val _ = trace_msg (fn () => "ISABELLE THM: " ^ Display.string_of_thm ctxt th)
   590     val _ = trace_msg (fn () => "=============================================")
   590     val _ = trace_msg (fn () => "=============================================")
   591     val n_metis_lits =
   591     val n_metis_lits =
   592       length (filter real_literal (Metis.LiteralSet.toList (Metis.Thm.clause fol_th)))
   592       length (filter real_literal (Metis.LiteralSet.toList (Metis.Thm.clause fol_th)))
   593     val _ =
   593     val _ = if nprems_of th = n_metis_lits then ()
   594       if nprems_of th = n_metis_lits then ()
   594             else error "Cannot replay Metis proof in Isabelle."
   595       else raise METIS ("translate", "Proof reconstruction has gone wrong.")
       
   596   in (fol_th, th) :: thpairs end
   595   in (fol_th, th) :: thpairs end
   597 
   596 
   598 (*Determining which axiom clauses are actually used*)
   597 (*Determining which axiom clauses are actually used*)
   599 fun used_axioms axioms (th, Metis.Proof.Axiom _) = SOME (lookth axioms th)
   598 fun used_axioms axioms (th, Metis.Proof.Axiom _) = SOME (lookth axioms th)
   600   | used_axioms _ _ = NONE;
   599   | used_axioms _ _ = NONE;
   803       (warning ("Metis: Proof state contains the universal sort {}"); Seq.empty)
   802       (warning ("Metis: Proof state contains the universal sort {}"); Seq.empty)
   804     else
   803     else
   805       Meson.MESON (maps neg_clausify)
   804       Meson.MESON (maps neg_clausify)
   806                   (fn cls => resolve_tac (FOL_SOLVE mode ctxt cls ths) 1)
   805                   (fn cls => resolve_tac (FOL_SOLVE mode ctxt cls ths) 1)
   807                   ctxt i st0
   806                   ctxt i st0
   808       handle ERROR msg => raise METIS ("generic_metis_tac", msg)
       
   809   end
   807   end
   810   handle METIS (loc, msg) =>
       
   811          if mode = HO then
       
   812            (warning ("Metis: Falling back on \"metisFT\".");
       
   813             generic_metis_tac FT ctxt ths i st0)
       
   814          else
       
   815            Seq.empty
       
   816 
   808 
   817 val metis_tac = generic_metis_tac HO
   809 val metis_tac = generic_metis_tac HO
   818 val metisF_tac = generic_metis_tac FO
   810 val metisF_tac = generic_metis_tac FO
   819 val metisFT_tac = generic_metis_tac FT
   811 val metisFT_tac = generic_metis_tac FT
   820 
   812