optional (empty) → 0.0.1
raw patch · 5 files changed
+262/−0 lines, 5 filesdep +QuickCheckdep +basedep +directorybuild-type:Customsetup-changed
Dependencies added: QuickCheck, base, directory, doctest, filepath
Files
- Setup.lhs +44/−0
- etc/LICENCE +27/−0
- optional.cabal +67/−0
- src/System/Args/Optional.hs +92/−0
- test/doctests.hs +32/−0
+ Setup.lhs view
@@ -0,0 +1,44 @@+#!/usr/bin/env runhaskell+\begin{code}+{-# OPTIONS_GHC -Wall #-}+module Main (main) where++import Data.List ( nub )+import Data.Version ( showVersion )+import Distribution.Package ( PackageName(PackageName), PackageId, InstalledPackageId, packageVersion, packageName )+import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) )+import Distribution.Simple ( defaultMainWithHooks, UserHooks(..), simpleUserHooks )+import Distribution.Simple.Utils ( rewriteFile, createDirectoryIfMissingVerbose )+import Distribution.Simple.BuildPaths ( autogenModulesDir )+import Distribution.Simple.Setup ( BuildFlags(buildVerbosity), fromFlag )+import Distribution.Simple.LocalBuildInfo ( withLibLBI, withTestLBI, LocalBuildInfo(), ComponentLocalBuildInfo(componentPackageDeps) )+import Distribution.Verbosity ( Verbosity )+import System.FilePath ( (</>) )++main :: IO ()+main = defaultMainWithHooks simpleUserHooks+ { buildHook = \pkg lbi hooks flags -> do+ generateBuildModule (fromFlag (buildVerbosity flags)) pkg lbi+ buildHook simpleUserHooks pkg lbi hooks flags+ }++generateBuildModule :: Verbosity -> PackageDescription -> LocalBuildInfo -> IO ()+generateBuildModule verbosity pkg lbi = do+ let dir = autogenModulesDir lbi+ createDirectoryIfMissingVerbose verbosity True dir+ withLibLBI pkg lbi $ \_ libcfg -> do+ withTestLBI pkg lbi $ \suite suitecfg -> do+ rewriteFile (dir </> "Build_" ++ testName suite ++ ".hs") $ unlines+ [ "module Build_" ++ testName suite ++ " where"+ , "deps :: [String]"+ , "deps = " ++ (show $ formatdeps (testDeps libcfg suitecfg))+ ]+ where+ formatdeps = map (formatone . snd)+ formatone p = case packageName p of+ PackageName n -> n ++ "-" ++ showVersion (packageVersion p)++testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)]+testDeps xs ys = nub $ componentPackageDeps xs ++ componentPackageDeps ys++\end{code}
+ etc/LICENCE view
@@ -0,0 +1,27 @@+Copyright 2013 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.
+ optional.cabal view
@@ -0,0 +1,67 @@+name: optional+version: 0.0.1+license: BSD3+license-File: etc/LICENCE+author: Tony Morris <ʇǝu˙sıɹɹoɯʇ@ןןǝʞsɐɥ>+maintainer: Tony Morris+copyright: Copyright (C) 2010-2013 Tony Morris+synopsis: Using type-classes for optional function arguments+category: Development+description: Using type-classes for optional function arguments. Although this implements that which is available in other programming languages, and is a lot safer than in those environments, it is still of dubious morality. Use with care, question and caution.+homepage: https://github.com/tonymorris/optional+bug-reports: https://github.com/tonymorris/optional/issues+cabal-version: >= 1.10+build-type: Custom++source-repository head+ type: git+ location: git@github.com:tonymorris/optional.git++flag small_base+ description: Choose the new, split-up base package.++library+ default-language:+ Haskell2010++ build-depends:+ base < 5 && >= 3++ ghc-options:+ -Wall++ default-extensions:+ NoImplicitPrelude+ , MultiParamTypeClasses+ , FlexibleContexts+ , FlexibleInstances++ hs-source-dirs:+ src++ exposed-modules:+ System.Args.Optional++test-suite doctests+ type:+ exitcode-stdio-1.0++ main-is:+ doctests.hs++ default-language:+ Haskell2010++ build-depends:+ base < 5 && >= 3,+ doctest >= 0.9.7,+ filepath >= 1.3,+ directory >= 1.1,+ QuickCheck >= 2.0++ ghc-options:+ -Wall+ -threaded++ hs-source-dirs:+ test
+ src/System/Args/Optional.hs view
@@ -0,0 +1,92 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FlexibleContexts #-}++-- | Type-classes to model optional function arguments. Use with care.+--+-- Lots of helpful type-inference is lost in using this technique.+module System.Args.Optional(+ Optional1(..)+, Optional2(..)+) where++import Prelude(id, const)++-- $setup+-- >>> import Prelude(Integer, Bool, String, Num(..), even, odd, (++))+-- >>> import Data.List(reverse, filter)+-- >>> import Data.Char(Char, isUpper, isLower)+-- >>> :set -XFlexibleContexts++-- | One optional argument.+--+-- >>> let timesTwo :: Optional1 Integer Integer x => x; timesTwo = optional1 (*2) 7 in timesTwo :: Integer+-- 14+--+-- >>> let timesTwo :: Optional1 Integer Integer x => x; timesTwo = optional1 (*2) 7 in timesTwo (12 :: Integer) :: Integer+-- 24+--+-- >>> let reverseI :: Optional1 [Integer] [Integer] x => x; reverseI = optional1 reverse ([] :: [Integer]) in reverseI ([1,2,3] :: [Integer]) :: [Integer]+-- [3,2,1]+--+-- >>> let reverseI :: Optional1 [Integer] [Integer] x => x; reverseI = optional1 reverse ([] :: [Integer]) in reverseI :: [Integer]+-- []+--+-- >>> let reverseI :: Optional1 [Integer] [Integer] x => x; reverseI = optional1 reverse [99, 98, 97] in reverseI :: [Integer]+-- [97,98,99]+--+-- >>> let filterI :: Optional1 (Integer -> Bool) [Integer] x => x; filterI = optional1 (\p -> filter p [1..20]) (even :: Integer -> Bool) in filterI :: [Integer]+-- [2,4,6,8,10,12,14,16,18,20]+--+-- >>> let filterI :: Optional1 (Integer -> Bool) [Integer] x => x; filterI = optional1 (\p -> filter p [1..20]) (even :: Integer -> Bool) in filterI (odd :: Integer -> Bool) :: [Integer]+-- [1,3,5,7,9,11,13,15,17,19]+class Optional1 a b x where+ optional1 ::+ (a -> b)+ -> a+ -> x++instance Optional1 a b b where+ optional1 =+ id++instance Optional1 a b (a -> b) where+ optional1 =+ const++-- | Two optional arguments, in the order of @a@ then @b@.+--+-- >>> let append :: Optional2 String String String x => x; append = optional2 (++) "abc" "def" in append :: String+-- "abcdef"+--+-- >>> let append :: Optional2 String String String x => x; append = optional2 (++) "abc" "def" in append "xyz" :: String+-- "xyzdef"+--+-- >>> let append :: Optional2 String String String x => x; append = optional2 (++) "abc" "def" in append "uvw" :: String+-- "uvwdef"+--+-- >>> let append :: Optional2 String String String x => x; append = optional2 (++) "abc" "def" in append "uvw" "xyz" :: String+-- "uvwxyz"+--+-- >>> let filterS :: Optional2 (Char -> Bool) String String x => x; filterS = optional2 filter isUpper "AbCdEfGhI" in filterS :: String+-- "ACEGI"+--+-- >>> let filterS :: Optional2 (Char -> Bool) String String x => x; filterS = optional2 filter isUpper "AbCdEfGhI" in filterS isLower :: String+-- "bdfh"+--+-- >>> let filterS :: Optional2 (Char -> Bool) String String x => x; filterS = optional2 filter isUpper "AbCdEfGhI" in filterS isLower "tUvWxYz" :: String+-- "tvxz"+class Optional2 a b c x where+ optional2 ::+ (a -> b -> c)+ -> a+ -> b+ -> x++instance Optional2 a b c c where+ optional2 =+ id++instance Optional1 b c x => Optional2 a b c (a -> x) where+ optional2 f _ b a =+ optional1 (f a) b
+ test/doctests.hs view
@@ -0,0 +1,32 @@+module Main where++import Build_doctests (deps)+import Control.Applicative+import Control.Monad+import Data.List+import System.Directory+import System.FilePath+import Test.DocTest++main ::+ IO ()+main =+ getSources >>= \sources -> doctest $+ "-isrc"+ : "-idist/build/autogen"+ : "-optP-include"+ : "-optPdist/build/autogen/cabal_macros.h"+ : "-hide-all-packages"+ : map ("-package="++) deps ++ sources++getSources :: IO [FilePath]+getSources = filter (isSuffixOf ".hs") <$> go "src"+ where+ go dir = do+ (dirs, files) <- getFilesAndDirectories dir+ (files ++) . concat <$> mapM go dirs++getFilesAndDirectories :: FilePath -> IO ([FilePath], [FilePath])+getFilesAndDirectories dir = do+ c <- map (dir </>) . filter (`notElem` ["..", "."]) <$> getDirectoryContents dir+ (,) <$> filterM doesDirectoryExist c <*> filterM doesFileExist c