{-# LANGUAGE LambdaCase, Rank2Types #-}
-- | Defines functions to extract certain kinds of information from a document.
-- Mainly used to pull the 'UID's of chunks out of 'Sentence's and 'Expr's.
module Drasil.ExtractDocDesc (getDocDesc, egetDocDesc, sentencePlate) where

import Control.Lens((^.))
import Drasil.DocumentLanguage.Core
import Drasil.Sections.SpecificSystemDescription (inDataConstTbl, outDataConstTbl)
import Language.Drasil hiding (Manual, Verb)
import Theory.Drasil
import Data.List(transpose)

import Data.Functor.Constant (Constant(Constant))
import Data.Generics.Multiplate (appendPlate, foldFor, purePlate, preorderFold)

-- | Creates a section contents plate that contains diferrent system subsections.
secConPlate :: Monoid b => (forall a. HasContents a => [a] -> b) ->
  ([Section] -> b) -> DLPlate (Constant b)
secConPlate :: forall b.
Monoid b =>
(forall a. HasContents a => [a] -> b)
-> ([Section] -> b) -> DLPlate (Constant b)
secConPlate forall a. HasContents a => [a] -> b
mCon [Section] -> b
mSec = DLPlate (Constant b) -> DLPlate (Constant b)
forall (p :: (* -> *) -> *) o.
(Multiplate p, Monoid o) =>
p (Constant o) -> p (Constant o)
preorderFold (DLPlate (Constant b) -> DLPlate (Constant b))
-> DLPlate (Constant b) -> DLPlate (Constant b)
forall a b. (a -> b) -> a -> b
$ DLPlate (Constant b)
forall (p :: (* -> *) -> *) (f :: * -> *).
(Multiplate p, Applicative f) =>
p f
purePlate {
  refSec = Constant <$> \(RefProg Contents
c [RefTab]
_) -> [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents
c],
  introSub = Constant <$> \case
    (IOrgSec CI
_ Section
s Sentence
_) -> [Section] -> b
mSec [Section
s]
    IntroSub
_ -> b
forall a. Monoid a => a
mempty,
  --gsdSec = Constant <$> \case
  --  (GSDProg _) -> mempty,
  gsdSub = Constant <$> \case
    (SysCntxt [Contents]
c) -> [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents]
c
    (UsrChars [Contents]
c) -> [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents]
c
    (SystCons [Contents]
c [Section]
s) -> [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents]
c b -> b -> b
forall a. Monoid a => a -> a -> a
`mappend` [Section] -> b
mSec [Section]
s,
  pdSec = Constant <$> \(PDProg Sentence
_ [Section]
s [PDSub]
_) -> [Section] -> b
mSec [Section]
s,
  pdSub = Constant <$> \case
    (TermsAndDefs Maybe Sentence
_ [c]
_) -> b
forall a. Monoid a => a
mempty
    (PhySysDesc a
_ [Sentence]
_ LabelledContent
lc [Contents]
c) -> [LabelledContent] -> b
forall a. HasContents a => [a] -> b
mCon [LabelledContent
lc] b -> b -> b
forall a. Monoid a => a -> a -> a
`mappend` [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents]
c
    (Goals [Sentence]
_ [ConceptInstance]
_) -> b
forall a. Monoid a => a
mempty,
  scsSub = Constant <$> \case
    (Constraints Sentence
_ [c]
c) -> [LabelledContent] -> b
forall a. HasContents a => [a] -> b
mCon [[c] -> LabelledContent
forall c.
(HasUncertainty c, Quantity c, Constrained c, HasReasVal c,
 MayHaveUnit c) =>
[c] -> LabelledContent
inDataConstTbl [c]
c]
    (CorrSolnPpties [c]
c [Contents]
cs) -> [LabelledContent] -> b
forall a. HasContents a => [a] -> b
mCon [[c] -> LabelledContent
forall c. (Quantity c, Constrained c) => [c] -> LabelledContent
outDataConstTbl [c]
c] b -> b -> b
forall a. Monoid a => a -> a -> a
`mappend` [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents]
cs
    SCSSub
_ -> b
forall a. Monoid a => a
mempty,
  reqSub = Constant <$> \case
    (FReqsSub' [ConceptInstance]
_ [LabelledContent]
c) -> [LabelledContent] -> b
forall a. HasContents a => [a] -> b
mCon [LabelledContent]
c
    (FReqsSub [ConceptInstance]
_ [LabelledContent]
c) -> [LabelledContent] -> b
forall a. HasContents a => [a] -> b
mCon [LabelledContent]
c
    (NonFReqsSub [ConceptInstance]
_) -> b
forall a. Monoid a => a
mempty,
  offShelfSec = Constant <$> \(OffShelfSolnsProg [Contents]
c) -> [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents]
c,
  appendSec = Constant <$> \(AppndxProg [Contents]
c) -> [Contents] -> b
forall a. HasContents a => [a] -> b
mCon [Contents]
c
}

-- | Creates a section plate for expressions.
exprPlate :: DLPlate (Constant [ModelExpr])
exprPlate :: DLPlate (Constant [ModelExpr])
exprPlate = ([Sentence] -> [ModelExpr]) -> DLPlate (Constant [ModelExpr])
forall a. Monoid a => ([Sentence] -> a) -> DLPlate (Constant a)
sentencePlate ((Sentence -> [ModelExpr]) -> [Sentence] -> [ModelExpr]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Sentence -> [ModelExpr]
sentToExp) DLPlate (Constant [ModelExpr])
-> DLPlate (Constant [ModelExpr]) -> DLPlate (Constant [ModelExpr])
forall (p :: (* -> *) -> *) o.
(Multiplate p, Monoid o) =>
p (Constant o) -> p (Constant o) -> p (Constant o)
`appendPlate` (forall a. HasContents a => [a] -> [ModelExpr])
-> ([Section] -> [ModelExpr]) -> DLPlate (Constant [ModelExpr])
forall b.
Monoid b =>
(forall a. HasContents a => [a] -> b)
-> ([Section] -> b) -> DLPlate (Constant b)
secConPlate ((a -> [ModelExpr]) -> [a] -> [ModelExpr]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap a -> [ModelExpr]
forall a. HasContents a => a -> [ModelExpr]
egetCon')
  ((Section -> [ModelExpr]) -> [Section] -> [ModelExpr]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Section -> [ModelExpr]
egetSec) DLPlate (Constant [ModelExpr])
-> DLPlate (Constant [ModelExpr]) -> DLPlate (Constant [ModelExpr])
forall (p :: (* -> *) -> *) o.
(Multiplate p, Monoid o) =>
p (Constant o) -> p (Constant o) -> p (Constant o)
`appendPlate` (DLPlate (Constant [ModelExpr]) -> DLPlate (Constant [ModelExpr])
forall (p :: (* -> *) -> *) o.
(Multiplate p, Monoid o) =>
p (Constant o) -> p (Constant o)
preorderFold (DLPlate (Constant [ModelExpr]) -> DLPlate (Constant [ModelExpr]))
-> DLPlate (Constant [ModelExpr]) -> DLPlate (Constant [ModelExpr])
forall a b. (a -> b) -> a -> b
$ DLPlate (Constant [ModelExpr])
forall (p :: (* -> *) -> *) (f :: * -> *).
(Multiplate p, Applicative f) =>
p f
purePlate {
  scsSub = Constant <$> \case
    (TMs [Sentence]
_ Fields
_ [TheoryModel]
t)   -> [TheoryModel] -> [ModelExpr]
goTM [TheoryModel]
t
    (DDs [Sentence]
_ Fields
_ [DataDefinition]
d DerivationDisplay
_) -> [DataDefinition] -> [ModelExpr]
forall a. Express a => [a] -> [ModelExpr]
go [DataDefinition]
d
    (GDs [Sentence]
_ Fields
_ [GenDefn]
g DerivationDisplay
_) -> [GenDefn] -> [ModelExpr]
forall a. Express a => [a] -> [ModelExpr]
go [GenDefn]
g
    (IMs [Sentence]
_ Fields
_ [InstanceModel]
i DerivationDisplay
_) -> [InstanceModel] -> [ModelExpr]
forall a. Express a => [a] -> [ModelExpr]
go [InstanceModel]
i
    SCSSub
_ -> [],
  auxConsSec = Constant <$> \(AuxConsProg CI
_ [ConstQDef]
qdef) -> [ConstQDef] -> [ModelExpr]
forall a. Express a => [a] -> [ModelExpr]
go [ConstQDef]
qdef
  }) where
      go :: Express a => [a] -> [ModelExpr]
      go :: forall a. Express a => [a] -> [ModelExpr]
go = (a -> ModelExpr) -> [a] -> [ModelExpr]
forall a b. (a -> b) -> [a] -> [b]
map a -> ModelExpr
forall c. Express c => c -> ModelExpr
express
      goTM :: [TheoryModel] -> [ModelExpr]
      goTM :: [TheoryModel] -> [ModelExpr]
goTM = (TheoryModel -> [ModelExpr]) -> [TheoryModel] -> [ModelExpr]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\TheoryModel
x -> [ModelQDef] -> [ModelExpr]
forall a. Express a => [a] -> [ModelExpr]
go (TheoryModel
x TheoryModel
-> Getting [ModelQDef] TheoryModel [ModelQDef] -> [ModelQDef]
forall s a. s -> Getting a s a -> a
^. Getting [ModelQDef] TheoryModel [ModelQDef]
forall t. Theory t => Lens' t [ModelQDef]
Lens' TheoryModel [ModelQDef]
defined_quant)
                           [ModelExpr] -> [ModelExpr] -> [ModelExpr]
forall a. [a] -> [a] -> [a]
++ TheoryModel
x TheoryModel
-> Getting [ModelExpr] TheoryModel [ModelExpr] -> [ModelExpr]
forall s a. s -> Getting a s a -> a
^. Getting [ModelExpr] TheoryModel [ModelExpr]
forall t. Theory t => Lens' t [ModelExpr]
Lens' TheoryModel [ModelExpr]
invariants
                           [ModelExpr] -> [ModelExpr] -> [ModelExpr]
forall a. [a] -> [a] -> [a]
++ [ModelExpr] -> [ModelExpr]
forall a. Express a => [a] -> [ModelExpr]
go ((ModelQDef -> ModelExpr) -> [ModelQDef] -> [ModelExpr]
forall a b. (a -> b) -> [a] -> [b]
map (ModelQDef -> Getting ModelExpr ModelQDef ModelExpr -> ModelExpr
forall s a. s -> Getting a s a -> a
^. Getting ModelExpr ModelQDef ModelExpr
forall e. Lens' (QDefinition e) e
forall (c :: * -> *) e. DefiningExpr c => Lens' (c e) e
defnExpr) (TheoryModel
x TheoryModel
-> Getting [ModelQDef] TheoryModel [ModelQDef] -> [ModelQDef]
forall s a. s -> Getting a s a -> a
^. Getting [ModelQDef] TheoryModel [ModelQDef]
forall t. Theory t => Lens' t [ModelQDef]
Lens' TheoryModel [ModelQDef]
defined_quant [ModelQDef] -> [ModelQDef] -> [ModelQDef]
forall a. [a] -> [a] -> [a]
++ TheoryModel
x TheoryModel
-> Getting [ModelQDef] TheoryModel [ModelQDef] -> [ModelQDef]
forall s a. s -> Getting a s a -> a
^. Getting [ModelQDef] TheoryModel [ModelQDef]
forall t. Theory t => Lens' t [ModelQDef]
Lens' TheoryModel [ModelQDef]
defined_fun))
                           [ModelExpr] -> [ModelExpr] -> [ModelExpr]
forall a. [a] -> [a] -> [a]
++ [TheoryModel] -> [ModelExpr]
goTM (TheoryModel
x TheoryModel
-> Getting [TheoryModel] TheoryModel [TheoryModel] -> [TheoryModel]
forall s a. s -> Getting a s a -> a
^. Getting [TheoryModel] TheoryModel [TheoryModel]
forall t. Theory t => Lens' t [TheoryModel]
Lens' TheoryModel [TheoryModel]
valid_context))

-- | Converts a 'Sentence' into a list of expressions. If the 'Sentence' cant be translated, returns an empty list.
sentToExp :: Sentence -> [ModelExpr]
sentToExp :: Sentence -> [ModelExpr]
sentToExp ((:+:) Sentence
s1 Sentence
s2) = Sentence -> [ModelExpr]
sentToExp Sentence
s1 [ModelExpr] -> [ModelExpr] -> [ModelExpr]
forall a. [a] -> [a] -> [a]
++ Sentence -> [ModelExpr]
sentToExp Sentence
s2
sentToExp (E ModelExpr
e) = [ModelExpr
e]
sentToExp Sentence
_ = []

-- | Helper that extracts a list of some type from the 'DLPlate' and 'DocDesc'.
fmGetDocDesc :: DLPlate (Constant [a]) -> DocDesc -> [a]
fmGetDocDesc :: forall a. DLPlate (Constant [a]) -> DocDesc -> [a]
fmGetDocDesc DLPlate (Constant [a])
p = (DocSection -> [a]) -> DocDesc -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Projector DLPlate DocSection
-> DLPlate (Constant [a]) -> DocSection -> [a]
forall (p :: (* -> *) -> *) a o.
Multiplate p =>
Projector p a -> p (Constant o) -> a -> o
foldFor DLPlate f -> DocSection -> f DocSection
Projector DLPlate DocSection
docSec DLPlate (Constant [a])
p)

