From 0ecad6464087f23c0fa963fdf41570d9c7933e63 Mon Sep 17 00:00:00 2001
From: Thibault Martin <thi.martin.pro@pm.me>
Date: Fri, 26 Apr 2024 17:55:18 +0200
Subject: [PATCH] [Kernel] Add a module in lib/utils for Result monadic
 operators

---
 src/libraries/utils/result.ml  | 36 ++++++++++++++++++++++++++++++++++
 src/libraries/utils/result.mli | 35 +++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 src/libraries/utils/result.ml
 create mode 100644 src/libraries/utils/result.mli

diff --git a/src/libraries/utils/result.ml b/src/libraries/utils/result.ml
new file mode 100644
index 0000000000..de456a3032
--- /dev/null
+++ b/src/libraries/utils/result.ml
@@ -0,0 +1,36 @@
+(**************************************************************************)
+(*                                                                        *)
+(*  This file is part of Frama-C.                                         *)
+(*                                                                        *)
+(*  Copyright (C) 2007-2024                                               *)
+(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
+(*         alternatives)                                                  *)
+(*                                                                        *)
+(*  you can redistribute it and/or modify it under the terms of the GNU   *)
+(*  Lesser General Public License as published by the Free Software       *)
+(*  Foundation, version 2.1.                                              *)
+(*                                                                        *)
+(*  It is distributed in the hope that it will be useful,                 *)
+(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
+(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
+(*  GNU Lesser General Public License for more details.                   *)
+(*                                                                        *)
+(*  See the GNU Lesser General Public License version 2.1                 *)
+(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
+(*                                                                        *)
+(**************************************************************************)
+
+include Stdlib.Result
+
+let zip l r =
+  match l, r with
+  | Ok l, Ok r -> Ok (l, r)
+  | Error e, _ -> Error e
+  | _, Error e -> Error e
+
+module Operators = struct
+  let ( let* ) r f = bind r f
+  let ( let+ ) r f = map f r
+  let ( and* ) l r = zip l r
+  let ( and+ ) l r = zip l r
+end
diff --git a/src/libraries/utils/result.mli b/src/libraries/utils/result.mli
new file mode 100644
index 0000000000..abbdeb65b2
--- /dev/null
+++ b/src/libraries/utils/result.mli
@@ -0,0 +1,35 @@
+(**************************************************************************)
+(*                                                                        *)
+(*  This file is part of Frama-C.                                         *)
+(*                                                                        *)
+(*  Copyright (C) 2007-2024                                               *)
+(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
+(*         alternatives)                                                  *)
+(*                                                                        *)
+(*  you can redistribute it and/or modify it under the terms of the GNU   *)
+(*  Lesser General Public License as published by the Free Software       *)
+(*  Foundation, version 2.1.                                              *)
+(*                                                                        *)
+(*  It is distributed in the hope that it will be useful,                 *)
+(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
+(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
+(*  GNU Lesser General Public License for more details.                   *)
+(*                                                                        *)
+(*  See the GNU Lesser General Public License version 2.1                 *)
+(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
+(*                                                                        *)
+(**************************************************************************)
+
+(* Adding let binding operators to the Result module. See
+   https://v2.ocaml.org/manual/bindingops.html for more information. *)
+
+include module type of Stdlib.Result
+
+val zip : ('a, 'e) result -> ('b, 'e) result -> ('a * 'b, 'e) result
+
+module Operators : sig
+  val ( let* ) : ('a, 'e) result -> ('a -> ('b, 'e) result) -> ('b, 'e) result
+  val ( let+ ) : ('a, 'e) result -> ('a -> 'b) -> ('b, 'e) result
+  val ( and* ) : ('a, 'e) result -> ('b, 'e) result -> ('a * 'b, 'e) result
+  val ( and+ ) : ('a, 'e) result -> ('b, 'e) result -> ('a * 'b, 'e) result
+end
-- 
GitLab