Skip to content

Commit 41d03d4

Browse files
authored
Merge pull request #194 from tlaplus/fix-lsp-obl-state-aggr
LSP: Use maximum obligation status instead of latest.
2 parents 132d225 + 31983e7 commit 41d03d4

File tree

11 files changed

+84
-37
lines changed

11 files changed

+84
-37
lines changed

Diff for: lsp/lib/docs/obl.ml

+52-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Role = struct
88
| Unknown (** Initially all the obligations are of unknown role. *)
99
| Unexpected
1010
(** Was not received from the parser, but got later from the prover. *)
11+
[@@deriving show]
1112

1213
let as_string = function
1314
| Main -> "main"
@@ -16,9 +17,26 @@ module Role = struct
1617
| Unexpected -> "unexpected"
1718
end
1819

20+
(** We define obligation's map explicitly to have ability to print it. *)
21+
module ObligationByProverMap = struct
22+
include StrMap
23+
24+
type t = Toolbox.Obligation.t StrMap.t
25+
26+
let pp ppf (m : t) =
27+
let it =
28+
iter
29+
(fun k v ->
30+
Format.fprintf ppf "@[%s -> %a;@]" k Toolbox.Obligation.pp v)
31+
m
32+
in
33+
it
34+
end
35+
1936
type t = {
2037
role : Role.t;
2138
parsed : Tlapm_lib.Proof.T.obligation option;
39+
[@printer fun _ fmt -> Printf.ifprintf fmt "...skipped..."]
2240
(** The obligation as received from the parser. *)
2341
parsed_text_plain : string option Lazy.t; (** Works as a cache. *)
2442
parsed_text_normalized : string option Lazy.t; (** Works as a cache. *)
@@ -27,11 +45,12 @@ type t = {
2745
reset all the prover results. *)
2846
p_obl_id : int option; (** Obligation ID, as assigned by the prover. *)
2947
checking : bool; (** Is obligation checking currently running? *)
30-
by_prover : Toolbox.Obligation.t StrMap.t;
48+
by_prover : ObligationByProverMap.t;
3149
prover_names : string list option;
3250
latest_prover : string option;
3351
status : Proof_status.t;
3452
}
53+
[@@deriving show]
3554

3655
let obl_to_str obl =
3756
let buf = Buffer.create 100 in
@@ -43,7 +62,7 @@ let obl_to_str obl =
4362
let obl_to_normalized_str obl =
4463
obl_to_str (Tlapm_lib.Backend.Toolbox.normalize true obl)
4564

46-
let of_parsed_obligation (parsed : Tlapm_lib.Proof.T.obligation) status =
65+
let of_parsed_obligation (parsed : Tlapm_lib.Proof.T.obligation) =
4766
let parsed_text_plain = lazy (Some (obl_to_str parsed)) in
4867
let parsed_text_normalized = lazy (Some (obl_to_normalized_str parsed)) in
4968
{
@@ -54,10 +73,10 @@ let of_parsed_obligation (parsed : Tlapm_lib.Proof.T.obligation) status =
5473
p_ref = 0;
5574
p_obl_id = None;
5675
checking = false;
57-
by_prover = StrMap.empty;
76+
by_prover = ObligationByProverMap.empty;
5877
prover_names = None;
5978
latest_prover = None;
60-
status;
79+
status = Pending;
6180
}
6281

6382
let reset obl p_ref =
@@ -66,7 +85,7 @@ let reset obl p_ref =
6685
p_ref;
6786
p_obl_id = None;
6887
checking = false;
69-
by_prover = StrMap.empty;
88+
by_prover = ObligationByProverMap.empty;
7089
prover_names = None;
7190
latest_prover = None;
7291
status = Proof_status.Pending;
@@ -82,7 +101,7 @@ let loc obl =
82101
(match obl.latest_prover with
83102
| None ->
84103
assert false (* there should be either parsed info or prover result *)
85-
| Some prover -> StrMap.find prover obl.by_prover)
104+
| Some prover -> ObligationByProverMap.find prover obl.by_prover)
86105
.loc
87106
| Some parsed -> Range.of_locus_must (Tlapm_lib.Util.get_locus parsed.obl)
88107

@@ -105,7 +124,9 @@ let is_final obl =
105124
match obl.prover_names with
106125
| None -> false
107126
| Some prover_names ->
108-
let have_prover_result pn = StrMap.mem pn obl.by_prover in
127+
let have_prover_result pn =
128+
ObligationByProverMap.mem pn obl.by_prover
129+
in
109130
List.for_all have_prover_result prover_names)
110131

111132
let status obl = if obl.checking then Proof_status.Progress else obl.status
@@ -119,12 +140,12 @@ let latest_status_msg obl =
119140
| None -> Proof_status.to_message obl.status
120141
| Some prover ->
121142
Toolbox.tlapm_obl_state_to_string
122-
(StrMap.find prover obl.by_prover).status
143+
(ObligationByProverMap.find prover obl.by_prover).status
123144

