1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
open Pp
open Util
open GenericLib
open GenLib
open Error
open UnifyQC
(* arguments to handle_branch *)
let fail_exp (dt : coq_expr) =
failEnum (gOption dt)
let not_enough_fuel_exp (dt : coq_expr) =
returnEnum (gApp ~explicit:true (gInject "None") [dt])
let ret_exp (dt : coq_expr) (c : coq_expr) =
msg_debug (str "Returning...." ++ fnl ());
debug_coq_expr c;
returnEnum (gApp ~explicit:true (gInject "Some") [dt; c])
let ret_type (s : var) f = hole
let instantiate_existential_method = (gInject "enum")
let instantiate_existential_methodST (n : int) (pred : coq_expr) =
gApp ~explicit:true (gInject "enumSuchThat")
[ hole (* Implicit argument - type A *)
; pred
; hole (* Implicit instance *)]
let rec_method (rec_name : coq_expr) (init_size : coq_expr) (size : coq_expr) (n : int) (letbinds : unknown list option) (l : coq_expr list) =
(* TODO: use letbinds *)
gApp rec_name (init_size :: size :: l)
let bind (opt : bool) (m : coq_expr) (x : string) (f : var -> coq_expr) =
bindEnumOpt m x f
(* (if opt then bindEnumOpt else bindEnum) m x f *)
let stMaybe (opt : bool) (g : coq_expr) (x : string) (checks : ((coq_expr -> coq_expr) * int) list) =
failwith "Implement stMaybe for enum"
(*
let rec sumbools_to_bool x lst =
match lst with
| [] -> gTrueb
| (chk, _) :: lst' ->
matchDec (chk (gVar x)) (fun heq -> gFalseb) (fun hneq -> sumbools_to_bool x lst')
in
let bool_pred =
gFun [x]
(fun [x] -> sumbools_to_bool x checks)
in
(gApp (gInject (if opt then "suchThatMaybeOpt" else "suchThatMaybe"))
[ g (* Use the generator provided for base generator *)
; bool_pred
])
*)
let ret_type_dec (s : var) (left : coq_expr) (right : coq_expr) =
gMatch (gVar s)
[ (injectCtr "left", ["eq"], fun _ -> left)
; (injectCtr "right", ["neq"], fun _ -> right) ]
let check_expr (n : int) (scrut : coq_expr) (left : coq_expr) (right : coq_expr) (out_of_fuel : coq_expr) =
gMatchReturn scrut
"s" (* as clause *)
(fun v -> ret_type v ret_type_dec)
[ (injectCtr "Some", ["res_b" ] , fun [b] ->
(* Why as clauses/returns? *)
gMatch (gVar b)
[ (injectCtr "true", [], fun _ -> left)
; (injectCtr "false", [], fun _ -> right)
])
; (injectCtr "None", [], fun _ -> out_of_fuel)
]
let match_inp (inp : var) (pat : matcher_pat) (left : coq_expr) (right : coq_expr) =
let ret v left right =
construct_match (gVar v) ~catch_all:(Some right) [(pat, left)]
in
let catch_case =
match pat with
| MatchCtr (c, ls) ->
(* Leo: This is a hack totality check for unary matches *)
if num_of_ctrs c = 1 && List.for_all (fun x -> match x with MatchU _ -> true | MatchCtr _ -> false) ls
then None
else Some right
| _ -> failwith "Toplevel match not a constructor?"
in
construct_match_with_return
(gVar inp) ~catch_all:(catch_case) "s" (fun v -> ret_type v ret)
[(pat,left)]
type generator_kind = Base_gen | Ind_gen
(* hoisting out base and ind gen to be able to call them from proof generation *)
let construct_generators
(kind : generator_kind)
(init_size : coq_expr)
(size : coq_expr)
(full_gtyp : coq_expr)
(gen_ctr : ty_ctr)
(dep_type : dep_type)
(ctrs : dep_ctr list)
(rec_name : coq_expr)
(input_ranges : range list)
(init_umap : range UM.t)
(init_tmap : dep_type UM.t)
(result : Unknown.t)
=
(* partially applied handle_branch *)
let handle_branch' =
handle_branch ["EnumSizedSuchThat"; "EnumSuchThat"] dep_type init_size (fail_exp full_gtyp) (not_enough_fuel_exp full_gtyp) (ret_exp full_gtyp)
instantiate_existential_method instantiate_existential_methodST bind
(rec_method rec_name init_size size) bind
stMaybe check_expr match_inp gLetIn gLetTupleIn
gen_ctr init_umap init_tmap input_ranges result
in
let all_gens = List.map handle_branch' ctrs in
let padNone =
if List.exists (fun gb -> not (snd gb)) all_gens
then [not_enough_fuel_exp full_gtyp] else [] in
match kind with
| Base_gen -> List.map fst (List.filter snd all_gens) @ padNone
| Ind_gen -> List.map fst all_gens
let base_gens = construct_generators Base_gen
let ind_gens = construct_generators Ind_gen
(* Advanced Generators *)
let enumSizedST
(gen_ctr : ty_ctr)
(ty_params : ty_param list)
(ctrs : dep_ctr list)
(dep_type : dep_type)
(input_names : var list)
(input_ranges : range list)
(init_umap : range UM.t)
(init_tmap : dep_type UM.t)
(inputs : arg list)
(result : Unknown.t)
(rec_name : coq_expr) =
(* type constructor *)
let _coqTyCtr = gTyCtr gen_ctr in
(* parameters of the type constructor *)
let _coqTyParams = List.map gTyParam ty_params in
(* The type we are generating for -- not the predicate! *)
let full_gtyp = (gType ty_params (UM.find result init_tmap)) in
(* The type of the dependent generator *)
let gen_type = gEnum (gOption full_gtyp) in
let aux_arb rec_name init_size size vars =
gMatch (gVar size)
[ (injectCtr "O", [],
fun _ ->
enumerating
(base_gens init_size (gVar size) full_gtyp gen_ctr dep_type ctrs rec_name
input_ranges init_umap init_tmap result))
; (injectCtr "S", ["size'"],
fun [size'] ->
(* let weights = List.map (fun (c,_) -> Weightmap.lookup_weight c size') ctrs in *)
enumerating
(ind_gens init_size (gVar size') full_gtyp gen_ctr dep_type ctrs rec_name
input_ranges init_umap init_tmap result))
]
in
let generator_body : coq_expr =
let sizeVar = fresh_name "size" in
gRecFunInWithArgs
~structRec:(Some sizeVar)
~assumType:(gen_type)
"aux_arb"
(gArg ~assumName:(gVar (fresh_name "init_size")) () ::
gArg ~assumName:(gVar sizeVar) ()
:: inputs)
(fun (rec_name, init_size::size::vars) -> aux_arb (gVar rec_name) (gVar init_size) size vars)
(fun rec_name -> gFun ["size"]
(fun [size] -> gApp (gVar rec_name)
(gVar size :: gVar size :: List.map (fun i -> gVar (arg_to_var i)) inputs)
))
in
msg_debug (fnl () ++ fnl () ++ str "`Final body produced:" ++ fnl ());
debug_coq_expr generator_body;
msg_debug (fnl ());
gRecord [("enumSizeST", generator_body)]
|