src/HOL/TPTP/TPTP_Parser/tptp_interpret.ML
changeset 64560 c48becd96398
parent 62552 53603d1aad5f
child 65999 ee4cf96a9406
equal deleted inserted replaced
64559:abd9a9fd030b 64560:c48becd96398
   213   Sign.declared_const thy (full_name thy config const_name)
   213   Sign.declared_const thy (full_name thy config const_name)
   214 
   214 
   215 fun declare_constant config const_name ty thy =
   215 fun declare_constant config const_name ty thy =
   216   if const_exists config thy const_name then
   216   if const_exists config thy const_name then
   217     raise MISINTERPRET_TERM
   217     raise MISINTERPRET_TERM
   218      ("Const already declared", Term_Func (Uninterpreted const_name, []))
   218      ("Const already declared", Term_FuncG (Uninterpreted const_name, [], []))
   219   else
   219   else
   220     Theory.specify_const ((mk_binding config const_name, ty), NoSyn) thy
   220     Theory.specify_const ((mk_binding config const_name, ty), NoSyn) thy
   221 
   221 
   222 
   222 
   223 (** Interpretation functions **)
   223 (** Interpretation functions **)
   224 
   224 
   225 (*Types in TFF/THF are encoded as formulas. These functions translate them to type form.*)
   225 (*Types in TFF/THF are encoded as formulas. These functions translate them to type form.*)
   226 
   226 
   227 fun termtype_to_type (Term_Func (TypeSymbol typ, [])) =
   227 fun termtype_to_type (Term_FuncG (TypeSymbol typ, [], [])) =
   228       Defined_type typ
   228       Defined_type typ
   229   | termtype_to_type (Term_Func (Uninterpreted str, tms)) =
   229   | termtype_to_type (Term_FuncG (Uninterpreted str, tys, tms)) =
   230       Atom_type (str, map termtype_to_type tms)
   230       Atom_type (str, tys @ map termtype_to_type tms)
   231   | termtype_to_type (Term_Var str) = Var_type str
   231   | termtype_to_type (Term_Var str) = Var_type str
   232 
   232 
   233 (*FIXME possibly incomplete hack*)
   233 (*FIXME possibly incomplete hack*)
   234 fun fmlatype_to_type (Atom (THF_Atom_term tm)) = termtype_to_type tm
   234 fun fmlatype_to_type (Atom (THF_Atom_term tm)) = termtype_to_type tm
   235   | fmlatype_to_type (Type_fmla (Fn_type (ty1, ty2))) =
   235   | fmlatype_to_type (Type_fmla (Fn_type (ty1, ty2))) =
   247   | fmlatype_to_type (Fmla (Uninterpreted str, fmla)) =
   247   | fmlatype_to_type (Fmla (Uninterpreted str, fmla)) =
   248       Atom_type (str, map fmlatype_to_type fmla)
   248       Atom_type (str, map fmlatype_to_type fmla)
   249   | fmlatype_to_type (Quant (Dep_Prod, _, fmla)) = fmlatype_to_type fmla
   249   | fmlatype_to_type (Quant (Dep_Prod, _, fmla)) = fmlatype_to_type fmla
   250   | fmlatype_to_type (Pred (Interpreted_ExtraLogic Apply, [tm])) =
   250   | fmlatype_to_type (Pred (Interpreted_ExtraLogic Apply, [tm])) =
   251       termtype_to_type tm
   251       termtype_to_type tm
       
   252   | fmlatype_to_type (Fmla (Interpreted_ExtraLogic Apply, [tm1, tm2])) =
       
   253       (case fmlatype_to_type tm1 of
       
   254         Atom_type (str, tys) => Atom_type (str, tys @ [fmlatype_to_type tm2]))
   252 
   255 
   253 fun tfree_name_of_var_type str = "'" ^ Name.desymbolize (SOME false) str
   256 fun tfree_name_of_var_type str = "'" ^ Name.desymbolize (SOME false) str
   254 fun tfree_of_var_type str = TFree (tfree_name_of_var_type str, @{sort type})
   257 fun tfree_of_var_type str = TFree (tfree_name_of_var_type str, @{sort type})
   255 
   258 
   256 fun interpret_type config thy type_map thf_ty =
   259 fun interpret_type config thy type_map thf_ty =
   321       if const_exists config thy str then
   324       if const_exists config thy str then
   322         Sign.mk_const thy ((Sign.full_name thy (mk_binding config str)), [])
   325         Sign.mk_const thy ((Sign.full_name thy (mk_binding config str)), [])
   323       else
   326       else
   324         raise MISINTERPRET_TERM
   327         raise MISINTERPRET_TERM
   325             ("Could not find the interpretation of this constant in the \
   328             ("Could not find the interpretation of this constant in the \
   326               \mapping to Isabelle/HOL", Term_Func (Uninterpreted str, [])))
   329               \mapping to Isabelle/HOL", Term_FuncG (Uninterpreted str, [], [])))
   327 
   330 
   328 (*Eta-expands n-ary function.
   331 (*Eta-expands n-ary function.
   329  "str" is the name of an Isabelle/HOL constant*)
   332  "str" is the name of an Isabelle/HOL constant*)
   330 fun mk_n_fun n str =
   333 fun mk_n_fun n str =
   331   let
   334   let
   446     val value_type =
   449     val value_type =
   447       if formula_level then
   450       if formula_level then
   448         interpret_type config thy type_map (Defined_type Type_Bool)
   451         interpret_type config thy type_map (Defined_type Type_Bool)
   449       else ind_type
   452       else ind_type
   450   in case tptp_atom_value of
   453   in case tptp_atom_value of
   451       Atom_App (Term_Func (symb, tptp_ts)) =>
   454       Atom_App (Term_FuncG (symb, tptp_tys, tptp_ts)) =>
   452         let
   455         let
   453           val thy' = fold (fn t =>
   456           val thy' = fold (fn t =>
   454             type_atoms_to_thy config false type_map (Atom_App t)) tptp_ts thy
   457             type_atoms_to_thy config false type_map (Atom_App t)) tptp_ts thy
   455         in
   458         in
   456           case symb of
   459           case symb of
   457              Uninterpreted const_name =>
   460              Uninterpreted const_name =>
   458                perhaps (try (snd o declare_constant config const_name
   461                perhaps (try (snd o declare_constant config const_name
   459                 (mk_fun_type (replicate (length tptp_ts) ind_type) value_type I))) thy'
   462                 (mk_fun_type (replicate (length tptp_tys + length tptp_ts) ind_type) value_type I)))
       
   463                 thy'
   460            | _ => thy'
   464            | _ => thy'
   461         end
   465         end
   462     | Atom_App _ => thy
   466     | Atom_App _ => thy
   463     | Atom_Arity (const_name, n_args) =>
   467     | Atom_Arity (const_name, n_args) =>
   464         perhaps (try (snd o declare_constant config const_name
   468         perhaps (try (snd o declare_constant config const_name
   497            symb_type const_name |> dom_type |> is_prod
   501            symb_type const_name |> dom_type |> is_prod
   498          else false
   502          else false
   499      | _ => false
   503      | _ => false
   500    end
   504    end
   501 
   505 
       
   506 fun strip_applies_term (Term_FuncG (Interpreted_ExtraLogic Apply, [], [tm1, tm2])) =
       
   507     strip_applies_term tm1 ||> (fn tms => tms @ [tm2])
       
   508   | strip_applies_term tm = (tm, [])
       
   509 
       
   510 fun termify_apply_fmla thy config (Fmla (Interpreted_ExtraLogic Apply, [fmla1, fmla2])) =
       
   511     (case termify_apply_fmla thy config fmla1 of
       
   512       SOME (Term_FuncG (symb, tys, tms)) =>
       
   513       let val typ_arity = type_arity_of_symbol thy config symb in
       
   514         (case (null tms andalso length tys < typ_arity, try fmlatype_to_type fmla2) of
       
   515           (true, SOME ty) => SOME (Term_FuncG (symb, tys @ [ty], []))
       
   516         | _ =>
       
   517           (case termify_apply_fmla thy config fmla2 of
       
   518             SOME tm2 => SOME (Term_FuncG (symb, tys, tms @ [tm2]))
       
   519           | NONE => NONE))
       
   520       end
       
   521     | _ => NONE)
       
   522   | termify_apply_fmla _ _ (Atom (THF_Atom_term tm)) = SOME tm
       
   523   | termify_apply_fmla _ _ _ = NONE
       
   524 
   502 (*
   525 (*
   503  Information needed to be carried around:
   526  Information needed to be carried around:
   504  - constant mapping: maps constant names to Isabelle terms with fully-qualified
   527  - constant mapping: maps constant names to Isabelle terms with fully-qualified
   505     names. This is fixed, and it is determined by declaration lines earlier
   528     names. This is fixed, and it is determined by declaration lines earlier
   506     in the script (i.e. constants must be declared before appearing in terms.
   529     in the script (i.e. constants must be declared before appearing in terms.
   507  - type context: maps bound variables to their Isabelle type. This is discarded
   530  - type context: maps bound variables to their Isabelle type. This is discarded
   508     after each call of interpret_term since variables' cannot be bound across
   531     after each call of interpret_term since variables' cannot be bound across
   509     terms.
   532     terms.
   510 *)
   533 *)
   511 fun interpret_term formula_level config language const_map var_types type_map
   534 fun interpret_term formula_level config language const_map var_types type_map tptp_t thy =
   512  tptp_t thy =
       
   513   case tptp_t of
   535   case tptp_t of
   514     Term_Func (symb, tptp_ts) =>
   536     Term_FuncG (symb, tptp_tys, tptp_ts) =>
   515        let
   537        let
   516          val thy' =
   538          val thy' =
   517            type_atoms_to_thy config formula_level type_map (Atom_App tptp_t) thy
   539            type_atoms_to_thy config formula_level type_map (Atom_App tptp_t) thy
   518        in
   540        in
   519          case symb of
   541          case symb of
   520            Interpreted_ExtraLogic Apply =>
   542            Interpreted_ExtraLogic Apply =>
       
   543            (case strip_applies_term tptp_t of
       
   544              (Term_FuncG (symb, tptp_tys, tptp_ts), extra_tptp_ts) =>
       
   545              interpret_term formula_level config language const_map var_types type_map
       
   546                (Term_FuncG (symb, tptp_tys, tptp_ts @ extra_tptp_ts)) thy
       
   547            | _ =>
   521              (*apply the head of the argument list to the tail*)
   548              (*apply the head of the argument list to the tail*)
   522              (mapply'
   549              (mapply'
   523                (fold_map (interpret_term false config language const_map
   550                (fold_map (interpret_term false config language const_map
   524                 var_types type_map) (tl tptp_ts) thy')
   551                 var_types type_map) (tl tptp_ts) thy')
   525                (interpret_term formula_level config language const_map
   552                (interpret_term formula_level config language const_map
   526                 var_types type_map (hd tptp_ts)))
   553                 var_types type_map (hd tptp_ts))))
   527            | _ =>
   554          | _ =>
   528               let
   555               let
   529                 val typ_arity = type_arity_of_symbol thy' config symb
   556                 val typ_arity = type_arity_of_symbol thy' config symb
   530                 val (tptp_type_args, tptp_term_args) = chop typ_arity tptp_ts
   557                 val (tptp_type_args, tptp_term_args) = chop (typ_arity - length tptp_tys) tptp_ts
   531                 val tyargs =
   558                 val tyargs =
   532                   map (interpret_type config thy' type_map o termtype_to_type)
   559                   map (interpret_type config thy' type_map)
   533                     tptp_type_args
   560                     (tptp_tys @ map termtype_to_type tptp_type_args)
   534               in
   561               in
   535                 (*apply symb to tptp_ts*)
   562                 (*apply symb to tptp_ts*)
   536                 if is_prod_typed thy' config symb then
   563                 if is_prod_typed thy' config symb then
   537                   let
   564                   let
   538                     val (t, thy'') =
   565                     val (t, thy'') =
   602       declare_constant config ("do_" ^ str)
   629       declare_constant config ("do_" ^ str)
   603         (interpret_type config thy type_map (Defined_type Type_Ind)) thy
   630         (interpret_type config thy type_map (Defined_type Type_Ind)) thy
   604 
   631 
   605 and interpret_formula config language const_map var_types type_map tptp_fmla thy =
   632 and interpret_formula config language const_map var_types type_map tptp_fmla thy =
   606   case tptp_fmla of
   633   case tptp_fmla of
   607       Pred app =>
   634       Pred (symb, ts) =>
   608         interpret_term true config language const_map
   635         interpret_term true config language const_map
   609          var_types type_map (Term_Func app) thy
   636          var_types type_map (Term_FuncG (symb, [], ts)) thy
   610     | Fmla (symbol, tptp_formulas) =>
   637     | Fmla (symbol, tptp_formulas) =>
   611        (case symbol of
   638        (case symbol of
   612           Interpreted_ExtraLogic Apply =>
   639           Interpreted_ExtraLogic Apply =>
       
   640           (case termify_apply_fmla thy config tptp_fmla of
       
   641             SOME tptp_t =>
       
   642             interpret_term true config language const_map var_types type_map tptp_t thy
       
   643           | NONE =>
   613             mapply'
   644             mapply'
   614               (fold_map (interpret_formula config language const_map
   645               (fold_map (interpret_formula config language const_map
   615                var_types type_map) (tl tptp_formulas) thy)
   646                var_types type_map) (tl tptp_formulas) thy)
   616               (interpret_formula config language const_map
   647               (interpret_formula config language const_map
   617                var_types type_map (hd tptp_formulas))
   648                var_types type_map (hd tptp_formulas)))
   618         | Uninterpreted const_name =>
   649         | Uninterpreted const_name =>
   619             let
   650             let
   620               val (args, thy') = (fold_map (interpret_formula config language
   651               val (args, thy') = (fold_map (interpret_formula config language
   621                const_map var_types type_map) tptp_formulas thy)
   652                 const_map var_types type_map) tptp_formulas thy)
   622               val thy' =
   653               val thy' =
   623                 type_atoms_to_thy config true type_map
   654                 type_atoms_to_thy config true type_map
   624                  (Atom_Arity (const_name, length tptp_formulas)) thy'
   655                   (Atom_Arity (const_name, length tptp_formulas)) thy'
   625             in
   656             in
   626               (if is_prod_typed thy' config symbol then
   657               (if is_prod_typed thy' config symbol then
   627                  mtimes thy' args (interpret_symbol config const_map symbol [] thy')
   658                  mtimes thy' args (interpret_symbol config const_map symbol [] thy')
   628                else
   659                else
   629                 mapply args (interpret_symbol config const_map symbol [] thy'),
   660                  mapply args (interpret_symbol config const_map symbol [] thy'),
   630               thy')
   661               thy')
   631             end
   662             end
   632         | _ =>
   663         | _ =>
   633             let
   664             let
   634               val (args, thy') =
   665               val (args, thy') =
   635                 fold_map
   666                 fold_map (interpret_formula config language const_map var_types type_map)
   636                  (interpret_formula config language
   667                   tptp_formulas thy
   637                   const_map var_types type_map)
       
   638                  tptp_formulas thy
       
   639             in
   668             in
   640               (if is_prod_typed thy' config symbol then
   669               (if is_prod_typed thy' config symbol then
   641                  mtimes thy' args (interpret_symbol config const_map symbol [] thy')
   670                  mtimes thy' args (interpret_symbol config const_map symbol [] thy')
   642                else
   671                else
   643                  mapply args (interpret_symbol config const_map symbol [] thy'),
   672                  mapply args (interpret_symbol config const_map symbol [] thy'),
   735     case tptp_type of
   764     case tptp_type of
   736         Fmla_type fmla => (type_vars_of_fmlatype fmla, fmlatype_to_type fmla)
   765         Fmla_type fmla => (type_vars_of_fmlatype fmla, fmlatype_to_type fmla)
   737       | _ => ([], tptp_type)
   766       | _ => ([], tptp_type)
   738 in
   767 in
   739   fun typeof_typing (THF_typing (_, tptp_type)) = extract_type tptp_type
   768   fun typeof_typing (THF_typing (_, tptp_type)) = extract_type tptp_type
   740   fun typeof_tff_typing (Atom (TFF_Typed_Atom (_, SOME tptp_type))) =
   769     | typeof_typing (Atom (TFF_Typed_Atom (_, SOME tptp_type))) = extract_type tptp_type
   741     extract_type tptp_type
       
   742 end
   770 end
   743 
   771 
   744 fun nameof_typing
   772 fun nameof_typing (THF_typing
   745   (THF_typing
   773      ((Atom (THF_Atom_term (Term_FuncG (Uninterpreted str, [], [])))), _)) = str
   746      ((Atom (THF_Atom_term (Term_Func (Uninterpreted str, [])))), _)) = str
   774   | nameof_typing (Atom (TFF_Typed_Atom (Uninterpreted str, _))) = str
   747 fun nameof_tff_typing (Atom (TFF_Typed_Atom (Uninterpreted str, _))) = str
       
   748 
   775 
   749 fun strip_prod_type (Prod_type (ty1, ty2)) = ty1 :: strip_prod_type ty2
   776 fun strip_prod_type (Prod_type (ty1, ty2)) = ty1 :: strip_prod_type ty2
   750   | strip_prod_type ty = [ty]
   777   | strip_prod_type ty = [ty]
   751 
   778 
   752 fun dest_fn_type (Fn_type (ty1, ty2)) = (strip_prod_type ty1, ty2)
   779 fun dest_fn_type (Fn_type (ty1, ty2)) =
       
   780     let val (tys2, ty3) = dest_fn_type ty2 in
       
   781       (strip_prod_type ty1 @ tys2, ty3)
       
   782     end
   753   | dest_fn_type ty = ([], ty)
   783   | dest_fn_type ty = ([], ty)
   754 
   784 
   755 fun resolve_include_path path_prefixes path_suffix =
   785 fun resolve_include_path path_prefixes path_suffix =
   756   case find_first (fn prefix => File.exists (Path.append prefix path_suffix))
   786   case find_first (fn prefix => File.exists (Path.append prefix path_suffix))
   757          path_prefixes of
   787          path_prefixes of
   758     SOME prefix => Path.append prefix path_suffix
   788     SOME prefix => Path.append prefix path_suffix
   759   | NONE => error ("Cannot find include file " ^ Path.print path_suffix)
   789   | NONE => error ("Cannot find include file " ^ Path.print path_suffix)
   760 
   790 
   761 (* Ideally, to be in sync with TFF1, we should perform full type skolemization here.
   791 fun is_type_type (Fmla_type (Atom (THF_Atom_term (Term_FuncG (TypeSymbol Type_Type, [], []))))) =
   762    But the problems originating from HOL systems are restricted to top-level
   792     true
   763    universal quantification for types. *)
   793   | is_type_type (Defined_type Type_Type) = true
       
   794   | is_type_type _ = false;
       
   795 
       
   796 (* Ideally, to be in sync with TFF1/THF1, we should perform full type
       
   797    skolemization here. But the problems originating from HOL systems are
       
   798    restricted to top-level universal quantification for types. *)
   764 fun remove_leading_type_quantifiers (Quant (Forall, varlist, tptp_fmla)) =
   799 fun remove_leading_type_quantifiers (Quant (Forall, varlist, tptp_fmla)) =
   765     (case filter_out (curry (op =) (SOME (Defined_type Type_Type)) o snd) varlist of
   800     (case filter_out (fn (_, SOME ty) => is_type_type ty | _ => false) varlist of
   766       [] => remove_leading_type_quantifiers tptp_fmla
   801       [] => remove_leading_type_quantifiers tptp_fmla
   767     | varlist' => Quant (Forall, varlist', tptp_fmla))
   802     | varlist' => Quant (Forall, varlist', tptp_fmla))
   768   | remove_leading_type_quantifiers tptp_fmla = tptp_fmla
   803   | remove_leading_type_quantifiers tptp_fmla = tptp_fmla
   769 
   804 
   770 fun interpret_line config inc_list type_map const_map path_prefixes line thy =
   805 fun interpret_line config inc_list type_map const_map path_prefixes line thy =
   786        if null inc_list orelse is_some (List.find (fn x => x = label) inc_list) then
   821        if null inc_list orelse is_some (List.find (fn x => x = label) inc_list) then
   787          case role of
   822          case role of
   788            Role_Type =>
   823            Role_Type =>
   789              let
   824              let
   790                val ((tptp_type_vars, tptp_ty), name) =
   825                val ((tptp_type_vars, tptp_ty), name) =
   791                  if lang = THF then
   826                  (typeof_typing tptp_formula (*assuming tptp_formula is a typing*),
   792                    (typeof_typing tptp_formula (*assuming tptp_formula is a typing*),
   827                   nameof_typing tptp_formula (*and that the LHS is a (atom) name*))
   793                     nameof_typing tptp_formula (*and that the LHS is a (atom) name*))
       
   794                  else
       
   795                    (typeof_tff_typing tptp_formula,
       
   796                     nameof_tff_typing tptp_formula)
       
   797              in
   828              in
   798                case dest_fn_type tptp_ty of
   829                case dest_fn_type tptp_ty of
   799                  (tptp_binders, Defined_type Type_Type) => (*add new type*)
   830                  (tptp_binders, Defined_type Type_Type) => (*add new type*)
   800                    (*generate an Isabelle/HOL type to interpret this TPTP type,
   831                    (*generate an Isabelle/HOL type to interpret this TPTP type,
   801                    and add it to both the Isabelle/HOL theory and to the type_map*)
   832                    and add it to both the Isabelle/HOL theory and to the type_map*)
   863          | _ => (*i.e. the AF is not a type declaration*)
   894          | _ => (*i.e. the AF is not a type declaration*)
   864              let
   895              let
   865                (*gather interpreted term, and the map of types occurring in that term*)
   896                (*gather interpreted term, and the map of types occurring in that term*)
   866                val (t, thy') =
   897                val (t, thy') =
   867                  interpret_formula config lang
   898                  interpret_formula config lang
   868                   const_map [] type_map (remove_leading_type_quantifiers tptp_formula) thy
   899                    const_map [] type_map (remove_leading_type_quantifiers tptp_formula) thy
   869                  |>> HOLogic.mk_Trueprop
   900                  |>> HOLogic.mk_Trueprop
   870                (*type_maps grow monotonically, so return the newest value (type_mapped);
   901                (*type_maps grow monotonically, so return the newest value (type_mapped);
   871                there's no need to unify it with the type_map parameter.*)
   902                there's no need to unify it with the type_map parameter.*)
   872              in
   903              in
   873               ((type_map, const_map,
   904               ((type_map, const_map,