{-# LANGUAGE DeriveDataTypeable        #-}
{-# LANGUAGE DeriveGeneric             #-}
{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables       #-}

{-# OPTIONS_GHC -Wno-orphans           #-}

module Language.Fixpoint.Types.Spans (

  -- * Concrete Location Type
    SourcePos(..)
  , SourceName
  , SrcSpan (..)
  , Pos
  , predPos
  , safePos
  , safeSourcePos
  , succPos
  , unPos
  , mkPos

  -- * Located Values
  , Loc (..)
  , Located (..)

  -- * Constructing spans
  , dummySpan
  , panicSpan
  , locAt
  , dummyLoc
  , dummyPos
  , atLoc
  , toSourcePos
  , ofSourcePos

  -- * Destructing spans
  , sourcePosElts
  , srcLine
  ) where

-- import           Control.Exception
import           Control.DeepSeq
-- import qualified Control.Monad.Error           as E
import           Data.Serialize                (Serialize (..))
import           Data.Generics                 (Data)
import           Data.Hashable
import           Data.Typeable
import           Data.String
import qualified Data.Store                   as S
import           GHC.Generics                  (Generic)
import           Language.Fixpoint.Types.PrettyPrint
-- import           Language.Fixpoint.Misc
import           Text.Megaparsec.Pos
import           Text.PrettyPrint.HughesPJ
import           Text.Printf
import Data.Functor.Contravariant (Contravariant(contramap))
import qualified Data.Binary as B
-- import           Debug.Trace


-----------------------------------------------------------------------
-- | Located Values ---------------------------------------------------
-----------------------------------------------------------------------

class Loc a where
  srcSpan :: a -> SrcSpan

-----------------------------------------------------------------------
-- Additional (orphan) instances for megaparsec's Pos type ------------
-----------------------------------------------------------------------
instance S.Store Pos where
  poke :: Pos -> Poke ()
poke = forall a. Store a => a -> Poke ()
S.poke forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pos -> Int
unPos
  peek :: Peek Pos
peek = Int -> Pos
mkPos forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Store a => Peek a
S.peek
  size :: Size Pos
size = forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap Pos -> Int
unPos forall a. Store a => Size a
S.size

instance Serialize Pos where
  put :: Putter Pos
put = forall t. Serialize t => Putter t
put forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pos -> Int
unPos
  get :: Get Pos
get = Int -> Pos
mkPos forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t. Serialize t => Get t
get

instance Hashable Pos where
  hashWithSalt :: Int -> Pos -> Int
hashWithSalt Int
i = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pos -> Int
unPos

instance PrintfArg Pos where
  formatArg :: Pos -> FieldFormatter
formatArg = forall a. PrintfArg a => a -> FieldFormatter
formatArg forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pos -> Int
unPos
  parseFormat :: Pos -> ModifierParser
parseFormat = forall a. PrintfArg a => a -> ModifierParser
parseFormat forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pos -> Int
unPos

-- | Computes, safely, the predecessor of a 'Pos' value, stopping at 1.
predPos :: Pos -> Pos
predPos :: Pos -> Pos
predPos Pos
pos =
  Int -> Pos
mkPos forall a b. (a -> b) -> a -> b
$
  case Pos -> Int
unPos Pos
pos of
    Int
1 -> Int
1
    Int
atLeastTwo -> Int
atLeastTwo forall a. Num a => a -> a -> a
- Int
1

-- | Computes the successor of a 'Pos' value.
succPos :: Pos -> Pos
succPos :: Pos -> Pos
succPos Pos
pos =
  Int -> Pos
mkPos forall a b. (a -> b) -> a -> b
$ Pos -> Int
unPos Pos
pos forall a. Num a => a -> a -> a
+ Int
1

-- | Create, safely, as position. If a non-positive number is given,
-- we use 1.
--
safePos :: Int -> Pos
safePos :: Int -> Pos
safePos Int
i
  | Int
i forall a. Ord a => a -> a -> Bool
<= Int
0    = Int -> Pos
mkPos Int
1
  | Bool
otherwise = Int -> Pos
mkPos Int
i

-- | Create a source position from integers, using 1 in case of
-- non-positive numbers.
safeSourcePos :: FilePath -> Int -> Int -> SourcePos
safeSourcePos :: FilePath -> Int -> Int -> SourcePos
safeSourcePos FilePath
file Int
line Int
col =
  FilePath -> Pos -> Pos -> SourcePos
SourcePos FilePath
file (Int -> Pos
safePos Int
line) (Int -> Pos
safePos Int
col)

-----------------------------------------------------------------------
-- | Retrofitting instances to SourcePos ------------------------------
-----------------------------------------------------------------------

instance S.Store SourcePos where
  -- poke = S.poke . ofSourcePos
  -- peek = toSourcePos <$> S.peek

instance Serialize SourcePos where
  put :: Putter SourcePos
put = forall t. Serialize t => Putter t
put forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourcePos -> (FilePath, Pos, Pos)
ofSourcePos
  get :: Get SourcePos
get = (FilePath, Pos, Pos) -> SourcePos
toSourcePos forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t. Serialize t => Get t
get

instance PPrint SourcePos where
  pprintTidy :: Tidy -> SourcePos -> Doc
pprintTidy Tidy
_ = SourcePos -> Doc
ppSourcePos

instance Hashable SourcePos where
  hashWithSalt :: Int -> SourcePos -> Int
hashWithSalt Int
i   = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourcePos -> (FilePath, Pos, Pos)
sourcePosElts

-- | This is a compatibility type synonym for megaparsec vs. parsec.
type SourceName = FilePath

-- | This is a compatibility type synonym for megaparsec vs. parsec.
type Line = Pos

-- | This is a compatibility type synonym for megaparsec vs. parsec.
type Column = Pos

ofSourcePos :: SourcePos -> (SourceName, Line, Column)
ofSourcePos :: SourcePos -> (FilePath, Pos, Pos)
ofSourcePos SourcePos
p = (FilePath
f, Pos
l, Pos
c)
  where
   f :: FilePath
f = SourcePos -> FilePath
sourceName   SourcePos
p
   l :: Pos
l = SourcePos -> Pos
sourceLine   SourcePos
p
   c :: Pos
c = SourcePos -> Pos
sourceColumn SourcePos
p

toSourcePos :: (SourceName, Line, Column) -> SourcePos
toSourcePos :: (FilePath, Pos, Pos) -> SourcePos
toSourcePos (FilePath
f, Pos
l, Pos
c) = FilePath -> Pos -> Pos -> SourcePos
SourcePos FilePath
f Pos
l Pos
c

sourcePosElts :: SourcePos -> (SourceName, Line, Column)
sourcePosElts :: SourcePos -> (FilePath, Pos, Pos)
sourcePosElts = SourcePos -> (FilePath, Pos, Pos)
ofSourcePos

ppSourcePos :: SourcePos -> Doc
ppSourcePos :: SourcePos -> Doc
ppSourcePos SourcePos
z = FilePath -> Doc
text (forall r. PrintfType r => FilePath -> r
printf FilePath
"%s:%d:%d" FilePath
f Pos
l Pos
c)
  where
    (FilePath
f,Pos
l,Pos
c) = SourcePos -> (FilePath, Pos, Pos)
sourcePosElts SourcePos
z

instance Fixpoint SourcePos where
  toFix :: SourcePos -> Doc
toFix = FilePath -> Doc
text forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> FilePath
show


data Located a = Loc { forall a. Located a -> SourcePos
loc  :: !SourcePos -- ^ Start Position
                     , forall a. Located a -> SourcePos
locE :: !SourcePos -- ^ End Position
                     , forall a. Located a -> a
val  :: !a
                     } deriving (Located a -> DataType
Located a -> Constr
forall {a}. Data a => Typeable (Located a)
forall a. Data a => Located a -> DataType
forall a. Data a => Located a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> Located a -> Located a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Located a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Located a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Located a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Located a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Located a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Located a -> c (Located a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Located a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Located a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Located a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Located a -> c (Located a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Located a))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Located a -> m (Located a)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Located a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Located a -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Located a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Located a -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Located a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Located a -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Located a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Located a -> r
gmapT :: (forall b. Data b => b -> b) -> Located a -> Located a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Located a -> Located a
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Located a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Located a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Located a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Located a))
dataTypeOf :: Located a -> DataType
$cdataTypeOf :: forall a. Data a => Located a -> DataType
toConstr :: Located a -> Constr
$ctoConstr :: forall a. Data a => Located a -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Located a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Located a)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Located a -> c (Located a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Located a -> c (Located a)
Data, Typeable, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Located a) x -> Located a
forall a x. Located a -> Rep (Located a) x
$cto :: forall a x. Rep (Located a) x -> Located a
$cfrom :: forall a x. Located a -> Rep (Located a) x
Generic)

instance Loc (Located a) where
  srcSpan :: Located a -> SrcSpan
srcSpan (Loc SourcePos
l SourcePos
l' a
_) = SourcePos -> SourcePos -> SrcSpan
SS SourcePos
l SourcePos
l'


instance (NFData a) => NFData (Located a)

instance Fixpoint a => Fixpoint (Located a) where
  toFix :: Located a -> Doc
toFix = forall a. Fixpoint a => a -> Doc
toFix forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Located a -> a
val

instance Functor Located where
  fmap :: forall a b. (a -> b) -> Located a -> Located b
fmap a -> b
f (Loc SourcePos
l SourcePos
l' a
x) =  forall a. SourcePos -> SourcePos -> a -> Located a
Loc SourcePos
l SourcePos
l' (a -> b
f a
x)

