{-| Position information for syntax. Crucial for giving good error messages.
-}

module Agda.Syntax.Position
  ( -- * Positions
    Position
  , PositionWithoutFile
  , Position'(..)
  , SrcFile
  , RangeFile(..)
  , mkRangeFile
  , positionInvariant
  , startPos
  , movePos
  , movePosByString
  , backupPos
  , startPos'

    -- * Intervals
  , Interval
  , IntervalWithoutFile
  , Interval'(..)
  , intervalInvariant
  , posToInterval
  , getIntervalFile
  , iLength
  , fuseIntervals
  , setIntervalFile

    -- * Ranges
  , Range
  , Range'(..)
  , rangeInvariant
  , consecutiveAndSeparated
  , intervalsToRange
  , intervalToRange
  , rangeIntervals
  , rangeFile
  , rangeModule'
  , rangeModule
  , rightMargin
  , noRange
  , posToRange, posToRange'
  , rStart, rStart'
  , rEnd, rEnd'
  , rangeToInterval
  , rangeToIntervalWithFile
  , continuous
  , continuousPerLine
  , PrintRange(..)
  , HasRange(..)
  , SetRange(..)
  , KillRange(..)
  , KillRangeT
  , killRangeMap
  , killRange1, killRange2, killRange3, killRange4, killRange5, killRange6, killRange7
  , killRange8, killRange9, killRange10, killRange11, killRange12, killRange13, killRange14
  , killRange15, killRange16, killRange17, killRange18, killRange19
  , withRangeOf
  , fuseRange
  , fuseRanges
  , beginningOf
  , beginningOfFile
  , interleaveRanges
  ) where

import Prelude hiding ( null )

import Control.DeepSeq
import Control.Monad
import Control.Monad.Writer (runWriter, tell)

import qualified Data.Foldable as Fold
import Data.Function
import Data.Int
import Data.List (sort)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Semigroup (Semigroup(..))
import Data.Void

import GHC.Generics (Generic)

import {-# SOURCE #-} Agda.Syntax.TopLevelModuleName
  (TopLevelModuleName)

import Agda.Utils.FileName
import Agda.Utils.List
import Agda.Utils.List1 (List1)
import Agda.Utils.List2 (List2)
import qualified Agda.Utils.Maybe.Strict as Strict
import Agda.Utils.Null
import Agda.Utils.Permutation
import Agda.Utils.Pretty

import Agda.Utils.Impossible

{--------------------------------------------------------------------------
    Types and classes
 --------------------------------------------------------------------------}

-- | Represents a point in the input.
--
-- If two positions have the same 'srcFile' and 'posPos' components,
-- then the final two components should be the same as well, but since
-- this can be hard to enforce the program should not rely too much on
-- the last two components; they are mainly there to improve error
-- messages for the user.
--
-- Note the invariant which positions have to satisfy: 'positionInvariant'.
data Position' a = Pn
  { forall a. Position' a -> a
srcFile :: !a
    -- ^ File.
  , forall a. Position' a -> Int32
posPos  :: !Int32
    -- ^ Position, counting from 1.
  , forall a. Position' a -> Int32
posLine :: !Int32
    -- ^ Line number, counting from 1.
  , forall a. Position' a -> Int32
posCol  :: !Int32
    -- ^ Column number, counting from 1.
  }
  deriving (Int -> Position' a -> ShowS
forall a. Show a => Int -> Position' a -> ShowS
forall a. Show a => [Position' a] -> ShowS
forall a. Show a => Position' a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Position' a] -> ShowS
$cshowList :: forall a. Show a => [Position' a] -> ShowS
show :: Position' a -> String
$cshow :: forall a. Show a => Position' a -> String
showsPrec :: Int -> Position' a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Position' a -> ShowS
Show, forall a b. a -> Position' b -> Position' a
forall a b. (a -> b) -> Position' a -> Position' b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Position' b -> Position' a
$c<$ :: forall a b. a -> Position' b -> Position' a
fmap :: forall a b. (a -> b) -> Position' a -> Position' b
$cfmap :: forall a b. (a -> b) -> Position' a -> Position' b
Functor, forall a. Eq a => a -> Position' a -> Bool
forall a. Num a => Position' a -> a
forall a. Ord a => Position' a -> a
forall m. Monoid m => Position' m -> m
forall a. Position' a -> Bool
forall a. Position' a -> Int
forall a. Position' a -> [a]
forall a. (a -> a -> a) -> Position' a -> a
forall m a. Monoid m => (a -> m) -> Position' a -> m
forall b a. (b -> a -> b) -> b -> Position' a -> b
forall a b. (a -> b -> b) -> b -> Position' a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Position' a -> a
$cproduct :: forall a. Num a => Position' a -> a
sum :: forall a. Num a => Position' a -> a
$csum :: forall a. Num a => Position' a -> a
minimum :: forall a. Ord a => Position' a -> a
$cminimum :: forall a. Ord a => Position' a -> a
maximum :: forall a. Ord a => Position' a -> a
$cmaximum :: forall a. Ord a => Position' a -> a
elem :: forall a. Eq a => a -> Position' a -> Bool
$celem :: forall a. Eq a => a -> Position' a -> Bool
length :: forall a. Position' a -> Int
$clength :: forall a. Position' a -> Int
null :: forall a. Position' a -> Bool
$cnull :: forall a. Position' a -> Bool
toList :: forall a. Position' a -> [a]
$ctoList :: forall a. Position' a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Position' a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Position' a -> a
foldr1 :: forall a. (a -> a -> a) -> Position' a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Position' a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Position' a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Position' a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Position' a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Position' a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Position' a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Position' a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Position' a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Position' a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Position' a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Position' a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Position' a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Position' a -> m
fold :: forall m. Monoid m => Position' m -> m
$cfold :: forall m. Monoid m => Position' m -> m
Foldable, Functor Position'
Foldable Position'
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
Position' (m a) -> m (Position' a)
forall (f :: * -> *) a.
Applicative f =>
Position' (f a) -> f (Position' a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Position' a -> m (Position' b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Position' a -> f (Position' b)
sequence :: forall (m :: * -> *) a.
Monad m =>
Position' (m a) -> m (Position' a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
Position' (m a) -> m (Position' a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Position' a -> m (Position' b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Position' a -> m (Position' b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Position' (f a) -> f (Position' a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Position' (f a) -> f (Position' a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Position' a -> f (Position' b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Position' a -> f (Position' b)
Traversable, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Position' a) x -> Position' a
forall a x. Position' a -> Rep (Position' a) x
$cto :: forall a x. Rep (Position' a) x -> Position' a
$cfrom :: forall a x. Position' a -> Rep (Position' a) x
Generic)

positionInvariant :: Position' a -> Bool
positionInvariant :: forall a. Position' a -> Bool
positionInvariant Position' a
p =
  forall a. Position' a -> Int32
posPos Position' a
p forall a. Ord a => a -> a -> Bool
> Int32
0 Bool -> Bool -> Bool
&& forall a. Position' a -> Int32
posLine Position' a
p forall a. Ord a => a -> a -> Bool
> Int32
0 Bool -> Bool -> Bool
&& forall a. Position' a -> Int32
posCol Position' a
p forall a. Ord a => a -> a -> Bool
> Int32
0

importantPart :: Position' a -> (a, Int32)
importantPart :: forall a. Position' a -> (a, Int32)
importantPart Position' a
p = (forall a. Position' a -> a
srcFile Position' a
p, forall a. Position' a -> Int32
posPos Position' a
p)