-- | Extracts expressions from the document description ('DocDesc') and default 'DLPlate'.
egetDocDesc :: DocDesc -> [ModelExpr]
egetDocDesc :: DocDesc -> [ModelExpr]
egetDocDesc = DLPlate (Constant [ModelExpr]) -> DocDesc -> [ModelExpr]
forall a. DLPlate (Constant [a]) -> DocDesc -> [a]
fmGetDocDesc DLPlate (Constant [ModelExpr])
exprPlate

-- | Extracts expressions from a 'Section'.
egetSec :: Section -> [ModelExpr]
egetSec :: Section -> [ModelExpr]
egetSec (Section Sentence
_ [SecCons]
sc Reference
_ ) = (SecCons -> [ModelExpr]) -> [SecCons] -> [ModelExpr]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap SecCons -> [ModelExpr]
egetSecCon [SecCons]
sc

-- | Extracts expressions from section contents.
egetSecCon :: SecCons -> [ModelExpr]
egetSecCon :: SecCons -> [ModelExpr]
egetSecCon (Sub Section
s) = Section -> [ModelExpr]
egetSec Section
s
egetSecCon (Con Contents
c) = Contents -> [ModelExpr]
forall a. HasContents a => a -> [ModelExpr]
egetCon' Contents
c

-- | Extracts expressions from something that has contents.
egetCon' :: HasContents a => a -> [ModelExpr]
egetCon' :: forall a. HasContents a => a -> [ModelExpr]
egetCon' = RawContent -> [ModelExpr]
egetCon (RawContent -> [ModelExpr])
-> (a -> RawContent) -> a -> [ModelExpr]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Getting RawContent a RawContent -> RawContent
forall s a. s -> Getting a s a -> a
^. Getting RawContent a RawContent
forall c. HasContents c => Lens' c RawContent
Lens' a RawContent
accessContents)