124145
let latest_obl_text obl =
125146
match obl.latest_prover with
126147
| None -> text_normalized obl
127-
| Some prover -> (StrMap.find prover obl.by_prover).obl
148+
| Some prover -> (ObligationByProverMap.find prover obl.by_prover).obl
128149

129150
let with_prover_terminated p_ref (obl : t) =
130151
if obl.p_ref <= p_ref then { obl with checking = false } else obl
@@ -144,7 +165,7 @@ let with_prover_obligation p_ref (tlapm_obl : Toolbox.Obligation.t)
144165
(* To have it reset bellow. *)
145166
p_obl_id = None;
146167
checking = false;
147-
by_prover = StrMap.empty;
168+
by_prover = ObligationByProverMap.empty;
148169
prover_names = None;
149170
latest_prover = None;
150171
status = Proof_status.Pending;
@@ -156,12 +177,17 @@ let with_prover_obligation p_ref (tlapm_obl : Toolbox.Obligation.t)
156177
match Toolbox.Obligation.(tlapm_obl.prover) with
157178
| None -> obl
158179
| Some prover ->
159-
{
160-
obl with
161-
by_prover = StrMap.add prover tlapm_obl obl.by_prover;
162-
latest_prover = Some prover;
163-
status = Proof_status.of_tlapm_obl_state tlapm_obl.status;
164-
}
180+
let latest_prover = Some prover in
181+
let by_prover =
182+
ObligationByProverMap.add prover tlapm_obl obl.by_prover
183+
in
184+
let status =
185+
ObligationByProverMap.fold
186+
(fun _ (o : Toolbox.Obligation.t) acc ->
187+
Proof_status.max (Proof_status.of_tlapm_obl_state o.status) acc)
188+
by_prover Pending
189+
in
190+
{ obl with by_prover; latest_prover; status }
165191
in
166192
{ obl with p_obl_id = Some tlapm_obl.id; checking = not (is_final obl) }
167193
in
@@ -246,10 +272,12 @@ let as_lsp_tlaps_proof_obligation_state obl =
246272
let final = is_final obl in
247273
match obl.prover_names with
248274
| None ->
249-
List.map (fun (_pn, o) -> make_result o) (StrMap.to_list obl.by_prover)
275+
List.map
276+
(fun (_pn, o) -> make_result o)
277+
(ObligationByProverMap.to_list obl.by_prover)
250278
| Some prover_names ->
251279
let planned pn =
252-
match StrMap.find_opt pn obl.by_prover with
280+
match ObligationByProverMap.find_opt pn obl.by_prover with
253281
| None -> if final then None else Some (make_pending pn)
254282
| Some o -> Some (make_result o)
255283
in
@@ -258,7 +286,12 @@ let as_lsp_tlaps_proof_obligation_state obl =
258286
in
259287
List.append
260288
(List.filter_map planned prover_names)
261-
(List.filter_map unplanned (StrMap.to_list obl.by_prover))
289+
(List.filter_map unplanned
290+
(ObligationByProverMap.to_list obl.by_prover))
262291
in
263292
Structs.TlapsProofObligationState.make ~role ~range ~status ~normalized
264293
~results
294+
295+
let%test_unit "Check Role.pp" =
296+
Format.printf "%a" Role.pp Role.Unexpected;
297+
assert ("Obl.Role.Unexpected" = Role.show Role.Unexpected)

Diff for: lsp/lib/docs/obl.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ end
77

88
type t
99

10-
val of_parsed_obligation : Tlapm_lib.Proof.T.obligation -> Proof_status.t -> t
10+
val of_parsed_obligation : Tlapm_lib.Proof.T.obligation -> t
1111
val with_role : Role.t -> t -> t
1212
val with_prover_terminated : int -> t -> t
1313
val with_prover_obligation : int -> Toolbox.Obligation.t -> t option -> t

Diff for: lsp/lib/docs/proof_status.ml

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
open Prover
22

33
type t = Proved | Failed | Omitted | Missing | Pending | Progress
4+
[@@deriving show]
45

56
let of_tlapm_obl_state = function
67
| Toolbox.ToBeProved -> Progress
@@ -28,25 +29,29 @@ let to_message = function
2829
| Pending -> "Proof pending."
2930
| Proved -> "Proof checked successfully."
3031

32+
(** For a proof step, we will take the maximum of statuses returned by different
33+
provers for each obligation and the minimum across the obligations. *)
3134
let to_order = function
32-
| Failed -> 0
33-
| Missing -> 1
34-
| Omitted -> 2
35-
| Progress -> 3
36-
| Pending -> 4
35+
| Pending -> 0
36+
| Failed -> 1
37+
| Missing -> 2
38+
| Omitted -> 3
39+
| Progress -> 4
3740
| Proved -> 5
3841

