packages feed

applicable (empty) → 0.1.0.0

raw patch · 5 files changed

+153/−0 lines, 5 filesdep +base

Dependencies added: base

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for applicable++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ Data/Applicable.hs view
@@ -0,0 +1,105 @@+{-# OPTIONS_HADDOCK show-extensions #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}++{- |+Module: Data.Applicable+Description: The 'Applicable' class+Copyright: ⓒ 2022 Anselm Schüler+License: MIT+Maintainer: mail@anselmschueler.com++The 'Applicable' class with its operator '($*)'.+You will likely need the @FlexibleContexts@ extension to use this module’s instances.+-}++module Data.Applicable (+  Applicable(($*)),+  ApplyTo,+  ApplyMap,+  ApplyAp,+  ApplyBind,+  GroupAction,+  ChurchBool,+  ChurchNumeral,+  ChurchTuple+) where++import Data.List (genericIndex)+import Data.Bifunctor (Bifunctor)++-- | A class for types whose values can be applied.+--   Instances are required to be uniquely determined by the applied and applied-to type.+class Applicable f a b | f a -> b where+  -- | Apply a value to another value, producing a result.+  ($*) :: f -> a -> b++instance Applicable (a -> b) a b where+  f $* x = f x++-- | A wrapper for values.+--   Can be applied to a function '(GHC.Types.->)', applying the function to the inner value.+newtype ApplyTo a = AppTo { unAppTo :: a } deriving (Eq, Ord, Show, Read, Functor)++instance Applicable (ApplyTo a) (a -> b) b where+  AppTo x $* f = f x++-- | A wrapper for functions.+--   Can be applied to a 'Functor', 'fmap'-ing the function over the inner values.+newtype ApplyMap a b = AppMap { unAppMap :: a -> b } deriving Functor++instance Functor f => Applicable (ApplyMap a b) (f a) (f b) where+  AppMap f $* xa = f <$> xa++-- | A wrapper for functions in an applicative functor.+--   Can be applied to an 'Applicative' functor, '(<*>)'-ing it on it.+newtype ApplyAp f a b = AppAp { unAppAp :: f (a -> b) } deriving Functor++instance Applicative f => Applicable (ApplyAp f a b) (f a) (f b) where+  AppAp f $* xa = f <*> xa++-- | A wrapper for 'Control.Arrow.Kleisli' arrows.+--   Can be applied to a 'Monad', '(>>=)'-ing it on it.+newtype ApplyBind m a b = AppBind { unAppBind :: a -> m b } deriving Functor++instance Monad m => Applicable (ApplyBind m a b) (m a) (m b) where+  AppBind f $* xa = xa >>= f++-- | A wrapper for 'Semigroup' members, representing the associated group action.+--   Can be applied to another member, '(<>)'-ing them.+newtype GroupAction a = GrpAct { unGrpAct :: a } deriving (Eq, Ord, Show, Read, Functor)++instance Semigroup a => Applicable (GroupAction a) a a where+  GrpAct a $* b = a <> b++-- | A wrapper for 'Bool's.+--   When applied to a value, uses the Church encoding of Booleans.+--   The Church encoding of Booleans is a binary function+--   that returns its first argument for 'True', and its second for 'False'.+newtype ChurchBool = ChBool { unChBool :: Bool } deriving (Eq, Ord, Show, Read, Enum, Bounded)++instance Applicable ChurchBool a (a -> a) where+  ($*) (ChBool True) t _ = t+  ($*) (ChBool False) _ f = f++-- | Update the 'Bool' in a 'ChurchBool'.+mapChBool :: (Bool -> Bool) -> ChurchBool -> ChurchBool+mapChBool f (ChBool x) = ChBool $ f x++-- | A wrapper for natural numbers (Approximated by 'Integral').+--   When applied to a value, uses the Church encoding of natural numbers.+--   Church numerals represent the number _n_ as a function that take another function and repeatedly applies it _n_ times.+newtype ChurchNumeral a = ChNum { unChNum :: a } deriving (Eq, Ord, Show, Read, Functor)++instance Integral a => Applicable (ChurchNumeral a) (a -> a) (a -> a) where+  ($*) (ChNum n) f x = genericIndex (iterate f x) n++-- | A wrapper for tuples '(,)'.+--   When applied to a value, uses the Church encoding of tuples.+--   The Church encoding of tuples applies a function to the values inside a tuple.+newtype ChurchTuple a b = ChTup { unChTup :: (a, b) } deriving (Eq, Ord, Show, Read, Functor, Bifunctor)++instance Applicable (ChurchTuple a b) (a -> b -> c) c where+  ChTup (x, y) $* f = f x y
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2022 Anselm Schüler++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,3 @@+# Applicable++This Haskell package provides the `Data.Applicable` module, containing the `Applicable` class, for things that can be applied. Use the `($*)` operator to apply them. You should probably use the `FlexibleContexts` extension.
+ applicable.cabal view
@@ -0,0 +1,20 @@+cabal-version:      2.4+name:               applicable+version:            0.1.0.0+synopsis:           A class for things that can be applied+description:        A class for things that can be applied, and utility newtypes+homepage:           https://github.com/schuelermine/applicable+bug-reports:        https://github.com/schuelermine/applicable/issues+license:            MIT+license-file:       LICENSE+author:             Anselm Schüler+maintainer:         mail@anselmschueler.com+copyright:          ⓒ 2022 Anselm Schüler+category:           Data+extra-source-files: CHANGELOG.md+                    README.md++library+    exposed-modules:  Data.Applicable+    build-depends:    base >=4.14 && <=4.16+    default-language: Haskell2010