Skip to content

Commit

Permalink
Clean up monad handling
Browse files Browse the repository at this point in the history
Make it more like the way we handle applicative

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg committed Mar 18, 2020
1 parent c912149 commit e72ee32
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 22 deletions.
18 changes: 5 additions & 13 deletions src/dune/coq_lib.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(* This file is licensed under The MIT License *)
(* (c) MINES ParisTech 2018-2019 *)
(* Written by: Emilio Jesús Gallego Arias *)
(* Written by: Emilio Jesús Gallego Arias *)

open! Stdune

Expand Down Expand Up @@ -117,17 +117,7 @@ module DB = struct

let find_many t ~loc = Result.List.map ~f:(fun name -> resolve t (loc, name))

(* Where should we move this? *)
module Result_monad : Monad_intf.S with type 'a t = 'a Or_exn.t =
struct
type 'a t = 'a Or_exn.t

let return x = Ok x

let ( >>= ) = Result.O.( >>= )
end

module Coq_lib_closure = Top_closure.Make (String.Set) (Result_monad)
module Coq_lib_closure = Top_closure.Make (String.Set) (Or_exn)

let requires db t : lib list Or_exn.t =
let theories =
Expand All @@ -139,7 +129,9 @@ module DB = struct
in
let open Result.O in
let allow_private_deps = Option.is_none t.package in
let* theories = Result.List.map ~f:(resolve ~allow_private_deps db) theories in
let* theories =
Result.List.map ~f:(resolve ~allow_private_deps db) theories
in
let key t = Coq_lib_name.to_string (snd t.name) in
let deps t =
Result.List.map ~f:(resolve ~allow_private_deps db) t.theories
Expand Down
2 changes: 1 addition & 1 deletion src/dune/coq_rules.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(* This file is licensed under The MIT License *)
(* (c) MINES ParisTech 2018-2019 *)
(* (c) INRIA 2020 *)
(* Written by: Emilio Jesús Gallego Arias *)
(* Written by: Emilio Jesús Gallego Arias *)

open! Stdune
module SC = Super_context
Expand Down
19 changes: 17 additions & 2 deletions src/stdune/monad.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
module Id = struct
module Make (M : Monad_intf.S1_base) = struct
include M

module O = struct
let ( let+ ) x f = M.( >>= ) x (fun x -> M.return (f x))

let ( and+ ) x y =
let open M in
x >>= fun x ->
y >>= fun y -> return (x, y)

let ( let* ) = M.( >>= )
end
end

module Id = Make (struct
type 'a t = 'a

let return x = x

let ( >>= ) x f = f x
end
end)
4 changes: 3 additions & 1 deletion src/stdune/monad.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(** Monad signatures *)

module Id : Monad_intf.S with type 'a t = 'a
module Make (M : Monad_intf.S1_base) : Monad_intf.S1 with type 'a t := 'a M.t

module Id : Monad_intf.S1 with type 'a t = 'a
14 changes: 13 additions & 1 deletion src/stdune/monad_intf.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
module type S = sig
module type S1_base = sig
type 'a t

val return : 'a -> 'a t

val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t
end

module type S1 = sig
include S1_base

module O : sig
val ( let+ ) : 'a t -> ('a -> 'b) -> 'b t

val ( and+ ) : 'a t -> 'b t -> ('a * 'b) t

val ( let* ) : 'a t -> ('a -> 'b t) -> 'b t
end
end
12 changes: 10 additions & 2 deletions src/stdune/or_exn.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
type 'a t = ('a, exn) Result.t

let equal eq x y = Result.equal eq Exn.equal x y

let hash h = Result.hash h Exn.hash

let to_dyn f = Result.to_dyn f Exn.to_dyn

type 'a t = ('a, exn) Result.t

include Monad.Make (struct
type nonrec 'a t = 'a t

let return = Result.return

let ( >>= ) = Result.( >>= )
end)
2 changes: 2 additions & 0 deletions src/stdune/or_exn.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val hash : ('a -> int) -> 'a t -> int

val to_dyn : ('a -> Dyn.t) -> 'a t Dyn.Encoder.t

include Monad_intf.S1 with type 'a t := 'a t
4 changes: 4 additions & 0 deletions src/stdune/result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type ('a, 'error) t = ('a, 'error) result =

let ok x = Ok x

let return = ok

let is_ok = function
| Ok _ -> true
| Error _ -> false
Expand All @@ -26,6 +28,8 @@ let bind t ~f =
| Ok x -> f x
| Error _ as t -> t

let ( >>= ) x f = bind x ~f

let map x ~f =
match x with
| Ok x -> Ok (f x)
Expand Down
4 changes: 4 additions & 0 deletions src/stdune/result.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type ('a, 'error) t = ('a, 'error) result =
| Ok of 'a
| Error of 'error

val return : 'a -> ('a, _) t

val ok : 'a -> ('a, _) t

val is_ok : _ t -> bool
Expand Down Expand Up @@ -37,6 +39,8 @@ val map : ('a, 'error) t -> f:('a -> 'b) -> ('b, 'error) t

val bind : ('a, 'error) t -> f:('a -> ('b, 'error) t) -> ('b, 'error) t

val ( >>= ) : ('a, 'error) t -> ('a -> ('b, 'error) t) -> ('b, 'error) t

val map_error : ('a, 'error1) t -> f:('error1 -> 'error2) -> ('a, 'error2) t

val to_option : ('a, 'error) t -> 'a option
Expand Down
2 changes: 1 addition & 1 deletion src/stdune/top_closure.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Make (Keys : Top_closure_intf.Keys) (Monad : Monad_intf.S) = struct
module Make (Keys : Top_closure_intf.Keys) (Monad : Monad_intf.S1) = struct
open Monad

let top_closure ~key ~deps elements =
Expand Down
2 changes: 1 addition & 1 deletion src/stdune/top_closure.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module Int :
module String :
Top_closure_intf.S with type key := string and type 'a monad := 'a Monad.Id.t

module Make (Keys : Top_closure_intf.Keys) (Monad : Monad_intf.S) :
module Make (Keys : Top_closure_intf.Keys) (Monad : Monad_intf.S1) :
Top_closure_intf.S with type key := Keys.elt and type 'a monad := 'a Monad.t
[@@inlined always]

0 comments on commit e72ee32

Please sign in to comment.