{-# LANGUAGE CPP                   #-}
{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards       #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE UndecidableInstances  #-}
{-# LANGUAGE ViewPatterns          #-}
-- | This module generalizes over types of code fragments
-- that may need to be iterated upon and measured separately.
module Language.Haskell.Homplexity.CodeFragment (
    CodeFragment   (fragmentName, fragmentSlice)
  , occurs
  , occursOf
  , allOccurs
  , allOccursOf
  , Program        (..)
  , programT
  , program
  , Module         (..)
  , moduleT
  , Function       (..)
  , functionT
  , DataDef       (..)
  , dataDefT
  , TypeSignature  (..)
  , typeSignatureT
  , TypeClass (..)
  , typeClassT
  , fragmentLoc
  -- TODO: add ClassSignature
  ) where

import           Data.Data
--import           Data.Functor
import           Data.Generics.Uniplate.Data
import           Data.List hiding (singleton)
import           Data.Maybe
--import           Data.Monoid
import           Language.Haskell.Exts.SrcLoc
import           Language.Haskell.Exts.Syntax
import           Language.Haskell.Homplexity.SrcSlice
import           Language.Haskell.Homplexity.Utilities

-- | Program
newtype Program = Program { Program -> [Module SrcLoc]
allModules :: [Module SrcLoc] }
  deriving (Typeable Program
Typeable Program
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Program -> c Program)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Program)
-> (Program -> Constr)
-> (Program -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Program))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Program))
-> ((forall b. Data b => b -> b) -> Program -> Program)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Program -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Program -> r)
-> (forall u. (forall d. Data d => d -> u) -> Program -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Program -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Program -> m Program)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Program -> m Program)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Program -> m Program)
-> Data Program
Program -> DataType
Program -> Constr
(forall b. Data b => b -> b) -> Program -> Program
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) -> Program -> u
forall u. (forall d. Data d => d -> u) -> Program -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Program -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Program -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Program -> m Program
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Program -> m Program
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Program
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Program -> c Program
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Program)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Program)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Program -> c Program
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Program -> c Program
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Program
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Program
$ctoConstr :: Program -> Constr
toConstr :: Program -> Constr
$cdataTypeOf :: Program -> DataType
dataTypeOf :: Program -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Program)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Program)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Program)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Program)
$cgmapT :: (forall b. Data b => b -> b) -> Program -> Program
gmapT :: (forall b. Data b => b -> b) -> Program -> Program
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Program -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Program -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Program -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Program -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Program -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Program -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Program -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Program -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Program -> m Program
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Program -> m Program
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Program -> m Program
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Program -> m Program
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Program -> m Program
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Program -> m Program
Data, Typeable, Int -> Program -> ShowS
[Program] -> ShowS
Program -> String
(Int -> Program -> ShowS)
-> (Program -> String) -> ([Program] -> ShowS) -> Show Program
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Program -> ShowS
showsPrec :: Int -> Program -> ShowS
$cshow :: Program -> String
show :: Program -> String
$cshowList :: [Program] -> ShowS
showList :: [Program] -> ShowS
Show)

-- | Smart constructor for adding cross-references in the future.
program :: [Module SrcLoc] -> Program
program :: [Module SrcLoc] -> Program
program  =  [Module SrcLoc] -> Program
Program

-- | Proxy for passing @Program@ type as an argument.
programT :: Proxy Program
programT :: Proxy Program
programT  = Proxy Program
forall {k} (t :: k). Proxy t
Proxy

