Skip to content

Commit

Permalink
Update more code to use let*
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg committed Apr 3, 2019
1 parent 73cf910 commit 126177d
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 121 deletions.
8 changes: 5 additions & 3 deletions src/action_exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ and exec_list l ~ectx ~dir ~env ~stdout_to ~stderr_to =
| [t] ->
exec t ~ectx ~dir ~env ~stdout_to ~stderr_to
| t :: rest ->
(let stdout_to = Process.Output.multi_use stdout_to in
let stderr_to = Process.Output.multi_use stderr_to in
exec t ~ectx ~dir ~env ~stdout_to ~stderr_to) >>= fun () ->
let* () =
let stdout_to = Process.Output.multi_use stdout_to in
let stderr_to = Process.Output.multi_use stderr_to in
exec t ~ectx ~dir ~env ~stdout_to ~stderr_to
in
exec_list rest ~ectx ~dir ~env ~stdout_to ~stderr_to

let exec ~targets ~context ~env t =
Expand Down
10 changes: 4 additions & 6 deletions src/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
in
let get_tool_using_findlib_config prog =
let open Option.O in
findlib_config >>= fun conf ->
Findlib.Config.get conf prog >>= fun s ->
let* conf = findlib_config in
let* s = Findlib.Config.get conf prog in
match Filename.analyze_program_name s with
| In_path | Relative_to_current_dir -> which s
| Absolute -> Some (Path.of_filename_relative_to_initial_cwd s)
Expand Down Expand Up @@ -559,8 +559,7 @@ let create_for_opam ~root ~env ~env_nodes ~targets ~profile
| None -> Utils.program_not_found "opam" ~loc:None
| Some fn -> fn
in
opam_version opam env
>>= fun version ->
let* version = opam_version opam env in
let args =
List.concat
[ [ "config"; "env" ]
Expand All @@ -571,8 +570,7 @@ let create_for_opam ~root ~env ~env_nodes ~targets ~profile
; if version < (2, 0, 0) then [] else ["--set-switch"]
]
in
Process.run_capture ~env Strict opam args
>>= fun s ->
let* s = Process.run_capture ~env Strict opam args in
let vars =
Dune_lang.parse_string ~fname:"<opam output>" ~mode:Single s
|> Dune_lang.Decoder.(parse (list (pair string string)) Univ_map.empty)
Expand Down
28 changes: 14 additions & 14 deletions src/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let relative_file =

let variants_field =
field_o "variants" (
Syntax.since Stanza.syntax (1, 9) >>= fun () ->
let* () = Syntax.since Stanza.syntax (1, 9) in
located (list Variant.decode >>| Variant.Set.of_list))

