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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
open Pp
open Libnames
open Util
open Constrexpr
open GenericLib
open ArbitrarySizedST
open EnumSizedST
open CheckerSizedST
open Error
open UnifyQC
(** Derivable classes *)
type derivable =
| DecOpt
| GenSizedSuchThat
| EnumSizedSuchThat
let derivable_to_string = function
| DecOpt -> "DecOpt"
| GenSizedSuchThat -> "GenSizedSuchThat"
| EnumSizedSuchThat -> "EnumSizedSuchThat"
(** Name of the instance to be generated *)
let mk_instance_name der tn =
var_to_string (fresh_name ((derivable_to_string der) ^ tn))
let derive_dependent
(class_name : derivable)
(constructor : constr_expr)
(umap : range UM.t)
(tmap : dep_type UM.t)
(input_names : var list)
(input_ranges : range list)
(ty_ctr, ty_params, ctrs, dep_type)
(letbinds : var list option)
(result : unknown) =
let ctr_name =
match constructor with
| { CAst.v = CRef (r,_); _ } -> string_of_qualid r
in
let instance_name = mk_instance_name class_name ctr_name in
(* type constructor *)
let coqTyCtr = gTyCtr ty_ctr in
(* parameters of the type constructor *)
let coqTyParams = List.map gTyParam ty_params in
(* Fully applied type constructor *)
let full_dt = gApp ~explicit:true coqTyCtr coqTyParams in
(* Type parameters as arguments *)
(* TODO: Needed?
let params = List.map (fun tp -> gArg ~assumName:(gTyParam tp)
~assumType:gType0
()) ty_params in
*)
(* List of input unknowns *)
let actual_input_list =
List.filter (fun u -> UM.find u umap == FixedInput) input_names in
(* Inputs as arguments *)
let actual_input_args =
List.map (fun u -> gArg ~assumName:(gVar u) ~assumType:(gType ty_params (UM.find u tmap)) ())
actual_input_list
in
(* Typeclass arguments - depends on the class *)
let param_class_names = match class_name with
| DecOpt -> ["Dec_Eq"; "Enum"]
| EnumSizedSuchThat -> ["Dec_Eq"; "Enum"]
| GenSizedSuchThat -> ["Dec_Eq"; "Gen"; "Enum"]
in
let typeclass_args =
List.concat (List.map (fun tp ->
((gArg ~assumName:tp ~assumImplicit:true ()) ::
(List.map (fun name -> gArg ~assumType:(gApp (gInject name) [tp])
~assumGeneralized:true ()) param_class_names))
) coqTyParams) in
(* The type we are generating for -- not the predicate! *)
let _full_gtyp = gType ty_params (UM.find result tmap) in
let _gen_needed = [] in
let _dec_needed = [] in
(* The dependent generator *)
let gen () =
arbitrarySizedST ty_ctr ty_params ctrs dep_type input_names
input_ranges umap tmap actual_input_args result coqTyCtr
in
(* Generate typeclass constraints. For each type parameter "A" we need `{_ : <Class Name> A} *)
(* TODO: Params? *)
let instance_arguments = match class_name with
| DecOpt -> (* params @ *) typeclass_args @ actual_input_args
| EnumSizedSuchThat ->
(* params @ *) typeclass_args @ actual_input_args
| GenSizedSuchThat ->
(* params @ *) typeclass_args
@ actual_input_args
in
(* Fully applied predicate (parameters and constructors) *)
let full_pred inputs =
match letbinds with
| None ->
gFun [Unknown.to_string result]
(fun _ -> gApp (full_dt) (List.map gVar inputs))
| Some letbinds ->
gFun [Unknown.to_string result]
(fun [result_var] ->
gLetTupleIn result_var letbinds (gApp (gInject ctr_name) (List.map gVar inputs)))
in
(* TODO: Easy solution : add Arbitrary/DecOpt as a requirement for all type parameters. *)
(*
let self_dec = [] in
(* (* Maybe somethign about type paramters here *)
if !need_dec then [gArg ~assumType:(gApp (gInject (Printf.sprintf "DepDec%n" (dep_type_len dep_type))) [gTyCtr ty_ctr])
~assumGeneralized:true ()]
else [] in
*)
(* The type of the dependent generator *)
let gen_type = gGen (gOption full_gtyp) in
(* Generate arbitrary parameters *)
let arb_needed =
let rec extract_params = function
| DTyParam tp -> ArbSet.singleton (DTyParam tp)
| DTyVar _ -> ArbSet.empty
| DTyCtr (_, dts) -> List.fold_left (fun acc dt -> ArbSet.union acc (extract_params dt)) ArbSet.empty dts
| _ -> failwith "Unhandled / arb_needed" in
let tps = ArbSet.fold (fun dt acc -> ArbSet.union acc (extract_params dt)) !arbitraries ArbSet.empty in
ArbSet.fold
(fun dt acc ->
(gArg ~assumType:(gApp (gInject "Arbitrary") [gType ty_params dt]) ~assumGeneralized:true ()) :: acc
) tps []
in
(* Generate typeclass constraints. For each type parameter "A" we need `{_ : <Class Name> A} *)
let instance_arguments = match cn with
| ArbitrarySizedSuchThat ->
params
@ gen_needed
@ dec_needed
@ self_dec
@ arb_needed
@ inputs
| GenSizedSuchThatMonotonicOpt -> params
| SizedProofEqs -> params @ inputs
| GenSizedSuchThatCorrect -> params @ inputs
| GenSizedSuchThatSizeMonotonicOpt -> params @ inputs
in
*)
(* The instance type *)
let instance_type iargs = match class_name with
| GenSizedSuchThat ->
gApp (gInject (derivable_to_string class_name))
[gType ty_params (UM.find result tmap);
full_pred input_names]
| EnumSizedSuchThat ->
gApp (gInject (derivable_to_string class_name))
[gType ty_params (UM.find result tmap);
full_pred input_names]
| DecOpt ->
gApp (gInject (derivable_to_string class_name))
[ gApp (full_dt) (List.map gVar input_names) ]
in
let instance_record iargs =
match class_name with
| GenSizedSuchThat -> gen ()
| EnumSizedSuchThat ->
enumSizedST ty_ctr ty_params ctrs dep_type input_names
input_ranges umap tmap actual_input_args result coqTyCtr
| DecOpt ->
checkerSizedST ty_ctr ty_params ctrs dep_type input_names
input_ranges umap tmap actual_input_args result coqTyCtr
in
msg_debug (str "Instance Type: " ++ fnl ());
debug_coq_expr (instance_type [gInject "input0"; gInject "input1"]);
declare_class_instance instance_arguments instance_name instance_type instance_record
;;
(* Creates the initial t and u maps. *)
let create_t_and_u_maps explicit_args dep_type actual_args : (range UM.t * dep_type UM.t) =
msg_debug (str ("create_t_u_maps for: " ^ dep_type_to_string dep_type) ++ fnl ());
(* Local references - the maps to be generated *)
let umap = ref UM.empty in
let tmap = ref explicit_args in
let rec populate_maps dep_type args =
(* Recurse down the unnamed arrow arguments *)
match dep_type,args with
| DProd ((_, dt1), dt2), arg::args'
| DArrow (dt1, dt2), arg::args' ->
msg_debug (str ("populating with: " ^ dep_type_to_string dt1) ++ fnl ());
begin match arg with
| ({ CAst.v = CRef (r,_); _ }, _) ->
begin
let current_r = Unknown.from_string (string_of_qualid r ^ "_") in
(* Lookup if the reference is meant to be generated *)
try begin match UM.find current_r !tmap with
| None ->
(* First occurence, update tmap and umap *)
tmap := UM.add current_r (Some dt1) !tmap;
umap := UM.add current_r (Undef dt1) !umap
| Some dt' ->
(* Check if the existing binding still typechecks *)
if not (dt1 == dt') then qcfail "Ill-typed application in derivation"
end
with Not_found ->
(* Logging the type in the tmap is ok, because we don't
update the umap in the "Some dt'" case above *)
tmap := UM.add current_r (Some dt1) !tmap;
umap := UM.add current_r FixedInput !umap;
end
(* TODO: If this is constructor applications, we need more type-checking machinery here *)
| _ -> qcfail "Non-variable patterns not implemented"
end;
populate_maps dt2 args'
(* Not an arrow -> Finalizer (TODO: add explicit fail?) *)
| _ -> ()
in
populate_maps dep_type actual_args;
(* Remove the option from the type map and ensure all are exercised *)
let tmap'=
UM.mapi (fun u mt ->
match mt with
| Some t -> t
| None -> failwith (Printf.sprintf "Pattern not exercised: %s\n" (var_to_string u))
) !tmap in
(!umap, tmap')
(* Assumption:
- generator-based classes include a "fun x => P ...." or "fun x => let (x1,x2,...) := x in P ..."
where "..." are bound names (to be generated), unbound names (implicitly quantified arguments)
or Constructors applied to such stuff.
- checker-based classes only include the name of the predicate "P". All arguments to P will
be considered Fixed inputs
*)
let dep_dispatch ind class_name : unit =
match ind with
#if COQ_VERSION >= (8, 20, 0)
| { CAst.v = CLambdaN ([CLocalAssum ([{ CAst.v = Names.Name id; CAst.loc = _loc2 }], _, _kind, _type)], body); _ } -> (* {CAst.v = CApp ((_flag, constructor), args) }) } -> *)
#else
| { CAst.v = CLambdaN ([CLocalAssum ([{ CAst.v = Names.Name id; CAst.loc = _loc2 }], _kind, _type)], body); _ } -> (* {CAst.v = CApp ((_flag, constructor), args) }) } -> *)
#endif
let idu = Unknown.from_string (Names.Id.to_string id ^ "_") in
(* Extract (x1,x2,...) if any, P and arguments *)
let (letbindsM, constructor, args) =
match body with
| { CAst.v = CApp (constructor, args); _ } -> (None, constructor, args)
| { CAst.v = CLetTuple (name_list, _,
_shouldbeid,
{ CAst.v = CApp (constructor, args); _ } ); _} ->
( Some (List.map (function { CAst.v = name; _ } -> name ) name_list), constructor, args )
in
(* Parse the constructor's information into the more convenient generic-lib representation *)
(* let (ty_ctr, ty_params, ctrs, dep_type) : (ty_ctr * (ty_param list) * (dep_ctr list) * dep_type) = *)
let dt : (ty_ctr * (ty_param list) * (dep_ctr list) * dep_type) =
match coerce_reference_to_dep_dt constructor with
| Some dt -> msg_debug (str (dep_dt_to_string dt) ++ fnl()); dt
| None -> failwith "Not supported type"
in
let (ty_ctr, ty_params, ctrs, dep_type) = dt in
let (letbinds, init_umap, init_tmap) : (unknown list option * range UM.t * dep_type UM.t) =
(* Create a temporary typing map for either the let binds/variable to be generated *)
let letbinds =
match letbindsM with
| Some binds -> Some (List.map (fun (Names.Name id) -> Unknown.from_string (Names.Id.to_string id ^ "_")) binds)
| None -> None
in
let explicit_args =
match letbinds with
| Some binds ->
List.fold_left (fun map u -> UM.add u None map) UM.empty binds
| None -> UM.singleton idu None
in
(* Call the actual creation function *)
let (umap, tmap) = create_t_and_u_maps explicit_args dep_type args in
(* Update with the toplevel variable as necessary *)
match letbinds with
| Some binds ->
(* Still need to package together the tuple *)
let bind_types = List.map (fun u ->
try UM.find u tmap
with Not_found -> failwith "All patterns should be exercised"
) binds
in
let tmap' = UM.add idu (dtTupleType bind_types) tmap in
let umap' =
let pair_ctr = injectCtr "Coq.Init.Datatypes.pair" in
let range = listToPairAux (fun (r1, r2) -> Ctr (pair_ctr, [RangeHole; RangeHole; r1; r2])) (List.map (fun u -> Unknown u) binds) in
UM.add idu range umap in
(letbinds, umap', tmap')
| None -> (letbinds, umap, tmap)
in
(* Print map *)
msg_debug (str "Initial map: " ++ fnl ());
UM.iter (fun x r -> msg_debug (str ("Bound: " ^ (var_to_string x) ^ " to Range: " ^ (range_to_string r)) ++ fnl ())) init_umap;
let umap = ref init_umap in
let tmap = ref init_tmap in
(* Rewrite the function applications in constructors. *)
let rewrite_ct ct =
let new_eqs = ref [] in
(* Check if a datatype contains an application *)
let rec contains_app dt =
match dt with
| DApp _ -> true
| DCtr (ctr, dts) -> List.exists contains_app dts
| _ -> false in
(* Rewrite the datatypes *)
let rec traverse_and_rewrite dts top_dt : dep_type list =
msg_debug (str (String.concat " " (List.map dep_type_to_string dts) ^ " vs " ^ (dep_type_to_string top_dt)) ++ fnl ());
match dts, top_dt with
| (DTyParam _)::dts', _ -> traverse_and_rewrite dts' top_dt
| [], _ -> []
| dt::dts', DArrow (dt1,dt2) when not (contains_app dt) ->
dt :: (traverse_and_rewrite dts' dt2)
| dt::dts', DProd ((_,dt1),dt2) when not (contains_app dt) ->
dt :: (traverse_and_rewrite dts' dt2)
| dt::dts', DProd ((_,dt1),dt2) when (contains_app dt) ->
begin
(* Create a fresh name *)
let x = make_up_name () in
new_eqs := (dt, x, dt1) :: !new_eqs;
(* tmap := UM.add x dt1 !tmap;
umap := UM.add x (Undef dt1) !umap; *)
DTyVar x :: (traverse_and_rewrite dts' dt2)
end
| dt::dts', DArrow (dt1,dt2) when (contains_app dt) ->
begin
(* Create a fresh name *)
let x = make_up_name () in
new_eqs := (dt, x, dt1) :: !new_eqs;
(* tmap := UM.add x dt1 !tmap;
umap := UM.add x (Undef dt1) !umap; *)
DTyVar x :: (traverse_and_rewrite dts' dt2)
end
| _, _ -> failwith (String.concat " " (List.map dep_type_to_string dts) ^ " vs " ^ (dep_type_to_string top_dt))
in
let rec construct_eqs eqs dt =
match eqs with
| [] -> dt
| (dteq, x, dtx)::eqs' ->
DArrow (DTyCtr (ctr_to_ty_ctr (injectCtr "eq"), [DHole; dteq; DTyVar x]),
construct_eqs eqs' dt)
in
(* - Find the result of the constructor
- Traverse its arguments, rewriting if necessary
*)
let rec recurse_to_result ct =
match ct with
| DProd ((x, ct1), ct2) ->
DProd ((x, ct1), recurse_to_result ct2)
| DArrow (ct1, ct2) ->
DArrow (ct1, recurse_to_result ct2)
| DTyCtr (ty_ctr, dts) ->
(* TODO: While recursing dts need top level dep-type for map. *)
let dts' = traverse_and_rewrite dts dep_type in
construct_eqs !new_eqs (DTyCtr (ty_ctr, dts'))
| _ -> failwith ("Not a result: " ^ dep_type_to_string ct)
in
let rec add_bindings ct eqs =
msg_debug (str "Adding bindings..." ++ fnl ());
match eqs with
| [] -> ct
| (_,x,dt)::eqs' -> DProd ((x,dt), add_bindings ct eqs')
in
let rewritten_result = recurse_to_result ct in
let rewritten = add_bindings rewritten_result !new_eqs in
msg_debug (str ("Rewritten from: " ^ dep_type_to_string ct ^ " to " ^ dep_type_to_string rewritten) ++ fnl ());
rewritten
in
let ctrs = List.map (fun (ctr, ct) -> (ctr, rewrite_ct ct)) ctrs in
(* When we add constructors to the ranges, this needs to change *)
let input_names = List.map (fun ({CAst.v = CRef (r, _); _},_) -> fresh_name (string_of_qualid r ^ "_")) args in
let input_ranges = List.map (fun v -> Unknown v) input_names in
(* Call the derivation dispatcher *)
derive_dependent class_name constructor !umap !tmap (* init_umap init_tmap *)
input_names input_ranges
(ty_ctr, ty_params, ctrs, dep_type) letbinds idu
| { CAst.v = CApp (constructor, args); _ } ->
msg_debug (str "Parsing constructor information for checker" ++ fnl ());
(* Parse the constructor's information into the more convenient generic-lib representation *)
let (ty_ctr, ty_params, ctrs, dep_type) : (ty_ctr * (ty_param list) * (dep_ctr list) * dep_type) =
match coerce_reference_to_dep_dt constructor with
| Some dt -> msg_debug (str (dep_dt_to_string dt) ++ fnl()); dt
| None -> failwith "Not supported type"
in
(* When we add constructors to the ranges, this needs to change *)
let input_names = List.map (fun ({CAst.v = CRef (r, _); _},_) -> fresh_name (string_of_qualid r ^ "_")) args in
let input_ranges = List.map (fun v -> Unknown v) input_names in
(* Call the actual creation function *)
let explicit_args = UM.empty (* No arguments to be generated *) in
let (umap, tmap) = create_t_and_u_maps explicit_args dep_type args in
let result = fresh_name "_result_bool" in
let umap = ref (UM.add result (Ctr (injectCtr "Coq.Init.Datatypes.true", [])) umap) in
let tmap = ref (UM.add result (DTyCtr (ctr_to_ty_ctr (injectCtr "Coq.Init.Datatypes.bool"), [])) tmap) in
(* let umap = ref init_umap in
let tmap = ref init_tmap in *)
(* Rewrite the function applications in constructors. *)
let rewrite_ct ct =
let new_eqs = ref [] in
(* Check if a datatype contains an application *)
let rec contains_app dt =
match dt with
| DApp _ -> true
| DCtr (ctr, dts) -> List.exists contains_app dts
| _ -> false in
(* Rewrite the datatypes *)
let rec traverse_and_rewrite dts top_dt : dep_type list =
match dts, top_dt with
| (DTyParam _)::dts', _ -> traverse_and_rewrite dts' top_dt
| [], _ -> []
| dt::dts', DArrow (dt1,dt2) when not (contains_app dt) ->
dt :: (traverse_and_rewrite dts' dt2)
| dt::dts', DProd ((_,dt1),dt2) when not (contains_app dt) ->
dt :: (traverse_and_rewrite dts' dt2)
| dt::dts', DProd ((_,dt1),dt2) when (contains_app dt) ->
begin
(* Create a fresh name *)
let x = make_up_name () in
new_eqs := (dt, x, dt1) :: !new_eqs;
(* tmap := UM.add x dt1 !tmap;
umap := UM.add x (Undef dt1) !umap; *)
DTyVar x :: (traverse_and_rewrite dts' dt2)
end
| dt::dts', DArrow (dt1,dt2) when (contains_app dt) ->
begin
(* Create a fresh name *)
let x = make_up_name () in
new_eqs := (dt, x, dt1) :: !new_eqs;
(* tmap := UM.add x dt1 !tmap;
umap := UM.add x (Undef dt1) !umap; *)
DTyVar x :: (traverse_and_rewrite dts' dt2)
end
in
let rec construct_eqs eqs dt =
match eqs with
| [] -> dt
| (dteq, x, _)::eqs' ->
DArrow (DTyCtr (ctr_to_ty_ctr (injectCtr "eq"), [DHole; dteq; DTyVar x]),
construct_eqs eqs' dt)
in
(* - Find the result of the constructor
- Traverse its arguments, rewriting if necessary
*)
let rec recurse_to_result ct =
match ct with
| DProd ((x, ct1), ct2) ->
DProd ((x, ct1), recurse_to_result ct2)
| DArrow (ct1, ct2) ->
DArrow (ct1, recurse_to_result ct2)
| DTyCtr (ty_ctr, dts) ->
(* TODO: While recursing dts need top level dep-type for map. *)
let dts' = traverse_and_rewrite dts dep_type in
construct_eqs !new_eqs (DTyCtr (ty_ctr, dts'))
| _ -> failwith ("Not a result: " ^ dep_type_to_string ct)
in
let rec add_bindings ct eqs =
msg_debug (str "Adding bindings..." ++ fnl ());
match eqs with
| [] -> ct
| (_,x,dt)::eqs' -> DProd ((x,dt), add_bindings ct eqs')
in
let rewritten_result = recurse_to_result ct in
let rewritten = add_bindings rewritten_result !new_eqs in
msg_debug (str ("Rewritten from: " ^ dep_type_to_string ct ^ " to " ^ dep_type_to_string rewritten) ++ fnl ());
rewritten
in
let ctrs = List.map (fun (ctr, ct) -> (ctr, rewrite_ct ct)) ctrs in
derive_dependent class_name constructor !umap !tmap input_names input_ranges
(ty_ctr, ty_params, ctrs, dep_type) None result
| _ -> qcfail "wrongformat/driver.mlg"
|