-- * Type aliases for type-based matching of substructures
-- | Alias for a function declaration
data Function = Function {
                  Function -> [String]
functionNames     :: [String]
                , Function -> [SrcLoc]
functionLocations :: [SrcLoc]
                , Function -> [Rhs SrcLoc]
functionRhs       :: [Rhs   SrcLoc]
                , Function -> [Binds SrcLoc]
functionBinds     :: [Binds SrcLoc]
                }
  deriving (Typeable Function
Typeable Function
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Function -> c Function)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Function)
-> (Function -> Constr)
-> (Function -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Function))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Function))
-> ((forall b. Data b => b -> b) -> Function -> Function)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Function -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Function -> r)
-> (forall u. (forall d. Data d => d -> u) -> Function -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Function -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Function -> m Function)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Function -> m Function)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Function -> m Function)
-> Data Function
Function -> DataType
Function -> Constr
(forall b. Data b => b -> b) -> Function -> Function
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) -> Function -> u
forall u. (forall d. Data d => d -> u) -> Function -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Function -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Function -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Function -> m Function
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Function -> m Function
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Function
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Function -> c Function
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Function)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Function)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Function -> c Function
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Function -> c Function
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Function
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Function
$ctoConstr :: Function -> Constr
toConstr :: Function -> Constr
$cdataTypeOf :: Function -> DataType
dataTypeOf :: Function -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Function)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Function)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Function)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Function)
$cgmapT :: (forall b. Data b => b -> b) -> Function -> Function
gmapT :: (forall b. Data b => b -> b) -> Function -> Function
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Function -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Function -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Function -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Function -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Function -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Function -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Function -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Function -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Function -> m Function
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Function -> m Function
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Function -> m Function
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Function -> m Function
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Function -> m Function
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Function -> m Function
Data, Typeable, Int -> Function -> ShowS
[Function] -> ShowS
Function -> String
(Int -> Function -> ShowS)
-> (Function -> String) -> ([Function] -> ShowS) -> Show Function
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Function -> ShowS
showsPrec :: Int -> Function -> ShowS
$cshow :: Function -> String
show :: Function -> String
$cshowList :: [Function] -> ShowS
showList :: [Function] -> ShowS
Show)

-- | Proxy for passing @Function@ type as an argument.
functionT :: Proxy Function
functionT :: Proxy Function
functionT  = Proxy Function
forall {k} (t :: k). Proxy t
Proxy

-- | Alias for a @data@ declaration
data DataDef = DataDef {
                 DataDef -> String
dataDefName  :: String
               , DataDef -> Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
dataDefCtors :: Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
               }
  deriving (Typeable DataDef
Typeable DataDef
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> DataDef -> c DataDef)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c DataDef)
-> (DataDef -> Constr)
-> (DataDef -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c DataDef))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DataDef))
-> ((forall b. Data b => b -> b) -> DataDef -> DataDef)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> DataDef -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> DataDef -> r)
-> (forall u. (forall d. Data d => d -> u) -> DataDef -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> DataDef -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> DataDef -> m DataDef)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DataDef -> m DataDef)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DataDef -> m DataDef)
-> Data DataDef
DataDef -> DataType
DataDef -> Constr
(forall b. Data b => b -> b) -> DataDef -> DataDef
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) -> DataDef -> u
forall u. (forall d. Data d => d -> u) -> DataDef -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DataDef -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DataDef -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DataDef
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DataDef -> c DataDef
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DataDef)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DataDef)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DataDef -> c DataDef
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DataDef -> c DataDef
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DataDef
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DataDef
$ctoConstr :: DataDef -> Constr
toConstr :: DataDef -> Constr
$cdataTypeOf :: DataDef -> DataType
dataTypeOf :: DataDef -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DataDef)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DataDef)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DataDef)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DataDef)
$cgmapT :: (forall b. Data b => b -> b) -> DataDef -> DataDef
gmapT :: (forall b. Data b => b -> b) -> DataDef -> DataDef
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DataDef -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DataDef -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DataDef -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DataDef -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> DataDef -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> DataDef -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> DataDef -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> DataDef -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DataDef -> m DataDef
Data, Typeable, Int -> DataDef -> ShowS
[DataDef] -> ShowS
DataDef -> String
(Int -> DataDef -> ShowS)
-> (DataDef -> String) -> ([DataDef] -> ShowS) -> Show DataDef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DataDef -> ShowS
showsPrec :: Int -> DataDef -> ShowS
$cshow :: DataDef -> String
show :: DataDef -> String
$cshowList :: [DataDef] -> ShowS
showList :: [DataDef] -> ShowS
Show)

