Skip to content

Commit 4d5f4f2

Browse files
committed
diffing_with_keys: switch to a record
1 parent 3cbf4fa commit 4d5f4f2

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

typing/includecore.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,11 @@ module Record_diffing = struct
475475
| Keep (x,y,_) ->
476476
(* We need to add equality between existential type parameters
477477
(in inline records) *)
478-
(snd x).ld_type::params1, (snd y).ld_type::params2
478+
x.data.ld_type::params1, y.data.ld_type::params2
479479

480480
let test _loc env (params1,params2)
481-
(pos, lbl1: Diff.left)
482-
(_, lbl2: Diff.right)
481+
({pos; data=lbl1}: Diff.left)
482+
({data=lbl2; _ }: Diff.right)
483483
=
484484
let name1, name2 = Ident.name lbl1.ld_id, Ident.name lbl2.ld_id in
485485
if name1 <> name2 then
@@ -626,7 +626,9 @@ module Variant_diffing = struct
626626
| Change _ -> 10
627627

628628

629-
let test loc env (params1,params2) (pos,cd1: D.left) (_,cd2: D.right) =
629+
let test loc env (params1,params2)
630+
({pos; data=cd1}: D.left)
631+
({data=cd2; _}: D.right) =
630632
let name1, name2 = Ident.name cd1.cd_id, Ident.name cd2.cd_id in
631633
if name1 <> name2 then
632634
let types_match =

utils/diffing_with_keys.ml

+17-20
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
(**************************************************************************)
1515

1616

17-
type 'a with_pos = int * 'a
18-
let with_pos l = List.mapi (fun n x -> n+1,x) l
19-
let pos (x,_) = x
20-
let data (_,x) = x
21-
let mk_pos pos data = pos, data
17+
type 'a with_pos = {pos:int; data:'a}
18+
let with_pos l = List.mapi (fun n data -> {pos=n+1; data}) l
2219

2320
(** Composite change and mismatches *)
2421
type ('l,'r,'diff) mismatch =
@@ -114,12 +111,12 @@ module Define(D:Diffing.Defs with type eq := unit) = struct
114111

115112
(** Compute the partial cycle and edge associated to an edge *)
116113
let edge state (x:left) (y:right) =
117-
let kx, ky = key_left (data x), key_right (data y) in
114+
let kx, ky = key_left x.data, key_right y.data in
118115
let edge =
119116
if kx <= ky then
120-
Left (pos x, state, (x,y))
117+
Left (x.pos, state, (x,y))
121118
else
122-
Right (pos x,state, (x,y))
119+
Right (x.pos,state, (x,y))
123120
in
124121
Two_cycle.create kx ky, edge
125122

@@ -140,40 +137,40 @@ module Define(D:Diffing.Defs with type eq := unit) = struct
140137
let k, edge = edge state x y in
141138
Swap.update k (merge_edge edge) swaps, moves
142139
| Insert nx ->
143-
let k = key_right (data nx) in
144-
let edge = Right (pos nx, state,nx) in
140+
let k = key_right nx.data in
141+
let edge = Right (nx.pos, state,nx) in
145142
swaps, Move.update k (merge_edge edge) moves
146143
| Delete nx ->
147-
let k, edge = key_left (data nx), Left (pos nx, state, nx) in
144+
let k, edge = key_left nx.data, Left (nx.pos, state, nx) in
148145
swaps, Move.update k (merge_edge edge) moves
149146
| _ -> swaps, moves
150147
in
151148
List.fold_left add (state,(Swap.empty,Move.empty)) changes
152149

153150
(** Check if an edge belongs to a known 2-cycle *)
154151
let swap swaps x y =
155-
let kx, ky = key_left (data x), key_right (data y) in
152+
let kx, ky = key_left x.data, key_right y.data in
156153
let key = Two_cycle.create kx ky in
157154
match Swap.find_opt key swaps with
158155
| None | Some (Left _ | Right _)-> None
159156
| Some Both (state, (ll,lr),(rl,rr)) ->
160157
match test state ll rr, test state rl lr with
161158
| Ok _, Ok _ ->
162-
Some (mk_pos (pos ll) kx, mk_pos (pos rl) ky)
159+
Some ({pos=ll.pos; data=kx}, {pos=rl.pos; data=ky})
163160
| Error _, _ | _, Error _ -> None
164161

165162
let move moves x =
166163
let name =
167164
match x with
168-
| Either.Left x -> key_left (data x)
169-
| Either.Right x -> key_right (data x)
165+
| Either.Left x -> key_left x.data
166+
| Either.Right x -> key_right x.data
170167
in
171168
match Move.find_opt name moves with
172169
| None | Some (Left _ | Right _)-> None
173170
| Some Both (state,got,expected) ->
174171
match test state got expected with
175172
| Ok _ ->
176-
Some (Move {name; got=pos got; expected=pos expected})
173+
Some (Move {name; got=got.pos; expected=expected.pos})
177174
| Error _ -> None
178175

179176
let refine state patch =
@@ -183,17 +180,17 @@ module Define(D:Diffing.Defs with type eq := unit) = struct
183180
| Insert x ->
184181
begin match move moves (Either.Right x) with
185182
| Some _ as move -> move
186-
| None -> Some (Insert {pos=pos x;insert=data x})
183+
| None -> Some (Insert {pos=x.pos;insert=x.data})
187184
end
188185
| Delete x ->
189186
begin match move moves (Either.Left x) with
190187
| Some _ -> None
191-
| None -> Some (Delete {pos=pos x;delete=data x})
188+
| None -> Some (Delete {pos=x.pos; delete=x.data})
192189
end
193190
| Change(x,y, reason) ->
194191
match swap swaps x y with
195-
| Some ((pos1,first),(pos2,last)) ->
196-
if pos x = pos1 then
192+
| Some ({pos=pos1; data=first}, {pos=pos2; data=last}) ->
193+
if x.pos = pos1 then
197194
Some (Swap { pos = pos1, pos2; first; last})
198195
else None
199196
| None -> Some (Change reason)

utils/diffing_with_keys.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
3030
*)
3131

32-
type 'a with_pos = int * 'a
32+
type 'a with_pos = {pos: int; data:'a}
3333
val with_pos: 'a list -> 'a with_pos list
3434

3535
type ('l,'r,'diff) mismatch =

0 commit comments

Comments
 (0)