Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes in range module #3525

Merged
merged 4 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ocaml/fstar-lib/generated/FStar_Class_Ord.ml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 28 additions & 31 deletions ocaml/fstar-lib/generated/FStar_Compiler_Range_Ops.ml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions ocaml/fstar-lib/generated/FStar_Compiler_Range_Type.ml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 28 additions & 24 deletions src/basic/FStar.Compiler.Range.Ops.fst
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,31 @@ friend FStar.Compiler.Range.Type
open FStar.Json
open FStar.Compiler.Effect
open FStar.Compiler.Util
open FStar.Class.Ord

module Options = FStar.Options

let union_rng r1 r2 =
if r1.file_name <> r2.file_name then r2
else let start_pos =
if pos_geq r1.start_pos r2.start_pos then
r2.start_pos
else r1.start_pos in
let end_pos =
if pos_geq r1.end_pos r2.end_pos then
r1.end_pos
else r2.end_pos in
mk_rng r1.file_name start_pos end_pos
if r1.file_name <> r2.file_name
then r2
else
let start_pos = min r1.start_pos r2.start_pos in
let end_pos = max r1.end_pos r2.end_pos in
mk_rng r1.file_name start_pos end_pos

let union_ranges r1 r2 = {
def_range=union_rng r1.def_range r2.def_range;
use_range=union_rng r1.use_range r2.use_range
}

(* is r1 included in r2? *)
let rng_included r1 r2 =
if r1.file_name <> r2.file_name then false
else pos_geq r1.start_pos r2.start_pos &&
pos_geq r2.end_pos r1.end_pos
if r1.file_name <> r2.file_name
then false
else
r2.start_pos <=? r1.start_pos &&
r2.end_pos >=? r1.end_pos

let string_of_pos pos =
format2 "%s,%s" (string_of_int pos.line) (string_of_int pos.col)
let string_of_file_name f =
Expand Down Expand Up @@ -95,7 +98,8 @@ let compare_rng r1 r2 =
let compare r1 r2 = compare_rng r1.def_range r2.def_range
let compare_use_range r1 r2 = compare_rng r1.use_range r2.use_range
let range_before_pos m1 p =
pos_geq p (end_of_range m1)
p >=? end_of_range m1

let end_of_line p = {p with col=max_int}
let extend_to_end_of_line r = mk_range (file_of_range r)
(start_of_range r)
Expand All @@ -122,16 +126,16 @@ let json_of_def_range r =
(end_of_range r)

let intersect_rng r1 r2 =
if r1.file_name <> r2.file_name then r2
else let start_pos =
if pos_geq r1.start_pos r2.start_pos then
r1.start_pos
else r2.start_pos in
let end_pos =
if pos_geq r1.end_pos r2.end_pos then
r2.end_pos
else r1.end_pos in
mk_rng r1.file_name start_pos end_pos
if r1.file_name <> r2.file_name
then r2
else
let start_pos = max r1.start_pos r2.start_pos in
let end_pos = min r1.end_pos r2.end_pos in
(* If start_pos > end_pos, then the intersection is empty, just take the bound *)
if start_pos >=? end_pos
then r2
else mk_rng r1.file_name start_pos end_pos

let intersect_ranges r1 r2 = {
def_range=intersect_rng r1.def_range r2.def_range;
use_range=intersect_rng r1.use_range r2.use_range
Expand Down
16 changes: 13 additions & 3 deletions src/basic/FStar.Compiler.Range.Type.fst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
module FStar.Compiler.Range.Type

open FStar.Compiler.Effect
open FStar.Class.Deq
open FStar.Class.Ord
open FStar.Compiler.Order

[@@ PpxDerivingYoJson; PpxDerivingShow ]
type file_name = string
Expand All @@ -26,9 +29,16 @@ type pos = {
col: int
}
let max i j = if i < j then j else i
let pos_geq p1 p2 =
(p1.line > p2.line ||
(p1.line = p2.line && p1.col >= p2.col))

let compare_pos (p1 p2 : pos) : order =
lex (cmp p1.line p2.line) (fun _ -> cmp p1.col p2.col)

instance deq_pos : deq pos = { (=?) = (=); }

instance ord_pos : ord pos = {
super = deq_pos;
cmp = compare_pos;
}

[@@ PpxDerivingYoJson; PpxDerivingShow ]
type rng = {
Expand Down
5 changes: 5 additions & 0 deletions src/basic/FStar.Compiler.Range.Type.fsti
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
module FStar.Compiler.Range.Type

open FStar.Compiler.Effect
open FStar.Class.Deq
open FStar.Class.Ord

[@@ PpxDerivingYoJson; PpxDerivingShow]
new val rng : Type0
Expand All @@ -26,6 +28,9 @@ new val range : Type0
[@@ PpxDerivingYoJson; PpxDerivingShow]
new val pos : Type0

instance val deq_pos : deq pos
instance val ord_pos : ord pos

val dummy_rng : rng
val mk_rng : string -> pos -> pos -> rng

Expand Down
3 changes: 3 additions & 0 deletions src/class/FStar.Class.Ord.fst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ let (<=?) x y = cmp x y <> Gt
let (>?) x y = cmp x y = Gt
let (>=?) x y = cmp x y <> Lt

let min x y = if x <=? y then x else y
let max x y = if x >=? y then x else y

instance ord_eq (a:Type) (d : ord a) : Tot (deq a) = d.super

let rec insert (#a:Type) {| ord a |} (x:a) (xs:list a) : list a =
Expand Down
3 changes: 3 additions & 0 deletions src/class/FStar.Class.Ord.fsti
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ val (<=?) : #a:Type -> {| ord a |} -> a -> a -> bool
val (>?) : #a:Type -> {| ord a |} -> a -> a -> bool
val (>=?) : #a:Type -> {| ord a |} -> a -> a -> bool

val min : #a:Type -> {| ord a |} -> a -> a -> a
val max : #a:Type -> {| ord a |} -> a -> a -> a

instance val ord_int : ord int
instance val ord_bool : ord bool
instance val ord_unit : ord unit
Expand Down
Loading