src/HOL/Tools/Predicate_Compile/code_prolog.ML
changeset 42361 23f352990944
parent 42159 234ec7011e5d
child 42489 db9b9e46131c
equal deleted inserted replaced
42360:da8817d01e7c 42361:23f352990944
   310       (Trueprop_conv (Conv.try_conv (Conv.rewr_conv @{thm Predicate.eq_is_eq}))))
   310       (Trueprop_conv (Conv.try_conv (Conv.rewr_conv @{thm Predicate.eq_is_eq}))))
   311     (Thm.transfer thy rule)
   311     (Thm.transfer thy rule)
   312 
   312 
   313 fun translate_intros ensure_groundness ctxt gr const constant_table =
   313 fun translate_intros ensure_groundness ctxt gr const constant_table =
   314   let
   314   let
   315     val intros = map (preprocess_intro (ProofContext.theory_of ctxt)) (Graph.get_node gr const)
   315     val intros = map (preprocess_intro (Proof_Context.theory_of ctxt)) (Graph.get_node gr const)
   316     val (intros', ctxt') = Variable.import_terms true (map prop_of intros) ctxt
   316     val (intros', ctxt') = Variable.import_terms true (map prop_of intros) ctxt
   317     val constant_table' = declare_consts (fold Term.add_const_names intros' []) constant_table
   317     val constant_table' = declare_consts (fold Term.add_const_names intros' []) constant_table
   318     fun translate_intro intro =
   318     fun translate_intro intro =
   319       let
   319       let
   320         val head = HOLogic.dest_Trueprop (Logic.strip_imp_concl intro)
   320         val head = HOLogic.dest_Trueprop (Logic.strip_imp_concl intro)
   548             ((rel_name, lim_var @ [maybe_AppF (translate_const constant_table'' constr_name, vars)]),
   548             ((rel_name, lim_var @ [maybe_AppF (translate_const constant_table'' constr_name, vars)]),
   549              Conj (map2 mk_prem vars Ts))
   549              Conj (map2 mk_prem vars Ts))
   550         in
   550         in
   551           (clause :: flat rec_clauses, (seen', constant_table''))
   551           (clause :: flat rec_clauses, (seen', constant_table''))
   552         end
   552         end
   553       val constrs = inst_constrs_of (ProofContext.theory_of ctxt) T
   553       val constrs = inst_constrs_of (Proof_Context.theory_of ctxt) T
   554       val constrs' = (constrs ~~ map (is_recursive_constr T) constrs)
   554       val constrs' = (constrs ~~ map (is_recursive_constr T) constrs)
   555         |> (fn cs => filter_out snd cs @ filter snd cs)
   555         |> (fn cs => filter_out snd cs @ filter snd cs)
   556       val (clauses, constant_table') =
   556       val (clauses, constant_table') =
   557         apfst flat (fold_map mk_impl constrs' (T :: seen, constant_table))
   557         apfst flat (fold_map mk_impl constrs' (T :: seen, constant_table))
   558       val size_term = funpow size (fn t => AppF ("suc", [t])) (Cons "zero")
   558       val size_term = funpow size (fn t => AppF ("suc", [t])) (Cons "zero")
   871   | restore_term ctxt constant_table (Number n, @{typ "int"}) = HOLogic.mk_number @{typ "int"} n
   871   | restore_term ctxt constant_table (Number n, @{typ "int"}) = HOLogic.mk_number @{typ "int"} n
   872   | restore_term ctxt constant_table (Number n, _) = raise (Fail "unexpected type for number") 
   872   | restore_term ctxt constant_table (Number n, _) = raise (Fail "unexpected type for number") 
   873   | restore_term ctxt constant_table (Cons s, T) = Const (restore_const constant_table s, T)
   873   | restore_term ctxt constant_table (Cons s, T) = Const (restore_const constant_table s, T)
   874   | restore_term ctxt constant_table (AppF (f, args), T) =
   874   | restore_term ctxt constant_table (AppF (f, args), T) =
   875     let
   875     let
   876       val thy = ProofContext.theory_of ctxt
   876       val thy = Proof_Context.theory_of ctxt
   877       val c = restore_const constant_table f
   877       val c = restore_const constant_table f
   878       val cT = Sign.the_const_type thy c
   878       val cT = Sign.the_const_type thy c
   879       val (argsT, resT) = strip_type cT
   879       val (argsT, resT) = strip_type cT
   880       val subst = Sign.typ_match thy (resT, T) Vartab.empty
   880       val subst = Sign.typ_match thy (resT, T) Vartab.empty
   881       val argsT' = map (Envir.subst_type subst) argsT
   881       val argsT' = map (Envir.subst_type subst) argsT
   921   compilation = Predicate_Compile_Aux.Pred
   921   compilation = Predicate_Compile_Aux.Pred
   922 }
   922 }
   923 
   923 
   924 fun values ctxt soln t_compr =
   924 fun values ctxt soln t_compr =
   925   let
   925   let
   926     val options = code_options_of (ProofContext.theory_of ctxt)
   926     val options = code_options_of (Proof_Context.theory_of ctxt)
   927     val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t
   927     val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t
   928       | _ => error ("Not a set comprehension: " ^ Syntax.string_of_term ctxt t_compr);
   928       | _ => error ("Not a set comprehension: " ^ Syntax.string_of_term ctxt t_compr);
   929     val (body, Ts, fp) = HOLogic.strip_psplits split;
   929     val (body, Ts, fp) = HOLogic.strip_psplits split;
   930     val output_names = Name.variant_list (Term.add_free_names body [])
   930     val output_names = Name.variant_list (Term.add_free_names body [])
   931       (map (fn i => "x" ^ string_of_int i) (1 upto length Ts))
   931       (map (fn i => "x" ^ string_of_int i) (1 upto length Ts))
   934     val (pred as Const (name, T), all_args) =
   934     val (pred as Const (name, T), all_args) =
   935       case strip_comb body of
   935       case strip_comb body of
   936         (Const (name, T), all_args) => (Const (name, T), all_args)
   936         (Const (name, T), all_args) => (Const (name, T), all_args)
   937       | (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head)
   937       | (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head)
   938     val _ = tracing "Preprocessing specification..."
   938     val _ = tracing "Preprocessing specification..."
   939     val T = Sign.the_const_type (ProofContext.theory_of ctxt) name
   939     val T = Sign.the_const_type (Proof_Context.theory_of ctxt) name
   940     val t = Const (name, T)
   940     val t = Const (name, T)
   941     val thy' =
   941     val thy' =
   942       Theory.copy (ProofContext.theory_of ctxt)
   942       Theory.copy (Proof_Context.theory_of ctxt)
   943       |> Predicate_Compile.preprocess preprocess_options t
   943       |> Predicate_Compile.preprocess preprocess_options t
   944     val ctxt' = ProofContext.init_global thy'
   944     val ctxt' = Proof_Context.init_global thy'
   945     val _ = tracing "Generating prolog program..."
   945     val _ = tracing "Generating prolog program..."
   946     val (p, constant_table) = generate (NONE, #ensure_groundness options) ctxt' name (* FIXME *)
   946     val (p, constant_table) = generate (NONE, #ensure_groundness options) ctxt' name (* FIXME *)
   947       |> post_process ctxt' options name
   947       |> post_process ctxt' options name
   948     val constant_table' = declare_consts (fold Term.add_const_names all_args []) constant_table
   948     val constant_table' = declare_consts (fold Term.add_const_names all_args []) constant_table
   949     val args' = map (translate_term ctxt constant_table') all_args
   949     val args' = map (translate_term ctxt constant_table') all_args
  1010 (* FIXME: a small clone of Predicate_Compile_Quickcheck - maybe refactor out commons *)
  1010 (* FIXME: a small clone of Predicate_Compile_Quickcheck - maybe refactor out commons *)
  1011 
  1011 
  1012 fun quickcheck ctxt [(t, [])] [_, size] =
  1012 fun quickcheck ctxt [(t, [])] [_, size] =
  1013   let
  1013   let
  1014     val t' = list_abs_free (Term.add_frees t [], t)
  1014     val t' = list_abs_free (Term.add_frees t [], t)
  1015     val options = code_options_of (ProofContext.theory_of ctxt)
  1015     val options = code_options_of (Proof_Context.theory_of ctxt)
  1016     val thy = Theory.copy (ProofContext.theory_of ctxt)
  1016     val thy = Theory.copy (Proof_Context.theory_of ctxt)
  1017     val ((((full_constname, constT), vs'), intro), thy1) =
  1017     val ((((full_constname, constT), vs'), intro), thy1) =
  1018       Predicate_Compile_Aux.define_quickcheck_predicate t' thy
  1018       Predicate_Compile_Aux.define_quickcheck_predicate t' thy
  1019     val thy2 = Context.theory_map (Predicate_Compile_Alternative_Defs.add_thm intro) thy1
  1019     val thy2 = Context.theory_map (Predicate_Compile_Alternative_Defs.add_thm intro) thy1
  1020     val thy3 = Predicate_Compile.preprocess preprocess_options (Const (full_constname, constT)) thy2
  1020     val thy3 = Predicate_Compile.preprocess preprocess_options (Const (full_constname, constT)) thy2
  1021     val ctxt' = ProofContext.init_global thy3
  1021     val ctxt' = Proof_Context.init_global thy3
  1022     val _ = tracing "Generating prolog program..."
  1022     val _ = tracing "Generating prolog program..."
  1023     val (p, constant_table) = generate (NONE, true) ctxt' full_constname
  1023     val (p, constant_table) = generate (NONE, true) ctxt' full_constname
  1024       |> post_process ctxt' (set_ensure_groundness options) full_constname
  1024       |> post_process ctxt' (set_ensure_groundness options) full_constname
  1025     val _ = tracing "Running prolog program..."
  1025     val _ = tracing "Running prolog program..."
  1026     val system_config = System_Config.get (Context.Proof ctxt)
  1026     val system_config = System_Config.get (Context.Proof ctxt)