| 1 |
open! Base |
| 2 |
|
| 3 |
module Location : sig |
| 4 |
include Identifiable.S |
| 5 |
end = struct |
| 6 |
include String |
| 7 |
end |
| 8 |
|
| 9 |
module Uuid : sig |
| 10 |
include Identifiable.S |
| 11 |
end = struct |
| 12 |
include String |
| 13 |
end |
| 14 |
|
| 15 |
let eval_fail loc fmt = |
| 16 |
Printf.ksprintf (fun s -> failwith (Printf.sprintf !"%{Location}: %s" loc s)) fmt |
| 17 |
;; |
| 18 |
|
| 19 |
let equal_option equal a b = |
| 20 |
match a, b with |
| 21 |
| Some _, None | None, Some _ -> false |
| 22 |
| None, None -> true |
| 23 |
| Some x, Some y -> equal x y |
| 24 |
;; |
| 25 |
|
| 26 |
module Sorted_table : sig |
| 27 |
type 'a t [@@deriving compare, sexp] |
| 28 |
|
| 29 |
val create : Location.t -> eq:('a -> 'a -> bool) -> (string * 'a) list -> 'a t |
| 30 |
val expose : 'a t -> (string * 'a) list |
| 31 |
val map : 'a t -> f:('a -> 'b) -> 'b t |
| 32 |
end = struct |
| 33 |
type 'a t = { sorted : (string * 'a) list } [@@deriving compare, sexp] |
| 34 |
|
| 35 |
let merge_check_adjacent_dups |
| 36 |
: eq:('a -> 'a -> bool) -> (string * 'a) list |
| 37 |
-> [ `Ok of (string * 'a) list | `Mismatch of string ] |
| 38 |
= |
| 39 |
fun ~eq -> |
| 40 |
let rec loop acc ~last_key ~last_value = function |
| 41 |
| [] -> `Ok (List.rev acc) |
| 42 |
| (key, value) :: xs -> |
| 43 |
if String.(last_key = key) |
| 44 |
then |
| 45 |
if eq last_value value then loop acc ~last_key ~last_value xs else `Mismatch key |
| 46 |
else loop ((key, value) :: acc) ~last_key:key ~last_value:value xs |
| 47 |
in |
| 48 |
function |
| 49 |
| [] -> `Ok [] |
| 50 |
| (key, value) :: xs -> loop [ key, value ] ~last_key:key ~last_value:value xs |
| 51 |
;; |
| 52 |
|
| 53 |
let create loc ~eq xs = |
| 54 |
let sorted = List.sort ~compare:(fun (s1, _) (s2, _) -> String.compare s1 s2) xs in |
| 55 |
match merge_check_adjacent_dups ~eq sorted with |
| 56 |
| `Ok sorted -> { sorted } |
| 57 |
| `Mismatch s -> |
| 58 |
eval_fail loc "Different shapes for duplicated polymorphic constructor: `%s" s () |
| 59 |
;; |
| 60 |
|
| 61 |
let expose t = t.sorted |
| 62 |
let map t ~f = { sorted = List.map t.sorted ~f:(fun (k, v) -> k, f v) } |
| 63 |
end |
| 64 |
|
| 65 |
module Digest : sig |
| 66 |
type t = Md5_lib.t [@@deriving compare, sexp] |
| 67 |
|
| 68 |
val to_md5 : t -> Md5_lib.t |
| 69 |
val of_md5 : Md5_lib.t -> t |
| 70 |
val to_hex : t -> string |
| 71 |
val constructor : string -> t list -> t |
| 72 |
val list : t list -> t |
| 73 |
val pair : t -> t -> t |
| 74 |
val string : string -> t |
| 75 |
val uuid : Uuid.t -> t |
| 76 |
val int : int -> t |
| 77 |
val option : t option -> t |
| 78 |
end = struct |
| 79 |
include Md5_lib |
| 80 |
|
| 81 |
let to_md5 t = t |
| 82 |
let of_md5 t = t |
| 83 |
let sexp_of_t t = t |> to_hex |> sexp_of_string |
| 84 |
let t_of_sexp s = s |> string_of_sexp |> of_hex_exn |
| 85 |
let uuid u = string (Uuid.to_string u) |
| 86 |
let int x = string (Int.to_string x) |
| 87 |
let pair x y = string (to_binary x ^ to_binary y) |
| 88 |
let list l = string (String.concat ~sep:"" (List.map ~f:to_binary l)) |
| 89 |
let constructor s l = string (s ^ to_binary (list l)) |
| 90 |
|
| 91 |
let option = function |
| 92 |
| None -> constructor "none" [] |
| 93 |
| Some x -> constructor "some" [ x ] |
| 94 |
;; |
| 95 |
end |
| 96 |
|
| 97 |
module Canonical_exp_constructor = struct |
| 98 |
|
| 99 |
|
| 100 |
type 'a t = |
| 101 |
| Annotate of Uuid.t * 'a |
| 102 |
| Base of Uuid.t * 'a list |
| 103 |
| Tuple of 'a list |
| 104 |
| Record of (string * 'a) list |
| 105 |
| Variant of (string * 'a list) list |
| 106 |
|
| 107 |
| Poly_variant of 'a option Sorted_table.t |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
| Application of 'a * 'a list |
| 115 |
| Rec_app of int * 'a list |
| 116 |
| Var of int |
| 117 |
[@@deriving sexp, compare] |
| 118 |
|
| 119 |
let map x ~f = |
| 120 |
match x with |
| 121 |
| Annotate (u, x) -> Annotate (u, f x) |
| 122 |
| Base (s, xs) -> Base (s, List.map ~f xs) |
| 123 |
| Tuple xs -> Tuple (List.map ~f xs) |
| 124 |
| Record l -> Record (List.map l ~f:(fun (s, x) -> s, f x)) |
| 125 |
| Variant l -> Variant (List.map l ~f:(fun (s, xs) -> s, List.map ~f xs)) |
| 126 |
| Poly_variant t -> Poly_variant (Sorted_table.map t ~f:(Option.map ~f)) |
| 127 |
| Application (x, l) -> Application (f x, List.map ~f l) |
| 128 |
| Rec_app (t, l) -> Rec_app (t, List.map ~f l) |
| 129 |
| Var v -> Var v |
| 130 |
;; |
| 131 |
|
| 132 |
let to_string t = Sexp.to_string (sexp_of_t (fun _ -> Atom "...") t) |
| 133 |
end |
| 134 |
|
| 135 |
module Create_digest : sig |
| 136 |
|
| 137 |
|
| 138 |
val digest_layer : Digest.t Canonical_exp_constructor.t -> Digest.t |
| 139 |
end = struct |
| 140 |
let digest_layer = function |
| 141 |
| Canonical_exp_constructor.Annotate (u, x) -> |
| 142 |
Digest.constructor "annotate" [ Digest.uuid u; x ] |
| 143 |
| Base (u, l) -> Digest.constructor "base" [ Digest.uuid u; Digest.list l ] |
| 144 |
| Tuple l -> Digest.constructor "tuple" [ Digest.list l ] |
| 145 |
| Record l -> |
| 146 |
Digest.constructor |
| 147 |
"record" |
| 148 |
[ Digest.list (List.map l ~f:(fun (s, t) -> Digest.pair (Digest.string s) t)) ] |
| 149 |
| Variant l -> |
| 150 |
Digest.constructor |
| 151 |
"variant" |
| 152 |
[ Digest.list |
| 153 |
(List.map l ~f:(fun (s, l) -> Digest.pair (Digest.string s) (Digest.list l))) |
| 154 |
] |
| 155 |
| Poly_variant table -> |
| 156 |
Digest.constructor |
| 157 |
"poly_variant" |
| 158 |
[ Digest.list |
| 159 |
(List.map (Sorted_table.expose table) ~f:(fun (x, y) -> |
| 160 |
Digest.pair (Digest.string x) (Digest.option y))) |
| 161 |
] |
| 162 |
| Application (x, l) -> Digest.constructor "application" [ x; Digest.list l ] |
| 163 |
| Rec_app (n, l) -> Digest.constructor "rec_app" [ Digest.int n; Digest.list l ] |
| 164 |
| Var n -> Digest.constructor "var" [ Digest.int n ] |
| 165 |
;; |
| 166 |
end |
| 167 |
|
| 168 |
module Visibility = struct |
| 169 |
type visible = Visible |
| 170 |
type opaque = Opaque |
| 171 |
|
| 172 |
let _ = Visible |
| 173 |
let _ = Opaque |
| 174 |
end |
| 175 |
|
| 176 |
module type Canonical = sig |
| 177 |
type t |
| 178 |
|
| 179 |
val to_digest : t -> Digest.t |
| 180 |
|
| 181 |
module Exp1 : sig |
| 182 |
type _ t |
| 183 |
|
| 184 |
val var : int -> _ t |
| 185 |
val recurse : int -> _ t list -> _ t |
| 186 |
val apply : 'a t -> 'a t list -> _ t |
| 187 |
val opaque : _ t -> Visibility.opaque t |
| 188 |
|
| 189 |
val get_poly_variant |
| 190 |
: Visibility.visible t |
| 191 |
-> (Visibility.opaque t option Sorted_table.t, string) Result.t |
| 192 |
end |
| 193 |
|
| 194 |
module Def : sig |
| 195 |
type t = Visibility.visible Exp1.t |
| 196 |
end |
| 197 |
|
| 198 |
module Create : sig |
| 199 |
val annotate : Uuid.t -> _ Exp1.t -> _ Exp1.t |
| 200 |
val basetype : Uuid.t -> _ Exp1.t list -> _ Exp1.t |
| 201 |
val tuple : _ Exp1.t list -> _ Exp1.t |
| 202 |
val poly_variant : Location.t -> (string * _ Exp1.t option) list -> _ Exp1.t |
| 203 |
val define : Visibility.visible Exp1.t -> Def.t |
| 204 |
val record : (string * _ Exp1.t) list -> _ Exp1.t |
| 205 |
val variant : (string * _ Exp1.t list) list -> _ Exp1.t |
| 206 |
val create : _ Exp1.t -> t |
| 207 |
end |
| 208 |
end |
| 209 |
|
| 210 |
module Canonical_digest : Canonical = struct |
| 211 |
type t = Canonical of Digest.t |
| 212 |
|
| 213 |
let to_digest (Canonical x) = x |
| 214 |
|
| 215 |
module CD = Create_digest |
| 216 |
|
| 217 |
module Exp1 = struct |
| 218 |
type opaque = Digest.t |
| 219 |
|
| 220 |
type 'a t = |
| 221 |
| Poly_variant of opaque option Sorted_table.t |
| 222 |
| Non_poly_variant of (string * opaque) |
| 223 |
| Opaque : opaque -> Visibility.opaque t |
| 224 |
|
| 225 |
let to_digest (type a) (x : a t) = |
| 226 |
match x with |
| 227 |
| Opaque x -> x |
| 228 |
| Non_poly_variant (_, x) -> x |
| 229 |
| Poly_variant x -> CD.digest_layer (Poly_variant x) |
| 230 |
;; |
| 231 |
|
| 232 |
let equal (type a) (x : a t) (y : a t) = |
| 233 |
Digest.compare (to_digest x) (to_digest y) = 0 |
| 234 |
;; |
| 235 |
|
| 236 |
let opaque x = Opaque (to_digest x) |
| 237 |
|
| 238 |
let create x = |
| 239 |
let x = Canonical_exp_constructor.map ~f:to_digest x in |
| 240 |
let desc = Canonical_exp_constructor.to_string x in |
| 241 |
match x with |
| 242 |
| Canonical_exp_constructor.Poly_variant l -> Poly_variant l |
| 243 |
| Base _ -> Non_poly_variant (desc, CD.digest_layer x) |
| 244 |
| Annotate _ -> |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
Non_poly_variant (desc, CD.digest_layer x) |
| 249 |
| Application _ -> |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
Non_poly_variant (desc, CD.digest_layer x) |
| 254 |
| Rec_app _ -> |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
Non_poly_variant (desc, CD.digest_layer x) |
| 276 |
| Var _ | Tuple _ | Record _ | Variant _ -> |
| 277 |
Non_poly_variant (desc, CD.digest_layer x) |
| 278 |
;; |
| 279 |
|
| 280 |
let var x = create (Var x) |
| 281 |
let apply def l = create (Application (def, l)) |
| 282 |
let recurse tid l = create (Rec_app (tid, l)) |
| 283 |
|
| 284 |
let get_poly_variant (x : Visibility.visible t) = |
| 285 |
match x with |
| 286 |
| Non_poly_variant (desc, _) -> Error desc |
| 287 |
| Poly_variant l -> Ok (Sorted_table.map ~f:(Option.map ~f:(fun x -> Opaque x)) l) |
| 288 |
;; |
| 289 |
end |
| 290 |
|
| 291 |
module Def = struct |
| 292 |
type t = Visibility.visible Exp1.t |
| 293 |
end |
| 294 |
|
| 295 |
module Create = struct |
| 296 |
let annotate u x = Exp1.create (Annotate (u, x)) |
| 297 |
let basetype u l = Exp1.create (Base (u, l)) |
| 298 |
let tuple l = Exp1.create (Tuple l) |
| 299 |
|
| 300 |
let poly_variant loc l = |
| 301 |
Exp1.create (Poly_variant (Sorted_table.create loc ~eq:(equal_option Exp1.equal) l)) |
| 302 |
;; |
| 303 |
|
| 304 |
let define x = x |
| 305 |
let record l = Exp1.create (Record l) |
| 306 |
let variant l = Exp1.create (Variant l) |
| 307 |
let create e = Canonical (Exp1.to_digest e) |
| 308 |
end |
| 309 |
end |
| 310 |
|
| 311 |
module Canonical_full = struct |
| 312 |
module CD = Create_digest |
| 313 |
|
| 314 |
module Exp1 = struct |
| 315 |
type t0 = Exp of t0 Canonical_exp_constructor.t [@@deriving compare, sexp] |
| 316 |
|
| 317 |
let equal_t0 x y = compare_t0 x y = 0 |
| 318 |
|
| 319 |
type 'a t = t0 [@@deriving compare, sexp] |
| 320 |
|
| 321 |
let var x = Exp (Canonical_exp_constructor.Var x) |
| 322 |
let apply d xs = Exp (Canonical_exp_constructor.Application (d, xs)) |
| 323 |
let recurse r xs = Exp (Canonical_exp_constructor.Rec_app (r, xs)) |
| 324 |
|
| 325 |
let poly_variant loc xs = |
| 326 |
Exp |
| 327 |
(Canonical_exp_constructor.Poly_variant |
| 328 |
(Sorted_table.create loc ~eq:(equal_option equal_t0) xs)) |
| 329 |
;; |
| 330 |
|
| 331 |
let get_poly_variant = function |
| 332 |
| Exp (Poly_variant tab) -> Ok tab |
| 333 |
| Exp cc -> Error (Canonical_exp_constructor.to_string cc) |
| 334 |
;; |
| 335 |
|
| 336 |
let opaque t = t |
| 337 |
|
| 338 |
let rec to_digest = function |
| 339 |
| Exp e -> CD.digest_layer (Canonical_exp_constructor.map ~f:to_digest e) |
| 340 |
;; |
| 341 |
end |
| 342 |
|
| 343 |
module Def = struct |
| 344 |
|
| 345 |
type t = Exp1.t0 [@@deriving compare, sexp] |
| 346 |
end |
| 347 |
|
| 348 |
|
| 349 |
type t = Exp1.t0 [@@deriving compare, sexp] |
| 350 |
|
| 351 |
let to_digest e = Exp1.to_digest e |
| 352 |
|
| 353 |
module Create = struct |
| 354 |
let annotate u x = Exp1.Exp (Annotate (u, x)) |
| 355 |
let basetype u xs = Exp1.Exp (Base (u, xs)) |
| 356 |
let tuple xs = Exp1.Exp (Tuple xs) |
| 357 |
let poly_variant loc xs = Exp1.poly_variant loc xs |
| 358 |
let var n = Exp1.Exp (Var n) |
| 359 |
let recurse r xs = Exp1.recurse r xs |
| 360 |
let apply d xs = Exp1.apply d xs |
| 361 |
let define x = x |
| 362 |
let record xs = Exp1.Exp (Record xs) |
| 363 |
let variant xs = Exp1.Exp (Variant xs) |
| 364 |
let create exp = exp |
| 365 |
end |
| 366 |
|
| 367 |
let to_string_hum t = Sexp.to_string_hum (sexp_of_t t) |
| 368 |
end |
| 369 |
|
| 370 |
module Tid : sig |
| 371 |
include Identifiable.S |
| 372 |
end = struct |
| 373 |
include String |
| 374 |
end |
| 375 |
|
| 376 |
module Vid : sig |
| 377 |
include Identifiable.S |
| 378 |
end = struct |
| 379 |
include String |
| 380 |
end |
| 381 |
|
| 382 |
module Gid : sig |
| 383 |
|
| 384 |
type t [@@deriving compare, equal, sexp] |
| 385 |
|
| 386 |
val create : unit -> t |
| 387 |
end = struct |
| 388 |
type t = int [@@deriving compare, equal, sexp] |
| 389 |
|
| 390 |
let r = ref 0 |
| 391 |
|
| 392 |
let create () = |
| 393 |
let u = !r in |
| 394 |
r := 1 + u; |
| 395 |
u |
| 396 |
;; |
| 397 |
end |
| 398 |
|
| 399 |
module Expression = struct |
| 400 |
type 't poly_constr = |
| 401 |
[ `Constr of string * 't option |
| 402 |
| `Inherit of Location.t * 't |
| 403 |
] |
| 404 |
[@@deriving compare, equal, sexp] |
| 405 |
|
| 406 |
module Group : sig |
| 407 |
type 'a t [@@deriving compare, equal, sexp] |
| 408 |
|
| 409 |
val create : Location.t -> (Tid.t * Vid.t list * 'a) list -> 'a t |
| 410 |
val id : 'a t -> Gid.t |
| 411 |
val lookup : 'a t -> Tid.t -> Vid.t list * 'a |
| 412 |
end = struct |
| 413 |
type 'a t = |
| 414 |
{ gid : Gid.t |
| 415 |
; loc : Location.t |
| 416 |
; members : (Tid.t * (Vid.t list * 'a)) list |
| 417 |
} |
| 418 |
[@@deriving compare, equal, sexp] |
| 419 |
|
| 420 |
let create loc trips = |
| 421 |
let gid = Gid.create () in |
| 422 |
let members = List.map trips ~f:(fun (x, vs, t) -> x, (vs, t)) in |
| 423 |
{ gid; loc; members } |
| 424 |
;; |
| 425 |
|
| 426 |
let id g = g.gid |
| 427 |
|
| 428 |
let lookup g tid = |
| 429 |
match List.Assoc.find g.members ~equal:Tid.( = ) tid with |
| 430 |
| Some scheme -> scheme |
| 431 |
| None -> |
| 432 |
eval_fail |
| 433 |
g.loc |
| 434 |
!"impossible: lookup_group, unbound type-identifier: %{Tid}" |
| 435 |
tid |
| 436 |
() |
| 437 |
;; |
| 438 |
end |
| 439 |
|
| 440 |
module Stable = struct |
| 441 |
module V1 = struct |
| 442 |
type t = |
| 443 |
| Annotate of Uuid.t * t |
| 444 |
| Base of Uuid.t * t list |
| 445 |
| Record of (string * t) list |
| 446 |
| Variant of (string * t list) list |
| 447 |
| Tuple of t list |
| 448 |
| Poly_variant of (Location.t * t poly_constr list) |
| 449 |
| Var of (Location.t * Vid.t) |
| 450 |
| Rec_app of Tid.t * t list |
| 451 |
| Top_app of t Group.t * Tid.t * t list |
| 452 |
[@@deriving equal, sexp, variants] |
| 453 |
end |
| 454 |
end |
| 455 |
|
| 456 |
include Stable.V1 |
| 457 |
|
| 458 |
type group = t Group.t |
| 459 |
|
| 460 |
let group = Group.create |
| 461 |
|
| 462 |
type poly_variant_row = t poly_constr |
| 463 |
|
| 464 |
let constr s t = `Constr (s, t) |
| 465 |
let inherit_ loc t = `Inherit (loc, t) |
| 466 |
let var loc t = Var (loc, t) |
| 467 |
let poly_variant loc xs = Poly_variant (loc, xs) |
| 468 |
let basetype = base |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
let is_cyclic_0 ~(via_VR : bool) : group -> Tid.t -> bool = |
| 473 |
fun group tid -> |
| 474 |
let set = ref [] in |
| 475 |
let visited tid = List.mem !set tid ~equal:Tid.equal in |
| 476 |
let add tid = set := tid :: !set in |
| 477 |
let rec trav = function |
| 478 |
|
| 479 |
| Annotate (_, t) -> trav t |
| 480 |
| Base (_, ts) | Tuple ts | Top_app (_, _, ts) -> List.iter ts ~f:trav |
| 481 |
|
| 482 |
| Poly_variant (_, cs) -> |
| 483 |
List.iter cs ~f:(function |
| 484 |
| `Constr (_, None) -> () |
| 485 |
| `Constr (_, Some t) -> trav t |
| 486 |
| `Inherit (_loc, t) -> trav t) |
| 487 |
|
| 488 |
| Record xs -> if via_VR then List.iter xs ~f:(fun (_, t) -> trav t) else () |
| 489 |
| Variant xs -> |
| 490 |
if via_VR then List.iter xs ~f:(fun (_, ts) -> List.iter ~f:trav ts) else () |
| 491 |
|
| 492 |
| Var _ -> () |
| 493 |
|
| 494 |
| Rec_app (tid, ts) -> |
| 495 |
if visited tid |
| 496 |
then () |
| 497 |
else ( |
| 498 |
add tid; |
| 499 |
trav_tid tid); |
| 500 |
List.iter ts ~f:trav |
| 501 |
and trav_tid tid = |
| 502 |
let _, body = Group.lookup group tid in |
| 503 |
trav body |
| 504 |
in |
| 505 |
trav_tid tid; |
| 506 |
let res = visited tid in |
| 507 |
|
| 508 |
|
| 509 |
res |
| 510 |
;; |
| 511 |
|
| 512 |
let is_cyclic = is_cyclic_0 ~via_VR:true |
| 513 |
let is_cyclic_with_no_intervening_VR = is_cyclic_0 ~via_VR:false |
| 514 |
end |
| 515 |
|
| 516 |
include Expression |
| 517 |
|
| 518 |
module Evaluation (Canonical : Canonical) = struct |
| 519 |
|
| 520 |
|
| 521 |
module Venv : sig |
| 522 |
type t |
| 523 |
|
| 524 |
val lookup : t -> Vid.t -> Visibility.visible Canonical.Exp1.t option |
| 525 |
val create : (Vid.t * Visibility.visible Canonical.Exp1.t) list -> t |
| 526 |
end = struct |
| 527 |
type t = Visibility.visible Canonical.Exp1.t Map.M(Vid).t |
| 528 |
|
| 529 |
let create = |
| 530 |
List.fold |
| 531 |
~init:(Map.empty (module Vid)) |
| 532 |
~f:(fun t (k, v) -> Map.set ~key:k ~data:v t) |
| 533 |
;; |
| 534 |
|
| 535 |
let lookup t k = Map.find t k |
| 536 |
end |
| 537 |
|
| 538 |
module Applicand = struct |
| 539 |
type t = |
| 540 |
| Recursion_level of int |
| 541 |
| Definition of Canonical.Def.t |
| 542 |
end |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
module Tenv : sig |
| 547 |
type key = Gid.t * Tid.t |
| 548 |
type t |
| 549 |
|
| 550 |
val find : t -> key -> [ `Recursion_level of int ] option |
| 551 |
val empty : t |
| 552 |
val extend : t -> key -> [ `Recursion_level of int ] -> t |
| 553 |
end = struct |
| 554 |
module Key = struct |
| 555 |
module T = struct |
| 556 |
type t = Gid.t * Tid.t [@@deriving compare, sexp_of] |
| 557 |
end |
| 558 |
|
| 559 |
include T |
| 560 |
include Comparator.Make (T) |
| 561 |
end |
| 562 |
|
| 563 |
type key = Key.t |
| 564 |
type t = [ `Recursion_level of int ] Map.M(Key).t |
| 565 |
|
| 566 |
let find t k = Map.find t k |
| 567 |
let empty = Map.empty (module Key) |
| 568 |
let extend t k v = Map.set ~key:k ~data:v t |
| 569 |
end |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
module Defining : sig |
| 574 |
type 'a t |
| 575 |
|
| 576 |
val return : 'a -> 'a t |
| 577 |
val bind : 'a t -> ('a -> 'b t) -> 'b t |
| 578 |
val look_env : Tenv.key -> Applicand.t option t |
| 579 |
val extend_new_tid : Tenv.key -> Canonical.Def.t t -> Applicand.t t |
| 580 |
val exec : 'a t -> 'a |
| 581 |
end = struct |
| 582 |
type 'a t = depth:int -> Tenv.t -> 'a |
| 583 |
|
| 584 |
let return x ~depth:_ _tenv = x |
| 585 |
|
| 586 |
let bind t f ~depth tenv = |
| 587 |
let x = t ~depth tenv in |
| 588 |
(f x) ~depth tenv |
| 589 |
;; |
| 590 |
|
| 591 |
let look_env key ~depth:_ tenv = |
| 592 |
let result = Tenv.find tenv key in |
| 593 |
Option.map ~f:(fun (`Recursion_level x) -> Applicand.Recursion_level x) result |
| 594 |
;; |
| 595 |
|
| 596 |
let extend_new_tid key def_t ~depth tenv = |
| 597 |
Applicand.Definition |
| 598 |
(let value = `Recursion_level depth in |
| 599 |
let tenv = Tenv.extend tenv key value in |
| 600 |
def_t ~depth:(depth + 1) tenv) |
| 601 |
;; |
| 602 |
|
| 603 |
let exec t = t ~depth:0 Tenv.empty |
| 604 |
end |
| 605 |
|
| 606 |
type 'a defining = 'a Defining.t |
| 607 |
|
| 608 |
let ( >>= ) = Defining.bind |
| 609 |
let return = Defining.return |
| 610 |
|
| 611 |
let sequence_defining : 'a list -> f:('a -> 'b defining) -> 'b list defining = |
| 612 |
fun xs ~f -> |
| 613 |
let rec loop acc_ys = function |
| 614 |
| [] -> return (List.rev acc_ys) |
| 615 |
| x :: xs -> f x >>= fun y -> loop (y :: acc_ys) xs |
| 616 |
in |
| 617 |
loop [] xs |
| 618 |
;; |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
let rec eval : group -> Venv.t -> t -> Visibility.visible Canonical.Exp1.t defining = |
| 638 |
fun group venv t -> |
| 639 |
match t with |
| 640 |
| Record binds -> |
| 641 |
sequence_defining binds ~f:(fun (s, x) -> |
| 642 |
eval group venv x >>= fun y -> return (s, y)) |
| 643 |
>>= fun binds -> return (Canonical.Create.record binds) |
| 644 |
| Variant alts -> |
| 645 |
sequence_defining alts ~f:(fun (s, xs) -> |
| 646 |
eval_list group venv xs >>= fun ys -> return (s, ys)) |
| 647 |
>>= fun alts -> return (Canonical.Create.variant alts) |
| 648 |
| Var (loc, vid) -> |
| 649 |
(match Venv.lookup venv vid with |
| 650 |
| Some x -> return x |
| 651 |
| None -> eval_fail loc !"Free type variable: '%{Vid}" vid ()) |
| 652 |
| Annotate (s, t) -> |
| 653 |
eval group venv t >>= fun v -> return (Canonical.Create.annotate s v) |
| 654 |
| Base (s, ts) -> |
| 655 |
eval_list group venv ts >>= fun vs -> return (Canonical.Create.basetype s vs) |
| 656 |
| Tuple ts -> eval_list group venv ts >>= fun vs -> return (Canonical.Create.tuple vs) |
| 657 |
| Top_app (in_group, tid, args) -> |
| 658 |
eval_list group venv args |
| 659 |
>>= fun args -> |
| 660 |
|
| 661 |
eval_app in_group tid args |
| 662 |
|
| 663 |
| Rec_app (tid, args) -> |
| 664 |
eval_list group venv args >>= fun args -> eval_app group tid args |
| 665 |
| Poly_variant (loc, cs) -> |
| 666 |
sequence_defining ~f:(eval_poly_constr group venv) cs |
| 667 |
>>= fun xss -> return (Canonical.Create.poly_variant loc (List.concat xss)) |
| 668 |
|
| 669 |
and eval_list : group -> Venv.t -> t list -> _ Canonical.Exp1.t list defining = |
| 670 |
fun group venv ts -> sequence_defining ts ~f:(eval group venv) |
| 671 |
|
| 672 |
and eval_poly_constr |
| 673 |
: group -> Venv.t -> t poly_constr |
| 674 |
-> (string * Visibility.opaque Canonical.Exp1.t option) list defining |
| 675 |
= |
| 676 |
fun group venv c -> |
| 677 |
match c with |
| 678 |
| `Constr (s, None) -> return [ s, None ] |
| 679 |
| `Constr (s, Some t) -> |
| 680 |
eval group venv t >>= fun v -> return [ s, Some (Canonical.Exp1.opaque v) ] |
| 681 |
| `Inherit (loc, t) -> |
| 682 |
eval group venv t |
| 683 |
>>= fun v -> |
| 684 |
(match Canonical.Exp1.get_poly_variant v with |
| 685 |
| Ok tab -> return (Sorted_table.expose tab) |
| 686 |
| Error desc -> |
| 687 |
eval_fail |
| 688 |
loc |
| 689 |
"The shape for an inherited type is not described as a polymorphic-variant: %s" |
| 690 |
desc |
| 691 |
()) |
| 692 |
|
| 693 |
and eval_definition : group -> Vid.t list -> t -> Canonical.Def.t defining = |
| 694 |
fun group formals body -> |
| 695 |
let venv = Venv.create (List.mapi formals ~f:(fun i x -> x, Canonical.Exp1.var i)) in |
| 696 |
eval group venv body >>= fun v -> return (Canonical.Create.define v) |
| 697 |
|
| 698 |
and eval_app : group -> Tid.t -> _ Canonical.Exp1.t list -> _ Canonical.Exp1.t defining = |
| 699 |
fun group tid args -> |
| 700 |
let gid = Group.id group in |
| 701 |
let formals, body = Group.lookup group tid in |
| 702 |
let record_or_normal_variant = |
| 703 |
match body with |
| 704 |
| Record _ | Variant _ -> true |
| 705 |
| Tuple _ | Annotate _ | Base _ | Poly_variant _ | Var _ | Rec_app _ | Top_app _ -> |
| 706 |
false |
| 707 |
in |
| 708 |
let cyclic = is_cyclic group tid in |
| 709 |
let cyclic_no_VR = is_cyclic_with_no_intervening_VR group tid in |
| 710 |
if (record_or_normal_variant && cyclic) || cyclic_no_VR |
| 711 |
then |
| 712 |
Defining.look_env (gid, tid) |
| 713 |
>>= (function |
| 714 |
| Some recurse -> return recurse |
| 715 |
| None -> |
| 716 |
Defining.extend_new_tid (gid, tid) (eval_definition group formals body)) |
| 717 |
>>= function |
| 718 |
| Recursion_level r -> return (Canonical.Exp1.recurse r args) |
| 719 |
| Definition def -> return (Canonical.Exp1.apply def args) |
| 720 |
else ( |
| 721 |
let venv = |
| 722 |
match List.zip formals args with |
| 723 |
| Ok x -> Venv.create x |
| 724 |
| Unequal_lengths -> failwith "apply, incorrect type application arity" |
| 725 |
in |
| 726 |
eval group venv body) |
| 727 |
;; |
| 728 |
|
| 729 |
|
| 730 |
let eval : t -> Canonical.t = |
| 731 |
fun t -> |
| 732 |
let group = group (Location.of_string "top-level") [] in |
| 733 |
let venv = Venv.create [] in |
| 734 |
let v = Defining.exec (eval group venv t) in |
| 735 |
Canonical.Create.create v |
| 736 |
;; |
| 737 |
end |
| 738 |
|
| 739 |
module Canonical = struct |
| 740 |
include Canonical_full |
| 741 |
|
| 742 |
module Exp = struct |
| 743 |
type t = Visibility.visible Exp1.t |
| 744 |
end |
| 745 |
end |
| 746 |
|
| 747 |
include Evaluation (Canonical_full) |
| 748 |
module Canonical_selected = Canonical_digest |
| 749 |
module Evaluation_to_digest = Evaluation (Canonical_selected) |
| 750 |
|
| 751 |
let eval_to_digest exp = Canonical_selected.to_digest (Evaluation_to_digest.eval exp) |
| 752 |
let eval_to_digest_string exp = Digest.to_hex (eval_to_digest exp) |
| 753 |
|
| 754 |
module For_typerep = struct |
| 755 |
exception Not_a_tuple of t [@@deriving sexp_of] |
| 756 |
|
| 757 |
let deconstruct_tuple_exn t = |
| 758 |
match t with |
| 759 |
| Tuple ts -> ts |
| 760 |
| _ -> raise (Not_a_tuple t) |
| 761 |
;; |
| 762 |
end |
| 763 |
|
| 764 |
module Expert = struct |
| 765 |
module Sorted_table = Sorted_table |
| 766 |
module Canonical_exp_constructor = Canonical_exp_constructor |
| 767 |
module Canonical = Canonical |
| 768 |
end |