-- | Extracts expressions from raw contents.
egetCon :: RawContent -> [ModelExpr]
egetCon :: RawContent -> [ModelExpr]
egetCon (EqnBlock ModelExpr
e) = [ModelExpr
e]
egetCon (Defini DType
_ []) = []
egetCon (Defini DType
dt ((Identifier, [Contents])
hd:[(Identifier, [Contents])]
tl)) = (Contents -> [ModelExpr]) -> [Contents] -> [ModelExpr]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Contents -> [ModelExpr]
forall a. HasContents a => a -> [ModelExpr]
egetCon' ((Identifier, [Contents]) -> [Contents]
forall a b. (a, b) -> b
snd (Identifier, [Contents])
hd) [ModelExpr] -> [ModelExpr] -> [ModelExpr]
forall a. [a] -> [a] -> [a]
++ RawContent -> [ModelExpr]
egetCon (DType -> [(Identifier, [Contents])] -> RawContent
Defini DType
dt [(Identifier, [Contents])]
tl)
egetCon RawContent
_ = []

-- | Creates a 'Sentence' plate.
sentencePlate :: Monoid a => ([Sentence] -> a) -> DLPlate (Constant a)
sentencePlate :: forall a. Monoid a => ([Sentence] -> a) -> DLPlate (Constant a)
sentencePlate [Sentence] -> a
f = DLPlate (Constant a)
-> DLPlate (Constant a) -> DLPlate (Constant a)
forall (p :: (* -> *) -> *) o.
(Multiplate p, Monoid o) =>
p (Constant o) -> p (Constant o) -> p (Constant o)
appendPlate ((forall a. HasContents a => [a] -> a)
-> ([Section] -> a) -> DLPlate (Constant a)
forall b.
Monoid b =>
(forall a. HasContents a => [a] -> b)
-> ([Section] -> b) -> DLPlate (Constant b)
secConPlate ([Sentence] -> a
f ([Sentence] -> a) -> ([a] -> [Sentence]) -> [a] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [Sentence]) -> [a] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap a -> [Sentence]
forall a. HasContents a => a -> [Sentence]
getCon') (([Section] -> a) -> DLPlate (Constant a))
-> ([Section] -> a) -> DLPlate (Constant a)
forall a b. (a -> b) -> a -> b
$ [Sentence] -> a
f ([Sentence] -> a) -> ([Section] -> [Sentence]) -> [Section] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Section -> [Sentence]) -> [Section] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Section -> [Sentence]
getSec) (DLPlate (Constant a) -> DLPlate (Constant a))
-> DLPlate (Constant a) -> DLPlate (Constant a)
forall a b. (a -> b) -> a -> b
$
  DLPlate (Constant a) -> DLPlate (Constant a)
