src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML
changeset 36020 3ee4c29ead7f
parent 36019 64bbbd858c39
child 36025 d25043e7843f
equal deleted inserted replaced
36019:64bbbd858c39 36020:3ee4c29ead7f
    37   val random_eval_ref : (unit -> int * int -> term Predicate.pred * (int * int))
    37   val random_eval_ref : (unit -> int * int -> term Predicate.pred * (int * int))
    38     option Unsynchronized.ref
    38     option Unsynchronized.ref
    39   val dseq_eval_ref : (unit -> term DSequence.dseq) option Unsynchronized.ref
    39   val dseq_eval_ref : (unit -> term DSequence.dseq) option Unsynchronized.ref
    40   val random_dseq_eval_ref : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int))
    40   val random_dseq_eval_ref : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int))
    41     option Unsynchronized.ref
    41     option Unsynchronized.ref
       
    42   val new_random_dseq_eval_ref :
       
    43     (unit -> int -> int -> int * int -> int -> term Lazy_Sequence.lazy_sequence)
       
    44       option Unsynchronized.ref
    42   val code_pred_intro_attrib : attribute
    45   val code_pred_intro_attrib : attribute
    43   
    46   
    44   (* used by Quickcheck_Generator *) 
    47   (* used by Quickcheck_Generator *) 
    45   (* temporary for testing of the compilation *)
    48   (* temporary for testing of the compilation *)
    46   
    49   
    56     mk_map : typ -> typ -> term -> term -> term
    59     mk_map : typ -> typ -> term -> term -> term
    57   };
    60   };
    58   
    61   
    59   val pred_compfuns : compilation_funs
    62   val pred_compfuns : compilation_funs
    60   val randompred_compfuns : compilation_funs
    63   val randompred_compfuns : compilation_funs
       
    64   val new_randompred_compfuns : compilation_funs
    61   val add_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
    65   val add_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
    62   val add_random_dseq_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
    66   val add_random_dseq_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
       
    67   val add_new_random_dseq_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory
    63   val mk_tracing : string -> term -> term
    68   val mk_tracing : string -> term -> term
    64 end;
    69 end;
    65 
    70 
    66 structure Predicate_Compile_Core : PREDICATE_COMPILE_CORE =
    71 structure Predicate_Compile_Core : PREDICATE_COMPILE_CORE =
    67 struct
    72 struct
  1018 end;
  1023 end;
  1019 
  1024 
  1020 (* for external use with interactive mode *)
  1025 (* for external use with interactive mode *)
  1021 val pred_compfuns = PredicateCompFuns.compfuns
  1026 val pred_compfuns = PredicateCompFuns.compfuns
  1022 val randompred_compfuns = Random_Sequence_CompFuns.compfuns;
  1027 val randompred_compfuns = Random_Sequence_CompFuns.compfuns;
       
  1028 val new_randompred_compfuns = New_Pos_Random_Sequence_CompFuns.compfuns
  1023 
  1029 
  1024 (* compilation modifiers *)
  1030 (* compilation modifiers *)
  1025 
  1031 
  1026 (* function types and names of different compilations *)
  1032 (* function types and names of different compilations *)
  1027 
  1033 
  1890     "(" ^ string_of_tderiv ctxt (t1, deriv1) ^ ", " ^ string_of_tderiv ctxt (t2, deriv2) ^ ")"
  1896     "(" ^ string_of_tderiv ctxt (t1, deriv1) ^ ", " ^ string_of_tderiv ctxt (t2, deriv2) ^ ")"
  1891   | (t, Term Input) => Syntax.string_of_term ctxt t ^ "[Input]"
  1897   | (t, Term Input) => Syntax.string_of_term ctxt t ^ "[Input]"
  1892   | (t, Term Output) => Syntax.string_of_term ctxt t ^ "[Output]"
  1898   | (t, Term Output) => Syntax.string_of_term ctxt t ^ "[Output]"
  1893   | (t, Context m) => Syntax.string_of_term ctxt t ^ "[" ^ string_of_mode m ^ "]")
  1899   | (t, Context m) => Syntax.string_of_term ctxt t ^ "[" ^ string_of_mode m ^ "]")
  1894 
  1900 
  1895 fun compile_expr compilation_modifiers ctxt pol (t, deriv) additional_arguments =
  1901 fun compile_expr compilation_modifiers ctxt (t, deriv) additional_arguments =
  1896   let
  1902   let
  1897     val compfuns = Comp_Mod.compfuns compilation_modifiers
  1903     val compfuns = Comp_Mod.compfuns compilation_modifiers
  1898     fun expr_of (t, deriv) =
  1904     fun expr_of (t, deriv) =
  1899       (case (t, deriv) of
  1905       (case (t, deriv) of
  1900         (t, Term Input) => SOME t
  1906         (t, Term Input) => SOME t
  1924   in
  1930   in
  1925     list_comb (the (expr_of (t, deriv)), additional_arguments)
  1931     list_comb (the (expr_of (t, deriv)), additional_arguments)
  1926   end
  1932   end
  1927 
  1933 
  1928 fun compile_clause compilation_modifiers ctxt all_vs param_vs additional_arguments
  1934 fun compile_clause compilation_modifiers ctxt all_vs param_vs additional_arguments
  1929   (pol, mode) inp (ts, moded_ps) =
  1935   mode inp (ts, moded_ps) =
  1930   let
  1936   let
  1931     val compfuns = Comp_Mod.compfuns compilation_modifiers
  1937     val compfuns = Comp_Mod.compfuns compilation_modifiers
  1932     val iss = ho_arg_modes_of mode
  1938     val iss = ho_arg_modes_of mode
  1933     val compile_match = compile_match compilation_modifiers
  1939     val compile_match = compile_match compilation_modifiers
  1934       additional_arguments param_vs iss ctxt
  1940       additional_arguments param_vs iss ctxt
  1957               Comp_Mod.transform_additional_arguments compilation_modifiers p additional_arguments
  1963               Comp_Mod.transform_additional_arguments compilation_modifiers p additional_arguments
  1958             val (compiled_clause, rest) = case p of
  1964             val (compiled_clause, rest) = case p of
  1959                Prem t =>
  1965                Prem t =>
  1960                  let
  1966                  let
  1961                    val u =
  1967                    val u =
  1962                      compile_expr compilation_modifiers ctxt
  1968                      compile_expr compilation_modifiers ctxt (t, deriv) additional_arguments'
  1963                        pol (t, deriv) additional_arguments'
       
  1964                    val (_, out_ts''') = split_mode mode (snd (strip_comb t))
  1969                    val (_, out_ts''') = split_mode mode (snd (strip_comb t))
  1965                    val rest = compile_prems out_ts''' vs' names'' ps
  1970                    val rest = compile_prems out_ts''' vs' names'' ps
  1966                  in
  1971                  in
  1967                    (u, rest)
  1972                    (u, rest)
  1968                  end
  1973                  end
  1969              | Negprem t =>
  1974              | Negprem t =>
  1970                  let
  1975                  let
  1971                    val neg_compilation_modifiers =
  1976                    val neg_compilation_modifiers =
  1972                      negative_comp_modifiers_of compilation_modifiers
  1977                      negative_comp_modifiers_of compilation_modifiers
  1973                    val u = mk_not compfuns
  1978                    val u = mk_not compfuns
  1974                      (compile_expr neg_compilation_modifiers ctxt
  1979                      (compile_expr neg_compilation_modifiers ctxt (t, deriv) additional_arguments')
  1975                        (not pol) (t, deriv) additional_arguments')
       
  1976                    val (_, out_ts''') = split_mode mode (snd (strip_comb t))
  1980                    val (_, out_ts''') = split_mode mode (snd (strip_comb t))
  1977                    val rest = compile_prems out_ts''' vs' names'' ps
  1981                    val rest = compile_prems out_ts''' vs' names'' ps
  1978                  in
  1982                  in
  1979                    (u, rest)
  1983                    (u, rest)
  1980                  end
  1984                  end
  2003   end
  2007   end
  2004 
  2008 
  2005 fun compile_pred compilation_modifiers thy all_vs param_vs s T (pol, mode) moded_cls =
  2009 fun compile_pred compilation_modifiers thy all_vs param_vs s T (pol, mode) moded_cls =
  2006   let
  2010   let
  2007     val ctxt = ProofContext.init thy
  2011     val ctxt = ProofContext.init thy
       
  2012     val compilation_modifiers = if pol then compilation_modifiers else
       
  2013       negative_comp_modifiers_of compilation_modifiers
  2008     val additional_arguments = Comp_Mod.additional_arguments compilation_modifiers
  2014     val additional_arguments = Comp_Mod.additional_arguments compilation_modifiers
  2009       (all_vs @ param_vs)
  2015       (all_vs @ param_vs)
  2010     val compfuns = Comp_Mod.compfuns compilation_modifiers
  2016     val compfuns = Comp_Mod.compfuns compilation_modifiers
  2011     fun is_param_type (T as Type ("fun",[_ , T'])) =
  2017     fun is_param_type (T as Type ("fun",[_ , T'])) =
  2012       is_some (try (dest_predT compfuns) T) orelse is_param_type T'
  2018       is_some (try (dest_predT compfuns) T) orelse is_param_type T'
  2027         (param_vs, (all_vs @ param_vs))
  2033         (param_vs, (all_vs @ param_vs))
  2028     val in_ts' = map_filter (map_filter_prod
  2034     val in_ts' = map_filter (map_filter_prod
  2029       (fn t as Free (x, _) => if member (op =) param_vs x then NONE else SOME t | t => SOME t)) in_ts
  2035       (fn t as Free (x, _) => if member (op =) param_vs x then NONE else SOME t | t => SOME t)) in_ts
  2030     val cl_ts =
  2036     val cl_ts =
  2031       map (compile_clause compilation_modifiers
  2037       map (compile_clause compilation_modifiers
  2032         ctxt all_vs param_vs additional_arguments (pol, mode) (HOLogic.mk_tuple in_ts')) moded_cls;
  2038         ctxt all_vs param_vs additional_arguments mode (HOLogic.mk_tuple in_ts')) moded_cls;
  2033     val compilation = Comp_Mod.wrap_compilation compilation_modifiers compfuns
  2039     val compilation = Comp_Mod.wrap_compilation compilation_modifiers compfuns
  2034       s T mode additional_arguments
  2040       s T mode additional_arguments
  2035       (if null cl_ts then
  2041       (if null cl_ts then
  2036         mk_bot compfuns (HOLogic.mk_tupleT outTs)
  2042         mk_bot compfuns (HOLogic.mk_tupleT outTs)
  2037       else foldr1 (mk_sup compfuns) cl_ts)
  2043       else foldr1 (mk_sup compfuns) cl_ts)
  3125 val random_eval_ref =
  3131 val random_eval_ref =
  3126   Unsynchronized.ref (NONE : (unit -> int * int -> term Predicate.pred * (int * int)) option);
  3132   Unsynchronized.ref (NONE : (unit -> int * int -> term Predicate.pred * (int * int)) option);
  3127 val dseq_eval_ref = Unsynchronized.ref (NONE : (unit -> term DSequence.dseq) option);
  3133 val dseq_eval_ref = Unsynchronized.ref (NONE : (unit -> term DSequence.dseq) option);
  3128 val random_dseq_eval_ref =
  3134 val random_dseq_eval_ref =
  3129   Unsynchronized.ref (NONE : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int)) option);
  3135   Unsynchronized.ref (NONE : (unit -> int -> int -> int * int -> term DSequence.dseq * (int * int)) option);
       
  3136 val new_random_dseq_eval_ref =
       
  3137   Unsynchronized.ref (NONE : (unit -> int -> int -> int * int -> int -> term Lazy_Sequence.lazy_sequence) option)
  3130 
  3138 
  3131 (*FIXME turn this into an LCF-guarded preprocessor for comprehensions*)
  3139 (*FIXME turn this into an LCF-guarded preprocessor for comprehensions*)
  3132 fun analyze_compr thy compfuns param_user_modes (compilation, arguments) t_compr =
  3140 fun analyze_compr thy compfuns param_user_modes (compilation, arguments) t_compr =
  3133   let
  3141   let
  3134     val all_modes_of = all_modes_of compilation
  3142     val all_modes_of = all_modes_of compilation
  3194           | Depth_Limited => [HOLogic.mk_number @{typ "code_numeral"} (hd arguments)]
  3202           | Depth_Limited => [HOLogic.mk_number @{typ "code_numeral"} (hd arguments)]
  3195           | Depth_Limited_Random => map (HOLogic.mk_number @{typ "code_numeral"}) arguments @
  3203           | Depth_Limited_Random => map (HOLogic.mk_number @{typ "code_numeral"}) arguments @
  3196             [@{term "(1, 1) :: code_numeral * code_numeral"}]
  3204             [@{term "(1, 1) :: code_numeral * code_numeral"}]
  3197           | DSeq => []
  3205           | DSeq => []
  3198           | Pos_Random_DSeq => []
  3206           | Pos_Random_DSeq => []
       
  3207           | New_Pos_Random_DSeq => []
  3199         val comp_modifiers =
  3208         val comp_modifiers =
  3200           case compilation of
  3209           case compilation of
  3201             Pred => predicate_comp_modifiers
  3210             Pred => predicate_comp_modifiers
  3202           | Random => random_comp_modifiers
  3211           | Random => random_comp_modifiers
  3203           | Depth_Limited => depth_limited_comp_modifiers
  3212           | Depth_Limited => depth_limited_comp_modifiers
  3204           | Depth_Limited_Random => depth_limited_random_comp_modifiers
  3213           | Depth_Limited_Random => depth_limited_random_comp_modifiers
  3205           (*| Annotated => annotated_comp_modifiers*)
  3214           (*| Annotated => annotated_comp_modifiers*)
  3206           | DSeq => dseq_comp_modifiers
  3215           | DSeq => dseq_comp_modifiers
  3207           | Pos_Random_DSeq => pos_random_dseq_comp_modifiers
  3216           | Pos_Random_DSeq => pos_random_dseq_comp_modifiers
       
  3217           | New_Pos_Random_DSeq => new_pos_random_dseq_comp_modifiers
  3208         val t_pred = compile_expr comp_modifiers (ProofContext.init thy)
  3218         val t_pred = compile_expr comp_modifiers (ProofContext.init thy)
  3209           true (body, deriv) additional_arguments;
  3219           (body, deriv) additional_arguments;
  3210         val T_pred = dest_predT compfuns (fastype_of t_pred)
  3220         val T_pred = dest_predT compfuns (fastype_of t_pred)
  3211         val arrange = split_lambda (HOLogic.mk_tuple outargs) output_tuple
  3221         val arrange = split_lambda (HOLogic.mk_tuple outargs) output_tuple
  3212       in
  3222       in
  3213         if null outargs then t_pred else mk_map compfuns T_pred T_compr arrange t_pred
  3223         if null outargs then t_pred else mk_map compfuns T_pred T_compr arrange t_pred
  3214       end
  3224       end
  3221     val compfuns =
  3231     val compfuns =
  3222       case compilation of
  3232       case compilation of
  3223         Random => PredicateCompFuns.compfuns
  3233         Random => PredicateCompFuns.compfuns
  3224       | DSeq => DSequence_CompFuns.compfuns
  3234       | DSeq => DSequence_CompFuns.compfuns
  3225       | Pos_Random_DSeq => Random_Sequence_CompFuns.compfuns
  3235       | Pos_Random_DSeq => Random_Sequence_CompFuns.compfuns
       
  3236       | New_Pos_Random_DSeq => New_Pos_Random_Sequence_CompFuns.compfuns
  3226       | _ => PredicateCompFuns.compfuns
  3237       | _ => PredicateCompFuns.compfuns
       
  3238       
  3227     val t = analyze_compr thy compfuns param_user_modes options t_compr;
  3239     val t = analyze_compr thy compfuns param_user_modes options t_compr;
  3228     val T = dest_predT compfuns (fastype_of t);
  3240     val T = dest_predT compfuns (fastype_of t);
  3229     val t' = mk_map compfuns T HOLogic.termT (HOLogic.term_of_const T) t;
  3241     val t' = mk_map compfuns T HOLogic.termT (HOLogic.term_of_const T) t;
  3230     val ts =
  3242     val ts =
  3231       case compilation of
  3243       case compilation of
  3247           end
  3259           end
  3248       | DSeq =>
  3260       | DSeq =>
  3249           fst (DSequence.yieldn k
  3261           fst (DSequence.yieldn k
  3250             (Code_Eval.eval NONE ("Predicate_Compile_Core.dseq_eval_ref", dseq_eval_ref)
  3262             (Code_Eval.eval NONE ("Predicate_Compile_Core.dseq_eval_ref", dseq_eval_ref)
  3251               DSequence.map thy t' []) (the_single arguments) true)
  3263               DSequence.map thy t' []) (the_single arguments) true)
       
  3264       | New_Pos_Random_DSeq =>
       
  3265           let
       
  3266             val [nrandom, size, depth] = arguments
       
  3267             val seed = Random_Engine.next_seed ()
       
  3268           in
       
  3269             fst (Lazy_Sequence.yieldn k
       
  3270               (Code_Eval.eval NONE
       
  3271                 ("Predicate_Compile_Core.new_random_dseq_eval_ref", new_random_dseq_eval_ref)
       
  3272                 (fn proc => fn g => fn nrandom => fn size => fn s => fn depth => g nrandom size s depth
       
  3273                   |> Lazy_Sequence.map proc)
       
  3274                   thy t' [] nrandom size seed depth))
       
  3275           end
  3252       | _ =>
  3276       | _ =>
  3253           fst (Predicate.yieldn k
  3277           fst (Predicate.yieldn k
  3254             (Code_Eval.eval NONE ("Predicate_Compile_Core.eval_ref", eval_ref)
  3278             (Code_Eval.eval NONE ("Predicate_Compile_Core.eval_ref", eval_ref)
  3255               Predicate.map thy t' []))
  3279               Predicate.map thy t' []))
  3256   in (T, ts) end;
  3280   in (T, ts) end;