@@ -2039,23 +2039,21 @@ module Knowledge = struct
2039
2039
type work = Done | Work of workers
2040
2040
2041
2041
type objects = {
2042
+ last : Oid .t ;
2042
2043
vals : Record .t Oid.Map .t ;
2043
2044
comp : work Map .M (Name ).t Oid.Map .t ;
2044
2045
syms : fullname Oid.Map .t ;
2045
- heap : cell Oid.Map .t ;
2046
- data : Oid .t Cell.Map .t ;
2047
2046
objs : Oid .t String.Map .t String.Map .t ;
2048
2047
pubs : Oid.Set .t String.Map .t ;
2049
2048
}
2050
2049
2051
2050
let empty_class = {
2051
+ last = Oid. first_atom;
2052
2052
vals = Map. empty (module Oid );
2053
2053
comp = Map. empty (module Oid );
2054
2054
objs = Map. empty (module String );
2055
2055
syms = Map. empty (module Oid );
2056
2056
pubs = Map. empty (module String );
2057
- heap = Map. empty (module Oid );
2058
- data = Map. empty (module Cell );
2059
2057
}
2060
2058
2061
2059
type t = {
@@ -2303,17 +2301,9 @@ module Knowledge = struct
2303
2301
type +'a t = 'a obj
2304
2302
type 'a ord = Oid .comparator_witness
2305
2303
2306
- let with_new_object objs f = match Map. max_elt objs.Env. vals with
2307
- | None -> f Oid. first_atom {
2308
- objs
2309
- with vals = Map. singleton (module Oid ) Oid. first_atom Record. empty
2310
- }
2311
- | Some (key ,_ ) ->
2312
- let key = Oid. next key in
2313
- f key {
2314
- objs
2315
- with vals = Map. add_exn objs.vals ~key ~data: Record. empty
2316
- }
2304
+ let with_new_object objs f =
2305
+ let next = Oid. next objs.Env. last in
2306
+ f next {objs with Env. last = next}
2317
2307
2318
2308
let create : ('a,_) cls -> 'a obj Knowledge.t = fun cls ->
2319
2309
objects cls >> = fun objs ->
@@ -2854,72 +2844,6 @@ module Knowledge = struct
2854
2844
let set_package name = update @@ fun s -> {s with package = name}
2855
2845
end
2856
2846
2857
-
2858
- module Data : sig
2859
- type +'a t
2860
- type 'a ord
2861
-
2862
- val atom : ('a ,_) cls -> 'a obj -> 'a t knowledge
2863
- val cons : ('a ,_) cls -> 'a t -> 'a t -> 'a t knowledge
2864
-
2865
- val case : ('a ,_) cls -> 'a t ->
2866
- null :'r knowledge ->
2867
- atom :('a obj -> 'r knowledge ) ->
2868
- cons :('a t -> 'a t -> 'r knowledge ) -> 'r knowledge
2869
-
2870
-
2871
- val id : 'a obj -> Int63 .t
2872
-
2873
-
2874
- module type S = sig
2875
- type t [@@deriving sexp]
2876
- include Base.Comparable .S with type t : = t
2877
- include Binable .S with type t : = t
2878
- end
2879
-
2880
- val derive : ('a ,_) cls -> (module S
2881
- with type t = 'a t
2882
- and type comparator_witness = 'a ord )
2883
- end = struct
2884
- type +'a t = 'a obj
2885
- type 'a ord = Oid .comparator_witness
2886
-
2887
- let atom _ x = Knowledge. return x
2888
-
2889
- let add_cell {Class. name} objects oid cell =
2890
- let {Env. data; heap} = objects in
2891
- let data = Map. add_exn data ~key: cell ~data: oid in
2892
- let heap = Map. add_exn heap ~key: oid ~data: cell in
2893
- update (fun s -> {
2894
- s with classes = Map. set s.classes name {
2895
- objects with data; heap
2896
- }}) >> | fun () ->
2897
- oid
2898
-
2899
- let cons cls car cdr =
2900
- let cell = {car; cdr} in
2901
- objects cls >> = function {data; heap} as s ->
2902
- match Map. find data cell with
2903
- | Some id -> Knowledge. return id
2904
- | None -> match Map. max_elt heap with
2905
- | None ->
2906
- add_cell cls s Oid. first_cell cell
2907
- | Some (id ,_ ) ->
2908
- add_cell cls s (Oid. next id) cell
2909
-
2910
- let case cls x ~null ~atom ~cons =
2911
- if Oid. is_null x then null else
2912
- if Oid. is_atom x || Oid. is_number x then atom x
2913
- else objects cls >> = fun {Env. heap} ->
2914
- let cell = Map. find_exn heap x in
2915
- cons cell.car cell.cdr
2916
-
2917
- let id = Object. id
2918
-
2919
- module type S = Object. S
2920
- let derive = Object. derive
2921
- end
2922
-
2923
2847
module Syntax = struct
2924
2848
include Knowledge. Syntax
2925
2849
include Knowledge. Let
@@ -3040,7 +2964,7 @@ module Knowledge = struct
3040
2964
Format. fprintf ppf " @]" ;
3041
2965
3042
2966
module Io = struct
3043
- type version = V1 [@@ deriving bin_io ]
2967
+ type version = V1 | V2 [@@ deriving bin_io ]
3044
2968
3045
2969
module List = Base. List
3046
2970
@@ -3051,12 +2975,14 @@ module Knowledge = struct
3051
2975
comp : Name .t list ;
3052
2976
} [@@ deriving bin_io ]
3053
2977
3054
- type objects = data list [@@ deriving bin_io ]
3055
- type payload = (Name .t * objects ) list [@@ deriving bin_io ]
2978
+ type v1 = data list [@@ deriving bin_io ]
2979
+ type v2 = Oid .t * v1 [@@ deriving bin_io ]
2980
+ type 'a objects = 'a [@@ deriving bin_io ]
2981
+ type 'a payload = (Name .t * 'a ) list [@@ deriving bin_io ]
3056
2982
3057
- type canonical = {
2983
+ type 'a canonical = {
3058
2984
version : version ;
3059
- payload : payload ;
2985
+ payload : 'a payload ;
3060
2986
} [@@ deriving bin_io ]
3061
2987
3062
2988
let magic = " CMU:KB"
@@ -3129,37 +3055,66 @@ module Knowledge = struct
3129
3055
| None -> []
3130
3056
| Some works -> Map. keys works
3131
3057
3132
-
3133
- let to_canonical {Env. classes} =
3058
+ let to_canonical {Env. classes} : v2 canonical =
3134
3059
let payload =
3135
3060
Map. to_alist classes |>
3136
- List. map ~f: (fun (cid , {Env. vals; syms; comp} ) ->
3137
- cid,
3138
- Map. to_alist vals |> List. filter_map ~f: (fun (oid ,value ) ->
3061
+ List. map ~f: (fun (cid , {Env. vals; syms; comp; last} ) ->
3062
+ let data = Map. to_alist vals |> List. filter_map ~f: (fun (oid ,value ) ->
3139
3063
let data = serialize_record value in
3140
3064
let sym = Map. find syms oid in
3141
3065
let comp = collect_comps comp oid in
3142
3066
if Array. is_empty data && Option. is_none sym
3143
3067
then None
3144
- else Some {key= oid; sym; data; comp})) in {
3068
+ else Some {key= oid; sym; data; comp}) in
3069
+ cid,(last,data)) in {
3145
3070
version = V1 ;
3146
3071
payload;
3147
3072
}
3148
3073
3149
- let of_canonical {payload} =
3074
+ let init_last : state -> state = fun state -> {
3075
+ state with
3076
+ classes = Map. map state.classes ~f: (fun cls -> {
3077
+ cls with
3078
+ last = match Map. max_elt cls.vals with
3079
+ | None -> cls.last
3080
+ | Some (k ,_ ) -> Oid. next k
3081
+ })
3082
+ }
3083
+
3084
+ let of_canonical_v1 {payload} =
3150
3085
let init = Map. empty (module Name ) in
3151
3086
let classes =
3152
3087
List. fold payload ~init ~f: (fun state (cid ,objs ) ->
3153
3088
Map. add_exn state ~key: cid
3154
3089
~data: (List. fold objs ~f: add_object
3155
3090
~init: Env. empty_class)) in
3091
+ init_last {empty with classes}
3092
+
3093
+ let of_canonical_v2 {payload} =
3094
+ let init = Map. empty (module Name ) in
3095
+ let classes =
3096
+ List. fold payload ~init ~f: (fun state (cid ,(last ,objs )) ->
3097
+ let init = {
3098
+ Env. empty_class with last
3099
+ } in
3100
+ Map. add_exn state ~key: cid
3101
+ ~data: (List. fold objs ~f: add_object
3102
+ ~init )) in
3156
3103
{empty with classes}
3157
3104
3105
+
3158
3106
let of_bigstring data =
3159
3107
let pos_ref = ref (check_magic data) in
3160
- let V1 = bin_read_version data ~pos_ref in
3161
- let payload = bin_read_payload data ~pos_ref in
3162
- of_canonical {version= V1 ; payload}
3108
+ let version = bin_read_version data ~pos_ref in
3109
+ match version with
3110
+ | V1 -> of_canonical_v1 {
3111
+ version;
3112
+ payload = bin_read_payload bin_read_v1 data ~pos_ref
3113
+ }
3114
+ | V2 -> of_canonical_v2 {
3115
+ version;
3116
+ payload = bin_read_payload bin_read_v2 data ~pos_ref
3117
+ }
3163
3118
3164
3119
let load path =
3165
3120
let fd = Unix. openfile path Unix. [O_RDONLY ] 0o400 in
@@ -3177,21 +3132,21 @@ module Knowledge = struct
3177
3132
let blit_canonical_to_bigstring repr buf =
3178
3133
Bigstring.From_string. blito ~src: magic ~dst: buf () ;
3179
3134
let pos = String. length magic in
3180
- let _p = bin_write_canonical ~pos buf repr in
3135
+ let _p = bin_write_canonical bin_write_v2 ~pos buf repr in
3181
3136
()
3182
3137
3183
3138
let to_bigstring state =
3184
3139
let repr = to_canonical state in
3185
3140
let size = String. length magic +
3186
- bin_size_canonical repr in
3141
+ bin_size_canonical bin_size_v2 repr in
3187
3142
let data = Bigstring. create size in
3188
3143
blit_canonical_to_bigstring repr data;
3189
3144
data
3190
3145
3191
3146
let save state path =
3192
3147
let repr = to_canonical state in
3193
3148
let size = String. length magic +
3194
- bin_size_canonical repr in
3149
+ bin_size_canonical bin_size_v2 repr in
3195
3150
let fd = Unix. openfile path Unix. [O_RDWR ; O_CREAT ; O_TRUNC ] 0o660 in
3196
3151
try
3197
3152
let dim = [|size |]in
0 commit comments