instance Foldable Located where
  foldMap :: forall m a. Monoid m => (a -> m) -> Located a -> m
foldMap a -> m
f (Loc SourcePos
_ SourcePos
_ a
x) = a -> m
f a
x

instance Traversable Located where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Located a -> f (Located b)
traverse a -> f b
f (Loc SourcePos
l SourcePos
l' a
x) = forall a. SourcePos -> SourcePos -> a -> Located a
Loc SourcePos
l SourcePos
l' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x

instance Show a => Show (Located a) where
  show :: Located a -> FilePath
show (Loc SourcePos
l SourcePos
l' a
x)
    | SourcePos
l forall a. Eq a => a -> a -> Bool
== SourcePos
l' Bool -> Bool -> Bool
&& SourcePos
l forall a. Eq a => a -> a -> Bool
== FilePath -> SourcePos
dummyPos FilePath
"Fixpoint.Types.dummyLoc" = forall a. Show a => a -> FilePath
show a
x forall a. [a] -> [a] -> [a]
++ FilePath
" (dummyLoc)"
    | Bool
otherwise  = forall a. Show a => a -> FilePath
show a
x forall a. [a] -> [a] -> [a]
++ FilePath
" defined at: " forall a. [a] -> [a] -> [a]
++ Doc -> FilePath
render (SrcSpan -> Doc
ppSrcSpan (SourcePos -> SourcePos -> SrcSpan
SS SourcePos
l SourcePos
l'))

instance PPrint a => PPrint (Located a) where
  pprintTidy :: Tidy -> Located a -> Doc
pprintTidy Tidy
k (Loc SourcePos
_ SourcePos
_ a
x) = forall a. PPrint a => Tidy -> a -> Doc
pprintTidy Tidy
k a
x

instance Eq a => Eq (Located a) where
  (Loc SourcePos
_ SourcePos
_ a
x) == :: Located a -> Located a -> Bool
== (Loc SourcePos
_ SourcePos
_ a
y) = a
x forall a. Eq a => a -> a -> Bool
== a
y

instance Ord a => Ord (Located a) where
  compare :: Located a -> Located a -> Ordering
compare Located a
x Located a
y = forall a. Ord a => a -> a -> Ordering
compare (forall a. Located a -> a
val Located a
x) (forall a. Located a -> a
val Located a
y)

instance (S.Store a) => S.Store (Located a)

instance Hashable a => Hashable (Located a) where
  hashWithSalt :: Int -> Located a -> Int
hashWithSalt Int
i = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Located a -> a
val

instance (IsString a) => IsString (Located a) where
  fromString :: FilePath -> Located a
fromString = forall a. a -> Located a
dummyLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => FilePath -> a
fromString

-- | We need the Binary instances for LH's spec serialization
instance B.Binary Pos where
  put :: Pos -> Put
put = forall t. Binary t => t -> Put
B.put forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pos -> Int
unPos
  get :: Get Pos
get = Int -> Pos
mkPos forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t. Binary t => Get t
B.get

instance B.Binary SourcePos

instance (B.Binary a) => B.Binary (Located a)

srcLine :: (Loc a) => a -> Pos
srcLine :: forall a. Loc a => a -> Pos
srcLine = SourcePos -> Pos
sourceLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. SrcSpan -> SourcePos
sp_start forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Loc a => a -> SrcSpan
srcSpan

-----------------------------------------------------------------------
-- | A Reusable SrcSpan Type ------------------------------------------
-----------------------------------------------------------------------

data SrcSpan = SS { SrcSpan -> SourcePos
sp_start :: !SourcePos
                  , SrcSpan -> SourcePos
sp_stop  :: !SourcePos}
                 deriving (SrcSpan -> SrcSpan -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SrcSpan -> SrcSpan -> Bool
$c/= :: SrcSpan -> SrcSpan -> Bool
== :: SrcSpan -> SrcSpan -> Bool
$c== :: SrcSpan -> SrcSpan -> Bool
Eq, Eq SrcSpan
SrcSpan -> SrcSpan -> Bool
SrcSpan -> SrcSpan -> Ordering
SrcSpan -> SrcSpan -> SrcSpan
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
min :: SrcSpan -> SrcSpan -> SrcSpan
$cmin :: SrcSpan -> SrcSpan -> SrcSpan
max :: SrcSpan -> SrcSpan -> SrcSpan
$cmax :: SrcSpan -> SrcSpan -> SrcSpan
>= :: SrcSpan -> SrcSpan -> Bool
$c>= :: SrcSpan -> SrcSpan -> Bool
> :: SrcSpan -> SrcSpan -> Bool
$c> :: SrcSpan -> SrcSpan -> Bool
<= :: SrcSpan -> SrcSpan -> Bool
$c<= :: SrcSpan -> SrcSpan -> Bool
< :: SrcSpan -> SrcSpan -> Bool
$c< :: SrcSpan -> SrcSpan -> Bool
compare :: SrcSpan -> SrcSpan -> Ordering
$ccompare :: SrcSpan -> SrcSpan -> Ordering
Ord, Int -> SrcSpan -> ShowS
[SrcSpan] -> ShowS
SrcSpan -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [SrcSpan] -> ShowS
$cshowList :: [SrcSpan] -> ShowS
show :: SrcSpan -> FilePath
$cshow :: SrcSpan -> FilePath
showsPrec :: Int -> SrcSpan -> ShowS
$cshowsPrec :: Int -> SrcSpan -> ShowS
Show, Typeable SrcSpan
SrcSpan -> DataType
SrcSpan -> Constr
(forall b. Data b => b -> b) -> SrcSpan -> SrcSpan
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> SrcSpan -> u
forall u. (forall d. Data d => d -> u) -> SrcSpan -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcSpan
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcSpan -> c SrcSpan
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcSpan)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SrcSpan)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SrcSpan -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SrcSpan -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> SrcSpan -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> SrcSpan -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
gmapT :: (forall b. Data b => b -> b) -> SrcSpan -> SrcSpan
$cgmapT :: (forall b. Data b => b -> b) -> SrcSpan -> SrcSpan
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SrcSpan)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SrcSpan)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcSpan)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcSpan)
dataTypeOf :: SrcSpan -> DataType
$cdataTypeOf :: SrcSpan -> DataType
toConstr :: SrcSpan -> Constr
$ctoConstr :: SrcSpan -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcSpan
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcSpan
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcSpan -> c SrcSpan
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcSpan -> c SrcSpan
Data, Typeable, forall x. Rep SrcSpan x -> SrcSpan
forall x. SrcSpan -> Rep SrcSpan x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SrcSpan x -> SrcSpan
$cfrom :: forall x. SrcSpan -> Rep SrcSpan x
Generic)

