Kriens (empty) → 0.1.0.0
raw patch · 4 files changed
+256/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- Kriens.cabal +70/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- src/Control/Category/Cont.hs +154/−0
+ Kriens.cabal view
@@ -0,0 +1,70 @@+-- Initial Kriens.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++-- The name of the package.+name: Kriens++-- 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: Category for Continuation Passing Style++-- A longer description of the package.+description: Provides a type for Continuation Passing Style development ++-- URL for the project homepage or repository.+homepage: https://github.com/matteoprovenzano/kriens-hs.git++-- The license under which the package is released.+license: BSD3++-- The file containing the license text.+license-file: LICENSE++-- The package author(s).+author: Matteo Provenzano++-- An email address to which users can send suggestions, bug reports, and +-- patches.+maintainer: matteo.provenzano@alephdue.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: ++-- Constraint on the version of Cabal needed to build this package.+cabal-version: >=1.10+++library+ -- Modules exported by the library.+ exposed-modules: Control.Category.Cont+ + -- Modules included in this library but not exported.+ -- other-modules: + + -- LANGUAGE extensions used by modules in this package.+ other-extensions: FlexibleInstances+ + -- Other library packages from which modules are imported.+ build-depends: base >=4.8 && <4.9+ + -- Directories containing source files.+ hs-source-dirs: src+ + -- Base language which the package is written in.+ default-language: Haskell2010+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Matteo Provenzano++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Matteo Provenzano nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Control/Category/Cont.hs view
@@ -0,0 +1,154 @@+{-# LANGUAGE FlexibleInstances #-}++-----------------------------------------------------------------------------+{- |+Module : Control.Category.Cont+Description : Provides a type for Continuation Passing Style development+Copyright : (c) Matteo Provenzano 2015++License : BSD-style (see the LICENSE file in the distribution)+Maintainer : matteo.provenzano@alephdue.com+Stability : experimental+Portability : portable+-}++module Control.Category.Cont ( -- * The Cont category+ -- $Category++ -- ** Category laws+ -- $Laws+ Cont+ -- * Utility functions+ , forget+ , withCont+ , lift+ , cont+ -- * Example: Composable sorter+ -- $Example1+ + -- * Example: Composable actions+ -- $Example2++ -- * Example: Monoid+ -- $Example3+ ) where++import Prelude hiding (id, (.))+import Control.Category+import Data.Monoid++{-$Category+The Continuation category is defined as follow:++- object are functions of the type @f :: a -> b@, @g :: c -> d@.+- arrows are functions of the type @t :: (a -> b) -> (c -> d)@.+- the identity @'id'@ is the function that takes a function f and returns the same function.+- the composition @.@ operator takes two functions+ @t1 :: (a -> b) -> (c -> d)@, @t2 :: (c -> d) -> (e -> f)@ and returns the function @t :: (a -> b) -> (e -> f)@.+-}++{-$Laws+The category laws are trivially verified:++- Identity law:+@'Cont' f . 'Cont' 'id' = 'Cont' f . 'id' = 'Cont' 'f' = 'Cont' 'id' . f = 'Cont' 'id' . 'Cont' f@+- Associativity law:+@('Cont' f . 'Cont' g) . 'Cont' h = 'Cont' (f . g) . 'Cont' h = 'Cont' (f . g . h) = 'Cont' (f . (g . h)) = 'Cont' f . 'Cont' (g .h) = 'Cont' f . ('Cont' g . 'Cont' h)@+-}++-- |A type for the Continuation category.+newtype Cont f g = Cont (f -> g)++instance Category Cont where+ (Cont f) . (Cont g) = Cont (f . g )+ id = Cont id++instance Monoid a => Monoid (Cont t (f -> a)) where+ Cont f `mappend` Cont g = Cont $ \h x -> f h x `mappend` g h x+ mempty = Cont $ \h x -> mempty++-- |Creates a continuation+cont :: (f -> g) -> Cont f g+cont f = Cont f++-- |Forgets the continuation.+forget :: Cont (a -> a) (b -> c) -> b -> c+forget (Cont f) = f id++-- |Apply a function to the continuation.+withCont :: (b -> c) -> Cont (a -> b) (a -> c)+withCont f = Cont $ \g -> f . g++-- |Lift the continuation into a Monad.+lift :: Monad m => Cont (a -> b) (a -> m b)+lift = withCont return++{-$Example1+Here is an example how to use the @Cont@ category to compose a custom sorter:++>import Prelude hiding (id, (.))+>import Control.Category+>import Control.Category.Cont+>import Data.List+>+>data User = User { name :: String+> , surname :: String+> , yob :: Int+> } deriving Show+>+>users = [ User { name = "Amadeus", surname = "Mozart", yob = 1756 }+> , User { name = "Amadeus", surname = "Brahms", yob = 1833 }+> , User { name = "Johannes", surname = "Brahms", yob = 1833 }+> , User { name = "Johannes", surname = "Mozart", yob = 1833 }+> , User { name = "Antonio", surname = "Vivaldi", yob = 1678 }+> , User { name = "Antonio", surname = "Vivaldi", yob = 1679 }+> ]+>+>order = cont $ \f x -> sortBy (curry f) x +>by field = cont $+> \f x -> if ord x == EQ then+> f x+> else+> ord x+> where+> ord x = compare ((field . fst) x) ((field . snd) x)+>+>eqOtherwise = cont $ \f x -> EQ+>+>mysort = forget $ order . (by surname) . (by name) . (by yob) . eqOtherwise+-}+{-$Example2+Here is an example how to combine the Cont category with the @IO@ monad:++>import Prelude hiding (id, (.))+>import Control.Category+>import Control.Category.Cont+>+>withPassword pwd = cont $ \f x -> do+> putStrLn "Enter the secret password:"+> pass <- getLine+> if pass == pwd then+> f x+> else+> return "you are not authorized to execute this action."+>+>greet = cont $ \f x -> f $ "hello to " ++ x+>+>secureGreet = forget $ (withPassword "secret") . lift . greet+>verySecureGreet = forget $ (withPassword "secret") . (withPassword "verySecret") . lift . greet++The action @withPassword@ requests the user to enter a string. If the string matches the password, the input is handed to the continuation.+@lift@ is used to inject the pure code into the IO monad.+-}+{-$Example3+>import Prelude hiding (id, (.))+>import Control.Category+>import Control.Category.Cont+>+>ins a = [a]+>select op = cont $+> \f x -> f $ op x+>+>toList :: (a, a) -> [a]+>toList = forget $ (select fst `mappend` select snd) . (withCont ins)+-}