lazy (empty) → 0.1
raw patch · 5 files changed
+368/−0 lines, 5 filesdep +basedep +comonadsetup-changed
Dependencies added: base, comonad
Files
- LICENSE +22/−0
- Setup.hs +2/−0
- lazy.cabal +56/−0
- library/Lazy.hs +64/−0
- library/Lazy/Prelude.hs +224/−0
+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2017, Nikita Volkov++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ lazy.cabal view
@@ -0,0 +1,56 @@+name:+ lazy+version:+ 0.1+category:+ Control+synopsis:+ Explicit laziness for Haskell+description:+ Now that we have the \"Strict\" pragma at hand we can finally work in Haskell as in a strict language.+ Sometimes though laziness is useful,+ but all Haskellers also know that types keep us safe.+ This library provides laziness as an abstraction with an explicit type-signature,+ and it so happens that this abstraction forms a monad!+ .+ This library takes inspiration from the following blog post:+ <https://nikita-volkov.github.io/if-haskell-were-strict/>+homepage:+ https://github.com/nikita-volkov/lazy+bug-reports:+ https://github.com/nikita-volkov/lazy/issues+author:+ Nikita Volkov <nikita.y.volkov@mail.ru>+maintainer:+ Nikita Volkov <nikita.y.volkov@mail.ru>+copyright:+ (c) 2017, Nikita Volkov+license:+ MIT+license-file:+ LICENSE+build-type:+ Simple+cabal-version:+ >=1.24++source-repository head+ type:+ git+ location:+ git://github.com/nikita-volkov/lazy.git++library+ hs-source-dirs:+ library+ default-extensions:+ Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFoldable, DeriveFunctor, DeriveGeneric, DeriveTraversable, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, PatternSynonyms, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples+ default-language:+ Haskell2010+ exposed-modules:+ Lazy+ other-modules:+ Lazy.Prelude+ build-depends:+ comonad >=5 && <6,+ base >=4.6 && <5
+ library/Lazy.hs view
@@ -0,0 +1,64 @@+module Lazy+(+ Lazy,+ lazy,+ unlazy,+)+where++import Lazy.Prelude hiding (lazy)+++newtype Lazy value = Lazy (IO value)++instance Functor Lazy where+ {-# INLINE fmap #-}+ fmap mapping (Lazy io) =+ Lazy (fmap mapping io)++instance Applicative Lazy where+ pure = lazy+ {-# INLINE (<*>) #-}+ (<*>) (Lazy leftIO) (Lazy rightIO) =+ Lazy (leftIO <*> rightIO)++instance Monad Lazy where+ return = pure+ {-# INLINE (>>=) #-}+ (>>=) (Lazy leftIO) rightK =+ Lazy $ do+ leftValue <- leftIO+ case rightK leftValue of+ Lazy rightIO -> rightIO++instance Comonad Lazy where+ extract = unlazy+ extend leftK right =+ lazy (leftK right)+++data LazyState value = UnevaluatedLazyState value | EvaluatedLazyState !value+++{-# INLINE lazy #-}+lazy :: value -> Lazy value+lazy value =+ unsafeDupablePerformIO $ do+ stateRef <- newIORef (UnevaluatedLazyState value)+ return $ Lazy $ do+ state <- readIORef stateRef+ case state of+ EvaluatedLazyState !value -> return value+ UnevaluatedLazyState !value -> do+ writeIORef stateRef (EvaluatedLazyState value)+ return value++{-# INLINE unlazy #-}+unlazy :: Lazy value -> value+unlazy (Lazy io) =+ -- Synchronisation is not an issue here,+ -- since there's nothing dangerous in two threads+ -- occasionally computing the same result.+ -- That would be the price of not having + -- to pay for locks-maintenance overhead.+ unsafeDupablePerformIO io
+ library/Lazy/Prelude.hs view
@@ -0,0 +1,224 @@+{-# LANGUAGE CPP #-}+-- |+-- This module reexports most of the definitions from the \"base\" package,+-- which are meant to be imported unqualified.+--+-- For details check out the source.+module Lazy.Prelude+(+ module Exports,+ -- * Reimplementations of functions presented in versions of \"base\" newer than 4.6+ -- ** Data.Bool+ bool,+ -- ** Data.Function+ (&),+ -- ** Data.Functor+ ($>),+ -- ** Data.List+ isSubsequenceOf,+ sortOn,+ uncons,+ -- ** Debug.Trace+ traceShowId,+ traceM,+ traceShowM,+)+where++-- Reexports+-------------------------++import Control.Applicative as Exports+import Control.Arrow as Exports hiding (first, second)+import Control.Category as Exports+import Control.Concurrent as Exports+import Control.Comonad as Exports hiding (($>))+import Control.Exception as Exports+import Control.Monad as Exports hiding (mapM_, sequence_, forM_, msum, mapM, sequence, forM)+import Control.Monad.Fix as Exports hiding (fix)+import Control.Monad.ST as Exports+import Data.Bits as Exports+import Data.Bool as Exports hiding (bool)+import Data.Char as Exports+import Data.Complex as Exports+import Data.Data as Exports+import Data.Dynamic as Exports+import Data.Either as Exports+import Data.Fixed as Exports+import Data.Foldable as Exports+import Data.Functor as Exports hiding (($>))+import Data.Function as Exports hiding ((.), id, (&))+import Data.Int as Exports+import Data.IORef as Exports+import Data.Ix as Exports+import Data.List as Exports hiding (sortOn, isSubsequenceOf, uncons, concat, foldr, foldl1, maximum, minimum, product, sum, all, and, any, concatMap, elem, foldl, foldr1, notElem, or, find, maximumBy, minimumBy, mapAccumL, mapAccumR, foldl')+import Data.Maybe as Exports+import Data.Monoid as Exports+import Data.Ord as Exports+import Data.Ratio as Exports+import Data.STRef as Exports+import Data.String as Exports+import Data.Traversable as Exports+import Data.Tuple as Exports+import Data.Unique as Exports+import Data.Version as Exports+import Data.Word as Exports+import Debug.Trace as Exports hiding (traceShowId, traceM, traceShowM)+import Foreign.Storable as Exports+import Foreign.Ptr as Exports+import Foreign.ForeignPtr as Exports+import Foreign.StablePtr as Exports+import GHC.Conc as Exports hiding (withMVar, threadWaitWriteSTM, threadWaitWrite, threadWaitReadSTM, threadWaitRead)+import GHC.Exts as Exports (lazy, inline, sortWith, groupWith)+import GHC.Generics as Exports (Generic)+import GHC.IO.Exception as Exports+import Numeric as Exports+import Prelude as Exports hiding (concat, foldr, mapM_, sequence_, foldl1, maximum, minimum, product, sum, all, and, any, concatMap, elem, foldl, foldr1, notElem, or, mapM, sequence, id, (.))+import System.Environment as Exports+import System.Exit as Exports+import System.IO as Exports (Handle, hClose)+import System.IO.Error as Exports+import System.IO.Unsafe as Exports+import System.Mem as Exports+import System.Mem.StableName as Exports+import System.Timeout as Exports+import Text.ParserCombinators.ReadP as Exports (ReadP, ReadS, readP_to_S, readS_to_P)+import Text.ParserCombinators.ReadPrec as Exports (ReadPrec, readPrec_to_P, readP_to_Prec, readPrec_to_S, readS_to_Prec)+import Text.Printf as Exports (printf, hPrintf)+import Text.Read as Exports (Read(..), readMaybe, readEither)+import Unsafe.Coerce as Exports++-- Conditional imports for newer bases+#if MIN_VERSION_base(4,7,0)+import Data.Coerce as Exports+import Data.Proxy as Exports+#endif+#if MIN_VERSION_base(4,8,0)+import Data.Bifunctor as Exports+import Data.Functor.Identity as Exports+import Data.Void as Exports+import Numeric.Natural as Exports+#endif+#if MIN_VERSION_base(4,9,0)+import Control.Monad.Fail as Exports (MonadFail)+import Control.Monad.IO.Class as Exports+import Data.Functor.Classes as Exports+import Data.Functor.Compose as Exports+import Data.List.NonEmpty as Exports (NonEmpty(..))+import Data.Semigroup as Exports hiding ((<>), First(..), Last(..))+#endif++-- Conditional imports for reimplementations+#if MIN_VERSION_base(4,7,0)+import Data.Bool (bool)+import Debug.Trace (traceShowId, traceM, traceShowM)+import Data.Functor (($>))+#endif+#if MIN_VERSION_base(4,8,0)+import Data.Function ((&))+import Data.List (isSubsequenceOf, sortOn, uncons)+#endif+++---------------------------------+-- Reimplementations for base-4.7+---------------------------------++#if !MIN_VERSION_base(4,7,0)++-- | Case analysis for the 'Bool' type.+-- @bool a b p@ evaluates to @a@ when @p@ is @False@, and evaluates to @b@+-- when @p@ is @True@.+bool :: a -> a -> Bool -> a+bool f t b = if b then t else f++{-|+Like 'traceShow' but returns the shown value instead of a third value.+-}+traceShowId :: (Show a) => a -> a+traceShowId a = trace (show a) a++{-|+Like 'trace' but returning unit in an arbitrary monad. Allows for convenient+use in do-notation. Note that the application of 'trace' is not an action in the+monad, as 'traceIO' is in the 'IO' monad.++> ... = do+> x <- ...+> traceM $ "x: " ++ show x+> y <- ...+> traceM $ "y: " ++ show y+-}+traceM :: (Monad m) => String -> m ()+traceM string = trace string $ return ()++{-|+Like 'traceM', but uses 'show' on the argument to convert it to a 'String'.++> ... = do+> x <- ...+> traceShowM $ x+> y <- ...+> traceShowM $ x + y+-}+traceShowM :: (Show a, Monad m) => a -> m ()+traceShowM = traceM . show++infixl 4 $>++-- | Flipped version of '<$'.+($>) :: Functor f => f a -> b -> f b+($>) = flip (<$)++#endif++---------------------------------+-- Reimplementations for base-4.8+---------------------------------++#if !MIN_VERSION_base(4,8,0)++infixl 1 &++-- | '&' is a reverse application operator. This provides notational+-- convenience. Its precedence is one higher than that of the forward+-- application operator '$', which allows '&' to be nested in '$'.+(&) :: a -> (a -> b) -> b+x & f = f x++-- | The 'isSubsequenceOf' function takes two lists and returns 'True' if the+-- first list is a subsequence of the second list.+--+-- @'isSubsequenceOf' x y@ is equivalent to @'elem' x ('subsequences' y)@.+--+-- ==== __Examples__+--+-- >>> isSubsequenceOf "GHC" "The Glorious Haskell Compiler"+-- True+-- >>> isSubsequenceOf ['a','d'..'z'] ['a'..'z']+-- True+-- >>> isSubsequenceOf [1..10] [10,9..0]+-- False+isSubsequenceOf :: (Eq a) => [a] -> [a] -> Bool+isSubsequenceOf [] _ = True+isSubsequenceOf _ [] = False+isSubsequenceOf a@(x:a') (y:b) | x == y = isSubsequenceOf a' b+ | otherwise = isSubsequenceOf a b++-- | Decompose a list into its head and tail. If the list is empty,+-- returns 'Nothing'. If the list is non-empty, returns @'Just' (x, xs)@,+-- where @x@ is the head of the list and @xs@ its tail.+uncons :: [a] -> Maybe (a, [a])+uncons [] = Nothing+uncons (x:xs) = Just (x, xs)++-- | Sort a list by comparing the results of a key function applied to each+-- element. @sortOn f@ is equivalent to @sortBy . comparing f@, but has the+-- performance advantage of only evaluating @f@ once for each element in the+-- input list. This is called the decorate-sort-undecorate paradigm, or+-- Schwartzian transform.+sortOn :: Ord b => (a -> b) -> [a] -> [a]+sortOn f =+ map snd . sortBy (comparing fst) . map (\x -> let y = f x in y `seq` (y, x))++#endif