-- | Proxy for passing @DataDef@ type as an argument.
dataDefT :: Proxy DataDef
dataDefT :: Proxy DataDef
dataDefT  = Proxy DataDef
forall {k} (t :: k). Proxy t
Proxy

-- ** Type signature of a function
-- | Type alias for a type signature of a function as a @CodeFragment@
data TypeSignature = TypeSignature { TypeSignature -> SrcLoc
loc         :: SrcLoc
                                   , TypeSignature -> [Name SrcLoc]
identifiers :: [Name SrcLoc]
                                   , TypeSignature -> Type SrcLoc
theType     ::  Type SrcLoc }
  deriving (Typeable TypeSignature
Typeable TypeSignature
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> TypeSignature -> c TypeSignature)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c TypeSignature)
-> (TypeSignature -> Constr)
-> (TypeSignature -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c TypeSignature))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c TypeSignature))
-> ((forall b. Data b => b -> b) -> TypeSignature -> TypeSignature)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> TypeSignature -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> TypeSignature -> r)
-> (forall u. (forall d. Data d => d -> u) -> TypeSignature -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> TypeSignature -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature)
-> Data TypeSignature
TypeSignature -> DataType
TypeSignature -> Constr
(forall b. Data b => b -> b) -> TypeSignature -> TypeSignature
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) -> TypeSignature -> u
forall u. (forall d. Data d => d -> u) -> TypeSignature -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TypeSignature -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TypeSignature -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TypeSignature
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TypeSignature -> c TypeSignature
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TypeSignature)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c TypeSignature)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TypeSignature -> c TypeSignature
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TypeSignature -> c TypeSignature
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TypeSignature
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TypeSignature
$ctoConstr :: TypeSignature -> Constr
toConstr :: TypeSignature -> Constr
$cdataTypeOf :: TypeSignature -> DataType
dataTypeOf :: TypeSignature -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TypeSignature)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TypeSignature)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c TypeSignature)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c TypeSignature)
$cgmapT :: (forall b. Data b => b -> b) -> TypeSignature -> TypeSignature
gmapT :: (forall b. Data b => b -> b) -> TypeSignature -> TypeSignature
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TypeSignature -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TypeSignature -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TypeSignature -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TypeSignature -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> TypeSignature -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> TypeSignature -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TypeSignature -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TypeSignature -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeSignature -> m TypeSignature
Data, Typeable, Int -> TypeSignature -> ShowS
[TypeSignature] -> ShowS
TypeSignature -> String
(Int -> TypeSignature -> ShowS)
-> (TypeSignature -> String)
-> ([TypeSignature] -> ShowS)
-> Show TypeSignature
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TypeSignature -> ShowS
showsPrec :: Int -> TypeSignature -> ShowS
$cshow :: TypeSignature -> String
show :: TypeSignature -> String
$cshowList :: [TypeSignature] -> ShowS
showList :: [TypeSignature] -> ShowS
Show)

-- | Proxy for passing @TypeSignature@ type as an argument.
typeSignatureT :: Proxy TypeSignature
typeSignatureT :: Proxy TypeSignature
typeSignatureT  = Proxy TypeSignature
forall {k} (t :: k). Proxy t
Proxy

