packages feed

dependency (empty) → 0.1.0.0

raw patch · 10 files changed

+317/−0 lines, 10 filesdep +ansi-wl-pprintdep +basedep +binarysetup-changed

Dependencies added: ansi-wl-pprint, base, binary, containers, criterion, deepseq, dependency, hspec, microlens, recursion-schemes

Files

+ LICENSE view
@@ -0,0 +1,11 @@+Copyright Vanessa McHale (c) 2018++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 copyright holder nor the names of its 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 HOLDER 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.
+ README.md view
@@ -0,0 +1,3 @@+# dependency++A (not fully working) library for dependency resolution.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ bench/Bench.hs view
@@ -0,0 +1,32 @@+module Main where++import           Criterion.Main+import           Data.Dependency++microlens :: Dependency+microlens = Dependency "microlens" mempty mempty++free :: Dependency+free = Dependency "free" mempty mempty++comonad :: Dependency+comonad = Dependency "comonad" mempty mempty++bifunctors :: Dependency+bifunctors = Dependency "bifunctors" mempty ["comonad"]++lens :: Dependency+lens = Dependency "lens" mempty ["free", "comonad"]++deps :: [Dependency]+deps = [free, lens, comonad]++main :: IO ()+main =+    defaultMain [ bgroup "buildSequence"+                      [ bench "3" $ nf buildSequence deps ]+                , bgroup "asGraph"+                      [ bench "3" $ nf (fst . asGraph) deps+                      , bench "6" $ nf (fst . asGraph) (bifunctors : microlens : deps)+                      ]+                ]
+ dependency.cabal view
@@ -0,0 +1,71 @@+name:                dependency+version:             0.1.0.0+synopsis:            Dependency resolution for package management+-- description:+license:             BSD3+license-file:        LICENSE+author:              Vanessa McHale+maintainer:          vamchale@gmail.com+copyright:           Copyright: (c) 2018 Vanessa McHale+category:            Development, Build+build-type:          Simple+extra-doc-files:     README.md+cabal-version:       1.18++Flag development {+  Description: Enable `-Werror`+  manual: True+  default: False+}++library+  hs-source-dirs:      src+  exposed-modules:     Data.Dependency+  other-modules:       Data.Dependency.Type+                     , Data.Dependency.Error+                     , Data.Dependency.Sort+  build-depends:       base >= 4.10 && < 5+                     , ansi-wl-pprint+                     , containers+                     , recursion-schemes+                     , microlens+                     , deepseq+                     , binary+  default-language:    Haskell2010+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -Wall++test-suite dependency-test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Spec.hs+  build-depends:       base+                     , dependency+                     , hspec+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -threaded -rtsopts -with-rtsopts=-N -Wall+  default-language:    Haskell2010++benchmark dependency-bench+  type:                exitcode-stdio-1.0+  hs-source-dirs:      bench+  main-is:             Bench.hs+  build-depends:       base+                     , dependency+                     , criterion+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -Wall+  default-language:    Haskell2010++source-repository head+  type:     darcs+  location: https://hub.darcs.net/vmchale/ats
+ src/Data/Dependency.hs view
@@ -0,0 +1,47 @@+module Data.Dependency+    ( -- * Functions+      resolveDependencies+    , isPresent+    , buildSequence+    , asGraph+    -- * Types+    , Dependency (..)+    , PackageSet (..)+    , Version (..)+    ) where++import           Data.Dependency.Sort+import           Data.Dependency.Type+import           Data.List            (groupBy)+import qualified Data.Map             as M+import qualified Data.Set             as S++isPresent :: PackageSet -> Dependency -> Bool+isPresent (PackageSet ps) (Dependency ln _ _) = g (M.lookup ln ps)+    where g (Just x) = not (S.null x)+          g Nothing  = False++latest :: PackageSet -> Dependency -> Maybe (String, Version)+latest (PackageSet ps) (Dependency ln _ _) = do+    s <- M.lookup ln ps+    (,) ln <$> S.lookupMax s++buildSequence :: [Dependency] -> [[Dependency]]+buildSequence = reverse . groupBy independent . sortDeps+    where independent (Dependency ln _ ls) (Dependency ln' _ ls') = ln' `notElem` ls && ln `notElem` ls'++-- | Heuristics:+--+-- 1. Always use a newer version when possible+--+-- 2. Obey constraints+--+-- 3. Specify an error for circular dependencies+--+-- 4. Specify an error for overconstrained builds+--+-- 5. Specify an error if a package is not present.+--+-- This doesn't do any package resolution beyond versioning.+resolveDependencies :: [[Dependency]] -> PackageSet -> Maybe [[(String, Version)]]+resolveDependencies sd ps = sequence (traverse (latest ps) <$> sd)
+ src/Data/Dependency/Error.hs view
@@ -0,0 +1,13 @@+module Data.Dependency.Error ( ResolveError (..)+                             , DepM+                             ) where++import           Data.Dependency.Type++type DepM = Either ResolveError++-- | An error that can occur during package resolution.+data ResolveError = NoSolution+                  | CircularDependencies String String+                  | NotPresent [String]+                  | Conflict String String (Constraint Version) (Constraint Version)
+ src/Data/Dependency/Sort.hs view
@@ -0,0 +1,19 @@+module Data.Dependency.Sort ( sortDeps+                            , asGraph+                            ) where++import           Data.Dependency.Type+import           Data.Graph+import           Lens.Micro+import           Lens.Micro.Extras++asGraph :: [Dependency] -> (Graph, Vertex -> Dependency)+asGraph ds = (f triple, keys)+    where triple = graphFromEdges (zip3 ds (_libName <$> ds) (_libDependencies <$> ds))+          f = view _1+          s = view _2+          keys = view _1 . s triple++sortDeps :: [Dependency] -> [Dependency]+sortDeps ds = fmap find . topSort $ g+    where (g, find) = asGraph ds
+ src/Data/Dependency/Type.hs view
@@ -0,0 +1,99 @@+{-# LANGUAGE DeriveAnyClass             #-}+{-# LANGUAGE DeriveFoldable             #-}+{-# LANGUAGE DeriveFunctor              #-}+{-# LANGUAGE DeriveGeneric              #-}+{-# LANGUAGE DeriveTraversable          #-}+{-# LANGUAGE DerivingStrategies         #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE KindSignatures             #-}+{-# LANGUAGE OverloadedStrings          #-}+{-# LANGUAGE PatternSynonyms            #-}+{-# LANGUAGE TemplateHaskell            #-}+{-# LANGUAGE TypeFamilies               #-}++module Data.Dependency.Type ( Dependency (..)+                            , Version (..)+                            , Constraint (..)+                            , PackageSet (..)+                            , BuildTarget (..)+                            -- * Helper functions+                            , satisfies+                            ) where++import           Control.DeepSeq              (NFData)+import           Data.Binary+import           Data.Functor.Foldable+import           Data.Functor.Foldable.TH+import           Data.List                    (intercalate)+import qualified Data.Map                     as M+import           Data.Semigroup+import qualified Data.Set                     as S+import           GHC.Generics                 (Generic)+import           Text.PrettyPrint.ANSI.Leijen hiding ((<$>), (<>))++newtype PackageSet = PackageSet (M.Map String (S.Set Version))+    deriving (Eq, Ord, Generic)+    deriving newtype Binary++newtype Version = Version [Integer]+    deriving (Eq, Generic)+    deriving newtype (NFData, Binary)++{-# COMPLETE V #-}++pattern V :: [Integer] -> Version+pattern V is = Version is++instance Show Version where+    show (Version is) = intercalate "." (show <$> is)++instance Ord Version where+    (V []) <= (V []) = True+    (V []) <= _ = True+    _ <= (V []) = False+    (V (x:xs)) <= (V (y:ys))+        | x == y = Version xs <= Version ys+        | otherwise = x <= y++-- TODO comonad??+data Constraint a = LessThanEq a+                  | GreaterThanEq a+                  | Eq a+                  | Bounded (Constraint a) (Constraint a)+                  | None+                  deriving (Show, Eq, Ord, Functor, Generic, NFData)++-- free moinoid but "pokey" because it has extra constructors+instance Semigroup (Constraint a) where+    (<>) None x = x+    (<>) x None = x+    (<>) x y    = Bounded x y++instance Monoid (Constraint a) where+    mempty = None+    mappend = (<>)++makeBaseFunctor ''Constraint++instance Pretty a => Pretty (Constraint a) where+    pretty = cata a where+        a (LessThanEqF v)    = "≤" <+> pretty v+        a (GreaterThanEqF v) = "≥" <+> pretty v+        a (EqF v)            = "≡" <+> pretty v+        a (BoundedF c c')    = c <+> "∧" <+> c'+        a NoneF              = mempty++satisfies :: (Ord a) => Constraint a -> a -> Bool+satisfies (LessThanEq x) y    = x <= y+satisfies (GreaterThanEq x) y = x >= y+satisfies (Eq x) y            = x == y+satisfies (Bounded x y) z     = satisfies x z && satisfies y z+satisfies None _              = True++data Dependency = Dependency { _libName         :: String+                             , _libConstraint   :: Constraint Version+                             , _libDependencies :: [String]+                             }+                             deriving (Show, Eq, Ord, Generic, NFData)++newtype BuildTarget a = BuildTarget { targetFunction :: (String, Version) -> a }
+ test/Spec.hs view
@@ -0,0 +1,20 @@+import           Data.Dependency+import           Test.Hspec++free :: Dependency+free = Dependency "free" mempty mempty++comonad :: Dependency+comonad = Dependency "comonad" mempty mempty++lens :: Dependency+lens = Dependency "lens" mempty ["free", "comonad"]++deps :: [Dependency]+deps = [free, lens, comonad]++main :: IO ()+main = hspec $ parallel $+    describe "buildSequence" $+        it "correctly orders dependencies" $+            buildSequence deps `shouldBe` [[free, comonad], [lens]]