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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
(******************************************************************************
* Fieldslib *
* *
* Copyright (C) 2008- Jane Street Holding, LLC *
* Contact: opensource@janestreet.com *
* WWW: http://www.janestreet.com/ocaml *
* *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* *
******************************************************************************)
module List = ListLabels
open Printf
open Camlp4.PreCast
open Pa_type_conv
(** Utility functions to construct and deconstruct camlp4 asts *)
module Create = struct
let record _loc bindings =
let bindings =
List.map bindings ~f:(fun (n, v) ->
<:rec_binding< $lid:n$ = $v$ >> ) in
<:expr< { $list:bindings$ } >>
;;
let lambda _loc patterns body =
List.fold_right patterns ~init:body ~f:(fun pattern acc ->
<:expr< fun [ $pattern$ -> $acc$ ] >> )
;;
let lambda_sig _loc arg_tys body_ty =
List.fold_right arg_tys ~init:body_ty ~f:(fun arg_ty acc ->
<:ctyp< $arg_ty$ -> $acc$ >> )
;;
end
module Inspect = struct
let field = function
| <:ctyp< $lid:name$ : mutable $field_ty$ >> -> (name, `Mutable, field_ty)
| <:ctyp< $lid:name$ : $field_ty$ >> -> (name, `Immutable, field_ty)
| _ -> assert false
;;
let string_of_ctyp ctyp =
let module PP = Camlp4.Printers.OCaml.Make (Syntax) in
let conv_ctyp = (new PP.printer ())#ctyp in
let buffer = Buffer.create 32 in
Format.bprintf buffer "%a%!" conv_ctyp ctyp;
let s = Buffer.contents buffer in
s
;;
let get_field_name fld =
let name, _, _ = field fld in
name
let field_names ty = List.map ( Ast.list_of_ctyp ty [] ) ~f:get_field_name
let fields ty = List.map (Ast.list_of_ctyp ty []) ~f:field
end
let raise_unsupported () =
failwith "Unsupported use of fields (you can only use it on records)."
module Gen_sig = struct
let apply_type _loc ~ty_name ~tps =
List.fold_left tps
~init:<:ctyp< $lid:ty_name$ >>
~f:(fun acc tp -> <:ctyp< $acc$ $tp$ >>)
let label_arg _loc name ty = Ast.TyLab (_loc, name, ty)
let field_arg _loc ~record f = fun (name, _m, ty) ->
label_arg _loc name (
f ~field: <:ctyp< Fieldslib.Field.t $record$ $ty$ >> ~ty)
;;
let create_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty ->
let create_f = <:ctyp< 'input__ -> ( $ty$ ) >> in
<:ctyp< $field$ -> 'compile_acc__ -> ($create_f$ * 'compile_acc__) >>
) in
let types = List.map fields ~f in
let create_record_f = <:ctyp< 'input__ -> ($record$) >> in
let t = Create.lambda_sig _loc
(types @ [ <:ctyp< 'compile_acc__ >> ]) (<:ctyp< ( $create_record_f$ * 'compile_acc__ ) >>)
in
<:sig_item< value make_creator : $t$ >>
;;
let fold_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty:_ ->
<:ctyp< 'acc__ -> $field$ -> 'acc__ >>) in
let types = List.map fields ~f in
let init_ty = label_arg _loc "init" <:ctyp< 'acc__ >> in
let t = Create.lambda_sig _loc
(init_ty :: types) <:ctyp< 'acc__ >> in
<:sig_item< value fold : $t$ >>
;;
let simple_create_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f (name,_,ty) = label_arg _loc name ty in
let types = List.map fields ~f in
let t = Create.lambda_sig _loc
types record in
<:sig_item< value create : $t$ >>
;;
let bool_fun fun_name ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty:_ ->
<:ctyp< $field$ -> bool >> ) in
let types = List.map fields ~f in
let t = Create.lambda_sig _loc types <:ctyp< bool >> in
<:sig_item< value $lid:fun_name$ : $t$ >>
;;
let iter_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty:_ ->
<:ctyp< $field$ -> unit >>) in
let types = List.map fields ~f in
let t = Create.lambda_sig _loc types <:ctyp< unit >> in
<:sig_item< value iter : $t$ >>
;;
let direct_iter_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty:field_ty ->
<:ctyp< $field$ -> $record$ -> $field_ty$ -> unit >>) in
let types = List.map fields ~f in
let t = Create.lambda_sig _loc (record :: types) <:ctyp< unit >> in
<:sig_item< value iter : $t$ >>
;;
let direct_fold_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty:field_ty ->
<:ctyp< 'acc__ -> $field$ -> $record$ -> $field_ty$ -> 'acc__ >>) in
let types = List.map fields ~f in
let init_ty = label_arg _loc "init" <:ctyp< 'acc__ >> in
let t = Create.lambda_sig _loc
(record :: init_ty :: types) <:ctyp< 'acc__ >> in
<:sig_item< value fold : $t$ >>
;;
let to_list_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty:_ ->
<:ctyp< $field$ -> 'elem__ >>)
in
let types = List.map fields ~f in
let t = Create.lambda_sig _loc types <:ctyp< list 'elem__ >> in
<:sig_item< value to_list : $t$ >>
;;
let map_fun ~ty_name ~tps _loc ty =
let record = apply_type _loc ~ty_name ~tps in
let fields = Inspect.fields ty in
let f = field_arg _loc ~record (fun ~field ~ty ->
<:ctyp< $field$ -> $ty$ >>) in
let types = List.map fields ~f in
let t = Create.lambda_sig _loc (types) record in
<:sig_item< value map : $t$ >>
;;
let map_poly ~ty_name ~tps _loc _ =
let record = apply_type _loc ~ty_name ~tps in
let tps_names =
List.map
~f:(function
| <:ctyp< '$a$ >> -> a
| _ -> assert false)
tps
in
let fresh_variable =
let rec loop i =
let ret = sprintf "x%i" i in
if List.mem ret ~set:tps_names then
loop (i+1)
else
ret
in
<:ctyp<'$lid:loop 0$>>
in
let t =
<:ctyp< Fieldslib.Field.user $record$ $fresh_variable$ -> list $fresh_variable$ >>
in
<:sig_item< value map_poly : $t$ >>
;;
let record ~ty_name ~tps _loc ty =
let fields = Inspect.fields ty in
let record_ty = apply_type _loc ~ty_name ~tps in
let conv_field (res_getset, res_fields) (name, m, ty) =
let getter = <:sig_item< value $lid:name$ : $record_ty$ -> $ty$ >> in
let field =
<:sig_item< value $lid:name$ : Fieldslib.Field.t $record_ty$ $ty$ >>
in
match m with
| `Immutable ->
( <:sig_item< $getter$ ; $res_getset$ >> ,
<:sig_item< $field$ ; $res_fields$ >>
)
| `Mutable ->
let setter =
<:sig_item< value $lid:"set_" ^ name$ : $record_ty$ -> $ty$ -> unit >> in
( <:sig_item< $getter$ ; $setter$ ; $res_getset$ >> ,
<:sig_item< $field$ ; $res_fields$ >>
)
in
let getters_and_setters, fields =
List.fold_left fields ~init:(<:sig_item<>>, <:sig_item<>>) ~f:conv_field
in
let create_fun = create_fun ~ty_name ~tps _loc ty in
let simple_create_fun = simple_create_fun ~ty_name ~tps _loc ty in
if ty_name = "t" then
let iter = iter_fun ~ty_name ~tps _loc ty in
let fold = fold_fun ~ty_name ~tps _loc ty in
let map = map_fun ~ty_name ~tps _loc ty in
let map_poly = map_poly ~ty_name ~tps _loc ty in
let and_f = bool_fun "for_all" ~ty_name ~tps _loc ty in
let or_f = bool_fun "exists" ~ty_name ~tps _loc ty in
let to_list = to_list_fun ~ty_name ~tps _loc ty in
let direct_iter = direct_iter_fun ~ty_name ~tps _loc ty in
let direct_fold = direct_fold_fun ~ty_name ~tps _loc ty in
<:sig_item< $getters_and_setters$ ;
module Fields : sig
value names : list string ;
$fields$ ;
$fold$ ;
$create_fun$ ; $simple_create_fun$ ; $iter$ ; $map$ ; $map_poly$ ; $and_f$ ; $or_f$ ; $to_list$ ;
module Direct : sig
$direct_iter$ ;
$direct_fold$ ;
end ;
end
>>
else
<:sig_item< $getters_and_setters$ ;
module Fields : sig
$fields$
end
>>
;;
let fields_of_ty_sig _loc ~ty_name ~tps ~rhs =
let unsupported = (fun _ _ -> raise_unsupported ()) in
Gen.switch_tp_def
~alias:unsupported
~sum:unsupported
~variants:unsupported
~mani:unsupported
~nil:(fun _ -> raise_unsupported ())
~record:(record ~ty_name ~tps)
rhs
let generate = function
| Ast.TyDcl (_loc, ty_name, tps, rhs, _) -> fields_of_ty_sig _loc ~ty_name ~tps ~rhs
| Ast.TyAnd (_loc, _, _) as tds ->
ignore (_loc, tds);
failwith "Not supported"
| _ -> assert false
end
module Gen_struct = struct
let fields _loc ty =
let fields = Inspect.fields ty in
let rec_id =
match fields with
| [_] -> Ast.ExNil _loc
| _ -> Ast.ExId (_loc, Ast.IdLid (_loc, "_r__"))
in
let conv_field (res_getset, res_fields) (name, m, field_ty) =
let getter = <:str_item< value $lid:name$ _r__ = _r__.$lid:name$ >> in
let setter, setter_field =
match m with
| `Mutable ->
let setter =
<:str_item<
value $lid:"set_" ^ name$ _r__ v__ = _r__.$lid:name$ := v__
>>
in
let setter_field = <:expr< Some $lid:"set_" ^ name$ >> in
setter, setter_field
| `Immutable -> <:str_item< >>, <:expr< None >>
in
let field =
let e =
Ast.ExRec (_loc, Ast.RbEq (_loc,
Ast.IdLid (_loc, name),
Ast.ExId (_loc, Ast.IdLid (_loc, "v__"))),
rec_id)
in
let fset = <:expr< fun _r__ v__ -> $e$ >> in
<:str_item<
value $lid:name$ =
( { Fieldslib.Field.
name = $str:name$;
getter = $lid:name$;
setter = $setter_field$;
fset = $fset$;
} : Fieldslib.Field.t _ $field_ty$ )
>>
in
( <:str_item< $getter$ ; $setter$ ; $res_getset$ >>,
<:str_item< $field$ ; $res_fields$ >> )
in
List.fold_left fields ~init:(<:str_item<>>, <:str_item<>>) ~f:conv_field
;;
let label_arg ?label _loc name =
let l =
match label with
| None -> name
| Some n -> n in
Ast.PaLab (_loc, l, <:patt< $lid:name$ >> )
;;
let label_arg_fun _loc name =
label_arg ~label:name _loc (name ^ "_fun__")
;;
let creation_fun _loc _record_name ty =
let names = Inspect.field_names ty in
let f =
(* create creation function *)
let body_record = Create.record _loc
(List.map names ~f:(fun n -> (n, <:expr< $lid:n$ >> ))) in
let body =
List.fold_right names
~init: <:expr< ( $body_record$ ) >>
~f: (fun field_name acc ->
<:expr<
let $lid:field_name$ =
$lid:field_name ^ "_gen__"$ acc__
in
$acc$
>> )
in
Create.lambda _loc [ <:patt< acc__ >> ] body
in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let body0 = <:expr< ($f$, compile_acc__) >> in
let body =
List.fold_right names
~init: body0
~f: (fun field_name acc ->
<:expr<
let ($lid:field_name ^ "_gen__"$, compile_acc__) =
$lid:field_name ^ "_fun__"$ $lid:field_name$ compile_acc__
in
$acc$
>>)
in
let f = Create.lambda _loc (patterns @ [ <:patt< compile_acc__ >> ]) body in
<:str_item< value make_creator = $f$ >>
;;
let simple_creation_fun _loc _record_name ty =
let names = Inspect.field_names ty in
let f =
Create.record _loc
(List.map names ~f:(fun n -> (n, <:expr< $lid:n$ >> )))
in
let patterns = List.map names ~f:(fun x -> label_arg _loc x ) in
let f = Create.lambda _loc patterns f in
<:str_item< value create = $f$ >>
;;
let fold_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let field_fold acc_expr field_name =
<:expr< $lid:field_name ^ "_fun__" $ $acc_expr$ $lid:field_name$ >> in
let body =
List.fold_left names ~init:<:expr< init__ >> ~f:field_fold in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let init = label_arg ~label:"init" _loc "init__" in
let lambda = Create.lambda _loc
( init :: patterns ) body in
<:str_item< value fold = $lambda$ >>
;;
let and_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let field_fold acc_expr field_name =
<:expr< $acc_expr$ && $lid:field_name ^ "_fun__"$ $lid:field_name$ >> in
let body =
List.fold_left names ~init:<:expr< True >> ~f:field_fold in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let lambda = Create.lambda _loc patterns body in
<:str_item< value for_all = $lambda$ >>
;;
let or_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let field_fold acc_expr field_name =
<:expr< $acc_expr$ || $lid:field_name ^ "_fun__"$ $lid:field_name$ >> in
let body =
List.fold_left names ~init:<:expr< False >> ~f:field_fold in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let lambda = Create.lambda _loc patterns body in
<:str_item< value exists = $lambda$ >>
;;
let iter_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let iter_field field_name =
<:expr< $lid:field_name ^ "_fun__" $ $lid:field_name$ >> in
let body =
List.fold_left (List.tl names)
~init:(iter_field (List.hd names))
~f:(fun acc n -> <:expr< ( $acc$ ; $iter_field n$ ) >>) in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let lambda = Create.lambda _loc
(patterns) body in
<:str_item< value iter = $lambda$ >>
;;
let direct_iter_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let iter_field field_name =
<:expr< $lid:field_name ^ "_fun__" $
$lid:field_name$ record__ record__.$lid:field_name$
>>
in
let body =
List.fold_left (List.tl names)
~init:(iter_field (List.hd names))
~f:(fun acc n -> <:expr< ( $acc$ ; $iter_field n$ ) >>) in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let lambda = Create.lambda _loc ( <:patt< record__ >> :: patterns) body in
<:str_item< value iter = $lambda$ >>
;;
let direct_fold_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let field_fold acc_expr field_name =
<:expr< $lid:field_name ^ "_fun__" $
$acc_expr$ $lid:field_name$ record__ record__.$lid:field_name$
>>
in
let body =
List.fold_left names ~init:<:expr< init__ >> ~f:field_fold in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let init = label_arg ~label:"init" _loc "init__" in
let lambda = Create.lambda _loc
( <:patt< record__ >> :: init :: patterns ) body in
<:str_item< value fold = $lambda$ >>
;;
let map_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let body = Create.record _loc (List.map names ~f:(fun field_name ->
let e = <:expr< $lid:field_name ^ "_fun__"$ $lid:field_name$ >> in
(field_name, e ))) in
let f = Create.lambda _loc patterns body in
<:str_item< value map = $f$ >>
;;
let to_list_fun ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let patterns = List.map names ~f:(label_arg_fun _loc) in
let body =
List.fold_right names ~init:<:expr< [ ] >> ~f:(fun field_name tail ->
<:expr< [ $lid:field_name ^ "_fun__" $ $lid:field_name$ :: $tail$ ] >> )
in
let f = Create.lambda _loc patterns body in
<:str_item< value to_list = $f$ >>
;;
let map_poly ~record_name:_ _loc ty =
let names = Inspect.field_names ty in
let fold name acc =
<:expr< [ (record__.Fieldslib.Field.f $lid:name$) :: $acc$ ] >>
in
let body =
List.fold_right
names
~init:<:expr<[]>>
~f:fold
in
<:str_item< value map_poly record__ = $body$ >>
;;
let record ~record_name _loc ty =
let getter_and_setters, fields = fields _loc ty in
let create = creation_fun _loc record_name ty in
let simple_create = simple_creation_fun _loc record_name ty in
let names =
List.fold_right (Inspect.field_names ty) ~init:<:expr< [ ] >> ~f:(fun head tail ->
<:expr< [ $str:head$ :: $tail$ ] >>)
in
if record_name = "t" then
let iter = iter_fun ~record_name _loc ty in
let fold = fold_fun ~record_name _loc ty in
let map = map_fun ~record_name _loc ty in
let map_poly = map_poly ~record_name _loc ty in
let andf = and_fun ~record_name _loc ty in
let orf = or_fun ~record_name _loc ty in
let direct_iter = direct_iter_fun ~record_name _loc ty in
let direct_fold = direct_fold_fun ~record_name _loc ty in
let to_list = to_list_fun ~record_name _loc ty in
<:str_item<
$getter_and_setters$ ;
module Fields = struct
value names = $names$ ;
$fields$ ;
$create$ ; $simple_create$ ; $iter$ ; $fold$ ; $map$ ; $map_poly$ ; $andf$ ; $orf$ ; $to_list$ ;
module Direct = struct
$direct_iter$ ;
$direct_fold$ ;
end ;
end
>>
else
<:str_item<
$getter_and_setters$ ;
module Fields = struct
$fields$ ;
end
>>
;;
let mani ~record_name ty =
match ty with
| <:ctyp@loc< { $x$ } >> ->
record ~record_name loc x
| _ -> failwith "the right hand side of the manifest must be a record"
let fields_of_ty _loc ~record_name ~tps:_ ~rhs =
let unsupported = (fun _ _ -> raise_unsupported ()) in
Gen.switch_tp_def
~alias: unsupported
~sum: unsupported
~variants: unsupported
~mani: (fun (_:Loc.t) _tp1 tp2 -> mani ~record_name tp2)
~nil: (fun _ -> raise_unsupported ())
~record: (record ~record_name)
rhs
let generate = function
| Ast.TyDcl (_loc, name, tps, rhs, _) -> fields_of_ty _loc ~record_name:name ~tps ~rhs
| Ast.TyAnd (_loc, _, _) as tds ->
ignore (_loc, tds);
failwith "Not supported"
| _ -> assert false
end
let () = add_generator "fields" Gen_struct.generate
let () = add_sig_generator "fields" Gen_sig.generate
|