-- ** TODO: class signatures (number of function decls inside)
-- | Alias for a class signature
data TypeClass = TypeClass { TypeClass -> String
tcName  :: String
                           , TypeClass -> Maybe [ClassDecl SrcLoc]
tcDecls :: Maybe [ClassDecl SrcLoc]
                           }
  deriving (Typeable TypeClass
Typeable TypeClass
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> TypeClass -> c TypeClass)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c TypeClass)
-> (TypeClass -> Constr)
-> (TypeClass -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c TypeClass))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TypeClass))
-> ((forall b. Data b => b -> b) -> TypeClass -> TypeClass)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> TypeClass -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> TypeClass -> r)
-> (forall u. (forall d. Data d => d -> u) -> TypeClass -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> TypeClass -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> TypeClass -> m TypeClass)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TypeClass -> m TypeClass)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TypeClass -> m TypeClass)
-> Data TypeClass
TypeClass -> DataType
TypeClass -> Constr
(forall b. Data b => b -> b) -> TypeClass -> TypeClass
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) -> TypeClass -> u
forall u. (forall d. Data d => d -> u) -> TypeClass -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TypeClass -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TypeClass -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TypeClass
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TypeClass -> c TypeClass
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TypeClass)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TypeClass)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TypeClass -> c TypeClass
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TypeClass -> c TypeClass
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TypeClass
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TypeClass
$ctoConstr :: TypeClass -> Constr
toConstr :: TypeClass -> Constr
$cdataTypeOf :: TypeClass -> DataType
dataTypeOf :: TypeClass -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TypeClass)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TypeClass)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TypeClass)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TypeClass)
$cgmapT :: (forall b. Data b => b -> b) -> TypeClass -> TypeClass
gmapT :: (forall b. Data b => b -> b) -> TypeClass -> TypeClass
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TypeClass -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TypeClass -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TypeClass -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TypeClass -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> TypeClass -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> TypeClass -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TypeClass -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TypeClass -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TypeClass -> m TypeClass
Data, Typeable, Int -> TypeClass -> ShowS
[TypeClass] -> ShowS
TypeClass -> String
(Int -> TypeClass -> ShowS)
-> (TypeClass -> String)
-> ([TypeClass] -> ShowS)
-> Show TypeClass
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TypeClass -> ShowS
showsPrec :: Int -> TypeClass -> ShowS
$cshow :: TypeClass -> String
show :: TypeClass -> String
$cshowList :: [TypeClass] -> ShowS
showList :: [TypeClass] -> ShowS
Show)

-- | Proxy for passing @TypeClass@ type as an argument.
typeClassT :: Proxy TypeClass
typeClassT :: Proxy TypeClass
typeClassT  = Proxy TypeClass
forall {k} (t :: k). Proxy t
Proxy

-- TODO: need combination of Fold and Biplate
-- Resulting record may be created to make pa

-- | Class @CodeFragment@ allows for:
-- * both selecting direct or all descendants
--   of the given type of object within another structure
--   (with @occurs@ and @allOccurs@)
-- * naming the object to allow user to distinguish it.
--
-- In order to compute selection, we just need to know which
-- @AST@ nodes contain the given object, and how to extract
-- this given object from @AST@, if it is there (@matchAST@).:w
class (Show c, Data (AST c), Data c) => CodeFragment c where
  type             AST c
  matchAST      :: AST c -> Maybe c
  fragmentName  ::     c -> String
  fragmentSlice ::     c -> SrcSlice
  fragmentSlice  = c -> SrcSlice
forall a. (Data a, Show a) => a -> SrcSlice
srcSlice

-- | First location for each @CodeFragment@ - for convenient reporting.
fragmentLoc :: (CodeFragment c) => c -> SrcLoc
fragmentLoc :: forall c. CodeFragment c => c -> SrcLoc
fragmentLoc =  SrcSlice -> SrcLoc
forall si. SrcInfo si => si -> SrcLoc
getPointLoc
            (SrcSlice -> SrcLoc) -> (c -> SrcSlice) -> c -> SrcLoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  c -> SrcSlice
forall c. CodeFragment c => c -> SrcSlice
fragmentSlice

instance CodeFragment Function where
  type AST Function            = Decl SrcLoc
  matchAST :: AST Function -> Maybe Function