3942
let of_order = function
40-
| 0 -> Failed
41-
| 1 -> Missing
42-
| 2 -> Omitted
43-
| 3 -> Progress
44-
| 4 -> Pending
43+
| 0 -> Pending
44+
| 1 -> Failed
45+
| 2 -> Missing
46+
| 3 -> Omitted
47+
| 4 -> Progress
4548
| 5 -> Proved
4649
| _ -> failwith "Impossible order"
4750

51+
let bot = Pending
4852
let top = Proved
4953
let min a b = of_order (min (to_order a) (to_order b))
54+
let max a b = of_order (max (to_order a) (to_order b))
5055
let yojson_of_t t = `String (to_string t)
5156

5257
let is_diagnostic = function

Diff for: lsp/lib/docs/proof_status.mli

+3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
open Prover
22

33
type t = Proved | Failed | Omitted | Missing | Pending | Progress
4+
[@@deriving show]
45

56
val of_tlapm_obl_state : Toolbox.tlapm_obl_state -> t
67
val to_string : t -> string
78
val to_message : t -> string
89
val to_order : t -> int
910
val of_order : int -> t
11+
val bot : t
1012
val top : t
1113
val min : t -> t -> t
14+
val max : t -> t -> t
1215
val yojson_of_t : t -> Yojson.Safe.t
1316

1417
val is_diagnostic : t -> bool

Diff for: lsp/lib/docs/proof_step.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ let of_module (mule : Tlapm_lib.Module.T.mule) prev : t option =
395395
| None -> Tlapm_lib.Backend.Fingerprints.write_fingerprint o
396396
| Some _ -> o
397397
in
398-
let o = Obl.of_parsed_obligation o Proof_status.Pending in
398+
let o = Obl.of_parsed_obligation o in
399399
let o = Obl.with_proof_state_from o (Hashtbl.find_opt prev_obs) in
400400
Some (o_range, o)
401401
in

Diff for: lsp/lib/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
; (flags -only-test docs/proof_step.ml -verbose)
1010
)
1111
(preprocess
12-
(pps ppx_inline_test)))
12+
(pps ppx_inline_test ppx_deriving.show)))
1313

1414
(include_subdirs qualified)

Diff for: lsp/lib/prover/prover.mli

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Toolbox : sig
2626
already : bool option;
2727
obl : string option;
2828
}
29+
[@@deriving show]
2930
end
3031

3132
type tlapm_notif_severity = TlapmNotifError | TlapmNotifWarning

Diff for: lsp/lib/prover/toolbox.ml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type tlapm_obl_state =
77
| Interrupted
88
| Trivial
99
| Unknown of string
10+
[@@deriving show]
1011

1112
let tlapm_obl_state_of_string s =
1213
match s with
@@ -42,7 +43,9 @@ module Obligation = struct
4243
reason : string option;
4344
already : bool option;
4445
obl : string option;
46+
[@printer fun _ fmt -> Printf.ifprintf fmt "...skipped..."]
4547
}
48+
[@@deriving show]
4649

4750
(** This is for testing only. *)
4851
module Test = struct

Diff for: lsp/lib/prover/toolbox.mli

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type tlapm_obl_state =
99
| Interrupted
1010
| Trivial
1111
| Unknown of string
12+
[@@deriving show]
1213

1314
val tlapm_obl_state_to_string : tlapm_obl_state -> string
1415

@@ -24,6 +25,7 @@ module Obligation : sig
2425
already : bool option;
2526
obl : string option;
2627
}
28+
[@@deriving show]
2729

2830
(** For testing only. *)
2931
module Test : sig

Diff for: lsp/lib/range.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module LspT = Lsp.Types
44

55
module Position : sig
6-
type t
6+
type t [@@deriving show]
77

88
val make : int -> int -> t
99
val of_pair : int * int -> t
@@ -16,7 +16,7 @@ module Position : sig
1616
val max : t -> t -> t
1717
val line : t -> int
1818
end = struct
19-
type t = P of int * int
19+
type t = P of int * int [@@deriving show]
2020

2121
let make l c = P (l, c)
2222
let of_pair (l, c) = P (l, c)
@@ -43,7 +43,7 @@ end = struct
4343
let line (P (l, _)) = l
4444
end
4545

46-
type t = R of (int * int) * (int * int)
46+
type t = R of (int * int) * (int * int) [@@deriving show]
4747

4848
let line_from (R ((fl, _), _)) = fl
4949
let line_till (R (_, (tl, _))) = tl

Diff for: lsp/lib/range.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module LspT := Lsp.Types
22

33
module Position : sig
4-
type t
4+
type t [@@deriving show]
55

66
val compare : t -> t -> int
77
end
88

9-
type t
9+
type t [@@deriving show]
1010

1111
val from : t -> Position.t
1212
val till : t -> Position.t

0 commit comments

Comments
 (0)