forall (p :: (* -> *) -> *) o.
(Multiplate p, Monoid o) =>
p (Constant o) -> p (Constant o)
preorderFold (DLPlate (Constant a) -> DLPlate (Constant a))
-> DLPlate (Constant a) -> DLPlate (Constant a)
forall a b. (a -> b) -> a -> b
$ DLPlate (Constant a)
forall (p :: (* -> *) -> *) (f :: * -> *).
(Multiplate p, Applicative f) =>
p f
purePlate {
    introSec = Constant . f <$> \(IntroProg Sentence
s1 Sentence
s2 [IntroSub]
_) -> [Sentence
s1, Sentence
s2],
    introSub = Constant . f <$> \case
      (IPurpose [Sentence]
s) -> [Sentence]
s
      (IScope Sentence
s) -> [Sentence
s]
      (IChar [Sentence]
s1 [Sentence]
s2 [Sentence]
s3) -> [[Sentence]] -> [Sentence]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Sentence]
s1, [Sentence]
s2, [Sentence]
s3]
      (IOrgSec CI
_ Section
_ Sentence
s1) -> [Sentence
s1],
    stkSub = Constant . f <$> \case
      (Client CI
_ Sentence
s) -> [Sentence
s]
      (Cstmr CI
_) -> [],
    pdSec = Constant . f <$> \(PDProg Sentence
s [Section]
_ [PDSub]
_) -> [Sentence
s],
    pdSub = Constant . f <$> \case
      (TermsAndDefs Maybe Sentence
Nothing [c]
cs) -> [c] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [c]
cs
      (TermsAndDefs (Just Sentence
s) [c]
cs) -> Sentence
s Sentence -> [Sentence] -> [Sentence]
forall a. a -> [a] -> [a]
: [c] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [c]
cs
      (PhySysDesc a
_ [Sentence]
s LabelledContent
_ [Contents]
_) -> [Sentence]
s
      (Goals [Sentence]
s [ConceptInstance]
c) -> [Sentence]
s [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [ConceptInstance] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConceptInstance]
c,
    scsSub = Constant . f <$> \case
      (Assumptions [ConceptInstance]
c) -> [ConceptInstance] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConceptInstance]
c
      (TMs [Sentence]
s Fields
_ [TheoryModel]
t) -> let r :: [TheoryModel] -> [Sentence]
r = [Sentence] -> [Sentence] -> [Sentence]
forall a. Semigroup a => a -> a -> a
(<>) [Sentence]
s ([Sentence] -> [Sentence])
-> ([TheoryModel] -> [Sentence]) -> [TheoryModel] -> [Sentence]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TheoryModel -> [Sentence]) -> [TheoryModel] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\TheoryModel
x -> [ConceptChunk] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def (TheoryModel
x TheoryModel
-> Getting [ConceptChunk] TheoryModel [ConceptChunk]
-> [ConceptChunk]
forall s a. s -> Getting a s a -> a
^. Getting [ConceptChunk] TheoryModel [ConceptChunk]
forall t. Theory t => Lens' t [ConceptChunk]
Lens' TheoryModel [ConceptChunk]
operations) [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++
                             [ModelQDef] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def (TheoryModel
x TheoryModel
-> Getting [ModelQDef] TheoryModel [ModelQDef] -> [ModelQDef]
forall s a. s -> Getting a s a -> a
^. Getting [ModelQDef] TheoryModel [ModelQDef]
forall t. Theory t => Lens' t [ModelQDef]
Lens' TheoryModel [ModelQDef]
defined_quant) [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [TheoryModel] -> [Sentence]
forall a. HasAdditionalNotes a => [a] -> [Sentence]
notes [TheoryModel
x] [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++
                             [TheoryModel] -> [Sentence]
r (TheoryModel
x TheoryModel
-> Getting [TheoryModel] TheoryModel [TheoryModel] -> [TheoryModel]
forall s a. s -> Getting a s a -> a
^. Getting [TheoryModel] TheoryModel [TheoryModel]
forall t. Theory t => Lens' t [TheoryModel]
Lens' TheoryModel [TheoryModel]
valid_context)) in [TheoryModel] -> [Sentence]
r [TheoryModel]
t
      (DDs [Sentence]
s Fields
_ [DataDefinition]
d DerivationDisplay
_) -> [Sentence]
s [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [DataDefinition] -> [Sentence]
forall a. MayHaveDerivation a => [a] -> [Sentence]
der [DataDefinition]
d [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [DataDefinition] -> [Sentence]
forall a. HasAdditionalNotes a => [a] -> [Sentence]
notes [DataDefinition]
d
      (GDs [Sentence]
s Fields
_ [GenDefn]
d DerivationDisplay
_) -> [GenDefn] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [GenDefn]
d [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [Sentence]
s [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [GenDefn] -> [Sentence]
forall a. MayHaveDerivation a => [a] -> [Sentence]
der [GenDefn]
d [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [GenDefn] -> [Sentence]
forall a. HasAdditionalNotes a => [a] -> [Sentence]
notes [GenDefn]
d
      (IMs [Sentence]
s Fields
_ [InstanceModel]
d DerivationDisplay
_) -> [Sentence]
s [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [InstanceModel] -> [Sentence]
forall a. MayHaveDerivation a => [a] -> [Sentence]
der [InstanceModel]
d [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [InstanceModel] -> [Sentence]
forall a. HasAdditionalNotes a => [a] -> [Sentence]
notes [InstanceModel]
d
      (Constraints Sentence
s [c]
_) -> [Sentence
s]
      (CorrSolnPpties [c]
_ [Contents]
_) -> [],
    reqSub = Constant . f <$> \case
      (FReqsSub' [ConceptInstance]
c [LabelledContent]
_) -> [ConceptInstance] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConceptInstance]
c
      (FReqsSub [ConceptInstance]
c [LabelledContent]
_) -> [ConceptInstance] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConceptInstance]
c
      (NonFReqsSub [ConceptInstance]
c) -> [ConceptInstance] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConceptInstance]
c,
    lcsSec = Constant . f <$> \(LCsProg [ConceptInstance]
c) -> [ConceptInstance] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConceptInstance]
c,
    ucsSec = Constant . f <$> \(UCsProg [ConceptInstance]
c) -> [ConceptInstance] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConceptInstance]
c,
    traceSec = Constant . f <$> \(TraceabilityProg [TraceConfig]
progs) ->
      (TraceConfig -> [Sentence]) -> [TraceConfig] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(TraceConfig UID
_ [Sentence]
ls Sentence
s [TraceViewCat]
_ [TraceViewCat]
_) -> Sentence
s Sentence -> [Sentence] -> [Sentence]
forall a. a -> [a] -> [a]
: [Sentence]
ls) [TraceConfig]
progs,
    auxConsSec = Constant . f <$> \(AuxConsProg CI
_ [ConstQDef]
qdef) -> [ConstQDef] -> [Sentence]
forall a. Definition a => [a] -> [Sentence]
def [ConstQDef]
qdef
  } where
    def :: Definition a => [a] -> [Sentence]
    def :: forall a. Definition a => [a] -> [Sentence]
def = (a -> Sentence) -> [a] -> [Sentence]
forall a b. (a -> b) -> [a] -> [b]
map (a -> Getting Sentence a Sentence -> Sentence
forall s a. s -> Getting a s a -> a
^. Getting Sentence a Sentence
forall c. Definition c => Lens' c Sentence
Lens' a Sentence
defn)
    der :: MayHaveDerivation a => [a] -> [Sentence]
    der :: forall a. MayHaveDerivation a => [a] -> [Sentence]
der = (a -> [Sentence]) -> [a] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Maybe Derivation -> [Sentence]
getDerivSent (Maybe Derivation -> [Sentence])
-> (a -> Maybe Derivation) -> a -> [Sentence]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
-> Getting (Maybe Derivation) a (Maybe Derivation)
-> Maybe Derivation
forall s a. s -> Getting a s a -> a
^. Getting (Maybe Derivation) a (Maybe Derivation)
forall c. MayHaveDerivation c => Lens' c (Maybe Derivation)
Lens' a (Maybe Derivation)
derivations))
    getDerivSent :: Maybe Derivation -> [Sentence]
    getDerivSent :: Maybe Derivation -> [Sentence]