matchAST (FunBind SrcLoc
_ [Match SrcLoc]
matches) = Function -> Maybe Function
forall a. a -> Maybe a
Just
      Function {[String]
[SrcLoc]
[Binds SrcLoc]
[Rhs SrcLoc]
functionNames :: [String]
functionLocations :: [SrcLoc]
functionRhs :: [Rhs SrcLoc]
functionBinds :: [Binds SrcLoc]
functionLocations :: [SrcLoc]
functionNames :: [String]
functionRhs :: [Rhs SrcLoc]
functionBinds :: [Binds SrcLoc]
..}
    where
      ([SrcLoc]
functionLocations,
       (Name SrcLoc -> String
forall a. Name a -> String
unName (Name SrcLoc -> String) -> [Name SrcLoc] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ([Name SrcLoc] -> [String])
-> ([Name SrcLoc] -> [Name SrcLoc]) -> [Name SrcLoc] -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Name SrcLoc] -> [Name SrcLoc]
forall a. Int -> [a] -> [a]
take Int
1 -> [String]
functionNames,
       [Rhs SrcLoc]
functionRhs,
       [Maybe (Binds SrcLoc)] -> [Binds SrcLoc]
forall a. [Maybe a] -> [a]
catMaybes -> [Binds SrcLoc]
functionBinds) = [(SrcLoc, Name SrcLoc, Rhs SrcLoc, Maybe (Binds SrcLoc))]
-> ([SrcLoc], [Name SrcLoc], [Rhs SrcLoc], [Maybe (Binds SrcLoc)])
forall a b c d. [(a, b, c, d)] -> ([a], [b], [c], [d])
unzip4 ([(SrcLoc, Name SrcLoc, Rhs SrcLoc, Maybe (Binds SrcLoc))]
 -> ([SrcLoc], [Name SrcLoc], [Rhs SrcLoc], [Maybe (Binds SrcLoc)]))
-> [(SrcLoc, Name SrcLoc, Rhs SrcLoc, Maybe (Binds SrcLoc))]
-> ([SrcLoc], [Name SrcLoc], [Rhs SrcLoc], [Maybe (Binds SrcLoc)])
forall a b. (a -> b) -> a -> b
$ (Match SrcLoc
 -> (SrcLoc, Name SrcLoc, Rhs SrcLoc, Maybe (Binds SrcLoc)))
-> [Match SrcLoc]
-> [(SrcLoc, Name SrcLoc, Rhs SrcLoc, Maybe (Binds SrcLoc))]
forall a b. (a -> b) -> [a] -> [b]
map Match SrcLoc
-> (SrcLoc, Name SrcLoc, Rhs SrcLoc, Maybe (Binds SrcLoc))
forall {a}.
Show a =>
Match a -> (a, Name a, Rhs a, Maybe (Binds a))
extract [Match SrcLoc]
matches
      extract :: Match a -> (a, Name a, Rhs a, Maybe (Binds a))
extract (Match      a
srcLoc Name a
name [Pat a]
_      Rhs a
rhs Maybe (Binds a)
binds) = (a
srcLoc, Name a
name, Rhs a
rhs, Maybe (Binds a)
binds)
      extract (InfixMatch a
srcLoc Pat a
_    Name a
name [Pat a]
_ Rhs a
rhs Maybe (Binds a)
binds) = (a
srcLoc, Name a
name, Rhs a
rhs, Maybe (Binds a)
binds)
      extract  Match a
other                          = String -> (a, Name a, Rhs a, Maybe (Binds a))
forall a. HasCallStack => String -> a
error (String -> (a, Name a, Rhs a, Maybe (Binds a)))
-> String -> (a, Name a, Rhs a, Maybe (Binds a))
forall a b. (a -> b) -> a -> b
$ String
"Undocumented constructor: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Match a -> String
forall a. Show a => a -> String
show Match a
other
  matchAST (PatBind (SrcLoc -> [SrcLoc]
forall a. a -> [a]
singleton -> [SrcLoc]
functionLocations) Pat SrcLoc
pat
                    (Rhs SrcLoc -> [Rhs SrcLoc]
forall a. a -> [a]
singleton -> [Rhs SrcLoc]
functionRhs      )
                    (Maybe (Binds SrcLoc) -> [Binds SrcLoc]
forall a. Maybe a -> [a]
maybeToList -> [Binds SrcLoc]
functionBinds  )) = Function -> Maybe Function
forall a. a -> Maybe a
Just Function {[String]
[SrcLoc]
[Binds SrcLoc]
[Rhs SrcLoc]
functionNames :: [String]
functionLocations :: [SrcLoc]
functionRhs :: [Rhs SrcLoc]
functionBinds :: [Binds SrcLoc]
functionLocations :: [SrcLoc]
functionRhs :: [Rhs SrcLoc]
functionBinds :: [Binds SrcLoc]
functionNames :: [String]
..}
    where
      functionNames :: [String]