(* Parse and resolve "package" fields *)
Expand Down Expand Up @@ -904,30 +904,30 @@ module Library = struct
and+ no_keep_locs = field_b "no_keep_locs"
~check:(Syntax.deprecated_in Stanza.syntax (1, 7))
and+ sub_systems =
return () >>= fun () ->
let* () = return () in
Sub_system_info.record_parser ()
and+ project = Dune_project.get_exn ()
and+ dune_version = Syntax.get_exn Stanza.syntax
and+ virtual_modules =
field_o "virtual_modules" (
Syntax.since Stanza.syntax (1, 7)
>>= fun () -> Ordered_set_lang.decode)
Syntax.since Stanza.syntax (1, 7) >>>
Ordered_set_lang.decode)
and+ implements =
field_o "implements" (
Syntax.since Stanza.syntax (1, 7)
>>= fun () -> located Lib_name.decode)
Syntax.since Stanza.syntax (1, 7) >>>
located Lib_name.decode)
and+ variant =
field_o "variant" (
Syntax.since Stanza.syntax (1, 9)
>>= fun () -> located Variant.decode)
Syntax.since Stanza.syntax (1, 9) >>>
located Variant.decode)
and+ default_implementation =
field_o "default_implementation" (
Syntax.since Stanza.syntax (1, 9)
>>= fun () -> located Lib_name.decode)
Syntax.since Stanza.syntax (1, 9) >>>
located Lib_name.decode)
and+ private_modules =
field_o "private_modules" (
Syntax.since Stanza.syntax (1, 2)
>>= fun () -> Ordered_set_lang.decode)
let* () = Syntax.since Stanza.syntax (1, 2) in
Ordered_set_lang.decode)
and+ stdlib =
field_o "stdlib" (Syntax.since Stdlib.syntax (0, 1) >>> Stdlib.decode)
in
Expand Down Expand Up @@ -2121,11 +2121,11 @@ module Stanzas = struct
(let+ x = Dune_env.Stanza.decode in
[Dune_env.T x])
; "include_subdirs",
(Dune_project.get_exn () >>= fun p ->
(let* project = Dune_project.get_exn () in
let+ () = Syntax.since Stanza.syntax (1, 1)
and+ t =
let enable_qualified =
Option.is_some (Dune_project.find_extension_args p Coq.key) in
Option.is_some (Dune_project.find_extension_args project Coq.key) in
Include_subdirs.decode ~enable_qualified
and+ loc = loc in
[Include_subdirs (loc, t)])
Expand Down
10 changes: 6 additions & 4 deletions src/dune_package.ml
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,13 @@ module Or_meta = struct

let decode ~dir =
let open Dune_lang.Decoder in
(* fields @@ *)
fields
(field_b "use_meta" >>= function
| true -> return Use_meta
| false -> decode ~dir >>| fun p -> Dune_package p)
(let* use_meta = field_b "use_meta" in
if use_meta then
return Use_meta
else
let+ package = decode ~dir in
Dune_package package)

let load p = Vfile.load p ~f:(fun _ -> decode ~dir:(Path.parent_exn p))
end
24 changes: 15 additions & 9 deletions src/fiber/fiber.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,17 @@ let map t ~f = t >>| f
let bind t ~f = t >>= f

let both a b =
a >>= fun x ->
b >>= fun y ->
let* x = a in
let* y = b in
return (x, y)

let all l =
let rec loop l acc =
match l with
| [] -> return (List.rev acc)
| t :: l -> t >>= fun x -> loop l (x :: acc)
| t :: l ->
let* x = t in
loop l (x :: acc)
in
loop l []

Expand All @@ -228,15 +230,19 @@ let map_all l ~f =
let rec loop l acc =
match l with
| [] -> return (List.rev acc)
| x :: l -> f x >>= fun x -> loop l (x :: acc)
| x :: l ->
let* x = f x in
loop l (x :: acc)
in
loop l []

let map_all_unit l ~f =
let rec loop l =
match l with
| [] -> return ()
| x :: l -> f x >>= fun () -> loop l
| x :: l ->
let* () = f x in
loop l
in
loop l

Expand Down Expand Up @@ -361,8 +367,8 @@ let collect_errors f =
~on_error:(fun e l -> e :: l)

let finalize f ~finally =
wait_errors f >>= fun res ->
finally () >>= fun () ->
let* res = wait_errors f in
let* () = finally () in
match res with
| Ok x -> return x
| Error () -> never
Expand Down Expand Up @@ -441,7 +447,7 @@ module Once = struct
| Running fut -> Future.wait fut
| Not_started f ->
t.state <- Starting;
fork f >>= fun fut ->
let* fut = fork f in
t.state <- Running fut;
Future.wait fut
| Starting ->
Expand Down Expand Up @@ -478,7 +484,7 @@ module Mutex = struct
k ()

let with_lock t f =
lock t >>= fun () ->
let* () = lock t in
finalize f ~finally:(fun () -> unlock t)

let create () =
Expand Down
4 changes: 2 additions & 2 deletions src/file_bindings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ module Unexpanded = struct
{ src; dst = None }
| List (_, [_; Atom (_, A "as"); _]) ->
enter
(decode >>= fun src ->
(let* src = decode in
keyword "as" >>>
decode >>= fun dst ->
let* dst = decode in
return { src; dst = Some dst })
| sexp ->
of_sexp_error (Dune_lang.Ast.loc sexp)
Expand Down
2 changes: 1 addition & 1 deletion src/installed_dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ let of_sexp =
in
sum
[ "dune",
(version >>= fun version ->
(let* version = version in
set (Syntax.key Stanza.syntax) version
(let+ parsing_context = get_all
and+ sub_systems = list raw
Expand Down
16 changes: 9 additions & 7 deletions src/merlin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,18 @@ let pp_flags sctx ~expander ~dir_kind { preprocess; libname; _ } =
begin match action with
| Run (exe, args) ->
let open Option.O in
List.destruct_last args >>= (fun (args, input_file) ->
let* (args, input_file) = List.destruct_last args in
let* args =
if String_with_vars.is_var input_file ~name:"input-file" then
Some args
else
None)
>>= fun args ->
Expander.Option.expand_path expander exe >>= fun exe ->
List.map ~f:(Expander.Option.expand_str expander) args
|> Option.List.all
>>= fun args ->
None
in
let* exe = Expander.Option.expand_path expander exe in
let* args =
List.map ~f:(Expander.Option.expand_str expander) args
|> Option.List.all
in
(Path.to_absolute_filename exe :: args)
|> List.map ~f:quote_for_merlin
|> String.concat ~sep:" "
Expand Down
1 change: 1 addition & 0 deletions src/ocaml-config/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(library
(name ocaml_config)
(public_name dune._ocaml_config)
(preprocess future_syntax)
(libraries stdune)
(synopsis "[Internal] Interpret the output of 'ocamlc -config'"))
2 changes: 1 addition & 1 deletion src/ocaml-config/ocaml_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ module Vars = struct
| None ->
Error (Printf.sprintf "Unrecognized line: %S" line)
in
loop [] lines >>= fun vars ->
let* vars = loop [] lines in
Result.map_error (String.Map.of_list vars) ~f:(fun (var, _, _) ->
Printf.sprintf "Variable %S present twice." var)

Expand Down
6 changes: 3 additions & 3 deletions src/ordered_set_lang.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ module Parse = struct
let+ to_remove = junk >>> many [] kind in
Diff (Union (List.rev acc), to_remove)
| Some _ ->
one kind >>= fun x ->
let* x = one kind in
many (x :: acc) kind
in
Stanza.file_kind () >>= fun kind ->
let* kind = Stanza.file_kind () in
match kind with
| Dune -> many [] kind
| Jbuild -> one kind
Expand All @@ -85,7 +85,7 @@ module Parse = struct
let without_include ~elt =
generic ~elt ~inc:(
enter
(loc >>= fun loc ->
(let* loc = loc in
Errors.fail loc "(:include ...) is not allowed here"))
end

Expand Down
6 changes: 3 additions & 3 deletions src/predicate_lang.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Ast = struct

let decode elt =
let open Stanza.Decoder in
let elt = elt >>| fun e -> Element e in
let elt = let+ e = elt in Element e in
let rec one (kind : Dune_lang.Syntax.t) =
peek_exn >>= function
| Atom (loc, A "\\") -> Errors.fail loc "unexpected \\"
Expand Down Expand Up @@ -61,10 +61,10 @@ module Ast = struct
junk >>> many union [] kind >>| fun to_remove ->
diff (k (List.rev acc)) to_remove
| Some _ ->
one kind >>= fun x ->
let* x = one kind in
many k (x :: acc) kind
in
Stanza.file_kind () >>= fun kind ->
let* kind = Stanza.file_kind () in
match kind with
| Dune -> many union [] kind
| Jbuild -> one kind
Expand Down
5 changes: 2 additions & 3 deletions src/preprocessing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,8 @@ let build_ppx_driver sctx ~dep_kind ~target ~dir_kind ~pps ~pp_names =
(* Extend the dependency stack as we don't have locations at
this point *)
Dep_path.prepend_exn e (Preprocess pp_names))
(pps
>>= Lib.closure ~linking:true
>>= fun pps ->
(let* pps = pps in
let* pps = Lib.closure ~linking:true pps in
match jbuild_driver with
| None ->
let+ driver =
Expand Down
41 changes: 21 additions & 20 deletions src/print_diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ let print ?(skip_trailing_cr=Sys.win32) path1 path2 =
| None -> fallback ()
| Some prog ->
Format.eprintf "%a@?" Errors.print loc;
Process.run ~dir ~env:Env.initial Strict prog
(List.concat
[ ["-u"]
; if skip_trailing_cr then ["--strip-trailing-cr"] else []
; [ file1; file2 ]
])
>>= fun () ->
let* () =
Process.run ~dir ~env:Env.initial Strict prog
(List.concat
[ ["-u"]
; if skip_trailing_cr then ["--strip-trailing-cr"] else []
; [ file1; file2 ]
])
in
fallback ()
in
match !Clflags.diff_command with
Expand All @@ -42,8 +43,7 @@ let print ?(skip_trailing_cr=Sys.win32) path1 path2 =
let cmd =
sprintf "%s %s %s" cmd (quote_for_shell file1) (quote_for_shell file2)
in
Process.run ~dir ~env:Env.initial Strict sh [arg; cmd]
>>= fun () ->
let* () = Process.run ~dir ~env:Env.initial Strict sh [arg; cmd] in
die "command reported no differences: %s"
(if Path.is_root dir then
cmd
Expand All @@ -56,16 +56,17 @@ let print ?(skip_trailing_cr=Sys.win32) path1 path2 =
match Bin.which ~path:(Env.path Env.initial) "patdiff" with
| None -> normal_diff ()
| Some prog ->
Process.run ~dir ~env:Env.initial Strict prog
[ "-keep-whitespace"
; "-location-style"; "omake"
; if Lazy.force Colors.stderr_supports_colors then
"-unrefined"
else
"-ascii"
; file1
; file2
]
>>= fun () ->
let* () =
Process.run ~dir ~env:Env.initial Strict prog
[ "-keep-whitespace"
; "-location-style"; "omake"
; if Lazy.force Colors.stderr_supports_colors then
"-unrefined"
else
"-ascii"
; file1
; file2
]
in
(* Use "diff" if "patdiff" reported no differences *)
normal_diff ()
Loading

0 comments on commit 126177d

Please sign in to comment.