1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 14:21:31 +03:00
semantic/src/Prologue.hs

85 lines
2.9 KiB
Haskell
Raw Normal View History

2018-02-28 19:27:25 +03:00
{-# LANGUAGE UndecidableInstances #-}
module Prologue
( module X
2018-11-05 19:06:59 +03:00
, eitherA
, foldMapA
, maybeM
2018-04-16 22:48:45 +03:00
, maybeLast
, fromMaybeLast
) where
2018-02-27 18:37:09 +03:00
import Debug.Trace as X (traceShowM, traceM)
Add NFData instances to enable more accurate benchmarking. Because we're getting serious about benchmarking in the run-up to Windrose, it's time to bring in the `deepseq` package to ensure that benchmarks can fully evaluate the result of a test case. The `deepseq` package provides an `NFData` typeclass: ``` class NFData a where rnf :: a -> () ``` Instances use the `seq` combinator to ensure that the argument to `rnf` is fully evaluated, returning (). If there is a `Generic` instance for `a`, the implementation can be omitted. This patch adds NFData for every syntax node, graph vertex, environment data structures, and exceptions. It is long, but the work is very straightforward, so don't panick. The benchmark suite (`stack bench`) now produces more accurate results. The benchmarks previously mimicked `rnf` by calling `show` on the result of an evaluation or graph construction; now that we have actual `NFData` instances we can use the `nfIO` combinator from criterion. This has sped up the evaluation benchmarks and reduced their memory consumption, while it has slowed down the call graph benchmarks, as those benchmarks weren't evaluating the whole of the graph. Unfortunately, this patch increases compile times, as we have to derive a few more Generic instances. I wish this weren't the case, but there's little we can do about it now. In the future I have some plans for how to reduce compile time, and I bet that those gains will at least nullify the speed hit from this patch. Now that we have NFData instances for every data type, we can start benchmarking assignments, in preparation for fixing #2205. This patch also pulls in updates to `effects` and `fastsum` that add appropriate NFData instances for the data they vend.
2018-10-17 21:08:47 +03:00
import Control.DeepSeq as X
2018-02-28 19:33:24 +03:00
import Data.Bifunctor.Join as X
import Data.Bits as X
2018-02-28 19:33:24 +03:00
import Data.ByteString as X (ByteString)
import Data.Coerce as X
import Data.Int as X (Int8, Int16, Int32, Int64)
2019-01-10 23:53:15 +03:00
import Data.Functor.Both as X (Both (Both), runBothWith)
import Data.Either as X (fromLeft, fromRight)
2018-02-28 19:33:24 +03:00
import Data.IntMap as X (IntMap)
import Data.IntSet as X (IntSet)
2018-03-22 20:01:25 +03:00
import Data.Ix as X (Ix (..))
import Data.List.NonEmpty as X (NonEmpty (..), nonEmpty, some1)
2018-02-28 19:33:24 +03:00
import Data.Map as X (Map)
import Data.Maybe as X
2018-03-22 20:01:25 +03:00
import Data.Monoid (Alt (..))
2018-02-28 19:33:24 +03:00
import Data.Sequence as X (Seq)
2018-06-15 18:41:15 +03:00
import Data.Semilattice.Lower as X (Lower(..))
2018-02-28 19:33:24 +03:00
import Data.Set as X (Set)
2018-05-17 01:25:02 +03:00
import Data.Sum as X (Sum, Element, Elements, (:<), (:<:), Apply (..), inject)
2018-02-28 19:33:24 +03:00
import Data.Text as X (Text)
import Data.These as X
import Data.Word as X (Word8, Word16, Word32, Word64)
2018-03-22 20:01:25 +03:00
import Control.Exception as X hiding (Handler (..), assert, evaluate, throw, throwIO, throwTo)
2018-02-28 19:33:24 +03:00
-- Typeclasses
import Control.Applicative as X
import Control.Arrow as X ((&&&), (***))
2018-09-12 01:54:37 +03:00
import Control.Monad as X hiding (fail, return)
2018-03-22 20:01:25 +03:00
import Control.Monad.Fail as X (MonadFail (..))
import Control.Monad.IO.Class as X (MonadIO (..))
2018-02-28 19:33:24 +03:00
import Data.Algebra as X
import Data.Bifoldable as X
2018-03-22 20:01:25 +03:00
import Data.Bifunctor as X (Bifunctor (..))
2018-02-28 19:33:24 +03:00
import Data.Bitraversable as X
2018-03-22 20:01:25 +03:00
import Data.Foldable as X hiding (product, sum)
2018-02-28 19:33:24 +03:00
import Data.Function as X (fix, on, (&))
2018-03-22 20:01:25 +03:00
import Data.Functor as X (void, ($>))
2018-02-28 19:33:24 +03:00
import Data.Functor.Classes as X
import Data.Functor.Classes.Generic as X
2018-03-22 20:01:25 +03:00
import Data.Functor.Foldable as X (Base, Corecursive (..), Recursive (..))
import Data.Hashable as X (Hashable, hash, hashUsing, hashWithSalt)
2018-05-16 21:43:58 +03:00
import Data.Hashable.Lifted as X (Hashable1(..), hashWithSalt1)
2018-03-22 20:01:25 +03:00
import Data.Monoid as X (First (..), Last (..), Monoid (..))
import Data.Monoid.Generic as X
2018-03-22 20:01:25 +03:00
import Data.Proxy as X (Proxy (..))
import Data.Semigroup as X (Semigroup (..))
2018-02-28 19:33:24 +03:00
import Data.Traversable as X
import Data.Typeable as X (Typeable)
-- Generics
2018-10-13 00:54:57 +03:00
import GHC.Generics as X (Generic, Generic1)
2018-02-28 19:33:24 +03:00
import GHC.Stack as X
2018-03-15 23:14:51 +03:00
-- | Fold a collection by mapping each element onto an 'Alternative' action.
foldMapA :: (Alternative m, Foldable t) => (b -> m a) -> t b -> m a
foldMapA f = getAlt . foldMap (Alt . f)
2018-04-16 22:48:45 +03:00
maybeLast :: Foldable t => b -> (a -> b) -> t a -> b
maybeLast b f = maybe b f . getLast . foldMap (Last . Just)
fromMaybeLast :: Foldable t => a -> t a -> a
fromMaybeLast b = fromMaybe b . getLast . foldMap (Last . Just)
-- | Extract the 'Just' of a 'Maybe' in an 'Applicative' context or, given 'Nothing', run the provided action.
maybeM :: Applicative f => f a -> Maybe a -> f a
maybeM f = maybe f pure
2018-11-05 19:06:59 +03:00
-- Promote a function to either-applicatives.
eitherA :: Applicative f => (b -> f (Either a c)) -> Either a b -> f (Either a c)
eitherA = either (pure . Left)