Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/Compiler/Facilities/DiagnosticsLogger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,8 @@ let CheckNoErrorsAndGetWarnings res =
| OkResult (warns, res2) -> Some(warns, res2)
| ErrorResult _ -> None

/// The bind in the monad. Stop on first error. Accumulate warnings and continue.
[<DebuggerHidden; DebuggerStepThrough>]
let (++) res f =
let bind f res =
match res with
| OkResult ([], res) -> (* tailcall *) f res
| OkResult (warns, res) ->
Expand All @@ -673,30 +672,30 @@ let (++) res f =
let rec IterateD f xs =
match xs with
| [] -> CompleteD
| h :: t -> f h ++ (fun () -> IterateD f t)
| h :: t -> f h |> bind (fun () -> IterateD f t)

[<DebuggerHidden; DebuggerStepThrough>]
let rec WhileD gd body =
if gd () then
body () ++ (fun () -> WhileD gd body)
body () |> bind (fun () -> WhileD gd body)
else
CompleteD

[<DebuggerHidden; DebuggerStepThrough>]
let rec MapD_loop f acc xs =
match xs with
| [] -> ResultD(List.rev acc)
| h :: t -> f h ++ (fun x -> MapD_loop f (x :: acc) t)
| h :: t -> f h |> bind (fun x -> MapD_loop f (x :: acc) t)

[<DebuggerHidden; DebuggerStepThrough>]
let MapD f xs = MapD_loop f [] xs

type TrackErrorsBuilder() =
member x.Bind(res, k) = res ++ k
member x.Bind(res, k) = bind k res
member x.Return res = ResultD res
member x.ReturnFrom res = res
member x.For(seq, k) = IterateD k seq
member x.Combine(expr1, expr2) = expr1 ++ expr2
member x.Combine(expr1, expr2) = bind expr2 expr1
member x.While(gd, k) = WhileD gd k
member x.Zero() = CompleteD
member x.Delay fn = fun () -> fn ()
Expand All @@ -717,7 +716,7 @@ let IterateIdxD f xs =
let rec loop xs i =
match xs with
| [] -> CompleteD
| h :: t -> f i h ++ (fun () -> loop t (i + 1))
| h :: t -> f i h |> bind (fun () -> loop t (i + 1))

loop xs 0

Expand All @@ -726,7 +725,7 @@ let IterateIdxD f xs =
let rec Iterate2D f xs ys =
match xs, ys with
| [], [] -> CompleteD
| h1 :: t1, h2 :: t2 -> f h1 h2 ++ (fun () -> Iterate2D f t1 t2)
| h1 :: t1, h2 :: t2 -> f h1 h2 |> bind (fun () -> Iterate2D f t1 t2)
| _ -> failwith "Iterate2D"

/// Keep the warnings, propagate the error to the exception continuation.
Expand All @@ -742,11 +741,12 @@ let TryD f g =

[<DebuggerHidden; DebuggerStepThrough>]
let rec RepeatWhileD nDeep body =
body nDeep ++ (fun x -> if x then RepeatWhileD (nDeep + 1) body else CompleteD)
body nDeep
|> bind (fun x -> if x then RepeatWhileD (nDeep + 1) body else CompleteD)

[<DebuggerHidden; DebuggerStepThrough>]
let inline AtLeastOneD f l =
MapD f l ++ (fun res -> ResultD(List.exists id res))
MapD f l |> bind (fun res -> ResultD(List.exists id res))

[<DebuggerHidden; DebuggerStepThrough>]
let inline AtLeastOne2D f xs ys =
Expand All @@ -755,7 +755,7 @@ let inline AtLeastOne2D f xs ys =
[<DebuggerHidden; DebuggerStepThrough>]
let inline MapReduceD mapper zero reducer l =
MapD mapper l
++ (fun res ->
|> bind (fun res ->
ResultD(
match res with
| [] -> zero
Expand Down
4 changes: 3 additions & 1 deletion src/Compiler/Facilities/DiagnosticsLogger.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ val ResultD: x: 'T -> OperationResult<'T>

val CheckNoErrorsAndGetWarnings: res: OperationResult<'T> -> (exn list * 'T) option

val (++): res: OperationResult<'T> -> f: ('T -> OperationResult<'b>) -> OperationResult<'b>
/// The bind in the monad. Stop on first error. Accumulate warnings and continue.
/// <remarks>Not meant for direct usage. Used in other inlined functions</remarks>
val bind: f: ('T -> OperationResult<'b>) -> res: OperationResult<'T> -> OperationResult<'b>

/// Stop on first error. Accumulate warnings and continue.
val IterateD: f: ('T -> OperationResult<unit>) -> xs: 'T list -> OperationResult<unit>
Expand Down