| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (* Type-safe implementation by whitequark *) |
| 12 | |
| 13 | (* An extensible variant has an internal representation equivalent |
| 14 | to the following: |
| 15 | |
| 16 | type constr = { |
| 17 | name: string, |
| 18 | id: int |
| 19 | } |
| 20 | type value = (*Object_tag*) constr * v1 * v2... |
| 21 | |
| 22 | and the code generated by the compiler looks like: |
| 23 | |
| 24 | (* type X += Y *) |
| 25 | let constr_Y = alloc { "Y", %caml_fresh_oo_id () } |
| 26 | (* match x with Y -> a | _ -> b *) |
| 27 | if x.0 == constr_Y then a else b |
| 28 | |
| 29 | and the polymorphic comparison function works like: |
| 30 | |
| 31 | let equal = fun (c1, ...) (c2, ...) -> |
| 32 | c1.id == c2.id |
| 33 | |
| 34 | In every new extension constructor, the name field is a constant |
| 35 | string and the id field is filled with an unique[1] value returned |
| 36 | by %caml_fresh_oo_id. Moreover, every value of an extensible variant |
| 37 | type is allocated as a new block. |
| 38 | |
| 39 | [1]: On 64-bit systems. On 32-bit systems, calling %caml_fresh_oo_id |
| 40 | 2**30 times will result in a wraparound. Note that this does |
| 41 | not affect soundness because constructors are compared by |
| 42 | physical equality during matching. See OCaml PR7809 for code |
| 43 | demonstrating this. |
| 44 | |
| 45 | An extensible variant can be marshalled and unmarshalled, and |
| 46 | is guaranteed to not be equal to itself after unmarshalling, |
| 47 | since the id field is filled with another unique value. |
| 48 | |
| 49 | Note that the explanation above is purely informative and we |
| 50 | do not depend on the exact representation of extensible variants, |
| 51 | only on the fact that no two constructor representations ever |
| 52 | alias. In particular, if the definition of constr is replaced with: |
| 53 | |
| 54 | type constr = int |
| 55 | |
| 56 | (where the value is truly unique for every created constructor), |
| 57 | correctness is preserved. |
| 58 | *) |
| 59 | type 'a typ = .. |
| 60 | |
| 61 | (* Erases the contained type so that the key can be put in a hash table. *) |
| 62 | type boxkey = Box : 'a typ -> boxkey [@@unboxed] |
| 63 | |
| 64 | (* Carry the type we just erased with the actual key. *) |
| 65 | type 'a key = 'a typ * boxkey |
| 66 | |
| 67 | module EHashtbl = Ephemeron.K1.Make(struct |
| 68 | type t = boxkey |
| 69 | let equal = (==) |
| 70 | let hash = Hashtbl.hash |
| 71 | end) |
| 72 | |
| 73 | type value = { get : 'k. 'k typ -> 'k } [@@unboxed] |
| 74 | |
| 75 | let values : value EHashtbl.t = |
| 76 | EHashtbl.create 1001 |
| 77 | |
| 78 | let create : type v. v -> v key = |
| 79 | fun value -> |
| 80 | let module M = struct |
| 81 | type _ typ += Typ : v typ |
| 82 | |
| 83 | let get : type k. k typ -> k = |
| 84 | fun typ -> |
| 85 | match typ with |
| 86 | | Typ -> value |
| 87 | | _ -> assert false |
| 88 | |
| 89 | let boxkey = Box Typ |
| 90 | let key = Typ, boxkey |
| 91 | let value = { get } |
| 92 | end in |
| 93 | EHashtbl.add values M.boxkey M.value; |
| 94 | M.key |
| 95 | |
| 96 | (* Avoid raising Not_found *) |
| 97 | exception InvalidKey |
| 98 | let get (typ, boxkey) = |
| 99 | try (EHashtbl.find values boxkey).get typ |
| 100 | with Not_found -> raise InvalidKey |
| 101 | |
| 102 | let default (typ, boxkey) default = |
| 103 | try (EHashtbl.find values boxkey).get typ |
| 104 | with Not_found -> default |
| 105 | |
| 106 | let clean () = EHashtbl.clean values |