Skip to content

Commit

Permalink
Require (<|>) instead of Append
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Mar 11, 2018
1 parent f121274 commit 9c03a49
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docsrc/content/abstraction-alternative.fsx
Expand Up @@ -19,7 +19,7 @@ Minimal complete definition
static member Return (x:'T) : 'Alternative<'T>
static member (<*>) (f:'T->'U, x:Alternative<'T>) : Alternative<'U>
static member get_Empty () :'Alternative
static member Append (x:'Alternative<'T>, y:'Alternative<'T>) :'Alternative<'T>
static member (<|>) (x:'Alternative<'T>, y:'Alternative<'T>) :'Alternative<'T>
*)
(**
Note: ``return`` can't be used outside computation expressions, use ``result`` instead.
Expand Down Expand Up @@ -143,7 +143,7 @@ type Maybe<'t> =
| Just f, Just x -> Just (f x)
| _ -> Nothing
static member inline get_Empty () = Nothing
static member inline Append (x, y) = match x with Nothing -> y | xs -> xs
static member inline (<|>) (x, y) = match x with Nothing -> y | xs -> xs

let r5 = Nothing ++ Just 5 ++ Just 6 ++ zero
let r6 = map string (Just 6)
Expand Down
2 changes: 1 addition & 1 deletion docsrc/content/computation-expressions.fsx
Expand Up @@ -22,7 +22,7 @@ There is a single computation expression: ``monad`` but it comes in 4 flavours:
Async workflows is an example of a side-effect computation expression and seq expressions are an example of monadplus.
Side effect workflows don't have any additional requirement over the type (apart from the monad operations), but monadplus requires the additional [get_Empty and Append](abstraction-alternative.html) methods.
Side effect workflows don't have any additional requirement over the type (apart from the monad operations), but monadplus requires the additional [get_Empty and (<|>)](abstraction-alternative.html) methods.
The generic computation expression ``monad`` is a side-effect one, but it can be turned into a monadplus by accessing the ``.plus`` property.
Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/DList.fs
Expand Up @@ -30,7 +30,7 @@ type DList<'T> with
static member (+) (DList x, DList y) = DList (x << y)

static member get_Empty = DList id
static member Append (DList x, DList y) = DList (x << y)
static member (<|>) (DList x, DList y) = DList (x << y)

static member ToSeq x = toSeq x
static member ToList x = toList x
Expand Down
14 changes: 7 additions & 7 deletions src/FSharpPlus/Functor.fs
Expand Up @@ -290,15 +290,15 @@ type Empty =

type Append =
inherit Default1
static member Append (x: 'T seq , y , [<Optional>]_mthd: Default2) = Seq.append x y
static member inline Append (x: '``Alt<'T>``, y: '``Alt<'T>`` , [<Optional>]_mthd: Default1) = (^``Alt<'T>`` : (static member Append : _*_ -> _) x, y) : '``Alt<'T>``
static member inline Append (_: ^t when ^t:null and ^t:struct, _, _mthd: Default1) = ()
static member Append (x: 'T option , y , [<Optional>]_mthd: Append ) = match x with None -> y | xs -> xs
static member Append (x: 'T list , y , [<Optional>]_mthd: Append ) = x @ y
static member Append (x: 'T [] , y , [<Optional>]_mthd: Append ) = Array.append x y
static member ``<|>`` (x: 'T seq , y , [<Optional>]_mthd: Default2) = Seq.append x y
static member inline ``<|>`` (x: '``Alt<'T>``, y: '``Alt<'T>`` , [<Optional>]_mthd: Default1) = (^``Alt<'T>`` : (static member (<|>) : _*_ -> _) x, y) : '``Alt<'T>``
static member inline ``<|>`` (_: ^t when ^t:null and ^t:struct, _, _mthd: Default1) = ()
static member ``<|>`` (x: 'T option , y , [<Optional>]_mthd: Append ) = match x with None -> y | xs -> xs
static member ``<|>`` (x: 'T list , y , [<Optional>]_mthd: Append ) = x @ y
static member ``<|>`` (x: 'T [] , y , [<Optional>]_mthd: Append ) = Array.append x y

static member inline Invoke (x: '``Alt<'T>``) (y: '``Alt<'T>``) : '``Alt<'T>`` =
let inline call (mthd: ^M, input1: ^I, input2: ^I) = ((^M or ^I) : (static member Append: _*_*_ -> _) input1, input2, mthd)
let inline call (mthd: ^M, input1: ^I, input2: ^I) = ((^M or ^I) : (static member ``<|>``: _*_*_ -> _) input1, input2, mthd)
call (Unchecked.defaultof<Append>, x, y)


Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Kleisli.fs
Expand Up @@ -33,7 +33,7 @@ type Kleisli<'t, '``monad<'u>``> = Kleisli of ('t -> '``monad<'u>``) with

// ArrowPlus
static member inline Empty (_output :Kleisli<'T,'``Monad<'U>``>, _mthd :Empty) = Kleisli (fun _ -> Empty.Invoke ())
static member inline Append (Kleisli f, Kleisli g, _mthd:Append) = Kleisli (fun x -> Append.Invoke (f x) (g x))
static member inline ``<|>`` (Kleisli f, Kleisli g, _mthd:Append) = Kleisli (fun x -> Append.Invoke (f x) (g x))

/// Basic operations on Kleisli
[<RequireQualifiedAccess>]module Kleisli = let run (Kleisli f) = f
2 changes: 1 addition & 1 deletion src/FSharpPlus/List.fs
Expand Up @@ -55,7 +55,7 @@ type ListT<'``monad<list<'t>>``> with
static member inline (>>=) (x : ListT<'``Monad<seq<'T>``>, f : 'T -> ListT<'``Monad<seq<'U>``>) = ListT.bind f x

static member inline get_Empty () = ListT <| result [] : ListT<'``MonadPlus<list<'T>``>
static member inline Append (ListT x, ListT y) = ListT (x >>= (fun a -> y >>= (fun b -> result (a @ b)))) : ListT<'``MonadPlus<list<'T>``>
static member inline (<|>) (ListT x, ListT y) = ListT (x >>= (fun a -> y >>= (fun b -> result (a @ b)))) : ListT<'``MonadPlus<list<'T>``>

static member inline Lift (x:'``Monad<'T>``) = x |> liftM List.singleton |> ListT : ListT<'``Monad<list<'T>>``>

Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Monoid.fs
Expand Up @@ -16,7 +16,7 @@ open FSharpPlus.Internals
[<Extension; Sealed>]
type Plus =
inherit Default1
static member inline ``+`` (x :'Plus , y:'Plus , _mthd : Default2) = (^Plus : (static member Append : _*_ -> _) x, y) : ^Plus
static member inline ``+`` (x :'Plus , y:'Plus , _mthd : Default2) = (^Plus : (static member (<|>) : _*_ -> _) x, y) : ^Plus
static member inline ``+`` (x :'Plus , y:'Plus , [<Optional>]_mthd : Default1) = x + y : ^Plus
static member inline ``+`` (_ :^t when ^t:null and ^t:struct, _:^t, [<Optional>]_mthd : Default1) = id
static member ``+`` (x:list<_> , y , [<Optional>]_mthd : Plus ) = x @ y
Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Monoids.fs
Expand Up @@ -101,7 +101,7 @@ type Compose<'``f<'g<'t>>``> = Compose of '``f<'g<'t>>`` with

// Alternative
static member inline get_Empty () = Compose (getEmpty ()) : Compose<'``F<'G<'T>``>
static member inline Append (Compose x, Compose y) = Compose (x <|> y) : Compose<'``F<'G<'T>``>
static member inline (<|>) (Compose x, Compose y) = Compose (x <|> y) : Compose<'``F<'G<'T>``>


/// Basic operations on Compose
Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Option.fs
Expand Up @@ -28,7 +28,7 @@ type OptionT<'``monad<option<'t>>``> with
static member inline (>>=) (x : OptionT<'``Monad<seq<'T>``>, f : 'T -> OptionT<'``Monad<seq<'U>``>) = OptionT.bind f x

static member inline get_Empty () = OptionT <| result None : OptionT<'``MonadPlus<option<'T>``>
static member inline Append (OptionT x, OptionT y) = OptionT <| (x >>= (fun maybe_value -> match maybe_value with Some _ -> x | _ -> y)) : OptionT<'``MonadPlus<option<'T>``>
static member inline (<|>) (OptionT x, OptionT y) = OptionT <| (x >>= (fun maybe_value -> match maybe_value with Some _ -> x | _ -> y)) : OptionT<'``MonadPlus<option<'T>``>

static member inline Lift (x:'``Monad<'T>``) = x |> liftM Some |> OptionT : OptionT<'``Monad<option<'T>>``>

Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Reader.fs
Expand Up @@ -57,7 +57,7 @@ type ReaderT<'r,'``monad<'t>``> with
static member inline (>>=) (x : ReaderT<_,'``Monad<'T>``>, f : 'T->ReaderT<'R,'``Monad<'U>``>) = ReaderT.bind f x : ReaderT<'R, '``Monad<'U>``>

static member inline get_Empty () = ReaderT (fun _ -> getEmpty()) : ReaderT<'R, '``MonadPlus<'T>``>
static member inline Append (ReaderT m, ReaderT n) = ReaderT (fun r -> m r <|> n r) : ReaderT<'R, '``MonadPlus<'T>``>
static member inline (<|>) (ReaderT m, ReaderT n) = ReaderT (fun r -> m r <|> n r) : ReaderT<'R, '``MonadPlus<'T>``>

static member Lift m = ReaderT (fun _ -> m) : ReaderT<'R,'``Monad<'T>``>

Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Seq.fs
Expand Up @@ -38,7 +38,7 @@ type SeqT<'``monad<seq<'t>>``> with
static member inline (>>=) (x : SeqT<'``Monad<seq<'T>``>, f : 'T -> SeqT<'``Monad<seq<'U>``>) = SeqT.bind f x

static member inline get_Empty () = SeqT <| result Seq.empty : SeqT<'``MonadPlus<seq<'T>``>
static member inline Append (SeqT x , SeqT y) = SeqT <| (x >>= (fun a -> y >>= (fun b -> result ((Seq.append:seq<_>->seq<_>->_) a b)))) : SeqT<'``MonadPlus<seq<'T>``>
static member inline (<|>) (SeqT x , SeqT y) = SeqT <| (x >>= (fun a -> y >>= (fun b -> result ((Seq.append:seq<_>->seq<_>->_) a b)))) : SeqT<'``MonadPlus<seq<'T>``>

static member inline Lift (x:'``Monad<'T>``) = x |> liftM Seq.singleton |> SeqT : SeqT<'``Monad<seq<'T>>``>

Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/State.fs
Expand Up @@ -53,7 +53,7 @@ type StateT<'s,'``monad<'t * 's>``> with
static member inline (>>=) (x : StateT<'S,'``Monad<'T * 'S>``>, f : 'T->StateT<'S,'``Monad<'U * 'S>``>) = StateT.bind f x

static member inline get_Empty () = StateT (fun _ -> getEmpty()) : StateT<'S,'``MonadPlus<'T * 'S>``>
static member inline Append (StateT m, StateT n) = StateT (fun s -> m s <|> n s) : StateT<'S,'``MonadPlus<'T * 'S>``>
static member inline (<|>) (StateT m, StateT n) = StateT (fun s -> m s <|> n s) : StateT<'S,'``MonadPlus<'T * 'S>``>

static member inline Lift (m:'``Monad<'T>``) : StateT<'S,'``Monad<'T * 'S>``> = StateT <| fun s -> m >>= fun a -> result (a, s)

Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Writer.fs
Expand Up @@ -78,7 +78,7 @@ type WriterT<'``monad<'t * 'monoid>``> with
static member inline (>>=) (x : WriterT<'``Monad<'T * 'Monoid>``>, f :'T -> _) = WriterT.bind f x : WriterT<'``Monad<'U * 'Monoid>``>

static member inline get_Empty () = WriterT (getEmpty()) : WriterT<'``MonadPlus<'T * 'Monoid>``>
static member inline Append (WriterT m, WriterT n) = WriterT (m <|> n) : WriterT<'``MonadPlus<'T * 'Monoid>``>
static member inline (<|>) (WriterT m, WriterT n) = WriterT (m <|> n) : WriterT<'``MonadPlus<'T * 'Monoid>``>

static member inline Tell (w:'Monoid) = WriterT (result ((), w)) : WriterT<'``Monad<unit * 'Monoid>``>
static member inline Listen (WriterT m: WriterT<'``Monad<('T * ('Monoid'T -> 'Monoid)) * 'Monoid>``>) = WriterT (m >>= (fun (a, w) -> result ((a, w), w))) : WriterT<'``Monad<('T * 'Monoid) * 'Monoid>``>
Expand Down
18 changes: 9 additions & 9 deletions tests/FSharpPlus.Tests/General.fs
Expand Up @@ -53,14 +53,14 @@ type WrappedListE<'s> = WrappedListE of 's list with
static member Return (x) = WrappedListE [x]
static member (>>=) (WrappedListE x: WrappedListE<'T>, f) = WrappedListE (List.collect (f >> (fun (WrappedListE x) -> x)) x)
static member get_Empty() = WrappedListE List.empty
static member Append (WrappedListE l, WrappedListE x) = WrappedListE (l @ x)
static member (<|>) (WrappedListE l, WrappedListE x) = WrappedListE (l @ x)

type WrappedListF<'s> = WrappedListF of 's list with
static member Return (x) = WrappedListF [x]
static member (>>=) (WrappedListF x: WrappedListF<'T>, f) = WrappedListF (List.collect (f >> (fun (WrappedListF x) -> x)) x)
static member Join (WrappedListF wlst) = SideEffects.add "Join"; WrappedListF wlst >>= id
static member get_Empty() = WrappedListF List.empty
static member Append (WrappedListF l, WrappedListF x) = WrappedListF (l @ x)
static member (<|>) (WrappedListF l, WrappedListF x) = WrappedListF (l @ x)

type WrappedListG<'s> = WrappedListG of 's list with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator() = (let (WrappedListG x) = x in x :> _ seq).GetEnumerator()
Expand All @@ -69,7 +69,7 @@ type WrappedListG<'s> = WrappedListG of 's list with
static member (>>=) (WrappedListG x: WrappedListG<'T>, f) = WrappedListG (List.collect (f >> (fun (WrappedListG x) -> x)) x)
static member Join (WrappedListG wlst) = (*SideEffects.add "Join";*) WrappedListG wlst >>= id
static member get_Empty() = WrappedListG List.empty
static member Append (WrappedListG l, WrappedListG x) = WrappedListG (l @ x)
static member (<|>) (WrappedListG l, WrappedListG x) = WrappedListG (l @ x)
static member Delay (f: unit -> WrappedListD<_>) = SideEffects.add "Using WrappedListG's Delay"; f()
static member Using (resource, body) = SideEffects.add "Using WrappedListG's Using"; using resource body

Expand All @@ -81,7 +81,7 @@ type WrappedSeqA<'s> = WrappedSeqA of 's seq with
static member (>>=) (WrappedSeqA x: WrappedSeqA<'T>, f) = WrappedSeqA (Seq.collect (f >> (fun (WrappedSeqA x) -> x)) x)
static member Join (WrappedSeqA wlst) = WrappedSeqA wlst >>= id
static member get_Empty() = WrappedSeqA List.empty
static member Append (WrappedSeqA l, WrappedSeqA x) = WrappedSeqA (Seq.append l x)
static member (<|>) (WrappedSeqA l, WrappedSeqA x) = WrappedSeqA (Seq.append l x)
static member Delay (f: unit -> WrappedSeqA<_>) =
let run (WrappedSeqA s) = s
WrappedSeqA (Seq.delay (f >> run))
Expand All @@ -93,7 +93,7 @@ type WrappedSeqB<'s> = WrappedSeqB of 's seq with
static member (>>=) (WrappedSeqB x: WrappedSeqB<'T>, f) = WrappedSeqB (Seq.collect (f >> (fun (WrappedSeqB x) -> x)) x)
static member Join (WrappedSeqB wlst) = WrappedSeqB wlst >>= id
static member get_Empty() = WrappedSeqB List.empty
static member Append (WrappedSeqB l, WrappedSeqB x) = WrappedSeqB (Seq.append l x)
static member (<|>) (WrappedSeqB l, WrappedSeqB x) = WrappedSeqB (Seq.append l x)
static member Delay (f: unit -> WrappedSeqB<_>) =
let run (WrappedSeqB s) = s
WrappedSeqB (Seq.delay (f >> run))
Expand All @@ -111,7 +111,7 @@ type WrappedSeqC<'s> = WrappedSeqC of 's seq with
static member (>>=) (WrappedSeqC x: WrappedSeqC<'T>, f) = WrappedSeqC (Seq.collect (f >> (fun (WrappedSeqC x) -> x)) x)
static member Join (WrappedSeqC wlst) = WrappedSeqC wlst >>= id
static member get_Empty() = WrappedSeqC List.empty
static member Append (WrappedSeqC l, WrappedSeqC x) = WrappedSeqC (Seq.append l x)
static member (<|>) (WrappedSeqC l, WrappedSeqC x) = WrappedSeqC (Seq.append l x)
static member Delay (f: unit -> WrappedSeqC<_>) =
let run (WrappedSeqC s) = s
WrappedSeqC (Seq.delay (f >> run))
Expand Down Expand Up @@ -148,7 +148,7 @@ module Monoid =

type MyList<'t> = MyList of list<'t> with
static member get_Empty () = MyList []
static member Append (MyList x, MyList y) = MyList (x @ y)
static member (<|>) (MyList x, MyList y) = MyList (x @ y)

type MyNum = MyNum of int with
static member get_Empty () = MyNum 0
Expand Down Expand Up @@ -767,7 +767,7 @@ module Alternative =
let y = seq [1;2] <|> seq [3;4]

// shoud not compile.
// Although WrappedListD implements IEnumerable, it should explicitely implement Append. Not all IEnumerables have append.
// Although WrappedListD implements IEnumerable, it should explicitely implement (<|>). Not all IEnumerables have (<|>).
// let z = WrappedListD [1;2] ++ WrappedListD [3;4]
()

Expand Down Expand Up @@ -846,7 +846,7 @@ module Categories =

// ArrowPlus
static member inline Empty (output :Kleisli<'T,'``Monad<'U>``>, mthd :Empty) = Kleisli (fun _ -> Empty.Invoke ())
static member inline Append (Kleisli f, Kleisli g, mthd:Append) = Kleisli (fun x -> Append.Invoke (f x) (g x))
static member inline ``<|>`` (Kleisli f, Kleisli g, mthd:Append) = Kleisli (fun x -> Append.Invoke (f x) (g x))

let runKleisli (Kleisli f) = f
let runFunc (f : System.Func<_,_>) = f.Invoke
Expand Down

0 comments on commit 9c03a49

Please sign in to comment.