Skip to content

Commit

Permalink
Update various utils
Browse files Browse the repository at this point in the history
  • Loading branch information
NatKarmios committed Dec 5, 2023
1 parent 6d6eb8c commit 4a89512
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
9 changes: 9 additions & 0 deletions GillianCore/utils/ext_list.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ let append x l =
l.last <- cell

let add = append
let add_all xs t = List.iter (fun x -> add x t) xs
let length t = t.length

let to_list t =
Expand Down Expand Up @@ -210,6 +211,14 @@ let exists f l =
in
aux l.first

let assoc_opt a l =
let rec aux = function
| Nil -> None
| Cons { contents = k, v; _ } when a = k -> Some v
| Cons { next; _ } -> aux next
in
aux l.first

let nth n l =
if n >= l.length || n < 0 then None
else
Expand Down
2 changes: 2 additions & 0 deletions GillianCore/utils/ext_list.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ val clear : 'a t -> unit
val prepend : 'a -> 'a t -> unit
val append : 'a -> 'a t -> unit
val add : 'a -> 'a t -> unit
val add_all : 'a list -> 'a t -> unit
val length : 'a t -> int
val to_list : 'a t -> 'a list
val of_list : 'a list -> 'a t
Expand All @@ -31,6 +32,7 @@ val iter : ('a -> unit) -> 'a t -> unit
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool

val exists : ('a -> bool) -> 'a t -> bool
val assoc_opt : 'a -> ('a * 'b) t -> 'b option
val remove_duplicates : ?equal:('a -> 'a -> bool) -> 'a t -> unit

(** Filter-maps the list in place according to [f]
Expand Down
10 changes: 10 additions & 0 deletions GillianCore/utils/list_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ let map_results f l =
in
aux [] l

let iter_results f l =
let rec aux = function
| [] -> Ok ()
| x :: xs -> (
match f x with
| Ok () -> aux xs
| Error e -> Error e)
in
aux l

let for_alli f l =
let rec aux i = function
| [] -> true
Expand Down
4 changes: 4 additions & 0 deletions GillianCore/utils/result_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ let of_option ~none x =
match x with
| None -> Error none
| Some x -> Ok x

let or_else f = function
| Ok x -> x
| Error e -> f e
1 change: 0 additions & 1 deletion GillianCore/utils/result_utils.mli

This file was deleted.

37 changes: 36 additions & 1 deletion GillianCore/utils/syntaxes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,43 @@ end

(** @canonical Gillian.Utils.Syntaxes.Option *)
module Option = struct
let ( let+ ) f o = Option.map o f
let ( let+ ) o f = Option.map f o
let ( let* ) o f = Option.bind o f

let ( let- ) o f =
match o with
| None -> f ()
| Some o -> o

let ( let/ ) o f =
match o with
| None -> f ()
| o -> o
end

(** @canonical Gillian.Utils.Syntaxes.Result_of_option *)
module Result_of_option = struct
let ( let=* ) o f =
match o with
| Ok (Some o) -> f o
| Ok None -> Ok None
| Error e -> Error e

let ( let=+ ) o f =
match o with
| Ok (Some o) -> Ok (Some (f o))
| Ok None -> Ok None
| Error e -> Error e

let ( let=/ ) o f =
match o with
| Ok None -> f ()
| o -> o

let ( let=- ) o f =
match o with
| Ok None -> Ok (Some (f ()))
| o -> o
end

(** @canonical Gillian.Utils.Syntaxes.List *)
Expand Down

0 comments on commit 4a89512

Please sign in to comment.