getDerivSent Maybe Derivation
Nothing = []
    getDerivSent (Just (Derivation Sentence
h [Sentence]
s)) = Sentence
h Sentence -> [Sentence] -> [Sentence]
forall a. a -> [a] -> [a]
: [Sentence]
s
    notes :: HasAdditionalNotes a => [a] -> [Sentence]
    notes :: forall a. HasAdditionalNotes a => [a] -> [Sentence]
notes = (a -> [Sentence]) -> [a] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (a -> Getting [Sentence] a [Sentence] -> [Sentence]
forall s a. s -> Getting a s a -> a
^. Getting [Sentence] a [Sentence]
forall c. HasAdditionalNotes c => Lens' c [Sentence]
Lens' a [Sentence]
getNotes)

-- | Extracts 'Sentence's from a document description.
getDocDesc :: DocDesc -> [Sentence]
getDocDesc :: DocDesc -> [Sentence]
getDocDesc = DLPlate (Constant [Sentence]) -> DocDesc -> [Sentence]
forall a. DLPlate (Constant [a]) -> DocDesc -> [a]
fmGetDocDesc (([Sentence] -> [Sentence]) -> DLPlate (Constant [Sentence])
forall a. Monoid a => ([Sentence] -> a) -> DLPlate (Constant a)
sentencePlate [Sentence] -> [Sentence]
forall a. a -> a
id)

