{-# LANGUAGE GADTs, PostfixOperators #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
module Language.Drasil.Sentence (
Sentence(..),
SentenceStyle(..), RefInfo(..), TermCapitalization(..),
(+:+), (+:+.), (+:), (!.), capSent, headSent, ch, eS, eS', sC, sDash, sParen,
sentencePlural, sentenceShort,
sentenceSymb, sentenceTerm,
sdep, shortdep, lnames, lnames', sentenceRefs
) where
import Control.Lens ((^.))
import Data.Char (toUpper)
import Drasil.Database (HasChunkRefs(..), HasUID(..), UID)
import Language.Drasil.ExprClasses (Express(express))
import Language.Drasil.ModelExpr.Lang (ModelExpr)
import Language.Drasil.ModelExpr.Extract (meNames)
import Language.Drasil.NounPhrase.Types (NP)
import Language.Drasil.UnitLang (USymb)
import Language.Drasil.Symbol (HasSymbol, Symbol)
import qualified Data.Set as Set
data SentenceStyle = PluralTerm
| TermStyle
| ShortStyle
data TermCapitalization = CapF | CapW | NoCap
data RefInfo = None
| Equation [Int]
| Page [Int]
| RefNote String
infixr 5 :+:
data Sentence where
Ch :: SentenceStyle -> TermCapitalization -> UID -> Sentence
SyCh :: UID -> Sentence
Sy :: USymb -> Sentence
NP :: NP -> Sentence
S :: String -> Sentence
P :: Symbol -> Sentence
E :: ModelExpr -> Sentence
Ref :: UID -> Sentence -> RefInfo -> Sentence
Quote :: Sentence -> Sentence
Percent :: Sentence
(:+:) :: Sentence -> Sentence -> Sentence
EmptyS :: Sentence
eS :: ModelExpr -> Sentence
eS :: ModelExpr -> Sentence
eS = ModelExpr -> Sentence
E
eS' :: Express t => t -> Sentence
eS' :: forall t. Express t => t -> Sentence
eS' = ModelExpr -> Sentence
E (ModelExpr -> Sentence) -> (t -> ModelExpr) -> t -> Sentence
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> ModelExpr
forall c. Express c => c -> ModelExpr
express
ch :: (HasUID c, HasSymbol c) => c -> Sentence
ch :: forall c. (HasUID c, HasSymbol c) => c -> Sentence
ch c
x = UID -> Sentence
SyCh (c
x c -> Getting UID c UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID c UID
forall c. HasUID c => Getter c UID
Getter c UID
uid)
instance Semigroup Sentence where
<> :: Sentence -> Sentence -> Sentence
(<>) = Sentence -> Sentence -> Sentence
(:+:)
instance Monoid Sentence where
mempty :: Sentence
mempty = Sentence
EmptyS
sentencePlural, sentenceShort, sentenceSymb, sentenceTerm :: UID -> Sentence
sentencePlural :: UID -> Sentence
sentencePlural = SentenceStyle -> TermCapitalization -> UID -> Sentence
Ch SentenceStyle
PluralTerm TermCapitalization
NoCap
sentenceShort :: UID -> Sentence
sentenceShort = SentenceStyle -> TermCapitalization -> UID -> Sentence
Ch SentenceStyle
ShortStyle TermCapitalization
NoCap
sentenceSymb :: UID -> Sentence
sentenceSymb = UID -> Sentence
SyCh
sentenceTerm :: UID -> Sentence
sentenceTerm = SentenceStyle -> TermCapitalization -> UID -> Sentence
Ch SentenceStyle
TermStyle TermCapitalization
NoCap
sParen :: Sentence -> Sentence
sParen :: Sentence -> Sentence
sParen Sentence
x = String -> Sentence
S String
"(" Sentence -> Sentence -> Sentence
:+: Sentence
x Sentence -> Sentence -> Sentence
:+: String -> Sentence
S String
")"
sDash :: Sentence -> Sentence -> Sentence
sDash :: Sentence -> Sentence -> Sentence
sDash Sentence
a Sentence
b = Sentence
a Sentence -> Sentence -> Sentence
+:+ String -> Sentence
S String
"-" Sentence -> Sentence -> Sentence
+:+ Sentence
b
(+:+) :: Sentence -> Sentence -> Sentence
Sentence
EmptyS +:+ :: Sentence -> Sentence -> Sentence
+:+ Sentence
b = Sentence
b
Sentence
a +:+ Sentence
EmptyS = Sentence
a
Sentence
a +:+ Sentence
b = Sentence
a Sentence -> Sentence -> Sentence
:+: String -> Sentence
S String
" " Sentence -> Sentence -> Sentence
:+: Sentence
b
sC :: Sentence -> Sentence -> Sentence
Sentence
a sC :: Sentence -> Sentence -> Sentence
`sC` Sentence
b = Sentence
a Sentence -> Sentence -> Sentence
:+: String -> Sentence
S String
"," Sentence -> Sentence -> Sentence
+:+ Sentence
b
(+:+.) :: Sentence -> Sentence -> Sentence
Sentence
a +:+. :: Sentence -> Sentence -> Sentence
+:+. Sentence
b = Sentence
a Sentence -> Sentence -> Sentence
+:+ Sentence
b Sentence -> Sentence -> Sentence
:+: String -> Sentence
S String
"."
(!.) :: Sentence -> Sentence
!. :: Sentence -> Sentence
(!.) Sentence
a = Sentence
a Sentence -> Sentence -> Sentence
:+: String -> Sentence
S String
"."
(+:) :: Sentence -> Sentence -> Sentence
Sentence
a +: :: Sentence -> Sentence -> Sentence
+: Sentence
b = Sentence
a Sentence -> Sentence -> Sentence
+:+ Sentence
b Sentence -> Sentence -> Sentence
:+: String -> Sentence
S String
":"
capSent :: Sentence -> Sentence
capSent :: Sentence -> Sentence
capSent (S (Char
s:String
ss)) = String -> Sentence
S (Char -> Char
toUpper Char
s Char -> String -> String
forall a. a -> [a] -> [a]
: String
ss)
capSent (Sentence
a :+: Sentence
b) = Sentence -> Sentence
capSent Sentence
a Sentence -> Sentence -> Sentence
:+: Sentence
b
capSent Sentence
x = Sentence
x
headSent :: Int -> Sentence -> Sentence
headSent :: Int -> Sentence -> Sentence
headSent Int
s Sentence
x = String -> Sentence
S ([String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Int -> String -> [String]
forall a. Int -> a -> [a]
replicate Int
s String
"#")) Sentence -> Sentence -> Sentence
:+: String -> Sentence
S String
" " Sentence -> Sentence -> Sentence
:+: Sentence
x
getUIDs :: Sentence -> [UID]
getUIDs :: Sentence -> [UID]
getUIDs (Ch SentenceStyle
ShortStyle TermCapitalization
_ UID
_) = []
getUIDs (Ch SentenceStyle
TermStyle TermCapitalization
_ UID
_) = []
getUIDs (Ch SentenceStyle
PluralTerm TermCapitalization
_ UID
_) = []
getUIDs (SyCh UID
a) = [UID
a]
getUIDs Sy {} = []
getUIDs NP {} = []
getUIDs S {} = []
getUIDs P {} = []
getUIDs Ref {} = []
getUIDs Sentence
Percent = []
getUIDs ((:+:) Sentence
a Sentence
b) = Sentence -> [UID]
getUIDs Sentence
a [UID] -> [UID] -> [UID]
forall a. [a] -> [a] -> [a]
++ Sentence -> [UID]
getUIDs Sentence
b
getUIDs (Quote Sentence
a) = Sentence -> [UID]
getUIDs Sentence
a
getUIDs (E ModelExpr
a) = ModelExpr -> [UID]
meNames ModelExpr
a
getUIDs Sentence
EmptyS = []
getUIDshort :: Sentence -> [UID]
getUIDshort :: Sentence -> [UID]
getUIDshort (Ch SentenceStyle
ShortStyle TermCapitalization
_ UID
a) = [UID
a]
getUIDshort (Ch SentenceStyle
TermStyle TermCapitalization
_ UID
_) = []
getUIDshort (Ch SentenceStyle
PluralTerm TermCapitalization
_ UID
_) = []
getUIDshort SyCh {} = []
getUIDshort Sy {} = []
getUIDshort NP {} = []
getUIDshort S {} = []
getUIDshort Sentence
Percent = []
getUIDshort P {} = []
getUIDshort Ref {} = []
getUIDshort ((:+:) Sentence
a Sentence
b) = Sentence -> [UID]
getUIDshort Sentence
a [UID] -> [UID] -> [UID]
forall a. [a] -> [a] -> [a]
++ Sentence -> [UID]
getUIDshort Sentence
b
getUIDshort (Quote Sentence
a) = Sentence -> [UID]
getUIDshort Sentence
a
getUIDshort E {} = []
getUIDshort Sentence
EmptyS = []
sdep :: Sentence -> Set.Set UID
sdep :: Sentence -> Set UID
sdep = [UID] -> Set UID
forall a. Ord a => [a] -> Set a
Set.fromList ([UID] -> Set UID) -> (Sentence -> [UID]) -> Sentence -> Set UID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sentence -> [UID]
getUIDs
{-# INLINE sdep #-}
shortdep :: Sentence -> Set.Set UID
shortdep :: Sentence -> Set UID
shortdep = [UID] -> Set UID
forall a. Ord a => [a] -> Set a
Set.fromList ([UID] -> Set UID) -> (Sentence -> [UID]) -> Sentence -> Set UID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sentence -> [UID]
getUIDshort
{-# INLINE shortdep #-}
lnames :: Sentence -> Set.Set UID
lnames :: Sentence -> Set UID
lnames Ch {} = Set UID
forall a. Set a
Set.empty
lnames SyCh {} = Set UID
forall a. Set a
Set.empty
lnames Sy {} = Set UID
forall a. Set a
Set.empty
lnames NP {} = Set UID
forall a. Set a
Set.empty
lnames S {} = Set UID
forall a. Set a
Set.empty
lnames Sentence
Percent = Set UID
forall a. Set a
Set.empty
lnames P {} = Set UID
forall a. Set a
Set.empty
lnames (Ref UID
a Sentence
_ RefInfo
_) = UID -> Set UID
forall a. a -> Set a
Set.singleton UID
a
lnames ((:+:) Sentence
a Sentence
b) = Sentence -> Set UID
lnames Sentence
a Set UID -> Set UID -> Set UID
forall a. Ord a => Set a -> Set a -> Set a
`Set.union` Sentence -> Set UID
lnames Sentence
b
lnames Quote {} = Set UID
forall a. Set a
Set.empty
lnames E {} = Set UID
forall a. Set a
Set.empty
lnames Sentence
EmptyS = Set UID
forall a. Set a
Set.empty
{-# INLINE lnames #-}
lnames' :: [Sentence] -> [UID]
lnames' :: [Sentence] -> [UID]
lnames' = (Sentence -> [UID]) -> [Sentence] -> [UID]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Set UID -> [UID]
forall a. Set a -> [a]
Set.toList (Set UID -> [UID]) -> (Sentence -> Set UID) -> Sentence -> [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sentence -> Set UID
lnames)
{-# INLINE lnames' #-}
sentenceRefs :: Sentence -> Set.Set UID
sentenceRefs :: Sentence -> Set UID
sentenceRefs Sentence
sent = [Set UID] -> Set UID
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions [Sentence -> Set UID
lnames Sentence
sent, Sentence -> Set UID
sdep Sentence
sent, Sentence -> Set UID
shortdep Sentence
sent]
{-# INLINE sentenceRefs #-}
instance HasChunkRefs Sentence where
chunkRefs :: Sentence -> Set UID
chunkRefs = Sentence -> Set UID
sentenceRefs
{-# INLINABLE chunkRefs #-}