instance Eq a => Eq (Position' a) where
  == :: Position' a -> Position' a -> Bool
(==) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. Position' a -> (a, Int32)
importantPart

instance Ord a => Ord (Position' a) where
  compare :: Position' a -> Position' a -> Ordering
compare = forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. Position' a -> (a, Int32)
importantPart

type SrcFile = Strict.Maybe RangeFile

-- | File information used in the 'Position', 'Interval' and 'Range'
-- types.
data RangeFile = RangeFile
  { RangeFile -> AbsolutePath
rangeFilePath :: !AbsolutePath
    -- ^ The file's path.
  , RangeFile -> Maybe TopLevelModuleName
rangeFileName :: !(Maybe TopLevelModuleName)
    -- ^ The file's top-level module name.
    --
    -- This field is optional, but some things may break if the field
    -- is not instantiated with an actual top-level module name. For
    -- instance, the 'Eq' and 'Ord' instances only make use of this
    -- field.
    --
    -- The field uses 'Maybe' rather than 'Strict.Maybe' because it
    -- should be possible to instantiate it with something that is not
    -- yet defined (see 'Agda.Interaction.Imports.parseSource').
    --
    -- This 'TopLevelModuleName' should not contain a range.
  }
  deriving (Int -> RangeFile -> ShowS
[RangeFile] -> ShowS
RangeFile -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RangeFile] -> ShowS
$cshowList :: [RangeFile] -> ShowS
show :: RangeFile -> String
$cshow :: RangeFile -> String
showsPrec :: Int -> RangeFile -> ShowS
$cshowsPrec :: Int -> RangeFile -> ShowS
Show, forall x. Rep RangeFile x -> RangeFile
forall x. RangeFile -> Rep RangeFile x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RangeFile x -> RangeFile
$cfrom :: forall x. RangeFile -> Rep RangeFile x
Generic)

-- | A smart constructor for 'RangeFile'.

mkRangeFile :: AbsolutePath -> Maybe TopLevelModuleName -> RangeFile
mkRangeFile :: AbsolutePath -> Maybe TopLevelModuleName -> RangeFile
mkRangeFile AbsolutePath
f Maybe TopLevelModuleName
top = RangeFile
  { rangeFilePath :: AbsolutePath
rangeFilePath = AbsolutePath
f
  , rangeFileName :: Maybe TopLevelModuleName
rangeFileName = forall a. KillRange a => KillRangeT a
killRange Maybe TopLevelModuleName
top
  }

-- | Only the 'rangeFileName' component is compared.

instance Eq RangeFile where
  == :: RangeFile -> RangeFile -> Bool
(==) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` RangeFile -> Maybe TopLevelModuleName
rangeFileName

-- | Only the 'rangeFileName' component is compared.

instance Ord RangeFile where
  compare :: RangeFile -> RangeFile -> Ordering
compare = forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` RangeFile -> Maybe TopLevelModuleName
rangeFileName

instance NFData RangeFile where
  rnf :: RangeFile -> ()
rnf (RangeFile AbsolutePath
_ Maybe TopLevelModuleName
n) = forall a. NFData a => a -> ()
rnf Maybe TopLevelModuleName
n

type Position            = Position' SrcFile
type PositionWithoutFile = Position' ()

instance NFData Position where
  rnf :: Position -> ()
rnf = (seq :: forall a b. a -> b -> b
`seq` ())

instance NFData PositionWithoutFile where
  rnf :: PositionWithoutFile -> ()
rnf = (seq :: forall a b. a -> b -> b
`seq` ())

-- | An interval. The @iEnd@ position is not included in the interval.
--
-- Note the invariant which intervals have to satisfy: 'intervalInvariant'.
data Interval' a = Interval { forall a. Interval' a -> Position' a
iStart, forall a. Interval' a -> Position' a
iEnd :: !(Position' a) }
  deriving (Int -> Interval' a -> ShowS
forall a. Show a => Int -> Interval' a -> ShowS
forall a. Show a => [Interval' a] -> ShowS
forall a. Show a => Interval' a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Interval' a] -> ShowS
$cshowList :: forall a. Show a => [Interval' a] -> ShowS
show :: Interval' a -> String
$cshow :: forall a. Show a => Interval' a -> String
showsPrec :: Int -> Interval' a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Interval' a -> ShowS
Show, Interval' a -> Interval' a -> Bool
forall a. Eq a => Interval' a -> Interval' a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Interval' a -> Interval' a -> Bool
$c/= :: forall a. Eq a => Interval' a -> Interval' a -> Bool
== :: Interval' a -> Interval' a -> Bool
$c== :: forall a. Eq a => Interval' a -> Interval' a -> Bool
Eq, Interval' a -> Interval' a -> Bool
Interval' a -> Interval' a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (Interval' a)
forall a. Ord a => Interval' a -> Interval' a -> Bool
forall a. Ord a => Interval' a -> Interval' a -> Ordering
forall a. Ord a => Interval' a -> Interval' a -> Interval' a
min :: Interval' a -> Interval' a -> Interval' a
$cmin :: forall a. Ord a => Interval' a -> Interval' a -> Interval' a
max :: Interval' a -> Interval' a -> Interval' a
$cmax :: forall a. Ord a => Interval' a -> Interval' a -> Interval' a
>= :: Interval' a -> Interval' a -> Bool
$c>= :: forall a. Ord a => Interval' a -> Interval' a -> Bool
> :: Interval' a -> Interval' a -> Bool
$c> :: forall a. Ord a => Interval' a -> Interval' a -> Bool
<= :: Interval' a -> Interval' a -> Bool
$c<= :: forall a. Ord a => Interval' a -> Interval' a -> Bool
< :: Interval' a -> Interval' a -> Bool
$c< :: forall a. Ord a => Interval' a -> Interval' a -> Bool
compare :: Interval' a -> Interval' a -> Ordering
$ccompare :: forall a. Ord a => Interval' a -> Interval' a -> Ordering
Ord, forall a b. a -> Interval' b -> Interval' a
forall a b. (a -> b) -> Interval' a -> Interval' b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Interval' b -> Interval' a
$c<$ :: forall a b. a -> Interval' b -> Interval' a
fmap :: forall a b. (a -> b) -> Interval' a -> Interval' b
$cfmap :: forall a b. (a -> b) -> Interval' a -> Interval' b
Functor, forall a. Eq a => a -> Interval' a -> Bool
forall a. Num a => Interval' a -> a
forall a. Ord a => Interval' a -> a
forall m. Monoid m => Interval' m -> m
forall a. Interval' a -> Bool
forall a. Interval' a -> Int
forall a. Interval' a -> [a]
forall a. (a -> a -> a) -> Interval' a -> a
forall m a. Monoid m => (a -> m) -> Interval' a -> m
forall b a. (b -> a -> b) -> b -> Interval' a -> b
forall a b. (a -> b -> b) -> b -> Interval' a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Interval' a -> a
$cproduct :: forall a. Num a => Interval' a -> a
sum :: forall a. Num a => Interval' a -> a
$csum :: forall a. Num a => Interval' a -> a
minimum :: forall a. Ord a => Interval' a -> a
$cminimum :: forall a. Ord a => Interval' a -> a
maximum :: forall a. Ord a => Interval' a -> a
$cmaximum :: forall a. Ord a => Interval' a -> a
elem :: forall a. Eq a => a -> Interval' a -> Bool
$celem :: forall a. Eq a => a -> Interval' a -> Bool
length :: forall a. Interval' a -> Int
$clength :: forall a. Interval' a -> Int
null :: forall a. Interval' a -> Bool
$cnull :: forall a. Interval' a -> Bool
toList :: forall a. Interval' a -> [a]
$ctoList :: forall a. Interval' a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Interval' a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Interval' a -> a
foldr1 :: forall a. (a -> a -> a) -> Interval' a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Interval' a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Interval' a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Interval' a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Interval' a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Interval' a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Interval' a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Interval' a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Interval' a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Interval' a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Interval' a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Interval' a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Interval' a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Interval' a -> m
fold :: forall m. Monoid m => Interval' m -> m
$cfold :: forall m. Monoid m => Interval' m -> m
Foldable, Functor Interval'
Foldable Interval'
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
Interval' (m a) -> m (Interval' a)
forall (f :: * -> *) a.
Applicative f =>
Interval' (f a) -> f (Interval' a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Interval' a -> m (Interval' b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Interval' a -> f (Interval' b)
sequence :: forall (m :: * -> *) a.
Monad m =>
Interval' (m a) -> m (Interval' a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
Interval' (m a) -> m (Interval' a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Interval' a -> m (Interval' b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Interval' a -> m (Interval' b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Interval' (f a) -> f (Interval' a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Interval' (f a) -> f (Interval' a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Interval' a -> f (Interval' b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Interval' a -> f (Interval' b)
Traversable, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Interval' a) x -> Interval' a
forall a x. Interval' a -> Rep (Interval' a) x
$cto :: forall a x. Rep (Interval' a) x -> Interval' a
$cfrom :: forall a x. Interval' a -> Rep (Interval' a) x
Generic)

type Interval            = Interval' SrcFile
type IntervalWithoutFile = Interval' ()

instance NFData Interval where
  rnf :: Interval -> ()
rnf = (seq :: forall a b. a -> b -> b
`seq` ())

instance NFData IntervalWithoutFile where
  rnf :: IntervalWithoutFile -> ()
rnf = (seq :: forall a b. a -> b -> b
`seq` ())

intervalInvariant :: Ord a => Interval' a -> Bool
intervalInvariant :: forall a. Ord a => Interval' a -> Bool
intervalInvariant Interval' a
i =
  forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall a. Position' a -> Bool
positionInvariant [forall a. Interval' a -> Position' a
iStart Interval' a
i, forall a. Interval' a -> Position' a
iEnd Interval' a
i]
    Bool -> Bool -> Bool
&&
  forall a. Interval' a -> Position' a
iStart Interval' a
i forall a. Ord a => a -> a -> Bool
<= forall a. Interval' a -> Position' a
iEnd Interval' a
i
    Bool -> Bool -> Bool
&&
  forall a. Position' a -> a
srcFile (forall a. Interval' a -> Position' a
iStart Interval' a
i) forall a. Eq a => a -> a -> Bool
== forall a. Position' a -> a
srcFile (forall a. Interval' a -> Position' a
iEnd Interval' a
i)

-- | Sets the 'srcFile' components of the interval.

setIntervalFile :: a -> Interval' b -> Interval' a
setIntervalFile :: forall a b. a -> Interval' b -> Interval' a
setIntervalFile a
f (Interval Position' b
p1 Position' b
p2) =
  forall a. Position' a -> Position' a -> Interval' a
Interval (Position' b
p1 { srcFile :: a
srcFile = a
f }) (Position' b
p2 { srcFile :: a
srcFile = a
f })

-- | Gets the 'srcFile' component of the interval. Because of the invariant,
--   they are both the same.
getIntervalFile :: Interval' a -> a
getIntervalFile :: forall a. Interval' a -> a
getIntervalFile = forall a. Position' a -> a
srcFile forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Interval' a -> Position' a
iStart

-- | Converts a file name and two positions to an interval.
posToInterval ::
  a -> PositionWithoutFile -> PositionWithoutFile -> Interval' a
posToInterval :: forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Interval' a
posToInterval a
f PositionWithoutFile
p1 PositionWithoutFile
p2 = forall a b. a -> Interval' b -> Interval' a
setIntervalFile a
f forall a b. (a -> b) -> a -> b
$
  if PositionWithoutFile
p1 forall a. Ord a => a -> a -> Bool
< PositionWithoutFile
p2
  then forall a. Position' a -> Position' a -> Interval' a
Interval PositionWithoutFile
p1 PositionWithoutFile
p2
  else forall a. Position' a -> Position' a -> Interval' a
Interval PositionWithoutFile
p2 PositionWithoutFile
p1

-- | The length of an interval.
iLength :: Interval' a -> Int32
iLength :: forall a. Interval' a -> Int32
iLength Interval' a
i = forall a. Position' a -> Int32
posPos (forall a. Interval' a -> Position' a
iEnd Interval' a
i) forall a. Num a => a -> a -> a
- forall a. Position' a -> Int32
posPos (forall a. Interval' a -> Position' a
iStart Interval' a
i)