-- | Extracts 'Sentence's from a 'Section'.
getSec :: Section -> [Sentence]
getSec :: Section -> [Sentence]
getSec (Section Sentence
t [SecCons]
sc Reference
_ ) = Sentence
t Sentence -> [Sentence] -> [Sentence]
forall a. a -> [a] -> [a]
: (SecCons -> [Sentence]) -> [SecCons] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap SecCons -> [Sentence]
getSecCon [SecCons]
sc

-- | Extracts 'Sentence's from section contents.
getSecCon :: SecCons -> [Sentence]
getSecCon :: SecCons -> [Sentence]
getSecCon (Sub Section
s) = Section -> [Sentence]
getSec Section
s
getSecCon (Con Contents
c) = Contents -> [Sentence]
forall a. HasContents a => a -> [Sentence]
getCon' Contents
c

-- | Extracts 'Sentence's from something that has contents.
getCon' :: HasContents a => a -> [Sentence]
getCon' :: forall a. HasContents a => a -> [Sentence]
getCon' = RawContent -> [Sentence]
getCon (RawContent -> [Sentence]) -> (a -> RawContent) -> a -> [Sentence]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Getting RawContent a RawContent -> RawContent
forall s a. s -> Getting a s a -> a
^. Getting RawContent a RawContent
forall c. HasContents c => Lens' c RawContent
Lens' a RawContent
accessContents)

-- | Extracts 'Sentence's from raw content.
getCon :: RawContent -> [Sentence]
getCon :: RawContent -> [Sentence]
getCon (Table [Sentence]
s1 [[Sentence]]
s2 Sentence
t Bool
_) = ([Sentence], [[Sentence]]) -> [Sentence]
isVar ([Sentence]
s1, [[Sentence]] -> [[Sentence]]
forall a. [[a]] -> [[a]]
transpose [[Sentence]]
s2) [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ [Sentence
t]
getCon (Paragraph Sentence
s)       = [Sentence
s]
getCon EqnBlock{}          = []
getCon CodeBlock{}         = []
getCon (DerivBlock Sentence
h [RawContent]
d)    = Sentence
h Sentence -> [Sentence] -> [Sentence]
forall a. a -> [a] -> [a]
: (RawContent -> [Sentence]) -> [RawContent] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap RawContent -> [Sentence]
getCon [RawContent]
d
getCon (Enumeration ListType
lst)   = ListType -> [Sentence]
getLT ListType
lst
getCon (Figure Sentence
l Identifier
_ MaxWidthPercent
_ HasCaption
_)    = [Sentence
l]
getCon (Bib BibRef
bref)          = BibRef -> [Sentence]
forall c. HasFields c => [c] -> [Sentence]
getBib BibRef
bref
getCon (Graph [(Sentence
s1, Sentence
s2)] Maybe MaxWidthPercent
_ Maybe MaxWidthPercent
_ Sentence
l) = [Sentence
s1, Sentence
s2, Sentence
l]
getCon Graph{}             = []
getCon (Defini DType
_ [])       = []
getCon (Defini DType
dt ((Identifier, [Contents])
hd:[(Identifier, [Contents])]
fs)) = (Contents -> [Sentence]) -> [Contents] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Contents -> [Sentence]
forall a. HasContents a => a -> [Sentence]
getCon' ((Identifier, [Contents]) -> [Contents]
forall a b. (a, b) -> b
snd (Identifier, [Contents])
hd) [Sentence] -> [Sentence] -> [Sentence]
forall a. [a] -> [a] -> [a]
++ RawContent -> [Sentence]
getCon (DType -> [(Identifier, [Contents])] -> RawContent
Defini DType
dt [(Identifier, [Contents])]
fs)

-- | This function is used in collecting 'Sentence's from a table.
-- Since only the table's first Column titled "Var" should be collected,
-- this function is used to filter out only the first column of 'Sentence's.
isVar :: ([Sentence], [[Sentence]]) -> [Sentence]
isVar :: ([Sentence], [[Sentence]]) -> [Sentence]
isVar (S Identifier
"Var" : [Sentence]
_, [Sentence]
hd1 : [[Sentence]]
_) = [Sentence]
hd1
isVar (Sentence
_ : [Sentence]
tl, [Sentence]
_ : [[Sentence]]
tl1) = ([Sentence], [[Sentence]]) -> [Sentence]
isVar ([Sentence]
tl, [[Sentence]]
tl1)
isVar ([], [[Sentence]]
_) = []
isVar ([Sentence]
_, []) = []

-- | Get the bibliography from something that has a field.
getBib :: (HasFields c) => [c] -> [Sentence]
getBib :: forall c. HasFields c => [c] -> [Sentence]
getBib [c]
a = (CiteField -> Sentence) -> [CiteField] -> [Sentence]
forall a b. (a -> b) -> [a] -> [b]
map CiteField -> Sentence
getField ([CiteField] -> [Sentence]) -> [CiteField] -> [Sentence]
forall a b. (a -> b) -> a -> b
$ (c -> [CiteField]) -> [c] -> [CiteField]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (c -> Getting [CiteField] c [CiteField] -> [CiteField]
forall s a. s -> Getting a s a -> a
^. Getting [CiteField] c [CiteField]
forall c. HasFields c => Lens' c [CiteField]
Lens' c [CiteField]
getFields) [c]
a

-- | Unwraps a 'CiteField' into a 'Sentence'.
getField :: CiteField -> Sentence
getField :: CiteField -> Sentence
getField (Address Identifier
s) = Identifier -> Sentence
S Identifier
s
getField Author{} = Sentence
EmptyS
getField (BookTitle Identifier
s) = Identifier -> Sentence
S Identifier
s
getField Chapter{} = Sentence
EmptyS
getField Edition{} = Sentence
EmptyS
getField Editor{} = Sentence
EmptyS
getField HowPublished{} = Sentence
EmptyS
getField (Institution Identifier
s) = Identifier -> Sentence
S Identifier
s
getField (Journal Identifier
s) = Identifier -> Sentence
S Identifier
s
getField Month{} = Sentence
EmptyS
getField (Note Identifier
s) = Identifier -> Sentence
S Identifier
s
getField Number{} = Sentence
EmptyS
getField (Organization Identifier
s) = Identifier -> Sentence
S Identifier
s
getField Pages{} = Sentence
EmptyS
getField (Publisher Identifier
s) = Identifier -> Sentence
S Identifier
s
getField (School Identifier
s) = Identifier -> Sentence
S Identifier
s
getField (Series Identifier
s) = Identifier -> Sentence
S Identifier
s
getField (Title Identifier
s) = Identifier -> Sentence
S Identifier
s
getField (Type Identifier
s) = Identifier -> Sentence
S Identifier
s
getField Volume{} = Sentence
EmptyS
getField Year{} = Sentence
EmptyS

-- | Translates different types of lists into a 'Sentence' form.
getLT :: ListType -> [Sentence]
getLT :: ListType -> [Sentence]
getLT (Bullet [(ItemType, Maybe Identifier)]
it) = ((ItemType, Maybe Identifier) -> [Sentence])
-> [(ItemType, Maybe Identifier)] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ItemType -> [Sentence]
getIL (ItemType -> [Sentence])
-> ((ItemType, Maybe Identifier) -> ItemType)
-> (ItemType, Maybe Identifier)
-> [Sentence]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ItemType, Maybe Identifier) -> ItemType
forall a b. (a, b) -> a
fst) [(ItemType, Maybe Identifier)]
it
getLT (Numeric [(ItemType, Maybe Identifier)]
it) = ((ItemType, Maybe Identifier) -> [Sentence])
-> [(ItemType, Maybe Identifier)] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ItemType -> [Sentence]
getIL (ItemType -> [Sentence])
-> ((ItemType, Maybe Identifier) -> ItemType)
-> (ItemType, Maybe Identifier)
-> [Sentence]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ItemType, Maybe Identifier) -> ItemType
forall a b. (a, b) -> a
fst) [(ItemType, Maybe Identifier)]
it
getLT (Simple [ListTuple]
lp) = (ListTuple -> [Sentence]) -> [ListTuple] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ListTuple -> [Sentence]
getLP [ListTuple]
lp
getLT (Desc [ListTuple]
lp) = (ListTuple -> [Sentence]) -> [ListTuple] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ListTuple -> [Sentence]
getLP [ListTuple]
lp
getLT (Definitions [ListTuple]
lp) = (ListTuple -> [Sentence]) -> [ListTuple] -> [Sentence]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ListTuple -> [Sentence]
getLP [ListTuple]
lp

-- | Translates a 'ListTuple' into 'Sentence's.
getLP :: ListTuple -> [Sentence]
getLP :: ListTuple -> [Sentence]
getLP (Sentence
t, ItemType
it, Maybe Identifier
_) = Sentence
t Sentence -> [Sentence] -> [Sentence]
forall a. a -> [a] -> [a]
: ItemType -> [Sentence]
getIL ItemType
it

-- | Flattens out an ItemType into 'Sentence's. Headers for 'Nested' items are prepended to its contents.
getIL :: ItemType -> [Sentence]
getIL :: ItemType -> [Sentence]
getIL (Flat Sentence
s) = [Sentence
s]
getIL (Nested Sentence
h ListType
lt) = Sentence
h Sentence -> [Sentence] -> [Sentence]
forall a. a -> [a] -> [a]
: ListType -> [Sentence]
getLT ListType
lt

-- ciPlate is not currently used. 
-- | A common idea plate.
-- ciPlate :: DLPlate (Constant [CI])
-- ciPlate = preorderFold $ purePlate {
--   introSub = Constant <$> \case
--     (IOrgSec _ ci _ _) -> [ci]
--     _ -> [],
--   stkSub = Constant <$> \case
--    (Client ci _) -> [ci]
--    (Cstmr ci) -> [ci],
--    auxConsSec = Constant <$> \(AuxConsProg ci _) -> [ci]
-- }