Commits

Tony Morris committed 9965c71

Initial commit from scratch work

Comments (0)

Files changed (4)

Data/Semigroup.hs

+-- | A semigroup is a binary associative operation.
+module Data.Semigroup(
+                     Semigroup,
+                     Identity,
+                     (<++>)
+                     ) where
+
+import Data.Monoid
+import Control.Applicative
+
+-- | A binary operation that must satisfy associativity. Unlike a @Monoid@, an identity in not essential.
+class Semigroup a where
+  (.++.) :: a -> a -> a
+
+-- | A wrapper used to construct a @Semigroup@ from a @Monoid@.
+newtype Identity a = Identity { run :: a }
+
+-- | A binary associative operation lifted into an applicative functor.
+(<++>) :: (Applicative f, Semigroup a) => f a -> f a -> f a
+(<++>) = liftA2 (.++.)
+
+instance Functor Identity where
+  fmap f (Identity a) = Identity (f a)
+
+instance Applicative Identity where
+  pure = Identity
+  Identity f <*> Identity a = Identity (f a)
+
+instance Monad Identity where
+  return = Identity
+  Identity a >>= f = f a
+
+instance Monoid a => Semigroup (Identity a) where
+  (.++.) = liftA2 mappend
+
+instance Semigroup () where
+  _ .++. _ = ()
+
+instance Semigroup b => Semigroup (a -> b) where
+  (.++.) = (<++>)
+
+instance Semigroup a => Semigroup (IO a) where
+  (.++.) = (<++>)
+
+instance (Semigroup a, Semigroup b) => Semigroup (a, b) where
+  (a1, b1) .++. (a2, b2) = (a1 .++. a2, b1 .++. b2)
+
+instance (Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) where
+  (a1, b1, c1) .++. (a2, b2, c2) = (a1 .++. a2, b1 .++. b2, c1 .++. c2)
+
+instance Semigroup All where
+  a .++. b = run (Identity a .++. Identity b)
+
+instance Semigroup Any where
+  a .++. b = run (Identity a .++. Identity b)
+
+instance Semigroup a => Semigroup (Dual a) where
+  Dual a .++. Dual b = Dual (b .++. a)
+
+instance Semigroup (Endo a) where
+  a .++. b = run (Identity a .++. Identity b)
+
+instance Num a => Semigroup (Product a) where
+  a .++. b = run (Identity a .++. Identity b)
+
+instance Num a => Semigroup (Sum a) where
+  a .++. b = run (Identity a .++. Identity b)
+
+instance Semigroup a => Semigroup (Maybe a) where
+  Nothing .++. b = b
+  a .++. Nothing = a
+  Just a .++. Just b = Just (a .++. b)
+
+instance Semigroup (First a) where
+  a .++. b = run (Identity a .++. Identity b)
+
+instance Semigroup (Last a) where
+  a .++. b = run (Identity a .++. Identity b)
+
+instance Semigroup [a] where
+  (.++.) = (++)
+Copyright 2010 Tony Morris
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+2. 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.
+3. Neither the name of the author nor the names of his contributors
+   may be used to endorse or promote products derived from this software
+   without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 AUTHORS 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.
+#!/usr/bin/env runhaskell
+> import Distribution.Simple
+> main = defaultMain
+Name:                Semigroup
+Version:             0.0.1
+License:             BSD3
+License-File:        LICENSE
+Synopsis:            A semigroup
+Description:         A semigroup is a binary associative operation
+Homepage:            http://code.google.com/p/semigroup/
+Category:            Data
+Author:              Tony Morris
+Maintainer:          code@tmorris.net
+Copyright:           2010 Tony Morris
+build-type:          Simple
+cabal-version:       >= 1.2
+
+Flag small_base
+  Description:     Choose the new, split-up base package.
+
+Library
+  if flag(small_base)
+    Build-Depends: base < 5 && >= 3
+  else
+    Build-Depends: base < 5 && >= 3
+
+  GHC-Options:    -Wall
+  Exposed-Modules: Data.Semigroup
+