Skip to content

Commit

Permalink
Import komo changes bd16w6 (#56)
Browse files Browse the repository at this point in the history
* kxclib - fix Direct_io.bind regarding exception handling
* kxclib - fix Int53p FloatImpl.{succ,pred}
* kxclib - add MonadOps.{mlift,mwrap,do_cond,do_if}
  • Loading branch information
haochenx authored Apr 20, 2024
1 parent 11e5f17 commit 4f7fdd9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
22 changes: 19 additions & 3 deletions classic/kxclib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ module type MonadOpsS = sig
(** monadic version of {!constant} *)
val returning : 'a -> 'b -> 'a t

val mlift : ('a -> 'b) -> ('a -> 'b t)
val mwrap : ('a -> 'b) -> ('a t -> 'b t)
val do_cond : bool -> ('a -> 'b t) -> ('a -> 'b t) -> 'a -> 'b t
val do_if : bool -> ('a -> unit t) -> 'a -> unit t

val sequence_list : 'a t list -> 'a list t

(** monadic binding version of {!sequence_list} *)
Expand Down Expand Up @@ -432,6 +437,14 @@ module MonadOps(M : sig

let returning x = fun _ -> return x

let mlift = fun f x -> f x |> return

let mwrap = fun f m -> m >>= mlift f

let do_cond = fun c f1 f2 -> (if c then f1 else f2)

let do_if = fun c f -> do_cond c f (returning ())

let sequence_list ms =
List.fold_left (fun acc m ->
acc >>= fun acc ->
Expand Down Expand Up @@ -1680,8 +1693,8 @@ module Int53p = struct
let zero = Float.zero
let one = Float.one
let minus_one = Float.minus_one
let succ = Float.succ
let pred = Float.pred
let succ n = n +. 1.
let pred n = n -. 1.
let neg = Float.neg
let add = Float.add
let sub = Float.sub
Expand Down Expand Up @@ -2420,7 +2433,6 @@ module Direct_io = struct
type 'x t = ('x, exn * Backtrace_info.t) result [@@deriving show]

let return : 'x -> 'x t = fun x -> Result.ok x
let bind : 'x t -> ('x -> 'y t) -> 'y t = fun x f -> Result.bind x f

[%%if mel]
let inject_error' : exn * backtrace_info option -> 'x t =
Expand Down Expand Up @@ -2453,6 +2465,10 @@ module Direct_io = struct
Log0.log ~label:"trace" ~header_style:(Some `Thin) ~header_color:`Yellow
"%s" s;
Ok ()

let bind : 'x t -> ('x -> 'y t) -> 'y t = fun x f ->
try Result.bind x f
with e -> inject_error e
end
module CheckDirectIo : Io_style = Direct_io

Expand Down
15 changes: 15 additions & 0 deletions intf/kxclib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ module type MonadOpsS = sig
(** monadic version of {!constant} *)
val returning : 'a -> 'b -> 'a t

val mlift : ('a -> 'b) -> ('a -> 'b t)

val mwrap : ('a -> 'b) -> ('a t -> 'b t)

(** [m >>= do_cond cond f_true f_false] performs [f_true] or [f_false]
on value enclosed in [m],
respectively when [cond] is [true] or [false];
functionally equiv. to [fun c f1 f2 -> if c then f1 else f2] but
return type of [f1] and [f2] is restricted to [_ t] *)
val do_cond : bool -> ('a -> 'b t) -> ('a -> 'b t) -> 'a -> 'b t

(** [m >>= do_if cond f] is equiv. to [m >>= do_cond cond f (returning ())] *)
val do_if : bool -> ('a -> unit t) -> 'a -> unit t

val sequence_list : 'a t list -> 'a list t

(** monadic binding version of {!sequence_list} *)
Expand Down

0 comments on commit 4f7fdd9

Please sign in to comment.