RichConditional (empty) → 0.1.0.0
raw patch · 5 files changed
+314/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- Control/RichConditional.hs +93/−0
- LICENSE +20/−0
- README.md +129/−0
- RichConditional.cabal +70/−0
- Setup.hs +2/−0
+ Control/RichConditional.hs view
@@ -0,0 +1,93 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE FlexibleInstances #-}++module Control.RichConditional (++ TotalIf+ , decide++ , PartialIf+ , indicate++ , ifElse+ , inCase+ , ensure+ , ensurePositive+ , ensureNegative++ ) where++import Data.Maybe+import Data.Either+import Control.Monad++type Indication = Maybe++type Decision = Either++-- | Instances of this class provide a test on some type a. If it passes, it+-- gives a value of type b; otherwise, it gives Nothing.+class PartialIf a b where+ indicate :: a -> Indication b++instance PartialIf (Maybe a) a where+ indicate = id++-- | Instances of this class provide a test on some type a with an+-- alternative case for when it fails. If it passes, it gives a value of+-- type b; otherwise, it gives a value of type c.+class TotalIf a b c where+ decide :: a -> Decision b c++instance TotalIf (Either a b) a b where+ decide = id++-- | A conditional using a PartialIf instance. This is just like the+-- function 'maybe' which decomposes a Maybe, except that the Maybe+-- is produced through an PartialIf instance determined by the type of+-- the positive case.+inCase :: PartialIf a b => a -> (b -> c) -> c -> c+inCase x ifYes ifNo = maybe ifNo ifYes (indicate x)++-- | A conditional using a TotalIf instance. This is just like the+-- function 'either' which decomposes an Either, except that the Either+-- is produced through a TotalIf instance determined by the types of+-- the cases.+ifElse :: TotalIf a b c => a -> (b -> d) -> (c -> d) -> d+ifElse x ifYes ifNo = either ifYes ifNo (decide x)++-- | Like 'guard' but with a bonus: if the condition passes, you actually get+-- a hold of some new information.+ensure :: (Monad m, MonadPlus m, PartialIf a b) => a -> m b+ensure x = inCase x return mzero++-- | Like 'ensure' but for the positive case (Left) of a TotalIf.+-- Requires a proxy on the negative type in order to disambiguate.+ensurePositive+ :: forall m a b c u .+ ( Monad m+ , MonadPlus m+ , TotalIf a b c+ )+ => a+ -> u c+ -> m b+ensurePositive x _ =+ let typedConstMzero = const mzero :: c -> m b+ in ifElse x return typedConstMzero++-- | Like 'ensure' but for the negative case (Right) of a TotalIf.+-- Requires a proxy on the positive type in order to disambiguate.+ensureNegative+ :: forall m a b c u .+ ( Monad m+ , MonadPlus m+ , TotalIf a b c+ )+ => a+ -> u b+ -> m c+ensureNegative x _ =+ let typedConstMzero = const mzero :: b -> m c+ in ifElse x typedConstMzero return
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Alexander Vieth++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,129 @@+Control.RichConditional+=======================++Typeclasses for describing "rich conditionals" which accomplish exactly what+a classic if/else would but without the use of `Bool`.++The inspiration is found in dependently typed languages, in which a `Bool` is+seen to be a comparatively pathetic datatype: it carries very little+information. A useful test for some condition always introduces new+information, but with a classical if/else, this information not known to the+compiler because it's lossfully compressed into a `Bool`.++There is no new technology here, only typeclasses and functions which might+make it more convenient to avoid using if/else.++#A motivating example++To demonstrate the point, suppose we defined the following module, in which+the constructors of `Visitor` are not exposed:++```Haskell+module Visitor (+ Visitor+ , isLoggedIn+ , isGuest+ ) where++data Visitor = LoggedIn User | Guest++isLoggedIn :: Visitor -> Bool+isLoggedIn v = case v of+ LoggedIn _ -> True+ Guest -> False++isGuest :: Visitor -> Bool+isGuest v = case v of+ LoggedIn _ -> False+ Guest -> True+```++We have indicator functions on `Visitor`, but they don't allow for a +useful interface. In the example below, we can't build a user's landing+page because we can't get a hold of a `User` value.++```Haskell+userLandingPage :: User -> LandingPage++guestLandingPage :: LandingPage++blindLandingPage :: Visitor -> LandingPage+blindLandingPage v =+ if isLoggedIn v+ -- We know there's a user, but GHC does not!+ then userLandingPage ?+ else guestLandingPage+```++Evidently the `Visitor` library must provide some way to get a hold of+a `User` from a `Visitor`, but if it does provide this, then why even bother+giving the indicators `isLoggedIn` and `isGuest`?++Contrast the above definitions with a `Bool`-free approach:++```Haskell+module Visitor (+ Visitor+ , ifVisitor+ ) where++data Visitor = LoggedIn User | Guest++ifVisitor :: Visitor -> (User -> a) -> a -> a+ifVisitor v ifUser ifGuest = case v of+ LoggedIn user -> ifUser user+ Guest -> ifGuest+```++`Visitor` is just `Maybe User` with a new name, and `ifVisitor` is just the+function `maybe` with its parameter order shuffled. It provides users of+`Visitor` a way to get a hold of a `User` for `LoggedIn` cases without+pattern matching on `Visitor` directly. It can be used to implement a well+factored version of the landing page example:++```Haskell+userLandingPage :: User -> LandingPage++guestLandingPage :: LandingPage++landingPage :: Visitor -> LandingPage+landingPage v = ifVisitor v userLandingPage guestLandingPage+```++#Use of RichConditional++This modification of the above examples shows how RichConditional could be+used:++```Haskell+{-# LANGUAGE MultiParamTypeClasses #-}++data User = User++data Guest = Human | Robot++data Visitor = LoggedIn User | NotLoggedIn Guest++instance PartialIf Visitor User where+ indicate v = case v of+ LoggedIn user -> Just user+ NotLoggedIn _ -> Nothing++instance PartialIf Visitor Guest where+ indicate v = case v of+ LoggedIn _ -> Nothing+ NotLoggedIn guest -> Just guest++instance TotalIf Visitor User Guest where+ decide v = case v of+ LoggedIn user -> Left user+ NotLoggedIn guest -> Right guest++allGuests :: [Visitor] -> [Guest]+allGuests vs = do+ v <- vs+ -- This is like guard, except that useful data comes+ -- out of it, so that it fulfills the type signature+ -- of allGuests.+ ensure v+```
+ RichConditional.cabal view
@@ -0,0 +1,70 @@+-- Initial RichConditional.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++-- The name of the package.+name: RichConditional++-- The package version. See the Haskell package versioning policy (PVP) +-- for standards guiding when and how versions should be incremented.+-- http://www.haskell.org/haskellwiki/Package_versioning_policy+-- PVP summary: +-+------- breaking API changes+-- | | +----- non-breaking API additions+-- | | | +--- code changes with no API change+version: 0.1.0.0++-- A short (one-line) description of the package.+synopsis: Tiny library to replace classic if/else++-- A longer description of the package.+-- description: ++-- URL for the project homepage or repository.+homepage: https://github.com/avieth/RichConditional++-- The license under which the package is released.+license: MIT++-- The file containing the license text.+license-file: LICENSE++-- The package author(s).+author: Alexander Vieth++-- An email address to which users can send suggestions, bug reports, and +-- patches.+maintainer: aovieth@gmail.com++-- A copyright notice.+-- copyright: ++category: Control++build-type: Simple++-- Extra files to be distributed with the package, such as examples or a +-- README.+extra-source-files: README.md++-- Constraint on the version of Cabal needed to build this package.+cabal-version: >=1.10+++library+ -- Modules exported by the library.+ exposed-modules: Control.RichConditional+ + -- Modules included in this library but not exported.+ -- other-modules: + + -- LANGUAGE extensions used by modules in this package.+ other-extensions: MultiParamTypeClasses, ScopedTypeVariables, FlexibleInstances+ + -- Other library packages from which modules are imported.+ build-depends: base >=4.7 && <4.8+ + -- Directories containing source files.+ -- hs-source-dirs: + + -- Base language which the package is written in.+ default-language: Haskell2010+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain