package ppx_deriving_madcast
Library deriving cast functions based on their types
Install
Dune Dependency
Authors
Maintainers
Sources
v0.2.tar.gz
md5=bca83dbf5a6fb47ced07756c6cab1d9a
sha512=f2f604685649d8fc0c750adfb89d997ea62f2fd09f383dc38b4580fdbb7f96e45a637745f1f06aa1b529d6abcfea6691ef309eb32e423c780c2370aff4678950
doc/src/ppx_deriving_madcast.api/madcast.ml.html
Source file madcast.ml
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
open Ppxlib open Parsetree open Asttypes open Longident open Ast_helper open Location let mkpatvar i = Pat.var (Parsetree_utils.mknoloc ("c"^(string_of_int i))) let mkident i = Exp.ident (Parsetree_utils.mknoloc (Lident ("c"^(string_of_int i)))) (* FIXME: proper location *) let loc = Location.none (* ============================= [ Base types ] ============================= *) let () = [ ( "bool -> float", [%type: bool], [%type: float], [%expr function | false -> 0. | true -> 1.] ); ( "bool -> int", [%type: bool], [%type: int], [%expr function | false -> 0 | true -> 1] ); ( "bool -> string", [%type: bool], [%type: string], [%expr string_of_bool] ); ( "char -> int", [%type: char], [%type: int], [%expr int_of_char] ); ( "char -> string" , [%type: char], [%type: string], [%expr String.make 1] ); ( "float -> string", [%type: float], [%type: string], [%expr string_of_float] ); ( "int -> bool", [%type: int], [%type: bool], [%expr function | 0 -> false | 1 -> true | _ -> failwith "madcast: int -> bool"] ); ( "int -> char", [%type: int], [%type: char], [%expr fun i -> try char_of_int i with Failure _ -> failwith "madcast: int -> char"] ); ( "int -> float", [%type: int], [%type: float], [%expr float_of_int] ); ( "int -> string", [%type: int], [%type: string], [%expr string_of_int] ); ( "string -> bool", [%type: string], [%type: bool], [%expr fun s -> try bool_of_string s with Failure _ -> failwith "madcast: string -> bool"] ); ( "string -> char", [%type: string], [%type: char], [%expr fun s -> if String.length s = 1 then s.[0] else failwith "madcast: string -> char"] ); ( "string -> float", [%type: string], [%type: float], [%expr fun s -> try float_of_string s with Failure _ -> failwith "madcast: string -> float"] ); ( "string -> int", [%type: string], [%type: int], [%expr fun s -> try int_of_string s with Failure _ -> failwith "madcast: string -> int"] ) ] |> List.iter (fun (name, itype, otype, expr) -> let matcher (itype', otype') = if Parsetree_utils.equal_core_type itype itype' && Parsetree_utils.equal_core_type otype otype' then Some [] else None in let builder casts = assert (casts = []); expr in RuleSet.register (Rule.make ~name ~matcher ~builder ())) (* ============================== [ Options ] =============================== *) let () = let name = "'a option -> 'b option" in let matcher = function | [%type: [%t? itype] option], [%type: [%t? otype] option] -> Some [itype, otype] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr function | None -> None | Some x -> Some ([%e List.hd casts] x)] in RuleSet.register (Rule.make ~name ~matcher ~builder ()) let () = let name = "'a -> 'b option" in let matcher = function | itype, [%type: [%t? otype] option] -> Some [itype, otype] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr fun x -> Some ([%e List.hd casts] x)] in RuleSet.(register ~applies_after:[lookup "'a option -> 'b option"] (Rule.make ~name ~matcher ~builder ())) let () = let name = "'a option -> 'b" in let matcher = function | [%type: [%t? itype] option], otype -> Some [itype, otype] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr function | None -> failwith "madcast: 'a option -> 'b" | Some x -> [%e List.hd casts] x] in RuleSet.(register ~applies_after:[lookup "'a option -> 'b option"] (Rule.make ~name ~matcher ~builder ())) (* =============================== [ Arrays ] =============================== *) let () = let name = "'a array -> 'b array" in let matcher = function | [%type: [%t? itype] array], [%type: [%t? otype] array] -> Some [itype, otype] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr Array.map [%e List.hd casts]] in RuleSet.register (Rule.make ~name ~matcher ~builder ()) let () = let name = "'a -> 'b array" in let matcher = function | itype, [%type: [%t? otype] array] -> Some [itype, otype] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr fun x -> [|[%e List.hd casts] x|]] in RuleSet.(register ~applies_after:[lookup "'a array -> 'b array"] (Rule.make ~name ~matcher ~builder ())) let () = let name = "'a array -> 'b" in let matcher = function | [%type: [%t? itype] array], otype -> Some [itype, otype] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr fun a -> if Array.length a = 1 then [%e List.hd casts] a.(0) else failwith "madcast: 'a array -> 'b"] in RuleSet.(register ~applies_after:[lookup "'a array -> 'b array"; lookup "'a -> 'b array"] (Rule.make ~name ~matcher ~builder ())) let () = let name = "<tuple> -> 'b array" in let matcher = function | {ptyp_desc=Ptyp_tuple itypes; _}, [%type: [%t? otype] array] -> Some (List.map (fun itype -> (itype, otype)) itypes) | _ -> None in let builder casts = (* fun (c0,...ck) -> [|cast0 c0; ... castk ck|] *) Exp.fun_ Nolabel None (Pat.tuple (List.mapi (fun i _ -> mkpatvar i) casts)) (Exp.array (List.mapi (fun i cast -> Exp.apply cast [Nolabel, mkident i]) casts)) in RuleSet.register (Rule.make ~name ~matcher ~builder ()) let () = let name = "'a array -> <tuple>" in let matcher = function | [%type: [%t? itype] array], {ptyp_desc=Ptyp_tuple otypes; _} -> Some (List.map (fun otype -> (itype, otype)) otypes) | _ -> None in let builder casts = (* function | [|c0;...ck|] -> (cast0 c0, ... castk ck) | _ -> failwith ... *) Exp.function_ [ Exp.case (Pat.array (List.mapi (fun i _ -> mkpatvar i) casts)) (Exp.tuple (List.mapi (fun i cast -> Exp.apply cast [Nolabel, mkident i]) casts)) ; Exp.case (Pat.any ()) [%expr failwith "madcast: 'a array -> <tuple>"] ] in RuleSet.register (Rule.make ~name ~matcher ~builder ()) let () = let name = "<tuple> array -> 'a array" in let matcher = function | [%type: [%t? {ptyp_desc=Ptyp_tuple itypes; _}] array], [%type: [%t? otype] array] -> Some [Typ.tuple itypes, [%type: [%t otype] array]] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr fun a -> Array.map [%e List.hd casts] a |> Array.to_list |> Array.concat] in RuleSet.(register ~applies_before:[lookup "'a -> 'b array"; lookup "'a array -> 'b"] (Rule.make ~name ~matcher ~builder ())) let () = let name = "'a array -> <tuple> array" in let matcher = function | [%type: [%t? itype] array], [%type: [%t? {ptyp_desc=Ptyp_tuple otypes; _}] array] -> Some (List.map (fun otype -> (itype, otype)) otypes) | _ -> None in let builder casts = let l = List.length casts in let exp_int n = Exp.constant (Const.int n) in [%expr fun a -> if Array.length a mod [%e exp_int l] <> 0 then failwith "madcast: 'a array -> <tuple> array" else Array.init (Array.length a / [%e exp_int l]) (fun i -> [%e Exp.tuple (List.mapi (fun j cast -> [%expr [%e cast] a.([%e exp_int j] + i * [%e exp_int l])]) casts)])] in RuleSet.(register ~applies_before:[lookup "'a -> 'b array"; lookup "'a array -> 'b"] ~applies_after:[lookup "<tuple> array -> 'a array"] (Rule.make ~name ~matcher ~builder ())) (* =============================== [ Lists ] ================================ *) (* using the rules for arrays *) let () = let name = "'a list -> 'a array -> 'b" in let matcher = function | [%type: [%t? itype] list], otype -> Some [[%type: [%t itype] array], otype] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr fun l -> Array.of_list l |> [%e List.hd casts]] in RuleSet.register (Rule.make ~name ~matcher ~builder ()) let () = let name = "'a -> 'b array -> 'b list" in let matcher = function | itype, [%type: [%t? otype] list] -> Some [itype, [%type: [%t otype] array]] | _ -> None in let builder casts = assert (List.length casts = 1); [%expr fun x -> [%e List.hd casts] x |> Array.to_list] in RuleSet.register (Rule.make ~name ~matcher ~builder ()) (* =============================== [ Tuples ] =============================== *) let () = let name = "<tuple> -> <tuple>" in let matcher = function | {ptyp_desc=Ptyp_tuple itypes; _} , {ptyp_desc=Ptyp_tuple otypes; _} when List.length itypes = List.length otypes -> Some (List.combine itypes otypes) | _ -> None in let builder casts = (* fun (c0,...ck) -> (cast0 c0, ... castk ck) *) Exp.fun_ Nolabel None (Pat.tuple (List.mapi (fun i _ -> mkpatvar i) casts)) (Exp.tuple (List.mapi (fun i cast -> Exp.apply cast [Nolabel, mkident i]) casts)) in RuleSet.register (Rule.make ~name ~matcher ~builder ()) (* ============================= [ Functions ] ============================== *) let () = let name = "('a -> 'b) -> ('c -> 'd)" in let matcher = function | [%type: [%t? iitype] -> [%t? iotype]], [%type: [%t? oitype] -> [%t? ootype]] -> Some [(oitype, iitype); (iotype, ootype)] | _ -> None in let builder = function | [icast; ocast] -> [%expr fun f x -> x |> [%e icast] |> f |> [%e ocast]] | _ -> assert false in RuleSet.register (Rule.make ~name ~matcher ~builder ()) let () = let name = "currying" in let matcher (itype, otype) = match itype with | [%type: [%t? {ptyp_desc=Ptyp_tuple iitypes; _}] -> [%t? iotype]] -> ( let rec matcher = function | ([], ootype) -> [(iotype, ootype)] (* this is the right order *) | (iitype :: iitypes, [%type: [%t? oitype] -> [%t? ootype]]) -> (oitype, iitype) :: matcher (iitypes, ootype) | _ -> failwith "matcher" in try Some (matcher (iitypes, otype)) with Failure _ -> None ) | _ -> None in let builder casts = let ocast = ExtList.ft casts in let icasts = ExtList.bd casts in [%expr fun f -> [%e ExtList.foldi_right (* imbricated functions *) (fun i _ exp -> Exp.fun_ Nolabel None (mkpatvar i) exp) icasts ( (* the body of the function *) Exp.apply ocast [Nolabel, Exp.apply [%expr f] [Nolabel, Exp.tuple ( List.mapi (fun i icast -> Exp.apply icast [Nolabel, mkident i]) icasts )]] )]] in RuleSet.register ~applies_after:[RuleSet.lookup "('a -> 'b) -> ('c -> 'd)"] (Rule.make ~name ~matcher ~builder ()) let () = let name = "uncurrying" in let matcher (itype, otype) = match otype with | [%type: [%t? {ptyp_desc=Ptyp_tuple oitypes; _}] -> [%t? ootype]] -> ( let rec matcher = function | (iotype, []) -> [(iotype, ootype)] (* this is the right order *) | ([%type: [%t? iitype] -> [%t? iotype]], oitype :: ootypes) -> (oitype, iitype) :: matcher (iotype, ootypes) | _ -> failwith "matcher" in try Some (matcher (itype, oitypes)) with Failure _ -> None ) | _ -> None in let builder casts = let ocast = ExtList.ft casts in let icasts = ExtList.bd casts in [%expr fun f -> [%e Exp.fun_ Nolabel None (Pat.tuple (List.mapi (fun i _ -> mkpatvar i) icasts)) (Exp.apply ocast [Nolabel, (Exp.apply [%expr f] (List.mapi (fun i icast -> (Nolabel, Exp.apply icast [Nolabel, mkident i])) icasts))])]] in RuleSet.register ~applies_after:[RuleSet.lookup "('a -> 'b) -> ('c -> 'd)"] (Rule.make ~name ~matcher ~builder ()) (* ======================= [ And now, the main loop ] ======================= *) let rec reverse_possibles = function (* changes a list of possibilities in possibilities of lists *) | [] -> [[]] | possible_heads :: tail_of_possibles -> List.map (fun possible_tail -> List.map (fun possible_head -> possible_head :: possible_tail) possible_heads) (reverse_possibles tail_of_possibles) |> List.flatten let rec find_caster (itype, otype) : Parsetree.expression list = RuleSet.fold_by_priority (fun rules -> function | [] -> (* Empty means that the stronger priorities have found nothing. We go through all the rules at our priority, apply them and see which ones did succeed. *) List.fold_left (fun casts rule -> match Rule.match_ rule (itype, otype) with | None -> (* the rule found nothing *) casts | Some premises -> ( List.map find_caster premises |> reverse_possibles |> List.map (fun premises -> Rule.build_ rule premises) ) @ casts) [] rules | _ as casts -> (* Non-empty means that the previous priorities have found something already, so we let that and do nothing. *) casts) [] (* ============================== [ Frontend ] ============================== *) exception NoCastFound exception SeveralCastsFound let find_caster itype otype = match find_caster (itype, otype) with | [cast] -> cast | [] -> raise NoCastFound | _ -> raise SeveralCastsFound let split_arrow = function | [%type: [%t? itype] -> [%t? otype]] -> itype, otype | _ -> invalid_arg "split_arrow" let annotate expr ty = [%expr let (cast : [%t ty]) = [%e expr] in cast] let derive ty = (* We ask [find_caster] to derive expressions for ty = itype -> otype. We then annotate them with that type where type variables are universally quantified. Since this can syntactically only happen in a let, we return something like: let cast : [vars]. [itype -> otype] = [expr] in cast *) let loc = ty.Parsetree.ptyp_loc in try let itype, otype = split_arrow ty in let cast = find_caster itype otype in annotate cast (Parsetree_utils.universal_closure_of_core_type ty) with | Invalid_argument msg when msg = "split_arrow" -> Ppx_deriving.(raise_errorf ~loc "Expected an arrow type, got %s" (string_of_core_type ty)) | NoCastFound -> Ppx_deriving.(raise_errorf ~loc "No cast found for %s" (string_of_core_type ty)) | SeveralCastsFound -> Ppx_deriving.(raise_errorf ~loc "Several casts found for %s" (string_of_core_type ty))
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>