functionNames  = [String]
wildcards [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ (Name SrcLoc -> String) -> [Name SrcLoc] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Name SrcLoc -> String
forall a. Name a -> String
unName (Pat SrcLoc -> [Name SrcLoc]
forall from to. Biplate from to => from -> [to]
universeBi Pat SrcLoc
pat :: [Name SrcLoc])
      wildcards :: [String]
wildcards = (Pat SrcLoc -> Maybe String) -> [Pat SrcLoc] -> [String]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Pat SrcLoc -> Maybe String
forall {l}. Pat l -> Maybe String
wildcard (Pat SrcLoc -> [Pat SrcLoc]
forall on. Uniplate on => on -> [on]
universe Pat SrcLoc
pat)
        where
          wildcard :: Pat l -> Maybe String
wildcard PWildCard {} = String -> Maybe String
forall a. a -> Maybe a
Just    String
".."
          wildcard Pat l
_            = Maybe String
forall a. Maybe a
Nothing
  matchAST AST Function
_                                          = Maybe Function
forall a. Maybe a
Nothing
  fragmentName :: Function -> String
fragmentName Function {[String]
[SrcLoc]
[Binds SrcLoc]
[Rhs SrcLoc]
functionNames :: Function -> [String]
functionLocations :: Function -> [SrcLoc]
functionRhs :: Function -> [Rhs SrcLoc]
functionBinds :: Function -> [Binds SrcLoc]
functionNames :: [String]
functionLocations :: [SrcLoc]
functionRhs :: [Rhs SrcLoc]
functionBinds :: [Binds SrcLoc]
..} = [String] -> String
unwords ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String
"function"String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
functionNames

instance CodeFragment DataDef where
  type AST DataDef = Decl SrcLoc
  matchAST :: AST DataDef -> Maybe DataDef
matchAST (DataDecl SrcLoc
_ DataOrNew SrcLoc
_ Maybe (Context SrcLoc)
_ DeclHead SrcLoc
declHead [QualConDecl SrcLoc]
qualConDecls [Deriving SrcLoc]
_) = do
    Name SrcLoc
name <- [Name SrcLoc] -> Maybe (Name SrcLoc)
forall a. [a] -> Maybe a
listToMaybe (DeclHead SrcLoc -> [Name SrcLoc]
forall from to. Biplate from to => from -> [to]
universeBi DeclHead SrcLoc
declHead :: [Name SrcLoc])
    DataDef -> Maybe DataDef
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DataDef { dataDefName :: String
dataDefName = Name SrcLoc -> String
forall a. Name a -> String
unName Name SrcLoc
name, dataDefCtors :: Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
dataDefCtors = [QualConDecl SrcLoc]
-> Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
forall a b. a -> Either a b
Left [QualConDecl SrcLoc]
qualConDecls }
  matchAST (GDataDecl SrcLoc
_ DataOrNew SrcLoc
_ Maybe (Context SrcLoc)
_ DeclHead SrcLoc
declHead Maybe (Type SrcLoc)
_ [GadtDecl SrcLoc]
gadtDecls [Deriving SrcLoc]
_) = do
    Name SrcLoc
name <- [Name SrcLoc] -> Maybe (Name SrcLoc)
forall a. [a] -> Maybe a
listToMaybe (DeclHead SrcLoc -> [Name SrcLoc]
forall from to. Biplate from to => from -> [to]
universeBi DeclHead SrcLoc
declHead :: [Name SrcLoc])
    DataDef -> Maybe DataDef
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DataDef { dataDefName :: String
dataDefName = Name SrcLoc -> String
forall a. Name a -> String
unName Name SrcLoc
name, dataDefCtors :: Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
dataDefCtors = [GadtDecl SrcLoc] -> Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
forall a b. b -> Either a b
Right [GadtDecl SrcLoc]
gadtDecls }
  matchAST AST DataDef
_ = Maybe DataDef
forall a. Maybe a
Nothing
  fragmentName :: DataDef -> String
fragmentName DataDef {String
Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
dataDefName :: DataDef -> String
dataDefCtors :: DataDef -> Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
dataDefName :: String
dataDefCtors :: Either [QualConDecl SrcLoc] [GadtDecl SrcLoc]
..} = String
"data " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
dataDefName

-- | Make a single element list.
singleton :: a -> [a]
singleton :: forall a. a -> [a]
singleton  = (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[])

-- | Direct occurences of given @CodeFragment@ fragment within another structure.
occurs :: (CodeFragment c, Data from) => from -> [c]
occurs :: forall c from. (CodeFragment c, Data from) => from -> [c]
occurs  = (AST c -> Maybe c) -> [AST c] -> [c]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe AST c -> Maybe c
forall c. CodeFragment c => AST c -> Maybe c
matchAST ([AST c] -> [c]) -> (from -> [AST c]) -> from -> [c]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. from -> [AST c]
forall from to. Biplate from to => from -> [to]
childrenBi

-- | Explicitly typed variant of @occurs@.
occursOf  :: (Data from, CodeFragment c) => Proxy c -> from -> [c]
occursOf :: forall from c.
(Data from, CodeFragment c) =>
Proxy c -> from -> [c]
occursOf Proxy c
_ =  from -> [c]
forall c from. (CodeFragment c, Data from) => from -> [c]
occurs

allOccurs :: (CodeFragment c, Data from) => from -> [c]
allOccurs :: forall c from. (CodeFragment c, Data from) => from -> [c]
allOccurs = (AST c -> Maybe c) -> [AST c] -> [c]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe AST c -> Maybe c
forall c. CodeFragment c => AST c -> Maybe c
matchAST ([AST c] -> [c]) -> (from -> [AST c]) -> from -> [c]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. from -> [AST c]
forall from to. Biplate from to => from -> [to]
universeBi

-- | Explicitly typed variant of @allOccurs@.
allOccursOf  :: (Data from, CodeFragment c) => Proxy c -> from -> [c]
allOccursOf :: forall from c.
(Data from, CodeFragment c) =>
Proxy c -> from -> [c]
allOccursOf Proxy c
_ =  from -> [c]
forall c from. (CodeFragment c, Data from) => from -> [c]
allOccurs

instance CodeFragment Program where
  type AST Program = Program
  matchAST :: AST Program -> Maybe Program
matchAST         = AST Program -> Maybe Program
Program -> Maybe Program
forall a. a -> Maybe a
Just
  fragmentName :: Program -> String
fragmentName Program
_   = String
"program"

instance CodeFragment (Module SrcLoc) where
  type AST (Module SrcLoc)= Module SrcLoc
  matchAST :: AST (Module SrcLoc) -> Maybe (Module SrcLoc)
matchAST = Module SrcLoc -> Maybe (Module SrcLoc)
AST (Module SrcLoc) -> Maybe (Module SrcLoc)
forall a. a -> Maybe a
Just
  fragmentName :: Module SrcLoc -> String
fragmentName (Module SrcLoc
_ (Just (ModuleHead SrcLoc
_ (ModuleName SrcLoc
_ String
theName) Maybe (WarningText SrcLoc)
_ Maybe (ExportSpecList SrcLoc)
_)) [ModulePragma SrcLoc]
_ [ImportDecl SrcLoc]
_ [Decl SrcLoc]
_) =
                String
"module " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
theName
  fragmentName (Module SrcLoc
_  Maybe (ModuleHead SrcLoc)
Nothing                                         [ModulePragma SrcLoc]
_ [ImportDecl SrcLoc]
_ [Decl SrcLoc]
_) =
                String
"<unnamed module>"
  fragmentName (XmlPage   SrcLoc
_ (ModuleName SrcLoc
_ String
theName) [ModulePragma SrcLoc]
_ XName SrcLoc
_ [XAttr SrcLoc]
_ Maybe (Exp SrcLoc)
_ [Exp SrcLoc]
_)            = String
"XML page " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
theName
  fragmentName (XmlHybrid SrcLoc
_ (Just (ModuleHead SrcLoc
_ (ModuleName SrcLoc
_ String
theName) Maybe (WarningText SrcLoc)
_ Maybe (ExportSpecList SrcLoc)
_))
                          [ModulePragma SrcLoc]
_ [ImportDecl SrcLoc]
_ [Decl SrcLoc]
_ XName SrcLoc
_ [XAttr SrcLoc]
_ Maybe (Exp SrcLoc)
_ [Exp SrcLoc]
_) = String
"module with XML " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
theName
  fragmentName (XmlHybrid SrcLoc
_  Maybe (ModuleHead SrcLoc)
Nothing                  [ModulePragma SrcLoc]
_ [ImportDecl SrcLoc]
_ [Decl SrcLoc]
_ XName SrcLoc
_ [XAttr SrcLoc]
_ Maybe (Exp SrcLoc)
_ [Exp SrcLoc]
_    ) = String
"<unnamed module with XML>"

-- | Proxy for passing @Module@ type as an argument.
moduleT :: Proxy (Module SrcLoc)
moduleT :: Proxy (Module SrcLoc)
moduleT  = Proxy (Module SrcLoc)
forall {k} (t :: k). Proxy t
Proxy

instance CodeFragment TypeSignature where
  type AST  TypeSignature = Decl SrcLoc
  matchAST :: AST TypeSignature -> Maybe TypeSignature
matchAST (TypeSig SrcLoc
loc [Name SrcLoc]
identifiers Type SrcLoc
theType) = TypeSignature -> Maybe TypeSignature
forall a. a -> Maybe a
Just TypeSignature {[Name SrcLoc]
SrcLoc
Type SrcLoc
loc :: SrcLoc
identifiers :: [Name SrcLoc]
theType :: Type SrcLoc
loc :: SrcLoc
identifiers :: [Name SrcLoc]
theType :: Type SrcLoc
..}
  matchAST  AST TypeSignature
_                                = Maybe TypeSignature
forall a. Maybe a
Nothing
  fragmentName :: TypeSignature -> String
fragmentName TypeSignature {[Name SrcLoc]
SrcLoc
Type SrcLoc
loc :: TypeSignature -> SrcLoc
identifiers :: TypeSignature -> [Name SrcLoc]
theType :: TypeSignature -> Type SrcLoc
loc :: SrcLoc
identifiers :: [Name SrcLoc]
theType :: Type SrcLoc
..} = String
"type signature for "
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((Name SrcLoc -> String) -> [Name SrcLoc] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Name SrcLoc -> String
forall a. Name a -> String
unName [Name SrcLoc]
identifiers)

instance CodeFragment TypeClass where
  type AST TypeClass = Decl SrcLoc

  matchAST :: AST TypeClass -> Maybe TypeClass
matchAST (ClassDecl SrcLoc
_ Maybe (Context SrcLoc)
_ DeclHead SrcLoc
declHead [FunDep SrcLoc]
_ Maybe [ClassDecl SrcLoc]
classDecls)
    = TypeClass -> Maybe TypeClass
forall a. a -> Maybe a
Just (TypeClass -> Maybe TypeClass) -> TypeClass -> Maybe TypeClass
forall a b. (a -> b) -> a -> b
$ String -> Maybe [ClassDecl SrcLoc] -> TypeClass
TypeClass (Name SrcLoc -> String
forall a. Name a -> String
unName (Name SrcLoc -> String)
-> (DeclHead SrcLoc -> Name SrcLoc) -> DeclHead SrcLoc -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DeclHead SrcLoc -> Name SrcLoc
forall l. DeclHead l -> Name l
declHeadName (DeclHead SrcLoc -> String) -> DeclHead SrcLoc -> String
forall a b. (a -> b) -> a -> b
$ DeclHead SrcLoc
declHead) Maybe [ClassDecl SrcLoc]
classDecls
  matchAST AST TypeClass
_ = Maybe TypeClass
forall a. Maybe a
Nothing

  fragmentName :: TypeClass -> String
fragmentName (TypeClass String
tcName Maybe [ClassDecl SrcLoc]
_) = String
"type class " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
tcName

-- | Unpack @Name@ identifier into a @String@.
unName :: Name a -> String
unName :: forall a. Name a -> String
unName (Symbol a
_ String
s) = String
s
unName (Ident  a
_ String
i) = String
i