660 val renaming = fold mk_renaming vars [] |
660 val renaming = fold mk_renaming vars [] |
661 in ((rel, map (rename_vars_term renaming) args), rename_vars_prem renaming prem) end |
661 in ((rel, map (rename_vars_term renaming) args), rename_vars_prem renaming prem) end |
662 |
662 |
663 val rename_vars_program = map rename_vars_clause |
663 val rename_vars_program = map rename_vars_clause |
664 |
664 |
|
665 (* post processing of generated prolog program *) |
|
666 |
|
667 fun post_process ctxt options (p, constant_table) = |
|
668 (p, constant_table) |
|
669 |> (if #ensure_groundness options then |
|
670 add_ground_predicates ctxt (#limited_types options) |
|
671 else I) |
|
672 |> add_limited_predicates (#limited_predicates options) |
|
673 |> apfst (fold replace (#replacing options)) |
|
674 |> apfst (reorder_manually (#manual_reorder options)) |
|
675 |> apfst rename_vars_program |
|
676 |
665 (* code printer *) |
677 (* code printer *) |
666 |
678 |
667 fun write_arith_op Plus = "+" |
679 fun write_arith_op Plus = "+" |
668 | write_arith_op Minus = "-" |
680 | write_arith_op Minus = "-" |
669 |
681 |
807 |
819 |
808 (* calling external interpreter and getting results *) |
820 (* calling external interpreter and getting results *) |
809 |
821 |
810 fun run (timeout, system) p (query_rel, args) vnames nsols = |
822 fun run (timeout, system) p (query_rel, args) vnames nsols = |
811 let |
823 let |
812 val p' = rename_vars_program p |
|
813 val _ = tracing "Renaming variable names..." |
|
814 val renaming = fold mk_renaming (fold add_vars args vnames) [] |
824 val renaming = fold mk_renaming (fold add_vars args vnames) [] |
815 val vnames' = map (fn v => the (AList.lookup (op =) renaming v)) vnames |
825 val vnames' = map (fn v => the (AList.lookup (op =) renaming v)) vnames |
816 val args' = map (rename_vars_term renaming) args |
826 val args' = map (rename_vars_term renaming) args |
817 val prog = prelude system ^ query system nsols (query_rel, args') vnames' ^ write_program p' |
827 val prog = prelude system ^ query system nsols (query_rel, args') vnames' ^ write_program p |
818 val _ = tracing ("Generated prolog program:\n" ^ prog) |
828 val _ = tracing ("Generated prolog program:\n" ^ prog) |
819 val solution = TimeLimit.timeLimit timeout (fn prog => Cache_IO.with_tmp_file "prolog_file" (fn prolog_file => |
829 val solution = TimeLimit.timeLimit timeout (fn prog => Cache_IO.with_tmp_file "prolog_file" (fn prolog_file => |
820 (File.write prolog_file prog; invoke system (Path.implode prolog_file)))) prog |
830 (File.write prolog_file prog; invoke system (Path.implode prolog_file)))) prog |
821 val _ = tracing ("Prolog returned solution(s):\n" ^ solution) |
831 val _ = tracing ("Prolog returned solution(s):\n" ^ solution) |
822 val tss = parse_solutions solution |
832 val tss = parse_solutions solution |
900 Theory.copy (ProofContext.theory_of ctxt) |
910 Theory.copy (ProofContext.theory_of ctxt) |
901 |> Predicate_Compile.preprocess preprocess_options t |
911 |> Predicate_Compile.preprocess preprocess_options t |
902 val ctxt' = ProofContext.init_global thy' |
912 val ctxt' = ProofContext.init_global thy' |
903 val _ = tracing "Generating prolog program..." |
913 val _ = tracing "Generating prolog program..." |
904 val (p, constant_table) = generate (NONE, #ensure_groundness options) ctxt' name (* FIXME *) |
914 val (p, constant_table) = generate (NONE, #ensure_groundness options) ctxt' name (* FIXME *) |
905 |> (if #ensure_groundness options then |
915 |> post_process ctxt' options |
906 add_ground_predicates ctxt' (#limited_types options) |
|
907 else I) |
|
908 |> add_limited_predicates (#limited_predicates options) |
|
909 |> apfst (fold replace (#replacing options)) |
|
910 |> apfst (reorder_manually (#manual_reorder options)) |
|
911 val constant_table' = declare_consts (fold Term.add_const_names all_args []) constant_table |
916 val constant_table' = declare_consts (fold Term.add_const_names all_args []) constant_table |
912 val args' = map (translate_term ctxt constant_table') all_args |
917 val args' = map (translate_term ctxt constant_table') all_args |
913 val _ = tracing "Running prolog program..." |
918 val _ = tracing "Running prolog program..." |
914 val system_config = System_Config.get (Context.Proof ctxt) |
919 val system_config = System_Config.get (Context.Proof ctxt) |
915 val tss = run (#timeout system_config, #prolog_system system_config) |
920 val tss = run (#timeout system_config, #prolog_system system_config) |
981 val thy2 = Context.theory_map (Predicate_Compile_Alternative_Defs.add_thm intro) thy1 |
986 val thy2 = Context.theory_map (Predicate_Compile_Alternative_Defs.add_thm intro) thy1 |
982 val thy3 = Predicate_Compile.preprocess preprocess_options (Const (full_constname, constT)) thy2 |
987 val thy3 = Predicate_Compile.preprocess preprocess_options (Const (full_constname, constT)) thy2 |
983 val ctxt' = ProofContext.init_global thy3 |
988 val ctxt' = ProofContext.init_global thy3 |
984 val _ = tracing "Generating prolog program..." |
989 val _ = tracing "Generating prolog program..." |
985 val (p, constant_table) = generate (NONE, true) ctxt' full_constname |
990 val (p, constant_table) = generate (NONE, true) ctxt' full_constname |
986 |> add_ground_predicates ctxt' (#limited_types options) |
991 |> post_process ctxt' (set_ensure_groundness options) |
987 |> add_limited_predicates (#limited_predicates options) |
|
988 |> apfst (fold replace (#replacing options)) |
|
989 |> apfst (reorder_manually (#manual_reorder options)) |
|
990 val _ = tracing "Running prolog program..." |
992 val _ = tracing "Running prolog program..." |
991 val system_config = System_Config.get (Context.Proof ctxt) |
993 val system_config = System_Config.get (Context.Proof ctxt) |
992 val tss = run (#timeout system_config, #prolog_system system_config) |
994 val tss = run (#timeout system_config, #prolog_system system_config) |
993 p (translate_const constant_table full_constname, map (Var o fst) vs') (map fst vs') (SOME 1) |
995 p (translate_const constant_table full_constname, map (Var o fst) vs') (map fst vs') (SOME 1) |
994 val _ = tracing "Restoring terms..." |
996 val _ = tracing "Restoring terms..." |