src/HOL/Tools/Metis/metis_translate.ML
changeset 43091 b0b30df60056
parent 43090 f6331d785128
child 43092 93ec303e1917
equal deleted inserted replaced
43090:f6331d785128 43091:b0b30df60056
    23   val num_type_args: theory -> string -> int
    23   val num_type_args: theory -> string -> int
    24   val new_skolem_var_name_from_const : string -> string
    24   val new_skolem_var_name_from_const : string -> string
    25   val reveal_old_skolem_terms : (string * term) list -> term -> term
    25   val reveal_old_skolem_terms : (string * term) list -> term -> term
    26   val string_of_mode : mode -> string
    26   val string_of_mode : mode -> string
    27   val prepare_metis_problem :
    27   val prepare_metis_problem :
    28     mode -> Proof.context -> thm list -> thm list list -> mode * metis_problem
    28     Proof.context -> mode -> thm list -> thm list list -> mode * metis_problem
    29 end
    29 end
    30 
    30 
    31 structure Metis_Translate : METIS_TRANSLATE =
    31 structure Metis_Translate : METIS_TRANSLATE =
    32 struct
    32 struct
    33 
    33 
   245   let fun add ((a,i),s) Ts = if i = ~1 then TFree(a,s) :: Ts else Ts in
   245   let fun add ((a,i),s) Ts = if i = ~1 then TFree(a,s) :: Ts else Ts in
   246     Vartab.fold add (#2 (Variable.constraints_of ctxt)) []
   246     Vartab.fold add (#2 (Variable.constraints_of ctxt)) []
   247     |> raw_type_literals_for_types
   247     |> raw_type_literals_for_types
   248   end;
   248   end;
   249 
   249 
   250 (*Insert non-logical axioms corresponding to all accumulated TFrees*)
       
   251 fun add_tfrees {axioms, tfrees, old_skolems} : metis_problem =
       
   252      {axioms = map (rpair TrueI o metis_of_tfree) (distinct (op =) tfrees) @
       
   253                axioms,
       
   254       tfrees = tfrees, old_skolems = old_skolems}
       
   255 
       
   256 (*transform isabelle type / arity clause to metis clause *)
       
   257 fun add_type_thm [] lmap = lmap
       
   258   | add_type_thm ((ith, mth) :: cls) {axioms, tfrees, old_skolems} =
       
   259       add_type_thm cls {axioms = (mth, ith) :: axioms, tfrees = tfrees,
       
   260                         old_skolems = old_skolems}
       
   261 
       
   262 fun const_in_metis c (pred, tm_list) =
   250 fun const_in_metis c (pred, tm_list) =
   263   let
   251   let
   264     fun in_mterm (Metis_Term.Var _) = false
   252     fun in_mterm (Metis_Term.Var _) = false
   265       | in_mterm (Metis_Term.Fn (nm, tm_list)) =
   253       | in_mterm (Metis_Term.Fn (nm, tm_list)) =
   266         c = nm orelse exists in_mterm tm_list
   254         c = nm orelse exists in_mterm tm_list
   277    Metis_Thm.axiom (Metis_LiteralSet.fromList
   265    Metis_Thm.axiom (Metis_LiteralSet.fromList
   278                         (map m_arity_cls (concl_lits :: prem_lits))));
   266                         (map m_arity_cls (concl_lits :: prem_lits))));
   279 
   267 
   280 (* CLASSREL CLAUSE *)
   268 (* CLASSREL CLAUSE *)
   281 fun m_class_rel_cls (subclass, _) (superclass, _) =
   269 fun m_class_rel_cls (subclass, _) (superclass, _) =
   282   [metis_lit false subclass [Metis_Term.Var "T"], metis_lit true superclass [Metis_Term.Var "T"]];
   270   [metis_lit false subclass [Metis_Term.Var "T"],
       
   271    metis_lit true superclass [Metis_Term.Var "T"]]
   283 fun class_rel_cls ({subclass, superclass, ...} : class_rel_clause) =
   272 fun class_rel_cls ({subclass, superclass, ...} : class_rel_clause) =
   284   (TrueI, Metis_Thm.axiom (Metis_LiteralSet.fromList (m_class_rel_cls subclass superclass)));
   273   (TrueI, m_class_rel_cls subclass superclass
       
   274           |> Metis_LiteralSet.fromList |> Metis_Thm.axiom)
   285 
   275 
   286 fun type_ext thy tms =
   276 fun type_ext thy tms =
   287   let val subs = tfree_classes_of_terms tms
   277   let
   288       val supers = tvar_classes_of_terms tms
   278     val subs = tfree_classes_of_terms tms
   289       val tycons = type_consts_of_terms thy tms
   279     val supers = tvar_classes_of_terms tms
   290       val (supers', arity_clauses) = make_arity_clauses thy tycons supers
   280     val tycons = type_consts_of_terms thy tms
   291       val class_rel_clauses = make_class_rel_clauses thy subs supers'
   281     val (supers', arity_clauses) = make_arity_clauses thy tycons supers
   292   in  map class_rel_cls class_rel_clauses @ map arity_cls arity_clauses
   282     val class_rel_clauses = make_class_rel_clauses thy subs supers'
   293   end;
   283   in map class_rel_cls class_rel_clauses @ map arity_cls arity_clauses end
   294 
   284 
   295 (* Function to generate metis clauses, including comb and type clauses *)
   285 (* Function to generate metis clauses, including comb and type clauses *)
   296 fun prepare_metis_problem New ctxt cls thss =
   286 fun prepare_metis_problem ctxt New conj_clauses fact_clausess =
   297     error "Not implemented yet"
   287     let
   298   | prepare_metis_problem mode ctxt cls thss =
   288       val x = 1
       
   289     in
       
   290       error "Not implemented yet"
       
   291     end
       
   292   | prepare_metis_problem ctxt mode conj_clauses fact_clausess =
   299     let
   293     let
   300       val thy = Proof_Context.theory_of ctxt
   294       val thy = Proof_Context.theory_of ctxt
   301       (* The modes FO and FT are sticky. HO can be downgraded to FO. *)
   295       (* The modes FO and FT are sticky. HO can be downgraded to FO. *)
   302       val mode =
   296       val mode =
   303         if mode = HO andalso
   297         if mode = HO andalso
   304            forall (forall (is_quasi_fol_clause thy)) (cls :: thss) then
   298            forall (forall (is_quasi_fol_clause thy))
       
   299                   (conj_clauses :: fact_clausess) then
   305           FO
   300           FO
   306         else
   301         else
   307           mode
   302           mode
   308       (*transform isabelle clause to metis clause *)
       
   309       fun add_thm is_conjecture (isa_ith, metis_ith)
   303       fun add_thm is_conjecture (isa_ith, metis_ith)
   310                   {axioms, tfrees, old_skolems} : metis_problem =
   304                   {axioms, tfrees, old_skolems} : metis_problem =
   311         let
   305         let
   312           val (mth, tfree_lits, old_skolems) =
   306           val (mth, tfree_lits, old_skolems) =
   313             hol_thm_to_fol is_conjecture ctxt mode (length axioms) old_skolems
   307             hol_thm_to_fol is_conjecture ctxt mode (length axioms) old_skolems
   314                            metis_ith
   308                            metis_ith
   315         in
   309         in
   316            {axioms = (mth, isa_ith) :: axioms,
   310           {axioms = (mth, isa_ith) :: axioms,
   317             tfrees = union (op =) tfree_lits tfrees, old_skolems = old_skolems}
   311            tfrees = union (op =) tfree_lits tfrees, old_skolems = old_skolems}
   318         end;
   312         end;
   319       val lmap = {axioms = [], tfrees = init_tfrees ctxt, old_skolems = []}
   313       fun add_type_thm (ith, mth) {axioms, tfrees, old_skolems} =
   320                  |> fold (add_thm true o `Meson.make_meta_clause) cls
   314         {axioms = (mth, ith) :: axioms, tfrees = tfrees,
   321                  |> add_tfrees
   315          old_skolems = old_skolems}
   322                  |> fold (fold (add_thm false o `Meson.make_meta_clause)) thss
   316       fun add_tfrees {axioms, tfrees, old_skolems} =
   323       val clause_lists = map (Metis_Thm.clause o #1) (#axioms lmap)
   317         {axioms =
       
   318            map (rpair TrueI o metis_of_tfree) (distinct (op =) tfrees) @ axioms,
       
   319          tfrees = tfrees, old_skolems = old_skolems}
       
   320       val problem =
       
   321         {axioms = [], tfrees = init_tfrees ctxt, old_skolems = []}
       
   322         |> fold (add_thm true o `Meson.make_meta_clause) conj_clauses
       
   323         |> add_tfrees
       
   324         |> fold (fold (add_thm false o `Meson.make_meta_clause)) fact_clausess
       
   325       val clause_lists = map (Metis_Thm.clause o #1) (#axioms problem)
   324       fun is_used c =
   326       fun is_used c =
   325         exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
   327         exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
   326       val lmap =
   328       val problem =
   327         if mode = FO then
   329         if mode = FO then
   328           lmap
   330           problem
   329         else
   331         else
   330           let
   332           let
   331             val fdefs = @{thms fFalse_def fTrue_def fNot_def fconj_def fdisj_def
   333             val fdefs = @{thms fFalse_def fTrue_def fNot_def fconj_def fdisj_def
   332                                fimplies_def fequal_def}
   334                                fimplies_def fequal_def}
   333             val prepare_helper =
   335             val prepare_helper =
   338               helper_table
   340               helper_table
   339               |> filter (is_used o prefix const_prefix o fst)
   341               |> filter (is_used o prefix const_prefix o fst)
   340               |> maps (fn (_, (needs_full_types, thms)) =>
   342               |> maps (fn (_, (needs_full_types, thms)) =>
   341                           if needs_full_types andalso mode <> FT then []
   343                           if needs_full_types andalso mode <> FT then []
   342                           else map prepare_helper thms)
   344                           else map prepare_helper thms)
   343           in lmap |> fold (add_thm false) helper_ths end
   345           in problem |> fold (add_thm false) helper_ths end
   344     in
   346       val type_ths =
   345       (mode,
   347         type_ext thy (maps (map prop_of) (conj_clauses :: fact_clausess))
   346        add_type_thm (type_ext thy (maps (map prop_of) (cls :: thss))) lmap)
   348     in (mode, fold add_type_thm type_ths problem) end
   347     end
       
   348 
   349 
   349 end;
   350 end;