-- | A range is a file name, plus a sequence of intervals, assumed to
-- point to the given file. The intervals should be consecutive and
-- separated.
--
-- Note the invariant which ranges have to satisfy: 'rangeInvariant'.
data Range' a
  = NoRange
  | Range !a (Seq IntervalWithoutFile)
  deriving
    (Int -> Range' a -> ShowS
forall a. Show a => Int -> Range' a -> ShowS
forall a. Show a => [Range' a] -> ShowS
forall a. Show a => Range' a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Range' a] -> ShowS
$cshowList :: forall a. Show a => [Range' a] -> ShowS
show :: Range' a -> String
$cshow :: forall a. Show a => Range' a -> String
showsPrec :: Int -> Range' a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Range' a -> ShowS
Show, Range' a -> Range' a -> Bool
forall a. Eq a => Range' a -> Range' a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Range' a -> Range' a -> Bool
$c/= :: forall a. Eq a => Range' a -> Range' a -> Bool
== :: Range' a -> Range' a -> Bool
$c== :: forall a. Eq a => Range' a -> Range' a -> Bool
Eq, Range' a -> Range' a -> Bool
Range' a -> Range' a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (Range' a)
forall a. Ord a => Range' a -> Range' a -> Bool
forall a. Ord a => Range' a -> Range' a -> Ordering
forall a. Ord a => Range' a -> Range' a -> Range' a
min :: Range' a -> Range' a -> Range' a
$cmin :: forall a. Ord a => Range' a -> Range' a -> Range' a
max :: Range' a -> Range' a -> Range' a
$cmax :: forall a. Ord a => Range' a -> Range' a -> Range' a
>= :: Range' a -> Range' a -> Bool
$c>= :: forall a. Ord a => Range' a -> Range' a -> Bool
> :: Range' a -> Range' a -> Bool
$c> :: forall a. Ord a => Range' a -> Range' a -> Bool
<= :: Range' a -> Range' a -> Bool
$c<= :: forall a. Ord a => Range' a -> Range' a -> Bool
< :: Range' a -> Range' a -> Bool
$c< :: forall a. Ord a => Range' a -> Range' a -> Bool
compare :: Range' a -> Range' a -> Ordering
$ccompare :: forall a. Ord a => Range' a -> Range' a -> Ordering
Ord, forall a b. a -> Range' b -> Range' a
forall a b. (a -> b) -> Range' a -> Range' b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Range' b -> Range' a
$c<$ :: forall a b. a -> Range' b -> Range' a
fmap :: forall a b. (a -> b) -> Range' a -> Range' b
$cfmap :: forall a b. (a -> b) -> Range' a -> Range' b
Functor, forall a. Eq a => a -> Range' a -> Bool
forall a. Num a => Range' a -> a
forall a. Ord a => Range' a -> a
forall m. Monoid m => Range' m -> m
forall a. Range' a -> Bool
forall a. Range' a -> Int
forall a. Range' a -> [a]
forall a. (a -> a -> a) -> Range' a -> a
forall m a. Monoid m => (a -> m) -> Range' a -> m
forall b a. (b -> a -> b) -> b -> Range' a -> b
forall a b. (a -> b -> b) -> b -> Range' a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Range' a -> a
$cproduct :: forall a. Num a => Range' a -> a
sum :: forall a. Num a => Range' a -> a
$csum :: forall a. Num a => Range' a -> a
minimum :: forall a. Ord a => Range' a -> a
$cminimum :: forall a. Ord a => Range' a -> a
maximum :: forall a. Ord a => Range' a -> a
$cmaximum :: forall a. Ord a => Range' a -> a
elem :: forall a. Eq a => a -> Range' a -> Bool
$celem :: forall a. Eq a => a -> Range' a -> Bool
length :: forall a. Range' a -> Int
$clength :: forall a. Range' a -> Int
null :: forall a. Range' a -> Bool
$cnull :: forall a. Range' a -> Bool
toList :: forall a. Range' a -> [a]
$ctoList :: forall a. Range' a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Range' a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Range' a -> a
foldr1 :: forall a. (a -> a -> a) -> Range' a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Range' a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Range' a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Range' a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Range' a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Range' a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Range' a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Range' a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Range' a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Range' a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Range' a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Range' a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Range' a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Range' a -> m
fold :: forall m. Monoid m => Range' m -> m
$cfold :: forall m. Monoid m => Range' m -> m
Foldable, Functor Range'
Foldable Range'
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Range' (m a) -> m (Range' a)
forall (f :: * -> *) a.
Applicative f =>
Range' (f a) -> f (Range' a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Range' a -> m (Range' b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Range' a -> f (Range' b)
sequence :: forall (m :: * -> *) a. Monad m => Range' (m a) -> m (Range' a)
$csequence :: forall (m :: * -> *) a. Monad m => Range' (m a) -> m (Range' a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Range' a -> m (Range' b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Range' a -> m (Range' b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Range' (f a) -> f (Range' a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Range' (f a) -> f (Range' a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Range' a -> f (Range' b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Range' a -> f (Range' b)
Traversable, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Range' a) x -> Range' a
forall a x. Range' a -> Rep (Range' a) x
$cto :: forall a x. Rep (Range' a) x -> Range' a
$cfrom :: forall a x. Range' a -> Rep (Range' a) x
Generic)

type Range = Range' SrcFile

