src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML
changeset 36038 385f706eff24
parent 36037 b1b21a8f6362
child 36046 c3946372f556
equal deleted inserted replaced
36037:b1b21a8f6362 36038:385f706eff24
    26     -> theory -> (string * Predicate_Compile_Aux.mode list) list
    26     -> theory -> (string * Predicate_Compile_Aux.mode list) list
    27   val all_random_modes_of : theory -> (string * Predicate_Compile_Aux.mode list) list
    27   val all_random_modes_of : theory -> (string * Predicate_Compile_Aux.mode list) list
    28   val intros_of : theory -> string -> thm list
    28   val intros_of : theory -> string -> thm list
    29   val add_intro : thm -> theory -> theory
    29   val add_intro : thm -> theory -> theory
    30   val set_elim : thm -> theory -> theory
    30   val set_elim : thm -> theory -> theory
       
    31   datatype compilation_funs = CompilationFuns of {
       
    32     mk_predT : typ -> typ,
       
    33     dest_predT : typ -> typ,
       
    34     mk_bot : typ -> term,
       
    35     mk_single : term -> term,
       
    36     mk_bind : term * term -> term,
       
    37     mk_sup : term * term -> term,
       
    38     mk_if : term -> term,
       
    39     mk_not : term -> term,
       
    40     mk_map : typ -> typ -> term -> term -> term
       
    41   };
    31   val register_alternative_function : string -> Predicate_Compile_Aux.mode -> string -> theory -> theory
    42   val register_alternative_function : string -> Predicate_Compile_Aux.mode -> string -> theory -> theory
    32   val alternative_function_of : theory -> string -> Predicate_Compile_Aux.mode -> string option
    43   val alternative_compilation_of : theory -> string -> Predicate_Compile_Aux.mode ->
       
    44     (compilation_funs -> typ -> term) option
       
    45   val functional_compilation : string -> Predicate_Compile_Aux.mode -> compilation_funs -> typ -> term
       
    46   val force_modes_and_functions : string ->
       
    47     (Predicate_Compile_Aux.mode * (string * bool)) list -> theory -> theory
       
    48   val force_modes_and_compilations : string ->
       
    49     (Predicate_Compile_Aux.mode * ((compilation_funs -> typ -> term) * bool)) list -> theory -> theory
    33   val preprocess_intro : theory -> thm -> thm
    50   val preprocess_intro : theory -> thm -> thm
    34   val print_stored_rules : theory -> unit
    51   val print_stored_rules : theory -> unit
    35   val print_all_modes : Predicate_Compile_Aux.compilation -> theory -> unit
    52   val print_all_modes : Predicate_Compile_Aux.compilation -> theory -> unit
    36   val mk_casesrule : Proof.context -> term -> thm list -> term
    53   val mk_casesrule : Proof.context -> term -> thm list -> term
    37   
       
    38   val eval_ref : (unit -> term Predicate.pred) option Unsynchronized.ref
    54   val eval_ref : (unit -> term Predicate.pred) option Unsynchronized.ref
    39   val random_eval_ref : (unit -> int * int -> term Predicate.pred * (int * int))
    55   val random_eval_ref : (unit -> int * int -> term Predicate.pred * (int * int))
    40     option Unsynchronized.ref
    56     option Unsynchronized.ref
    41   val dseq_eval_ref : (unit -> term DSequence.dseq) option Unsynchronized.ref
    57   val dseq_eval_ref : (unit -> term DSequence.dseq) option Unsynchronized.ref
    42   val random_dseq_eval_ref : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int))
    58   val random_dseq_eval_ref : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int))
    46       option Unsynchronized.ref
    62       option Unsynchronized.ref
    47   val new_random_dseq_stats_eval_ref :
    63   val new_random_dseq_stats_eval_ref :
    48     (unit -> int -> int -> int * int -> int -> (term * int) Lazy_Sequence.lazy_sequence)
    64     (unit -> int -> int -> int * int -> int -> (term * int) Lazy_Sequence.lazy_sequence)
    49       option Unsynchronized.ref
    65       option Unsynchronized.ref
    50   val code_pred_intro_attrib : attribute
    66   val code_pred_intro_attrib : attribute
    51   
       
    52   (* used by Quickcheck_Generator *) 
    67   (* used by Quickcheck_Generator *) 
    53   (* temporary for testing of the compilation *)
    68   (* temporary for testing of the compilation *)
    54   
       
    55   datatype compilation_funs = CompilationFuns of {
       
    56     mk_predT : typ -> typ,
       
    57     dest_predT : typ -> typ,
       
    58     mk_bot : typ -> term,
       
    59     mk_single : term -> term,
       
    60     mk_bind : term * term -> term,
       
    61     mk_sup : term * term -> term,
       
    62     mk_if : term -> term,
       
    63     mk_not : term -> term,
       
    64     mk_map : typ -> typ -> term -> term -> term
       
    65   };
       
    66   
       
    67   val pred_compfuns : compilation_funs
    69   val pred_compfuns : compilation_funs
    68   val randompred_compfuns : compilation_funs
    70   val randompred_compfuns : compilation_funs
    69   val new_randompred_compfuns : compilation_funs
    71   val new_randompred_compfuns : compilation_funs
    70   val add_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
    72   val add_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
    71   val add_depth_limited_random_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
    73   val add_depth_limited_random_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
   720     val set = (apsnd o apsnd o apsnd) (K modes)
   722     val set = (apsnd o apsnd o apsnd) (K modes)
   721   in
   723   in
   722     PredData.map (Graph.map_node name (map_pred_data set))
   724     PredData.map (Graph.map_node name (map_pred_data set))
   723   end
   725   end
   724 
   726 
   725 (* registration of alternative function names *)
       
   726 
       
   727 structure Alt_Names_Data = Theory_Data
       
   728 (
       
   729   type T = (mode * string) list Symtab.table;
       
   730   val empty = Symtab.empty;
       
   731   val extend = I;
       
   732   val merge = Symtab.merge (op =);
       
   733 );
       
   734 
       
   735 fun register_alternative_function pred_name mode fun_name =
       
   736   Alt_Names_Data.map (Symtab.insert_list (op =) (pred_name, (mode, fun_name)))
       
   737 
       
   738 fun alternative_function_of thy pred_name mode =
       
   739   AList.lookup eq_mode (Symtab.lookup_list (Alt_Names_Data.get thy) pred_name) mode
       
   740 
       
   741 (* datastructures and setup for generic compilation *)
   727 (* datastructures and setup for generic compilation *)
   742 
   728 
   743 datatype compilation_funs = CompilationFuns of {
   729 datatype compilation_funs = CompilationFuns of {
   744   mk_predT : typ -> typ,
   730   mk_predT : typ -> typ,
   745   dest_predT : typ -> typ,
   731   dest_predT : typ -> typ,
   759 fun mk_bind (CompilationFuns funs) = #mk_bind funs
   745 fun mk_bind (CompilationFuns funs) = #mk_bind funs
   760 fun mk_sup (CompilationFuns funs) = #mk_sup funs
   746 fun mk_sup (CompilationFuns funs) = #mk_sup funs
   761 fun mk_if (CompilationFuns funs) = #mk_if funs
   747 fun mk_if (CompilationFuns funs) = #mk_if funs
   762 fun mk_not (CompilationFuns funs) = #mk_not funs
   748 fun mk_not (CompilationFuns funs) = #mk_not funs
   763 fun mk_map (CompilationFuns funs) = #mk_map funs
   749 fun mk_map (CompilationFuns funs) = #mk_map funs
       
   750 
       
   751 (* registration of alternative function names *)
       
   752 
       
   753 structure Alt_Compilations_Data = Theory_Data
       
   754 (
       
   755   type T = (mode * (compilation_funs -> typ -> term)) list Symtab.table;
       
   756   val empty = Symtab.empty;
       
   757   val extend = I;
       
   758   val merge = Symtab.merge (K true);
       
   759 );
       
   760 
       
   761 fun alternative_compilation_of thy pred_name mode =
       
   762   AList.lookup eq_mode (Symtab.lookup_list (Alt_Compilations_Data.get thy) pred_name) mode
       
   763 
       
   764 fun force_modes_and_compilations pred_name compilations =
       
   765   let
       
   766     (* thm refl is a dummy thm *)
       
   767     val modes = map fst compilations
       
   768     val (needs_random, non_random_modes) = pairself (map fst)
       
   769       (List.partition (fn (m, (fun_name, random)) => random) compilations)
       
   770     val non_random_dummys = map (rpair "dummy") non_random_modes
       
   771     val all_dummys = map (rpair "dummy") modes
       
   772     val dummy_function_names = map (rpair all_dummys) Predicate_Compile_Aux.random_compilations
       
   773       @ map (rpair non_random_dummys) Predicate_Compile_Aux.non_random_compilations
       
   774     val alt_compilations = map (apsnd fst) compilations
       
   775   in
       
   776     PredData.map (Graph.new_node
       
   777       (pred_name, mk_pred_data (([], SOME @{thm refl}), (dummy_function_names, ([], needs_random)))))
       
   778     #> Alt_Compilations_Data.map (Symtab.insert (K false) (pred_name, alt_compilations))
       
   779   end
       
   780 
       
   781 fun functional_compilation fun_name mode compfuns T =
       
   782   let
       
   783     val (inpTs, outpTs) = split_map_modeT (fn _ => fn T => (SOME T, NONE))
       
   784       mode (binder_types T)
       
   785     val bs = map (pair "x") inpTs
       
   786     val bounds = map Bound (rev (0 upto (length bs) - 1))
       
   787     val f = Const (fun_name, inpTs ---> HOLogic.mk_tupleT outpTs)
       
   788   in list_abs (bs, mk_single compfuns (list_comb (f, bounds))) end
       
   789 
       
   790 fun register_alternative_function pred_name mode fun_name =
       
   791   Alt_Compilations_Data.map (Symtab.insert_list (eq_pair eq_mode (K false))
       
   792     (pred_name, (mode, functional_compilation fun_name mode)))
       
   793 
       
   794 fun force_modes_and_functions pred_name fun_names =
       
   795   force_modes_and_compilations pred_name
       
   796     (map (fn (mode, (fun_name, random)) => (mode, (functional_compilation fun_name mode, random)))
       
   797     fun_names)
       
   798 
       
   799 (* structures for different compilations *)
   764 
   800 
   765 structure PredicateCompFuns =
   801 structure PredicateCompFuns =
   766 struct
   802 struct
   767 
   803 
   768 fun mk_predT T = Type (@{type_name Predicate.pred}, [T])
   804 fun mk_predT T = Type (@{type_name Predicate.pred}, [T])
  1904     val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) [];
  1940     val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) [];
  1905     val name = Name.variant names "x";
  1941     val name = Name.variant names "x";
  1906     val name' = Name.variant (name :: names) "y";
  1942     val name' = Name.variant (name :: names) "y";
  1907     val T = HOLogic.mk_tupleT (map fastype_of out_ts);
  1943     val T = HOLogic.mk_tupleT (map fastype_of out_ts);
  1908     val U = fastype_of success_t;
  1944     val U = fastype_of success_t;
  1909     val U' = dest_predT compfuns U;
  1945     val U' = dest_predT compfuns U;        
  1910     val v = Free (name, T);
  1946     val v = Free (name, T);
  1911     val v' = Free (name', T);
  1947     val v' = Free (name', T);
  1912   in
  1948   in
  1913     lambda v (fst (Datatype.make_case ctxt Datatype_Case.Quiet [] v
  1949     lambda v (fst (Datatype.make_case ctxt Datatype_Case.Quiet [] v
  1914       [(HOLogic.mk_tuple out_ts,
  1950       [(HOLogic.mk_tuple out_ts,
  1935     fun expr_of (t, deriv) =
  1971     fun expr_of (t, deriv) =
  1936       (case (t, deriv) of
  1972       (case (t, deriv) of
  1937         (t, Term Input) => SOME t
  1973         (t, Term Input) => SOME t
  1938       | (t, Term Output) => NONE
  1974       | (t, Term Output) => NONE
  1939       | (Const (name, T), Context mode) =>
  1975       | (Const (name, T), Context mode) =>
  1940         (case alternative_function_of (ProofContext.theory_of ctxt) name mode of
  1976         (case alternative_compilation_of (ProofContext.theory_of ctxt) name mode of
  1941           SOME alt_function_name =>
  1977           SOME alt_comp => SOME (alt_comp compfuns T)
  1942             let
       
  1943               val (inpTs, outpTs) = split_map_modeT (fn _ => fn T => (SOME T, NONE))
       
  1944                 mode (binder_types T)
       
  1945               val bs = map (pair "x") inpTs
       
  1946               val bounds = map Bound (rev (0 upto (length bs) - 1))
       
  1947               val f = Const (alt_function_name, inpTs ---> HOLogic.mk_tupleT outpTs)
       
  1948             in SOME (list_abs (bs, mk_single compfuns (list_comb (f, bounds)))) end
       
  1949         | NONE =>
  1978         | NONE =>
  1950           SOME (Const (function_name_of (Comp_Mod.compilation compilation_modifiers)
  1979           SOME (Const (function_name_of (Comp_Mod.compilation compilation_modifiers)
  1951             (ProofContext.theory_of ctxt) name mode,
  1980             (ProofContext.theory_of ctxt) name mode,
  1952             Comp_Mod.funT_of compilation_modifiers mode T)))
  1981             Comp_Mod.funT_of compilation_modifiers mode T)))
  1953       | (Free (s, T), Context m) =>
  1982       | (Free (s, T), Context m) =>