packages feed

provide (empty) → 0.1.0.0

raw patch · 5 files changed

+219/−0 lines, 5 filesdep +basedep +lensdep +provide

Dependencies added: base, lens, provide, reflection, vinyl

Files

+ LICENSE view
@@ -0,0 +1,12 @@+Copyright (c) 2024, Obsidian Systems LLC+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 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.
+ example.hs view
@@ -0,0 +1,53 @@+import Data.Proxy+import Data.Vinyl++import Control.Provide+import Control.Provide.ARec++----------------------------------------------------------------------+-- Write a library that uses some implicit arguments+----------------------------------------------------------------------++data A+type instance ValueType _ A = Int++data B+type instance ValueType _ B = Bool++needsA+  :: p `Provides` A+  => Proxy p+  -> IO ()+needsA p = putStrLn $ "A is " <> show (providedP @A p)++needsB+  :: p `Provides` B+  => Proxy p+  -> IO ()+needsB p = putStrLn $ "B is " <> show (providedP @B p)++needsBoth+  :: ( p `Provides` A+     , p `Provides` B+     )+  => Proxy p+  -> IO ()+needsBoth p = do+  needsA p+  needsB p++----------------------------------------------------------------------+-- Invoke functions from the library using an application-specific+-- provider based on a `vinyl` record+----------------------------------------------------------------------++main :: IO ()+main = do+  let provider =+        ProviderField @_ @A 5 :&+        ProviderField @_ @B True :&+        RNil+  provideARec provider $ \p -> do+    needsA p+    needsB p+    needsBoth p
+ provide.cabal view
@@ -0,0 +1,105 @@+cabal-version:       2.2+name:                provide+version:             0.1.0.0+synopsis:            Lightweight dependency injection / namespaced+typed implicit-ish arguments+description:+  In many applications, there are arguments that need to be passed deeply into call hierarchies, which can be annoying.  Implicit arguments are one potential solution to this, but they have a few issues.  Firstly, their names are based on strings, which can be noncomposable e.g. in the case that multiple libraries choose the same name.  Secondly, the type of an implicit argument can be freely chosen wherever it is used, which is more flexibility than most libraries want.  Thirdly, the semantics of implicit variables is somewhat strange with respect to let bindings, etc.+  This library instead supports associating an unlimited number of values with a context type.  Although this context type does need to be passed down somehow into the child functions, it's only one argument, not many, and it can be passed as a type or as a Proxy value.  The keys of the context are types, which libraries can provide to designate the context arguments they want.+license:             BSD-3-Clause+license-file:        LICENSE+author:              Obsidian Systems LLC+maintainer:          maintainer@obsidian.systems+category:            Data Structures+build-type:          Simple++flag strict+  description: Build with options, such as -Werror, which are inconvenient for development but good for CI+  default: False+  manual: True++common common+  default-extensions:+    AllowAmbiguousTypes+    BangPatterns+    ConstraintKinds+    DataKinds+    DefaultSignatures+    DeriveDataTypeable+    DeriveFoldable+    DeriveFunctor+    DeriveGeneric+    DeriveTraversable+    DerivingStrategies+    EmptyCase+    EmptyDataDecls+    FlexibleContexts+    FlexibleInstances+    FunctionalDependencies+    GADTs+    GeneralizedNewtypeDeriving+    ImportQualifiedPost+    InstanceSigs+    KindSignatures+    KindSignatures+    LambdaCase+    MultiParamTypeClasses+    NumericUnderscores+    OverloadedStrings+    PartialTypeSignatures+    PatternSynonyms+    PolyKinds+    QuantifiedConstraints+    QuasiQuotes+    RankNTypes+    RecursiveDo+    ScopedTypeVariables+    StandaloneDeriving+    TemplateHaskell+    TypeApplications+    TypeFamilies+    TypeOperators+    UndecidableInstances+    ViewPatterns+  if flag(strict)+    ghc-options:+      -Werror+      -Wunused-packages+  ghc-options: -Wall -fno-show-valid-hole-fits+               -- unnecessary checks+               -Wno-partial-type-signatures+               -- unsafe code+               -Wincomplete-record-updates -Werror=incomplete-record-updates+               -Wincomplete-patterns -Werror=incomplete-patterns+               -Wincomplete-uni-patterns -Werror=incomplete-uni-patterns+               -Wpartial-fields -Werror=partial-fields+               -Wmissing-fields -Werror=missing-fields+               -- unneeded code+               -Widentities+               -Wredundant-constraints+  if impl(ghc >= 8.8)+    ghc-options:+               -Wmissing-deriving-strategies+               -Werror=missing-deriving-strategies++library+  import: common+  hs-source-dirs: src+  exposed-modules:+    Control.Provide+    Control.Provide.ARec+  build-depends:+    base >= 4.9 && < 5,+    lens >= 5.3 && < 5.4,+    reflection >= 2.1 && < 2.2,+    vinyl >= 0.14 && <0.15+  default-language: Haskell2010++test-suite example+  import: common+  main-is: example.hs+  type: exitcode-stdio-1.0+  build-depends:+    base,+    provide,+    vinyl+  default-language: Haskell2010
+ src/Control/Provide.hs view
@@ -0,0 +1,20 @@+module Control.Provide where++import Data.Proxy+import Data.Kind (Type)++-- | The "type context" of a provider.  Think of this as an open record of+-- types, whose members can influence the types of values in the provider.+type family ProviderTypeContext provider++type family ValueType (t :: Type) (a :: Type) :: Type++-- | A class indicating that the given provider type provides a value for a+-- given key.  The key is typically an empty datatype declared inside the+-- library which receives values using this key.+class provider `Provides` key where+  provided :: ValueType (ProviderTypeContext provider) key+infix 4 `Provides`++providedP :: forall key provider. provider `Provides` key => Proxy provider -> ValueType (ProviderTypeContext provider) key+providedP _ = provided @provider @key
+ src/Control/Provide/ARec.hs view
@@ -0,0 +1,29 @@+module Control.Provide.ARec where++import Control.Lens+import Data.Reflection+import Data.Proxy+import Data.Vinyl hiding (Dict)+import Data.Vinyl.ARec+import Data.Vinyl.TypeLevel+import Data.Kind (Type)++import Control.Provide++data ARecProvider tctx recVal+type instance ProviderTypeContext (ARecProvider tctx recVal) = tctx++newtype ProviderField tctx (key :: Type) = ProviderField { unProviderField :: ValueType tctx key }++instance (Reifies recVal (ARec (ProviderField tctx) items), RecElem ARec key key items items (RIndex key items)) => ARecProvider tctx recVal `Provides` key where+  provided = unProviderField $ reflect (Proxy @recVal) ^. rlens @key++provideARec+  :: forall tctx items r+  .  ( NatToInt (RLength items)+     , ToARec items+     )+  => Rec (ProviderField tctx) items+  -> (forall (recVal :: Type). Reifies recVal (ARec (ProviderField tctx) items) => Proxy (ARecProvider tctx recVal) -> r)+  -> r+provideARec items r = reify (toARec items) $ \(Proxy :: Proxy recVal) -> r (Proxy @(ARecProvider tctx recVal))