instance NFData a => NFData (Range' a)

instance Null (Range' a) where
  null :: Range' a -> Bool
null Range' a
NoRange = Bool
True
  null Range{} = Bool
False

  empty :: Range' a
empty = forall a. Range' a
NoRange

instance Eq a => Semigroup (Range' a) where
  Range' a
NoRange <> :: Range' a -> Range' a -> Range' a
<> Range' a
r = Range' a
r
  Range' a
r <> Range' a
NoRange = Range' a
r
  Range a
f Seq IntervalWithoutFile
is <> Range a
f' Seq IntervalWithoutFile
is'
    | a
f forall a. Eq a => a -> a -> Bool
/= a
f'   = forall a. HasCallStack => a
__IMPOSSIBLE__
    | Bool
otherwise = forall a. a -> Seq IntervalWithoutFile -> Range' a
Range a
f (Seq IntervalWithoutFile
is forall a. Semigroup a => a -> a -> a
<> Seq IntervalWithoutFile
is')

instance Eq a => Monoid (Range' a) where
  mempty :: Range' a
mempty  = forall a. Null a => a
empty
  mappend :: Range' a -> Range' a -> Range' a
mappend = forall a. Semigroup a => a -> a -> a
(<>)

-- | The intervals that make up the range. The intervals are
-- consecutive and separated ('consecutiveAndSeparated').
rangeIntervals :: Range' a -> [IntervalWithoutFile]
rangeIntervals :: forall a. Range' a -> [IntervalWithoutFile]
rangeIntervals Range' a
NoRange      = []
rangeIntervals (Range a
_ Seq IntervalWithoutFile
is) = forall (t :: * -> *) a. Foldable t => t a -> [a]
Fold.toList Seq IntervalWithoutFile
is

-- | Turns a file name plus a list of intervals into a range.
--
-- Precondition: 'consecutiveAndSeparated'.
intervalsToRange :: a -> [IntervalWithoutFile] -> Range' a
intervalsToRange :: forall a. a -> [IntervalWithoutFile] -> Range' a
intervalsToRange a
_ [] = forall a. Range' a
NoRange
intervalsToRange a
f [IntervalWithoutFile]
is = forall a. a -> Seq IntervalWithoutFile -> Range' a
Range a
f (forall a. [a] -> Seq a
Seq.fromList [IntervalWithoutFile]
is)

-- | Are the intervals consecutive and separated, do they all point to
-- the same file, and do they satisfy the interval invariant?
consecutiveAndSeparated :: Ord a => [Interval' a] -> Bool
consecutiveAndSeparated :: forall a. Ord a => [Interval' a] -> Bool
consecutiveAndSeparated [Interval' a]
is =
  forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall a. Ord a => Interval' a -> Bool
intervalInvariant [Interval' a]
is
    Bool -> Bool -> Bool
&&
  forall a. Eq a => [a] -> Bool
allEqual (forall a b. (a -> b) -> [a] -> [b]
map (forall a. Position' a -> a
srcFile forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Interval' a -> Position' a
iStart) [Interval' a]
is)
    Bool -> Bool -> Bool
&&
  (forall a. Null a => a -> Bool
null [Interval' a]
is
     Bool -> Bool -> Bool
||
   forall (t :: * -> *). Foldable t => t Bool -> Bool
and (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a. Ord a => a -> a -> Bool
(<) (forall a b. (a -> b) -> [a] -> [b]
map forall a. Interval' a -> Position' a
iEnd   (forall a. [a] -> [a]
init [Interval' a]
is))
                    (forall a b. (a -> b) -> [a] -> [b]
map forall a. Interval' a -> Position' a
iStart (forall a. [a] -> [a]
tail [Interval' a]
is))))

-- | Range invariant.
rangeInvariant :: Ord a => Range' a -> Bool
rangeInvariant :: forall a. Ord a => Range' a -> Bool
rangeInvariant Range' a
r =
  forall a. Ord a => [Interval' a] -> Bool
consecutiveAndSeparated (forall a. Range' a -> [IntervalWithoutFile]
rangeIntervals Range' a
r)
    Bool -> Bool -> Bool
&&
  case Range' a
r of
    Range a
_ Seq IntervalWithoutFile
is -> Bool -> Bool
not (forall a. Null a => a -> Bool
null Seq IntervalWithoutFile
is)
    Range' a
NoRange    -> Bool
True

-- | The file the range is pointing to.
rangeFile :: Range -> SrcFile
rangeFile :: Range -> SrcFile
rangeFile Range
NoRange     = forall a. Maybe a
Strict.Nothing
rangeFile (Range SrcFile
f Seq IntervalWithoutFile
_) = SrcFile
f

-- | The range's top-level module name, if any.
--
-- If there is no range, then 'Nothing' is returned. If there is a
-- range without a module name, then @'Just' 'Nothing'@ is returned.
rangeModule' :: Range -> Maybe (Maybe TopLevelModuleName)
rangeModule' :: Range -> Maybe (Maybe TopLevelModuleName)
rangeModule' Range
NoRange     = forall a. Maybe a
Nothing
rangeModule' (Range SrcFile
f Seq IntervalWithoutFile
_) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ case SrcFile
f of
  SrcFile
Strict.Nothing -> forall a. Maybe a
Nothing
  Strict.Just RangeFile
f  -> RangeFile -> Maybe TopLevelModuleName
rangeFileName RangeFile
f

-- | The range's top-level module name, if any.
rangeModule :: Range -> Maybe TopLevelModuleName
rangeModule :: Range -> Maybe TopLevelModuleName
rangeModule = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall b c a. (b -> c) -> (a -> b) -> a -> c
. Range -> Maybe (Maybe TopLevelModuleName)
rangeModule'

-- | Conflate a range to its right margin.
rightMargin :: Range -> Range
rightMargin :: Range -> Range
rightMargin r :: Range
r@Range
NoRange      = Range
r
rightMargin r :: Range
r@(Range SrcFile
f Seq IntervalWithoutFile
is) = case forall a. Seq a -> ViewR a
Seq.viewr Seq IntervalWithoutFile
is of
  ViewR IntervalWithoutFile
Seq.EmptyR -> forall a. HasCallStack => a
__IMPOSSIBLE__
  Seq IntervalWithoutFile
_ Seq.:> IntervalWithoutFile
i -> forall a. a -> IntervalWithoutFile -> Range' a
intervalToRange SrcFile
f (IntervalWithoutFile
i { iStart :: PositionWithoutFile
iStart = forall a. Interval' a -> Position' a
iEnd IntervalWithoutFile
i })

-- | Wrapper to indicate that range should be printed.
newtype PrintRange a = PrintRange a
  deriving (PrintRange a -> PrintRange a -> Bool
forall a. Eq a => PrintRange a -> PrintRange a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PrintRange a -> PrintRange a -> Bool
$c/= :: forall a. Eq a => PrintRange a -> PrintRange a -> Bool
== :: PrintRange a -> PrintRange a -> Bool
$c== :: forall a. Eq a => PrintRange a -> PrintRange a -> Bool
Eq, PrintRange a -> PrintRange a -> Bool
PrintRange a -> PrintRange a -> Ordering
PrintRange a -> PrintRange a -> PrintRange a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (PrintRange a)
forall a. Ord a => PrintRange a -> PrintRange a -> Bool
forall a. Ord a => PrintRange a -> PrintRange a -> Ordering
forall a. Ord a => PrintRange a -> PrintRange a -> PrintRange a
min :: PrintRange a -> PrintRange a -> PrintRange a
$cmin :: forall a. Ord a => PrintRange a -> PrintRange a -> PrintRange a
max :: PrintRange a -> PrintRange a -> PrintRange a
$cmax :: forall a. Ord a => PrintRange a -> PrintRange a -> PrintRange a
>= :: PrintRange a -> PrintRange a -> Bool
$c>= :: forall a. Ord a => PrintRange a -> PrintRange a -> Bool
> :: PrintRange a -> PrintRange a -> Bool
$c> :: forall a. Ord a => PrintRange a -> PrintRange a -> Bool
<= :: PrintRange a -> PrintRange a -> Bool
$c<= :: forall a. Ord a => PrintRange a -> PrintRange a -> Bool
< :: PrintRange a -> PrintRange a -> Bool
$c< :: forall a. Ord a => PrintRange a -> PrintRange a -> Bool
compare :: PrintRange a -> PrintRange a -> Ordering
$ccompare :: forall a. Ord a => PrintRange a -> PrintRange a -> Ordering
Ord, PrintRange a -> Range
forall a. HasRange a => PrintRange a -> Range
forall a. (a -> Range) -> HasRange a
getRange :: PrintRange a -> Range
$cgetRange :: forall a. HasRange a => PrintRange a -> Range
HasRange, Range -> PrintRange a -> PrintRange a
forall {a}. SetRange a => HasRange (PrintRange a)
forall a. SetRange a => Range -> PrintRange a -> PrintRange a
forall a. HasRange a -> (Range -> a -> a) -> SetRange a
setRange :: Range -> PrintRange a -> PrintRange a
$csetRange :: forall a. SetRange a => Range -> PrintRange a -> PrintRange a
SetRange, KillRangeT (PrintRange a)
forall a. KillRange a => KillRangeT (PrintRange a)
forall a. KillRangeT a -> KillRange a
killRange :: KillRangeT (PrintRange a)
$ckillRange :: forall a. KillRange a => KillRangeT (PrintRange a)
KillRange)

-- | Things that have a range are instances of this class.
class HasRange a where
  getRange :: a -> Range

  default getRange :: (Foldable t, HasRange b, t b ~ a) => a -> Range
  getRange = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Fold.foldr forall u t. (HasRange u, HasRange t) => u -> t -> Range
fuseRange forall a. Range' a
noRange

instance HasRange Interval where
    getRange :: Interval -> Range
getRange Interval
i =
      forall a. a -> IntervalWithoutFile -> Range' a
intervalToRange (forall a. Position' a -> a
srcFile (forall a. Interval' a -> Position' a
iStart Interval
i))
                      (forall a b. a -> Interval' b -> Interval' a
setIntervalFile () Interval
i)

instance HasRange Range where
    getRange :: Range -> Range
getRange = forall a. a -> a
id

instance HasRange () where
  getRange :: () -> Range
getRange ()
_ = forall a. Range' a
noRange

instance HasRange Bool where
    getRange :: Bool -> Range
getRange Bool
_ = forall a. Range' a
noRange

-- | Precondition: The ranges of the list elements must point to the
-- same file (or be empty).
instance HasRange a => HasRange [a]

-- | Precondition: The ranges of the list elements must point to the
-- same file (or be empty).
instance HasRange a => HasRange (List1 a)
instance HasRange a => HasRange (List2 a)
instance HasRange a => HasRange (Maybe a)

-- | Precondition: The ranges of the tuple elements must point to the
-- same file (or be empty).
instance (HasRange a, HasRange b) => HasRange (a,b) where
    getRange :: (a, b) -> Range
getRange = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall u t. (HasRange u, HasRange t) => u -> t -> Range
fuseRange

-- | Precondition: The ranges of the tuple elements must point to the
-- same file (or be empty).
instance (HasRange a, HasRange b, HasRange c) => HasRange (a,b,c) where
    getRange :: (a, b, c) -> Range
getRange (a
x,b
y,c
z) = forall a. HasRange a => a -> Range
getRange (a
x,(b
y,c
z))

-- | Precondition: The ranges of the tuple elements must point to the
-- same file (or be empty).
instance (HasRange a, HasRange b, HasRange c, HasRange d) => HasRange (a,b,c,d) where
    getRange :: (a, b, c, d) -> Range
getRange (a
x,b
y,c
z,d
w) = forall a. HasRange a => a -> Range
getRange (a
x,(b
y,(c
z,d
w)))

-- | Precondition: The ranges of the tuple elements must point to the
-- same file (or be empty).
instance (HasRange a, HasRange b, HasRange c, HasRange d, HasRange e) => HasRange (a,b,c,d,e) where
    getRange :: (a, b, c, d, e) -> Range
getRange (a
x,b
y,c
z,d
w,e
v) = forall a. HasRange a => a -> Range
getRange (a
x,(b
y,(c
z,(d
w,e
v))))

-- | Precondition: The ranges of the tuple elements must point to the
-- same file (or be empty).
instance (HasRange a, HasRange b, HasRange c, HasRange d, HasRange e, HasRange f) => HasRange (a,b,c,d,e,f) where
    getRange :: (a, b, c, d, e, f) -> Range
getRange (a
x,b
y,c
z,d
w,e
v,f
u) = forall a. HasRange a => a -> Range
getRange (a
x,(b
y,(c
z,(d
w,(e
v,f
u)))))

-- | Precondition: The ranges of the tuple elements must point to the
-- same file (or be empty).
instance (HasRange a, HasRange b, HasRange c, HasRange d, HasRange e, HasRange f, HasRange g) => HasRange (a,b,c,d,e,f,g) where
    getRange :: (a, b, c, d, e, f, g) -> Range
getRange (a
x,b
y,c
z,d
w,e
v,f
u,g
t) = forall a. HasRange a => a -> Range
getRange (a
x,(b
y,(c
z,(d
w,(e
v,(f
u,g
t))))))

instance (HasRange a, HasRange b) => HasRange (Either a b) where
    getRange :: Either a b -> Range
getRange = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. HasRange a => a -> Range
getRange forall a. HasRange a => a -> Range
getRange

-- | If it is also possible to set the range, this is the class.
--
--   Instances should satisfy @'getRange' ('setRange' r x) == r@.
class HasRange a => SetRange a where
  setRange :: Range -> a -> a

  default setRange :: (Functor f, SetRange b, f b ~ a) => Range -> a -> a
  setRange = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SetRange a => Range -> a -> a
setRange

instance SetRange Range where
  setRange :: Range -> Range -> Range
setRange = forall a b. a -> b -> a
const

instance SetRange a => SetRange [a]
instance SetRange a => SetRange (Maybe a)

-- | Killing the range of an object sets all range information to 'noRange'.
class KillRange a where
  killRange :: KillRangeT a

  default killRange :: (Functor f, KillRange b, f b ~ a) => KillRangeT a
  killRange = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. KillRange a => KillRangeT a
killRange

type KillRangeT a = a -> a

-- | Remove ranges in keys and values of a map.
killRangeMap :: (KillRange k, KillRange v) => KillRangeT (Map k v)
killRangeMap :: forall k v. (KillRange k, KillRange v) => KillRangeT (Map k v)
killRangeMap = forall k1 k2 a. (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeysMonotonic forall a. KillRange a => KillRangeT a
killRange forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
Map.map forall a. KillRange a => KillRangeT a
killRange

killRange1 :: KillRange a => (a -> b) -> a -> b

killRange2 :: (KillRange a, KillRange b) => (a -> b -> c) -> a -> b -> c

killRange3 :: (KillRange a, KillRange b, KillRange c) =>
              (a -> b -> c -> d) -> a -> b -> c -> d

killRange4 :: (KillRange a, KillRange b, KillRange c, KillRange d) =>
              (a -> b -> c -> d -> e) -> a -> b -> c -> d -> e

killRange5 :: ( KillRange a, KillRange b, KillRange c, KillRange d
              , KillRange e ) =>
              (a -> b -> c -> d -> e -> f) -> a -> b -> c -> d -> e -> f

killRange6 :: ( KillRange a, KillRange b, KillRange c, KillRange d
              , KillRange e, KillRange f ) =>
              (a -> b -> c -> d -> e -> f -> g) -> a -> b -> c -> d -> e -> f -> g

killRange7 :: ( KillRange a, KillRange b, KillRange c, KillRange d
              , KillRange e, KillRange f, KillRange g ) =>
              (a -> b -> c -> d -> e -> f -> g -> h) -> a -> b -> c -> d -> e -> f -> g -> h

killRange8 :: ( KillRange a, KillRange b, KillRange c, KillRange d
              , KillRange e, KillRange f, KillRange g, KillRange h ) =>
              (a -> b -> c -> d -> e -> f -> g -> h -> i) ->
              a -> b -> c -> d -> e -> f -> g -> h -> i

killRange9 :: ( KillRange a, KillRange b, KillRange c, KillRange d
              , KillRange e, KillRange f, KillRange g, KillRange h
              , KillRange i ) =>
              (a -> b -> c -> d -> e -> f -> g -> h -> i -> j) ->
              a -> b -> c -> d -> e -> f -> g -> h -> i -> j

killRange10 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k

killRange11 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l

killRange12 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m

killRange13 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l
               , KillRange m ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n

killRange14 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l
               , KillRange m, KillRange n ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o

killRange15 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l
               , KillRange m, KillRange n, KillRange o ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p

killRange16 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l
               , KillRange m, KillRange n, KillRange o, KillRange p ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q

killRange17 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l
               , KillRange m, KillRange n, KillRange o, KillRange p
               , KillRange q ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r

killRange18 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l
               , KillRange m, KillRange n, KillRange o, KillRange p
               , KillRange q, KillRange r ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s

killRange19 :: ( KillRange a, KillRange b, KillRange c, KillRange d
               , KillRange e, KillRange f, KillRange g, KillRange h
               , KillRange i, KillRange j, KillRange k, KillRange l
               , KillRange m, KillRange n, KillRange o, KillRange p
               , KillRange q, KillRange r, KillRange s ) =>
               (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t) ->
               a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t

killRange1 :: forall a b. KillRange a => (a -> b) -> a -> b
killRange1  a -> b
f a
a = a -> b
f (forall a. KillRange a => KillRangeT a
killRange a
a)
killRange2 :: forall a b c.
(KillRange a, KillRange b) =>
(a -> b -> c) -> a -> b -> c
killRange2  a -> b -> c
f a
a = forall a b. KillRange a => (a -> b) -> a -> b
killRange1 (a -> b -> c
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange3 :: forall a b c d.
(KillRange a, KillRange b, KillRange c) =>
(a -> b -> c -> d) -> a -> b -> c -> d
killRange3  a -> b -> c -> d
f a
a = forall a b c.
(KillRange a, KillRange b) =>
(a -> b -> c) -> a -> b -> c
killRange2 (a -> b -> c -> d
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange4 :: forall a b c d e.
(KillRange a, KillRange b, KillRange c, KillRange d) =>
(a -> b -> c -> d -> e) -> a -> b -> c -> d -> e
killRange4  a -> b -> c -> d -> e
f a
a = forall a b c d.
(KillRange a, KillRange b, KillRange c) =>
(a -> b -> c -> d) -> a -> b -> c -> d
killRange3 (a -> b -> c -> d -> e
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange5 :: forall a b c d e f.
(KillRange a, KillRange b, KillRange c, KillRange d,
 KillRange e) =>
(a -> b -> c -> d -> e -> f) -> a -> b -> c -> d -> e -> f
killRange5  a -> b -> c -> d -> e -> f
f a
a = forall a b c d e.
(KillRange a, KillRange b, KillRange c, KillRange d) =>
(a -> b -> c -> d -> e) -> a -> b -> c -> d -> e
killRange4 (a -> b -> c -> d -> e -> f
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange6 :: forall a b c d e f g.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f) =>
(a -> b -> c -> d -> e -> f -> g)
-> a -> b -> c -> d -> e -> f -> g
killRange6  a -> b -> c -> d -> e -> f -> g
f a
a = forall a b c d e f.
(KillRange a, KillRange b, KillRange c, KillRange d,
 KillRange e) =>
(a -> b -> c -> d -> e -> f) -> a -> b -> c -> d -> e -> f
killRange5 (a -> b -> c -> d -> e -> f -> g
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange7 :: forall a b c d e f g h.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g) =>
(a -> b -> c -> d -> e -> f -> g -> h)
-> a -> b -> c -> d -> e -> f -> g -> h
killRange7  a -> b -> c -> d -> e -> f -> g -> h
f a
a = forall a b c d e f g.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f) =>
(a -> b -> c -> d -> e -> f -> g)
-> a -> b -> c -> d -> e -> f -> g
killRange6 (a -> b -> c -> d -> e -> f -> g -> h
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange8 :: forall a b c d e f g h i.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i)
-> a -> b -> c -> d -> e -> f -> g -> h -> i
killRange8  a -> b -> c -> d -> e -> f -> g -> h -> i
f a
a = forall a b c d e f g h.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g) =>
(a -> b -> c -> d -> e -> f -> g -> h)
-> a -> b -> c -> d -> e -> f -> g -> h
killRange7 (a -> b -> c -> d -> e -> f -> g -> h -> i
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange9 :: forall a b c d e f g h i j.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j
killRange9  a -> b -> c -> d -> e -> f -> g -> h -> i -> j
f a
a = forall a b c d e f g h i.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i)
-> a -> b -> c -> d -> e -> f -> g -> h -> i
killRange8 (a -> b -> c -> d -> e -> f -> g -> h -> i -> j
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange10 :: forall a b c d e f g h i j k.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k
killRange10 a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k
f a
a = forall a b c d e f g h i j.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j
killRange9 (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange11 :: forall a b c d e f g h i j k l.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l
killRange11 a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l
f a
a = forall a b c d e f g h i j k.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k
killRange10 (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange12 :: forall a b c d e f g h i j k l m.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m
killRange12 a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m
f a
a = forall a b c d e f g h i j k l.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l
killRange11 (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange13 :: forall a b c d e f g h i j k l m n.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m) =>
(a
 -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
killRange13 a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n
f a
a = forall a b c d e f g h i j k l m.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l) =>
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m)
-> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m
killRange12 (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange14 :: forall a b c d e f g h i j k l m n o.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
killRange14 a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
f a
a = forall a b c d e f g h i j k l m n.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m) =>
(a
 -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
killRange13 (a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange15 :: forall a b c d e f g h i j k l m n o p.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
killRange15 a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
f a
a = forall a b c d e f g h i j k l m n o.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
killRange14 (a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange16 :: forall a b c d e f g h i j k l m n o p q.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o,
 KillRange p) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p
 -> q)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
killRange16 a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
f a
a = forall a b c d e f g h i j k l m n o p.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
killRange15 (a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange17 :: forall a b c d e f g h i j k l m n o p q r.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o,
 KillRange p, KillRange q) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p
 -> q
 -> r)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
killRange17 a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
f a
a = forall a b c d e f g h i j k l m n o p q.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o,
 KillRange p) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p
 -> q)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
killRange16 (a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange18 :: forall a b c d e f g h i j k l m n o p q r s.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o,
 KillRange p, KillRange q, KillRange r) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p
 -> q
 -> r
 -> s)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
-> s
killRange18 a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
-> s
f a
a = forall a b c d e f g h i j k l m n o p q r.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o,
 KillRange p, KillRange q) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p
 -> q
 -> r)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
killRange17 (a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
-> s
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)
killRange19 :: forall a b c d e f g h i j k l m n o p q r s t.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o,
 KillRange p, KillRange q, KillRange r, KillRange s) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p
 -> q
 -> r
 -> s
 -> t)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
-> s
-> t
killRange19 a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
-> s
-> t
f a
a = forall a b c d e f g h i j k l m n o p q r s.
(KillRange a, KillRange b, KillRange c, KillRange d, KillRange e,
 KillRange f, KillRange g, KillRange h, KillRange i, KillRange j,
 KillRange k, KillRange l, KillRange m, KillRange n, KillRange o,
 KillRange p, KillRange q, KillRange r) =>
(a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> k
 -> l
 -> m
 -> n
 -> o
 -> p
 -> q
 -> r
 -> s)
-> a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
-> s
killRange18 (a
-> b
-> c
-> d
-> e
-> f
-> g
-> h
-> i
-> j
-> k
-> l
-> m
-> n
-> o
-> p
-> q
-> r
-> s
-> t
f forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
a)

instance KillRange Range where
  killRange :: Range -> Range
killRange Range
_ = forall a. Range' a
noRange

instance KillRange Void where
  killRange :: KillRangeT Void
killRange = forall a. a -> a
id

instance KillRange () where
  killRange :: KillRangeT ()
killRange = forall a. a -> a
id

instance KillRange Bool where
  killRange :: Bool -> Bool
killRange = forall a. a -> a
id

instance KillRange Int where
  killRange :: KillRangeT Int
killRange = forall a. a -> a
id

instance KillRange Integer where
  killRange :: KillRangeT Integer
killRange = forall a. a -> a
id

instance KillRange Permutation where
  killRange :: KillRangeT Permutation
killRange = forall a. a -> a
id

-- | Overlaps with @KillRange [a]@.
instance {-# OVERLAPPING #-} KillRange String where
  killRange :: ShowS
killRange = forall a. a -> a
id

instance {-# OVERLAPPABLE #-} KillRange a => KillRange [a]
instance {-# OVERLAPPABLE #-} KillRange a => KillRange (Map k a)

instance KillRange a => KillRange (Drop a)
instance KillRange a => KillRange (List1 a)
instance KillRange a => KillRange (List2 a)
instance KillRange a => KillRange (Maybe a)
instance KillRange a => KillRange (Strict.Maybe a)

instance {-# OVERLAPPABLE #-} (Ord a, KillRange a) => KillRange (Set a) where
  killRange :: KillRangeT (Set a)
killRange = forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map forall a. KillRange a => KillRangeT a
killRange

instance (KillRange a, KillRange b) => KillRange (a, b) where
  killRange :: KillRangeT (a, b)
killRange (a
x, b
y) = (forall a. KillRange a => KillRangeT a
killRange a
x, forall a. KillRange a => KillRangeT a
killRange b
y)

instance (KillRange a, KillRange b, KillRange c) =>
         KillRange (a, b, c) where
  killRange :: KillRangeT (a, b, c)
killRange (a
x, b
y, c
z) = forall a b c d.
(KillRange a, KillRange b, KillRange c) =>
(a -> b -> c -> d) -> a -> b -> c -> d
killRange3 (,,) a
x b
y c
z

instance (KillRange a, KillRange b, KillRange c, KillRange d) =>
         KillRange (a, b, c, d) where
  killRange :: KillRangeT (a, b, c, d)
killRange (a
x, b
y, c
z, d
u) = forall a b c d e.
(KillRange a, KillRange b, KillRange c, KillRange d) =>
(a -> b -> c -> d -> e) -> a -> b -> c -> d -> e
killRange4 (,,,) a
x b
y c
z d
u

instance (KillRange a, KillRange b) => KillRange (Either a b) where
  killRange :: KillRangeT (Either a b)
killRange (Left  a
x) = forall a b. a -> Either a b
Left  forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange a
x
  killRange (Right b
x) = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a. KillRange a => KillRangeT a
killRange b
x

------------------------------------------------------------------------
-- Printing
------------------------------------------------------------------------

instance Pretty RangeFile where
  pretty :: RangeFile -> Doc
pretty = forall a. Pretty a => a -> Doc
pretty forall b c a. (b -> c) -> (a -> b) -> a -> c
. RangeFile -> AbsolutePath
rangeFilePath

instance Pretty a => Pretty (Position' (Strict.Maybe a)) where
  pretty :: Position' (Maybe a) -> Doc
pretty (Pn Maybe a
Strict.Nothing  Int32
_ Int32
l Int32
c) = forall a. Pretty a => a -> Doc
pretty Int32
l forall a. Semigroup a => a -> a -> a
<> Doc
"," forall a. Semigroup a => a -> a -> a
<> forall a. Pretty a => a -> Doc
pretty Int32
c
  pretty (Pn (Strict.Just a
f) Int32
_ Int32
l Int32
c) =
    forall a. Pretty a => a -> Doc
pretty a
f forall a. Semigroup a => a -> a -> a
<> Doc
":" forall a. Semigroup a => a -> a -> a
<> forall a. Pretty a => a -> Doc
pretty Int32
l forall a. Semigroup a => a -> a -> a
<> Doc
"," forall a. Semigroup a => a -> a -> a
<> forall a. Pretty a => a -> Doc
pretty Int32
c

instance Pretty PositionWithoutFile where
  pretty :: PositionWithoutFile -> Doc
pretty PositionWithoutFile
p = forall a. Pretty a => a -> Doc
pretty (PositionWithoutFile
p { srcFile :: SrcFile
srcFile = forall a. Maybe a
Strict.Nothing } :: Position)

instance Pretty IntervalWithoutFile where
  pretty :: IntervalWithoutFile -> Doc
pretty (Interval PositionWithoutFile
s PositionWithoutFile
e) = Doc
start forall a. Semigroup a => a -> a -> a
<> Doc
"-" forall a. Semigroup a => a -> a -> a
<> Doc
end
    where
      sl :: Int32
sl = forall a. Position' a -> Int32
posLine PositionWithoutFile
s
      el :: Int32
el = forall a. Position' a -> Int32
posLine PositionWithoutFile
e
      sc :: Int32
sc = forall a. Position' a -> Int32
posCol PositionWithoutFile
s
      ec :: Int32
ec = forall a. Position' a -> Int32
posCol PositionWithoutFile
e

      start :: Doc
      start :: Doc
start = forall a. Pretty a => a -> Doc
pretty Int32
sl forall a. Semigroup a => a -> a -> a
<> Doc
comma forall a. Semigroup a => a -> a -> a
<> forall a. Pretty a => a -> Doc
pretty Int32
sc

      Doc
end :: Doc
        | Int32
sl forall a. Eq a => a -> a -> Bool
== Int32
el  = forall a. Pretty a => a -> Doc
pretty Int32
ec
        | Bool
otherwise = forall a. Pretty a => a -> Doc
pretty Int32
el forall a. Semigroup a => a -> a -> a
<> Doc
comma forall a. Semigroup a => a -> a -> a
<> forall a. Pretty a => a -> Doc
pretty Int32
ec

instance Pretty a => Pretty (Interval' (Strict.Maybe a)) where
  pretty :: Interval' (Maybe a) -> Doc
pretty i :: Interval' (Maybe a)
i@(Interval Position' (Maybe a)
s Position' (Maybe a)
_) = Doc
file forall a. Semigroup a => a -> a -> a
<> forall a. Pretty a => a -> Doc
pretty (forall a b. a -> Interval' b -> Interval' a
setIntervalFile () Interval' (Maybe a)
i)
    where
      file :: Doc
      file :: Doc
file = case forall a. Position' a -> a
srcFile Position' (Maybe a)
s of
               Maybe a
Strict.Nothing -> forall a. Null a => a
empty
               Strict.Just a
f  -> forall a. Pretty a => a -> Doc
pretty a
f forall a. Semigroup a => a -> a -> a
<> Doc
colon

instance Pretty a => Pretty (Range' (Strict.Maybe a)) where
  pretty :: Range' (Maybe a) -> Doc
pretty Range' (Maybe a)
r = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Null a => a
empty forall a. Pretty a => a -> Doc
pretty (forall a. Range' a -> Maybe (Interval' a)
rangeToIntervalWithFile Range' (Maybe a)
r)

instance (Pretty a, HasRange a) => Pretty (PrintRange a) where
  pretty :: PrintRange a -> Doc
pretty (PrintRange a
a) = forall a. Pretty a => a -> Doc
pretty a
a Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Doc
"at" Doc -> Doc -> Doc
<+> forall a. Pretty a => a -> Doc
pretty (forall a. HasRange a => a -> Range
getRange a
a))

{--------------------------------------------------------------------------
    Functions on positions and ranges
 --------------------------------------------------------------------------}

-- | The first position in a file: position 1, line 1, column 1.
startPos' :: a -> Position' a
startPos' :: forall a. a -> Position' a
startPos' a
f = Pn
  { srcFile :: a
srcFile = a
f
  , posPos :: Int32
posPos  = Int32
1
  , posLine :: Int32
posLine = Int32
1
  , posCol :: Int32
posCol  = Int32
1
  }

-- | The first position in a file: position 1, line 1, column 1.
startPos :: Maybe RangeFile -> Position
startPos :: Maybe RangeFile -> Position
startPos = forall a. a -> Position' a
startPos' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lazy strict. Strict lazy strict => lazy -> strict
Strict.toStrict

-- | Ranges between two unknown positions
noRange :: Range' a
noRange :: forall a. Range' a
noRange = forall a. Range' a
NoRange

-- | Advance the position by one character.
--   A newline character (@'\n'@) moves the position to the first
--   character in the next line. Any other character moves the
--   position to the next column.
movePos :: Position' a -> Char -> Position' a
movePos :: forall a. Position' a -> Char -> Position' a
movePos (Pn a
f Int32
p Int32
l Int32
c) Char
'\n' = forall a. a -> Int32 -> Int32 -> Int32 -> Position' a
Pn a
f (Int32
p forall a. Num a => a -> a -> a
+ Int32
1) (Int32
l forall a. Num a => a -> a -> a
+ Int32
1) Int32
1
movePos (Pn a
f Int32
p Int32
l Int32
c) Char
_    = forall a. a -> Int32 -> Int32 -> Int32 -> Position' a
Pn a
f (Int32
p forall a. Num a => a -> a -> a
+ Int32
1) Int32
l (Int32
c forall a. Num a => a -> a -> a
+ Int32
1)

-- | Advance the position by a string.
--
--   > movePosByString = foldl' movePos
movePosByString :: Foldable t => Position' a -> t Char -> Position' a
movePosByString :: forall (t :: * -> *) a.
Foldable t =>
Position' a -> t Char -> Position' a
movePosByString = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Fold.foldl' forall a. Position' a -> Char -> Position' a
movePos

-- | Backup the position by one character.
--
-- Precondition: The character must not be @'\n'@.
backupPos :: Position' a -> Position' a
backupPos :: forall a. Position' a -> Position' a
backupPos (Pn a
f Int32
p Int32
l Int32
c) = forall a. a -> Int32 -> Int32 -> Int32 -> Position' a
Pn a
f (Int32
p forall a. Num a => a -> a -> a
- Int32
1) Int32
l (Int32
c forall a. Num a => a -> a -> a
- Int32
1)

-- | Converts a file name and two positions to a range.
posToRange' ::
  a -> PositionWithoutFile -> PositionWithoutFile -> Range' a
posToRange' :: forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Range' a
posToRange' a
f PositionWithoutFile
p1 PositionWithoutFile
p2 = forall a. a -> IntervalWithoutFile -> Range' a
intervalToRange a
f (forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Interval' a
posToInterval () PositionWithoutFile
p1 PositionWithoutFile
p2)

-- | Converts two positions to a range.
--
-- Precondition: The positions have to point to the same file.
posToRange :: Position' a -> Position' a -> Range' a
posToRange :: forall a. Position' a -> Position' a -> Range' a
posToRange Position' a
p1 Position' a
p2 =
  forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Range' a
posToRange' (forall a. Position' a -> a
srcFile Position' a
p1) (Position' a
p1 { srcFile :: ()
srcFile = () }) (Position' a
p2 { srcFile :: ()
srcFile = () })

-- | Converts a file name and an interval to a range.
intervalToRange :: a -> IntervalWithoutFile -> Range' a
intervalToRange :: forall a. a -> IntervalWithoutFile -> Range' a
intervalToRange a
f IntervalWithoutFile
i = forall a. a -> Seq IntervalWithoutFile -> Range' a
Range a
f (forall a. a -> Seq a
Seq.singleton IntervalWithoutFile
i)

-- | Converts a range to an interval, if possible.
rangeToIntervalWithFile :: Range' a -> Maybe (Interval' a)
rangeToIntervalWithFile :: forall a. Range' a -> Maybe (Interval' a)
rangeToIntervalWithFile Range' a
NoRange      = forall a. Maybe a
Nothing
rangeToIntervalWithFile (Range a
f Seq IntervalWithoutFile
is) = case (forall a. Seq a -> ViewL a
Seq.viewl Seq IntervalWithoutFile
is, forall a. Seq a -> ViewR a
Seq.viewr Seq IntervalWithoutFile
is) of
  (IntervalWithoutFile
head Seq.:< Seq IntervalWithoutFile
_, Seq IntervalWithoutFile
_ Seq.:> IntervalWithoutFile
last) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> Interval' b -> Interval' a
setIntervalFile a
f forall a b. (a -> b) -> a -> b
$
                                      Interval { iStart :: PositionWithoutFile
iStart = forall a. Interval' a -> Position' a
iStart IntervalWithoutFile
head
                                               , iEnd :: PositionWithoutFile
iEnd   = forall a. Interval' a -> Position' a
iEnd   IntervalWithoutFile
last
                                               }
  (ViewL IntervalWithoutFile, ViewR IntervalWithoutFile)
_                              -> forall a. HasCallStack => a
__IMPOSSIBLE__

-- | Converts a range to an interval, if possible. Note that the
-- information about the source file is lost.
rangeToInterval :: Range' a -> Maybe IntervalWithoutFile
rangeToInterval :: forall a. Range' a -> Maybe IntervalWithoutFile
rangeToInterval Range' a
NoRange      = forall a. Maybe a
Nothing
rangeToInterval (Range a
_ Seq IntervalWithoutFile
is) = case (forall a. Seq a -> ViewL a
Seq.viewl Seq IntervalWithoutFile
is, forall a. Seq a -> ViewR a
Seq.viewr Seq IntervalWithoutFile
is) of
  (IntervalWithoutFile
head Seq.:< Seq IntervalWithoutFile
_, Seq IntervalWithoutFile
_ Seq.:> IntervalWithoutFile
last) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
                                      Interval { iStart :: PositionWithoutFile
iStart = forall a. Interval' a -> Position' a
iStart IntervalWithoutFile
head
                                               , iEnd :: PositionWithoutFile
iEnd   = forall a. Interval' a -> Position' a
iEnd   IntervalWithoutFile
last
                                               }
  (ViewL IntervalWithoutFile, ViewR IntervalWithoutFile)
_                              -> forall a. HasCallStack => a
__IMPOSSIBLE__

-- | Returns the shortest continuous range containing the given one.
continuous :: Range' a -> Range' a
continuous :: forall a. Range' a -> Range' a
continuous Range' a
NoRange = forall a. Range' a
NoRange
continuous r :: Range' a
r@(Range a
f Seq IntervalWithoutFile
_) = case forall a. Range' a -> Maybe IntervalWithoutFile
rangeToInterval Range' a
r of
  Maybe IntervalWithoutFile
Nothing -> forall a. HasCallStack => a
__IMPOSSIBLE__
  Just IntervalWithoutFile
i  -> forall a. a -> IntervalWithoutFile -> Range' a
intervalToRange a
f IntervalWithoutFile
i

-- | Removes gaps between intervals on the same line.
continuousPerLine :: Ord a => Range' a -> Range' a
continuousPerLine :: forall a. Ord a => Range' a -> Range' a
continuousPerLine r :: Range' a
r@Range' a
NoRange     = Range' a
r
continuousPerLine r :: Range' a
r@(Range a
f Seq IntervalWithoutFile
_) =
  forall a. a -> Seq IntervalWithoutFile -> Range' a
Range a
f (forall b a. (b -> Maybe (a, b)) -> b -> Seq a
Seq.unfoldr forall {a}.
Ord a =>
[Interval' a] -> Maybe (Interval' a, [Interval' a])
step (forall a. Range' a -> [IntervalWithoutFile]
rangeIntervals Range' a
r))
  where
  step :: [Interval' a] -> Maybe (Interval' a, [Interval' a])
step []  = forall a. Maybe a
Nothing
  step [Interval' a
i] = forall a. a -> Maybe a
Just (Interval' a
i, [])
  step (Interval' a
i : is :: [Interval' a]
is@(Interval' a
j : [Interval' a]
js))
    | Bool
sameLine  = [Interval' a] -> Maybe (Interval' a, [Interval' a])
step (forall a. Ord a => Interval' a -> Interval' a -> Interval' a
fuseIntervals Interval' a
i Interval' a
j forall a. a -> [a] -> [a]
: [Interval' a]
js)
    | Bool
otherwise = forall a. a -> Maybe a
Just (Interval' a
i, [Interval' a]
is)
    where
    sameLine :: Bool
sameLine = forall a. Position' a -> Int32
posLine (forall a. Interval' a -> Position' a
iEnd Interval' a
i) forall a. Eq a => a -> a -> Bool
== forall a. Position' a -> Int32
posLine (forall a. Interval' a -> Position' a
iStart Interval' a
j)

-- | The initial position in the range, if any.
rStart' :: Range' a -> Maybe PositionWithoutFile
rStart' :: forall a. Range' a -> Maybe PositionWithoutFile
rStart' Range' a
r = forall a. Interval' a -> Position' a
iStart forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Range' a -> Maybe IntervalWithoutFile
rangeToInterval Range' a
r

-- | The initial position in the range, if any.
rStart :: Range' a -> Maybe (Position' a)
rStart :: forall a. Range' a -> Maybe (Position' a)
rStart Range' a
NoRange       = forall a. Maybe a
Nothing
rStart r :: Range' a
r@(Range a
f Seq IntervalWithoutFile
_) = (\PositionWithoutFile
p -> PositionWithoutFile
p { srcFile :: a
srcFile = a
f }) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Range' a -> Maybe PositionWithoutFile
rStart' Range' a
r

-- | The position after the final position in the range, if any.
rEnd' :: Range' a -> Maybe PositionWithoutFile
rEnd' :: forall a. Range' a -> Maybe PositionWithoutFile
rEnd' Range' a
r = forall a. Interval' a -> Position' a
iEnd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Range' a -> Maybe IntervalWithoutFile
rangeToInterval Range' a
r

-- | The position after the final position in the range, if any.
rEnd :: Range' a -> Maybe (Position' a)
rEnd :: forall a. Range' a -> Maybe (Position' a)
rEnd Range' a
NoRange       = forall a. Maybe a
Nothing
rEnd r :: Range' a
r@(Range a
f Seq IntervalWithoutFile
_) = (\PositionWithoutFile
p -> PositionWithoutFile
p { srcFile :: a
srcFile = a
f }) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Range' a -> Maybe PositionWithoutFile
rEnd' Range' a
r

-- | Finds the least interval which covers the arguments.
--
-- Precondition: The intervals must point to the same file.
fuseIntervals :: Ord a => Interval' a -> Interval' a -> Interval' a
fuseIntervals :: forall a. Ord a => Interval' a -> Interval' a -> Interval' a
fuseIntervals Interval' a
x Interval' a
y = Interval { iStart :: Position' a
iStart = Position' a
s, iEnd :: Position' a
iEnd = Position' a
e }
    where
    s :: Position' a
s = forall a. a -> [a] -> a
headWithDefault forall a. HasCallStack => a
__IMPOSSIBLE__ forall a b. (a -> b) -> a -> b
$ forall a. Ord a => [a] -> [a]
sort [forall a. Interval' a -> Position' a
iStart Interval' a
x, forall a. Interval' a -> Position' a
iStart Interval' a
y]
    e :: Position' a
e = forall a. a -> [a] -> a
lastWithDefault forall a. HasCallStack => a
__IMPOSSIBLE__ forall a b. (a -> b) -> a -> b
$ forall a. Ord a => [a] -> [a]
sort [forall a. Interval' a -> Position' a
iEnd   Interval' a
x, forall a. Interval' a -> Position' a
iEnd   Interval' a
y]

-- | @fuseRanges r r'@ unions the ranges @r@ and @r'@.
--
--   Meaning it finds the least range @r0@ that covers @r@ and @r'@.
--
-- Precondition: The ranges must point to the same file (or be empty).
fuseRanges :: (Ord a) => Range' a -> Range' a -> Range' a
fuseRanges :: forall a. Ord a => Range' a -> Range' a -> Range' a
fuseRanges Range' a
NoRange       Range' a
is2           = Range' a
is2
fuseRanges Range' a
is1           Range' a
NoRange       = Range' a
is1
fuseRanges (Range a
f Seq IntervalWithoutFile
is1) (Range a
_ Seq IntervalWithoutFile
is2) = forall a. a -> Seq IntervalWithoutFile -> Range' a
Range a
f (forall {a}.
Ord a =>
Seq (Interval' a) -> Seq (Interval' a) -> Seq (Interval' a)
fuse Seq IntervalWithoutFile
is1 Seq IntervalWithoutFile
is2)
  where
  fuse :: Seq (Interval' a) -> Seq (Interval' a) -> Seq (Interval' a)
fuse Seq (Interval' a)
is1 Seq (Interval' a)
is2 = case (forall a. Seq a -> ViewL a
Seq.viewl Seq (Interval' a)
is1, forall a. Seq a -> ViewR a
Seq.viewr Seq (Interval' a)
is1,
                       forall a. Seq a -> ViewL a
Seq.viewl Seq (Interval' a)
is2, forall a. Seq a -> ViewR a
Seq.viewr Seq (Interval' a)
is2) of
    (ViewL (Interval' a)
Seq.EmptyL, ViewR (Interval' a)
_, ViewL (Interval' a)
_, ViewR (Interval' a)
_) -> Seq (Interval' a)
is2
    (ViewL (Interval' a)
_, ViewR (Interval' a)
_, ViewL (Interval' a)
Seq.EmptyL, ViewR (Interval' a)
_) -> Seq (Interval' a)
is1
    (Interval' a
s1 Seq.:< Seq (Interval' a)
r1, Seq (Interval' a)
l1 Seq.:> Interval' a
e1, Interval' a
s2 Seq.:< Seq (Interval' a)
r2, Seq (Interval' a)
l2 Seq.:> Interval' a
e2)
        -- Special cases.
      | forall a. Interval' a -> Position' a
iEnd Interval' a
e1 forall a. Ord a => a -> a -> Bool
<  forall a. Interval' a -> Position' a
iStart Interval' a
s2 -> Seq (Interval' a)
is1 forall a. Seq a -> Seq a -> Seq a
Seq.>< Seq (Interval' a)
is2
      | forall a. Interval' a -> Position' a
iEnd Interval' a
e2 forall a. Ord a => a -> a -> Bool
<  forall a. Interval' a -> Position' a
iStart Interval' a
s1 -> Seq (Interval' a)
is2 forall a. Seq a -> Seq a -> Seq a
Seq.>< Seq (Interval' a)
is1
      | forall a. Interval' a -> Position' a
iEnd Interval' a
e1 forall a. Eq a => a -> a -> Bool
== forall a. Interval' a -> Position' a
iStart Interval' a
s2 -> forall {a}.
Seq (Interval' a)
-> Interval' a
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
mergeTouching Seq (Interval' a)
l1 Interval' a
e1 Interval' a
s2 Seq (Interval' a)
r2
      | forall a. Interval' a -> Position' a
iEnd Interval' a
e2 forall a. Eq a => a -> a -> Bool
== forall a. Interval' a -> Position' a
iStart Interval' a
s1 -> forall {a}.
Seq (Interval' a)
-> Interval' a
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
mergeTouching Seq (Interval' a)
l2 Interval' a
e2 Interval' a
s1 Seq (Interval' a)
r1
        -- General cases.
      | forall a. Interval' a -> Position' a
iEnd Interval' a
s1 forall a. Ord a => a -> a -> Bool
<  forall a. Interval' a -> Position' a
iStart Interval' a
s2 -> Interval' a
-> Seq (Interval' a)
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
outputLeftPrefix Interval' a
s1 Seq (Interval' a)
r1 Interval' a
s2 Seq (Interval' a)
is2
      | forall a. Interval' a -> Position' a
iEnd Interval' a
s2 forall a. Ord a => a -> a -> Bool
<  forall a. Interval' a -> Position' a
iStart Interval' a
s1 -> Interval' a
-> Seq (Interval' a)
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
outputLeftPrefix Interval' a
s2 Seq (Interval' a)
r2 Interval' a
s1 Seq (Interval' a)
is1
      | forall a. Interval' a -> Position' a
iEnd Interval' a
s1 forall a. Ord a => a -> a -> Bool
<  forall a. Interval' a -> Position' a
iEnd   Interval' a
s2 -> Interval' a
-> Seq (Interval' a)
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
fuseSome Interval' a
s1 Seq (Interval' a)
r1 Interval' a
s2 Seq (Interval' a)
r2
      | Bool
otherwise            -> Interval' a
-> Seq (Interval' a)
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
fuseSome Interval' a
s2 Seq (Interval' a)
r2 Interval' a
s1 Seq (Interval' a)
r1
    (ViewL (Interval' a), ViewR (Interval' a), ViewL (Interval' a),
 ViewR (Interval' a))
_ -> forall a. HasCallStack => a
__IMPOSSIBLE__

  mergeTouching :: Seq (Interval' a)
-> Interval' a
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
mergeTouching Seq (Interval' a)
l Interval' a
e Interval' a
s Seq (Interval' a)
r = Seq (Interval' a)
l forall a. Seq a -> Seq a -> Seq a
Seq.>< Interval' a
i forall a. a -> Seq a -> Seq a
Seq.<| Seq (Interval' a)
r
    where
    i :: Interval' a
i = Interval { iStart :: Position' a
iStart = forall a. Interval' a -> Position' a
iStart Interval' a
e, iEnd :: Position' a
iEnd = forall a. Interval' a -> Position' a
iEnd Interval' a
s }

  -- The following two functions could use binary search instead of
  -- linear.

  outputLeftPrefix :: Interval' a
-> Seq (Interval' a)
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
outputLeftPrefix Interval' a
s1 Seq (Interval' a)
r1 Interval' a
s2 Seq (Interval' a)
is2 = Interval' a
s1 forall a. a -> Seq a -> Seq a
Seq.<| Seq (Interval' a)
r1' forall a. Seq a -> Seq a -> Seq a
Seq.>< Seq (Interval' a) -> Seq (Interval' a) -> Seq (Interval' a)
fuse Seq (Interval' a)
r1'' Seq (Interval' a)
is2
    where
    (Seq (Interval' a)
r1', Seq (Interval' a)
r1'') = forall a. (a -> Bool) -> Seq a -> (Seq a, Seq a)
Seq.spanl (\Interval' a
s -> forall a. Interval' a -> Position' a
iEnd Interval' a
s forall a. Ord a => a -> a -> Bool
< forall a. Interval' a -> Position' a
iStart Interval' a
s2) Seq (Interval' a)
r1

  fuseSome :: Interval' a
-> Seq (Interval' a)
-> Interval' a
-> Seq (Interval' a)
-> Seq (Interval' a)
fuseSome Interval' a
s1 Seq (Interval' a)
r1 Interval' a
s2 Seq (Interval' a)
r2 = Seq (Interval' a) -> Seq (Interval' a) -> Seq (Interval' a)
fuse Seq (Interval' a)
r1' (forall a. Ord a => Interval' a -> Interval' a -> Interval' a
fuseIntervals Interval' a
s1 Interval' a
s2 forall a. a -> Seq a -> Seq a
Seq.<| Seq (Interval' a)
r2)
    where
    r1' :: Seq (Interval' a)
r1' = forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileL (\Interval' a
s -> forall a. Interval' a -> Position' a
iEnd Interval' a
s forall a. Ord a => a -> a -> Bool
<= forall a. Interval' a -> Position' a
iEnd Interval' a
s2) Seq (Interval' a)
r1

-- | Precondition: The ranges must point to the same file (or be
-- empty).
fuseRange :: (HasRange u, HasRange t) => u -> t -> Range
fuseRange :: forall u t. (HasRange u, HasRange t) => u -> t -> Range
fuseRange u
x t
y = forall a. Ord a => Range' a -> Range' a -> Range' a
fuseRanges (forall a. HasRange a => a -> Range
getRange u
x) (forall a. HasRange a => a -> Range
getRange t
y)

-- | @beginningOf r@ is an empty range (a single, empty interval)
-- positioned at the beginning of @r@. If @r@ does not have a
-- beginning, then 'noRange' is returned.
beginningOf :: Range -> Range
beginningOf :: Range -> Range
beginningOf Range
NoRange       = forall a. Range' a
NoRange
beginningOf r :: Range
r@(Range SrcFile
f Seq IntervalWithoutFile
_) = case forall a. Range' a -> Maybe PositionWithoutFile
rStart' Range
r of
  Maybe PositionWithoutFile
Nothing  -> forall a. HasCallStack => a
__IMPOSSIBLE__
  Just PositionWithoutFile
pos -> forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Range' a
posToRange' SrcFile
f PositionWithoutFile
pos PositionWithoutFile
pos

-- | @beginningOfFile r@ is an empty range (a single, empty interval)
-- at the beginning of @r@'s starting position's file. If there is no
-- such position, then an empty range is returned.
beginningOfFile :: Range -> Range
beginningOfFile :: Range -> Range
beginningOfFile Range
NoRange     = forall a. Range' a
NoRange
beginningOfFile (Range SrcFile
f Seq IntervalWithoutFile
_) = forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Range' a
posToRange' SrcFile
f PositionWithoutFile
p PositionWithoutFile
p
  where p :: PositionWithoutFile
p = forall a. a -> Position' a
startPos' ()

-- | @x \`withRangeOf\` y@ sets the range of @x@ to the range of @y@.
withRangeOf :: (SetRange t, HasRange u) => t -> u -> t
t
x withRangeOf :: forall t u. (SetRange t, HasRange u) => t -> u -> t
`withRangeOf` u
y = forall a. SetRange a => Range -> a -> a
setRange (forall a. HasRange a => a -> Range
getRange u
y) t
x

-- | Interleaves two streams of ranged elements
--
--   It will report the conflicts as a list of conflicting pairs.
--   In case of conflict, the element with the earliest start position
--   is placed first. In case of a tie, the element with the earliest
--   ending position is placed first. If both tie, the element from the
--   first list is placed first.
interleaveRanges :: (HasRange a) => [a] -> [a] -> ([a], [(a,a)])
interleaveRanges :: forall a. HasRange a => [a] -> [a] -> ([a], [(a, a)])
interleaveRanges [a]
as [a]
bs = forall w a. Writer w a -> (a, w)
runWriterforall a b. (a -> b) -> a -> b
$ forall {m :: * -> *} {b}.
(HasRange b, MonadWriter [(b, b)] m) =>
[b] -> [b] -> m [b]
go [a]
as [a]
bs
  where
    go :: [b] -> [b] -> m [b]
go []         [b]
as = forall (m :: * -> *) a. Monad m => a -> m a
return [b]
as
    go [b]
as         [] = forall (m :: * -> *) a. Monad m => a -> m a
return [b]
as
    go as :: [b]
as@(b
a:[b]
as') bs :: [b]
bs@(b
b:[b]
bs') =
      let ra :: Range
ra = forall a. HasRange a => a -> Range
getRange b
a
          rb :: Range
rb = forall a. HasRange a => a -> Range
getRange b
b

          ra0 :: Maybe Position
ra0 = forall a. Range' a -> Maybe (Position' a)
rStart Range
ra
          rb0 :: Maybe Position
rb0 = forall a. Range' a -> Maybe (Position' a)
rStart Range
rb

          ra1 :: Maybe Position
ra1 = forall a. Range' a -> Maybe (Position' a)
rEnd Range
ra
          rb1 :: Maybe Position
rb1 = forall a. Range' a -> Maybe (Position' a)
rEnd Range
rb
      in
      if Maybe Position
ra1 forall a. Ord a => a -> a -> Bool
<= Maybe Position
rb0 then
        (b
aforall a. a -> [a] -> [a]
:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [b] -> [b] -> m [b]
go [b]
as' [b]
bs
      else if Maybe Position
rb1 forall a. Ord a => a -> a -> Bool
<= Maybe Position
ra0 then
        (b
bforall a. a -> [a] -> [a]
:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [b] -> [b] -> m [b]
go [b]
as [b]
bs'
      else do
        forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [(b
a,b
b)]
        if Maybe Position
ra0 forall a. Ord a => a -> a -> Bool
< Maybe Position
rb0 Bool -> Bool -> Bool
|| (Maybe Position
ra0 forall a. Eq a => a -> a -> Bool
== Maybe Position
rb0 Bool -> Bool -> Bool
&& Maybe Position
ra1 forall a. Ord a => a -> a -> Bool
<= Maybe Position
rb1) then
          (b
aforall a. a -> [a] -> [a]
:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [b] -> [b] -> m [b]
go [b]
as' [b]
bs
        else
          (b
bforall a. a -> [a] -> [a]
:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [b] -> [b] -> m [b]
go [b]
as [b]
bs'