instance NFData SrcSpan
instance S.Store SrcSpan
instance Serialize SrcSpan
instance B.Binary SrcSpan

instance PPrint SrcSpan where
  pprintTidy :: Tidy -> SrcSpan -> Doc
pprintTidy Tidy
_ = SrcSpan -> Doc
ppSrcSpan

-- ppSrcSpan_short z = parens
--                   $ text (printf "file %s: (%d, %d) - (%d, %d)" (takeFileName f) l c l' c')
--   where
--     (f,l ,c )     = sourcePosElts $ sp_start z
--     (_,l',c')     = sourcePosElts $ sp_stop  z

ppSrcSpan :: SrcSpan -> Doc
ppSrcSpan :: SrcSpan -> Doc
ppSrcSpan SrcSpan
z       = FilePath -> Doc
text (forall r. PrintfType r => FilePath -> r
printf FilePath
"%s:%d:%d-%d:%d" FilePath
f Pos
l Pos
c Pos
l' Pos
c')
                -- parens $ text (printf "file %s: (%d, %d) - (%d, %d)" (takeFileName f) l c l' c')
  where
    (FilePath
f,Pos
l ,Pos
c )     = SourcePos -> (FilePath, Pos, Pos)
sourcePosElts forall a b. (a -> b) -> a -> b
$ SrcSpan -> SourcePos
sp_start SrcSpan
z
    (FilePath
_,Pos
l',Pos
c')     = SourcePos -> (FilePath, Pos, Pos)
sourcePosElts forall a b. (a -> b) -> a -> b
$ SrcSpan -> SourcePos
sp_stop  SrcSpan
z


instance Hashable SrcSpan where
  hashWithSalt :: Int -> SrcSpan -> Int
hashWithSalt Int
i SrcSpan
z = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
i (SrcSpan -> SourcePos
sp_start SrcSpan
z, SrcSpan -> SourcePos
sp_stop SrcSpan
z)

instance Loc SrcSpan where
  srcSpan :: SrcSpan -> SrcSpan
srcSpan SrcSpan
x = SrcSpan
x

instance Loc () where
  srcSpan :: () -> SrcSpan
srcSpan ()
_ = SrcSpan
dummySpan

instance Loc SourcePos where
  srcSpan :: SourcePos -> SrcSpan
srcSpan SourcePos
l = SourcePos -> SourcePos -> SrcSpan
SS SourcePos
l SourcePos
l

dummySpan :: SrcSpan
dummySpan :: SrcSpan
dummySpan = FilePath -> SrcSpan
panicSpan FilePath
""

panicSpan :: String -> SrcSpan
panicSpan :: FilePath -> SrcSpan
panicSpan FilePath
s = SourcePos -> SourcePos -> SrcSpan
SS SourcePos
l SourcePos
l
  where l :: SourcePos
l = FilePath -> SourcePos
initialPos FilePath
s

-- atLoc :: Located a -> b -> Located b
-- atLoc (Loc l l' _) = Loc l l'

atLoc :: (Loc l) => l -> b -> Located b
atLoc :: forall l b. Loc l => l -> b -> Located b
atLoc l
z b
x   = forall a. SourcePos -> SourcePos -> a -> Located a
Loc SourcePos
l SourcePos
l' b
x
  where
    SS SourcePos
l SourcePos
l' = forall a. Loc a => a -> SrcSpan
srcSpan l
z


locAt :: String -> a -> Located a
locAt :: forall a. FilePath -> a -> Located a
locAt FilePath
s  = forall a. SourcePos -> SourcePos -> a -> Located a
Loc SourcePos
l SourcePos
l
  where
    l :: SourcePos
l    = FilePath -> SourcePos
dummyPos FilePath
s

dummyLoc :: a -> Located a
dummyLoc :: forall a. a -> Located a
dummyLoc = forall a. SourcePos -> SourcePos -> a -> Located a
Loc SourcePos
l SourcePos
l
  where
    l :: SourcePos
l    = FilePath -> SourcePos
dummyPos FilePath
"Fixpoint.Types.dummyLoc"

dummyPos   :: FilePath -> SourcePos
dummyPos :: FilePath -> SourcePos
dummyPos = FilePath -> SourcePos
initialPos