{-# LANGUAGE RankNTypes #-}
module Language.Drasil.Debug.Print where

import           Language.Drasil hiding (symbol)
import           Language.Drasil.Development (showUID)
import qualified Language.Drasil as L (symbol)
import           Database.Drasil
import           Utils.Drasil (stringList)
import qualified Data.Map as Map
import           Control.Lens ((^.), view)
import           Data.List (nub, sort, sortBy)
import           Data.Maybe (fromMaybe)
import           Data.Bifunctor (second)
import           Data.Function (on)
import           Text.PrettyPrint.HughesPJ
import           Language.Drasil.Plain.Print
import           Language.Drasil.Printing.PrintingInformation
import           Prelude hiding ((<>))

-- * Main Function
-- | Gathers all printing functions and creates the debugging tables from them.
printAllDebugInfo :: PrintingInformation -> [Doc]
printAllDebugInfo :: PrintingInformation -> [Doc]
printAllDebugInfo PrintingInformation
pinfo = forall a b. (a -> b) -> [a] -> [b]
map
  (Doc -> Doc
cdbSection forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. (a -> b) -> a -> b
$ PrintingInformation
pinfo))
  [ PrintingInformation -> Doc
mkTableReferencedChunks
  , PrintingInformation -> Doc
mkTableDepChunks
  , PrintingInformation -> Doc
mkTableDepReffedChunks
  , PrintingInformation -> Doc
mkTableSymb
  , PrintingInformation -> Doc
mkTableOfTerms
  , PrintingInformation -> Doc
mkTableConcepts
  , PrintingInformation -> Doc
mkTableUnitDefn
  , PrintingInformation -> Doc
mkTableDataDef
  , PrintingInformation -> Doc
mkTableGenDef
  , PrintingInformation -> Doc
mkTableTMod
  , PrintingInformation -> Doc
mkTableIMod
  , PrintingInformation -> Doc
mkTableCI
  , PrintingInformation -> Doc
mkTableSec
  , PrintingInformation -> Doc
mkTableLC
  , PrintingInformation -> Doc
mkTableRef
  , [(UID, String)] -> Doc
renderUsedUIDs forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrintingInformation -> [(UID, String)]
mkListShowUsedUIDs]

-- * Helpers
-- ** Separators
-- | Debugging table separator.
cdbSection :: Doc -> Doc
cdbSection :: Doc -> Doc
cdbSection Doc
dd = String -> Doc
text (forall a. Int -> a -> [a]
replicate Int
100 Char
'#' forall a. [a] -> [a] -> [a]
++ String
"\n") Doc -> Doc -> Doc
$$ Doc
dd Doc -> Doc -> Doc
$$ String -> Doc
text String
"\n"

-- | Header for debugging tables.
header :: Doc -> Doc
header :: Doc -> Doc
header Doc
d = String -> Doc
text (forall a. Int -> a -> [a]
replicate Int
100 Char
'-') Doc -> Doc -> Doc
$$ Doc
d Doc -> Doc -> Doc
$$ String -> Doc
text (forall a. Int -> a -> [a]
replicate Int
100 Char
'-')

-- ** Table Generators

-- | General function to make the debugging tables. Takes in printing
-- information, a function that extracts a certain field from the printing
-- information, a title, three column headers, and three functions that sort the
-- data from the printing information field into the required display formats
-- (often 'UID's, terms, shortnames, definitions, etc.).
mkTableFromLenses
  :: HasUID a => PrintingInformation
  -> (ChunkDB -> UMap a)
  -> String
  -> [PrintingInformation -> (String, a -> Doc)]
  -> Doc
mkTableFromLenses :: forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses pin :: PrintingInformation
pin@PI { _ckdb :: PrintingInformation -> ChunkDB
_ckdb = ChunkDB
db } ChunkDB -> UMap a
tableLens String
ttle [PrintingInformation -> (String, a -> Doc)]
hsNEs =
  String -> Doc
text String
ttle Doc -> Doc -> Doc
<> Doc
colon
  Doc -> Doc -> Doc
$$ Doc -> Doc
header Doc
hdr
  Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map a -> Doc
col [a]
chunks)
  where
    namedLenses :: [(String, a -> Doc)]
namedLenses = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> a -> b
$ PrintingInformation
pin) [PrintingInformation -> (String, a -> Doc)]
hsNEs
    ins :: [Int]
    ins :: [Int]
ins = [Int
1..]

    hdr :: Doc
hdr   = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Doc
r (String, Int)
l -> Doc
r Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest (Int
nestNum forall a. Num a => a -> a -> a
* forall a b. (a, b) -> b
snd (String, Int)
l) (String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst (String, Int)
l)) (String -> Doc
text String
"UID")       (forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(String, a -> Doc)]
namedLenses) [Int]
ins)
    col :: a -> Doc
col a
a = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Doc
r (a -> Doc, Int)
l -> Doc
r Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest (Int
nestNum forall a. Num a => a -> a -> a
* forall a b. (a, b) -> b
snd (a -> Doc, Int)
l) (forall a b. (a, b) -> a
fst (a -> Doc, Int)
l a
a)     ) (String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall a. HasUID a => a -> String
showUID a
a) (forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(String, a -> Doc)]
namedLenses) [Int]
ins)

    chunks :: [a]
chunks = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB -> UMap a
tableLens ChunkDB
db)

    nestNum :: Int
nestNum = Int
30

openTerm :: NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm :: forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm PrintingInformation
pinfo = (String
"Term", ChunkDB -> Stage -> SingleLine -> Sentence -> Doc
sentenceDoc (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation ChunkDB
ckdb) (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation Stage
stg) SingleLine
MultiLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. NounPhrase n => n -> Sentence
phraseNP forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall c. NamedIdea c => Lens' c NP
term)

openSymbol :: HasSymbol a =>PrintingInformation -> (String, a -> Doc)
openSymbol :: forall a. HasSymbol a => PrintingInformation -> (String, a -> Doc)
openSymbol PrintingInformation
pinfo = (String
"Symbol", Symbol -> Doc
symbolDoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall c. HasSymbol c => c -> Stage -> Symbol
L.symbol (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation Stage
stg))

openDefSymbol :: DefinesQuantity s => PrintingInformation -> (String, s -> Doc)
openDefSymbol :: forall s.
DefinesQuantity s =>
PrintingInformation -> (String, s -> Doc)
openDefSymbol PrintingInformation
pinfo = (String
"Symbol Defining", Symbol -> Doc
symbolDoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall c. HasSymbol c => c -> Stage -> Symbol
L.symbol (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation Stage
stg) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall d. DefinesQuantity d => Getter d QuantityDict
defLhs)

openAbbreviation :: Idea a => PrintingInformation -> (String, a -> Doc)
openAbbreviation :: forall a. Idea a => PrintingInformation -> (String, a -> Doc)
openAbbreviation PrintingInformation
_ = (String
"Abbreviation", String -> Doc
text forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe String
"" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. Idea c => c -> Maybe String
getA)

openDefinition :: Definition a => PrintingInformation -> (String, a -> Doc)
openDefinition :: forall a. Definition a => PrintingInformation -> (String, a -> Doc)
openDefinition PrintingInformation
pinfo = (String
"Definition", ChunkDB -> Stage -> SingleLine -> Sentence -> Doc
sentenceDoc (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation ChunkDB
ckdb) (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation Stage
stg) SingleLine
OneLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall c. Definition c => Lens' c Sentence
defn)

openUnitSymbol :: HasUnitSymbol a => PrintingInformation -> (String, a -> Doc)
openUnitSymbol :: forall a.
HasUnitSymbol a =>
PrintingInformation -> (String, a -> Doc)
openUnitSymbol PrintingInformation
pinfo = (String
"Unit Symbol", ChunkDB -> Stage -> SingleLine -> Sentence -> Doc
sentenceDoc (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation ChunkDB
ckdb) (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation Stage
stg) SingleLine
OneLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. USymb -> Sentence
Sy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall u. HasUnitSymbol u => u -> USymb
usymb)

openShortName :: HasShortName a => PrintingInformation -> (String, a -> Doc)
openShortName :: forall a.
HasShortName a =>
PrintingInformation -> (String, a -> Doc)
openShortName PrintingInformation
pinfo = (String
"Short Name", ChunkDB -> Stage -> SingleLine -> Sentence -> Doc
sentenceDoc (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation ChunkDB
ckdb) (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation Stage
stg) SingleLine
OneLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortName -> Sentence
getSentSN forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. HasShortName s => s -> ShortName
shortname)

openTitle :: PrintingInformation -> (String, Section -> Doc)
openTitle :: PrintingInformation -> (String, Section -> Doc)
openTitle PrintingInformation
pinfo = (String
"Title", ChunkDB -> Stage -> SingleLine -> Sentence -> Doc
sentenceDoc (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation ChunkDB
ckdb) (PrintingInformation
pinfo forall s a. s -> Getting a s a -> a
^. Lens' PrintingInformation Stage
stg) SingleLine
MultiLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. Section -> Sentence
tle)

cntntToStr :: RawContent -> String
cntntToStr :: RawContent -> String
cntntToStr Table {} = String
"Table"
cntntToStr Paragraph {} = String
"Paragraph"
cntntToStr EqnBlock {} = String
"Equation"
cntntToStr DerivBlock {} = String
"Derivation"
cntntToStr Enumeration {} = String
"Enumeration"
cntntToStr Defini {} = String
"Definition or Model"
cntntToStr Figure {} = String
"Figure"
cntntToStr Bib {} = String
"Bibliography"
cntntToStr Graph {} = String
"Graph"
cntntToStr CodeBlock {} = String
"Code"

openContentType :: HasContents s => p -> (String, s -> Doc)
openContentType :: forall s p. HasContents s => p -> (String, s -> Doc)
openContentType p
_ = (String
"Content Type", String -> Doc
text forall b c a. (b -> c) -> (a -> b) -> a -> c
. RawContent -> String
cntntToStr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall c. HasContents c => Lens' c RawContent
accessContents)

openRef :: HasRefAddress a => p -> (String, a -> Doc)
openRef :: forall a p. HasRefAddress a => p -> (String, a -> Doc)
openRef p
_ = (String
"Reference Address", String -> Doc
text forall b c a. (b -> c) -> (a -> b) -> a -> c
. LblType -> String
getAdd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b. HasRefAddress b => b -> LblType
getRefAdd)

-- | Makes a table with all symbolic quantities in the SRS.
mkTableSymb :: PrintingInformation -> Doc
mkTableSymb :: PrintingInformation -> Doc
mkTableSymb PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  ChunkDB -> SymbolMap
symbolTable
  String
"Symbol Chunks"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a. HasSymbol a => PrintingInformation -> (String, a -> Doc)
openSymbol]

-- | Makes a table with terms in the SRS.
mkTableOfTerms :: PrintingInformation -> Doc
mkTableOfTerms :: PrintingInformation -> Doc
mkTableOfTerms PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  ChunkDB -> TermMap
termTable
  String
"Term Chunks"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a. Idea a => PrintingInformation -> (String, a -> Doc)
openAbbreviation]

-- | Makes a table with all concepts in the SRS.
mkTableConcepts :: PrintingInformation -> Doc
mkTableConcepts :: PrintingInformation -> Doc
mkTableConcepts PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  ChunkDB -> ConceptMap
defTable
  String
"Concepts"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a. Definition a => PrintingInformation -> (String, a -> Doc)
openDefinition]

-- | Makes a table with all units used in the SRS.
mkTableUnitDefn :: PrintingInformation -> Doc
mkTableUnitDefn :: PrintingInformation -> Doc
mkTableUnitDefn PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (UnitDefn, Int))
unitTable)
  String
"Unit Definitions"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a.
HasUnitSymbol a =>
PrintingInformation -> (String, a -> Doc)
openUnitSymbol]

-- | Makes a table with all data definitions in the SRS.
mkTableDataDef :: PrintingInformation -> Doc
mkTableDataDef :: PrintingInformation -> Doc
mkTableDataDef PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (DataDefinition, Int))
dataDefnTable)
  String
"Data Definitions"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall s.
DefinesQuantity s =>
PrintingInformation -> (String, s -> Doc)
openDefSymbol]

-- | Makes a table with all general definitions in the SRS.
mkTableGenDef :: PrintingInformation -> Doc
mkTableGenDef :: PrintingInformation -> Doc
mkTableGenDef PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (GenDefn, Int))
gendefTable)
  String
"General Definitions"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a. Definition a => PrintingInformation -> (String, a -> Doc)
openDefinition]

-- | Makes a table with all theoretical models in the SRS.
mkTableTMod :: PrintingInformation -> Doc
mkTableTMod :: PrintingInformation -> Doc
mkTableTMod PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (TheoryModel, Int))
theoryModelTable)
  String
"Theory Models"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a. Definition a => PrintingInformation -> (String, a -> Doc)
openDefinition]

-- | Makes a table with all instance models in the SRS.
mkTableIMod :: PrintingInformation -> Doc
mkTableIMod :: PrintingInformation -> Doc
mkTableIMod PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (InstanceModel, Int))
insmodelTable)
  String
"Instance Models"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a. Definition a => PrintingInformation -> (String, a -> Doc)
openDefinition]

-- | Makes a table with all concept instances in the SRS.
mkTableCI :: PrintingInformation -> Doc
mkTableCI :: PrintingInformation -> Doc
mkTableCI PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (ConceptInstance, Int))
conceptinsTable)
  String
"ConceptInstance"
  [forall a. NamedIdea a => PrintingInformation -> (String, a -> Doc)
openTerm, forall a.
HasShortName a =>
PrintingInformation -> (String, a -> Doc)
openShortName]

-- | Makes a table with all sections in the SRS.
mkTableSec :: PrintingInformation -> Doc
mkTableSec :: PrintingInformation -> Doc
mkTableSec PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (Section, Int))
sectionTable)
  String
"Sections"
  [PrintingInformation -> (String, Section -> Doc)
openTitle, forall a.
HasShortName a =>
PrintingInformation -> (String, a -> Doc)
openShortName]

-- | Makes a table with all labelled content in the SRS.
mkTableLC :: PrintingInformation -> Doc
mkTableLC :: PrintingInformation -> Doc
mkTableLC PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (LabelledContent, Int))
labelledcontentTable)
  String
"LabelledContent"
  [forall a.
HasShortName a =>
PrintingInformation -> (String, a -> Doc)
openShortName, forall s p. HasContents s => p -> (String, s -> Doc)
openContentType]

-- | Makes a table with all references in the SRS.
mkTableRef :: PrintingInformation -> Doc
mkTableRef :: PrintingInformation -> Doc
mkTableRef PrintingInformation
pinfo = forall a.
HasUID a =>
PrintingInformation
-> (ChunkDB -> UMap a)
-> String
-> [PrintingInformation -> (String, a -> Doc)]
-> Doc
mkTableFromLenses
  PrintingInformation
pinfo
  (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ChunkDB (Map UID (Reference, Int))
refTable)
  String
"Reference"
  [forall a p. HasRefAddress a => p -> (String, a -> Doc)
openRef, forall a.
HasShortName a =>
PrintingInformation -> (String, a -> Doc)
openShortName]

-- | Chunks that depend on other chunks. An empty list means the chunks do not depend on anything.
mkTableDepChunks :: PrintingInformation -> Doc
mkTableDepChunks :: PrintingInformation -> Doc
mkTableDepChunks PI { _ckdb :: PrintingInformation -> ChunkDB
_ckdb = ChunkDB
db } = String -> Doc
text
  String
"Dependent Chunks (the chunks on the left use the chunks on the right in some capacity)"
  Doc -> Doc -> Doc
<> Doc
colon
  Doc -> Doc -> Doc
$$ Doc -> Doc
header (String -> Doc
text String
"UID" Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
nestNum (String -> Doc
text String
"Dependent UIDs"))
  Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (UID, [UID]) -> Doc
testIndepLayout [(UID, [UID])]
traceMapUIDs)
  where
    testIndepLayout :: (UID, [UID]) -> Doc
    testIndepLayout :: (UID, [UID]) -> Doc
testIndepLayout (UID
x, [UID]
ys) = String -> Doc
text (forall a. Show a => a -> String
show UID
x) Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
nestNum (String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show [UID]
ys)

    traceMapUIDs :: [(UID, [UID])]
    traceMapUIDs :: [(UID, [UID])]
traceMapUIDs = forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID [UID])
traceTable

    nestNum :: Int
nestNum = Int
30

-- | Chunks that are referenced and used by other chunks.
-- Those chunks build on top of the ones listed here.
mkTableReferencedChunks :: PrintingInformation -> Doc
mkTableReferencedChunks :: PrintingInformation -> Doc
mkTableReferencedChunks PI { _ckdb :: PrintingInformation -> ChunkDB
_ckdb = ChunkDB
db } =
  String -> Doc
text String
"Referenced Chunks (other chunks build from these)" Doc -> Doc -> Doc
<> Doc
colon
  Doc -> Doc -> Doc
$$ Doc -> Doc
header (String -> Doc
text String
"UID" Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
nestNum (String -> Doc
text String
"UIDs that use the left UID"))
  Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (UID, [UID]) -> Doc
testIsolateLayout [(UID, [UID])]
refbyUIDs)
  where
    testIsolateLayout :: (UID, [UID]) -> Doc
    testIsolateLayout :: (UID, [UID]) -> Doc
testIsolateLayout (UID
x, [UID]
ys) = String -> Doc
text (forall a. Show a => a -> String
show UID
x) Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
nestNum (String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show [UID]
ys)

    refbyUIDs :: [(UID, [UID])]
    refbyUIDs :: [(UID, [UID])]
refbyUIDs = forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID [UID])
refbyTable

    nestNum :: Int
nestNum = Int
30

-- | Chunks that use and are used by other chunks.
mkTableDepReffedChunks :: PrintingInformation -> Doc
mkTableDepReffedChunks :: PrintingInformation -> Doc
mkTableDepReffedChunks PI { _ckdb :: PrintingInformation -> ChunkDB
_ckdb = ChunkDB
db } = String -> Doc
text
  String
"Dependent and Referenced Chunks (chunks dependent on middle UIDs and used in the chunks on the right)"
  Doc -> Doc -> Doc
<> Doc
colon
  Doc -> Doc -> Doc
$$ Doc -> Doc
header
    (String -> Doc
text String
"UID"
     Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
nestNum (String -> Doc
text String
"Dependent Chunk")
     Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest (Int
nestNum forall a. Num a => a -> a -> a
* Int
3) (String -> Doc
text String
"Used-in Chunk"))
  Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (UID, ([UID], [UID])) -> Doc
traceRefLayout forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.assocs Map UID ([UID], [UID])
combinedMaps)
  where
    traceRefLayout :: (UID, ([UID], [UID])) -> Doc
    traceRefLayout :: (UID, ([UID], [UID])) -> Doc
traceRefLayout (UID, ([UID], [UID]))
x = String -> Doc
text (forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst (UID, ([UID], [UID]))
x)
      Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
nestNum (String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd (UID, ([UID], [UID]))
x)
      Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest (Int
nestNum forall a. Num a => a -> a -> a
* Int
3) (String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd (UID, ([UID], [UID]))
x)

    combinedMaps :: Map UID ([UID], [UID])
combinedMaps =
      forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith (\([UID], [UID])
x ([UID], [UID])
y -> (forall a b. (a, b) -> a
fst ([UID], [UID])
x, forall a b. (a, b) -> b
snd ([UID], [UID])
y)) forall {a}. Map UID ([UID], [a])
traceMapUIDs forall {a}. Map UID ([a], [UID])
refByUIDs

    traceMapUIDs :: Map UID ([UID], [a])
traceMapUIDs = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
      forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(UID
x, [UID]
y) -> (UID
x, ([UID]
y, [])))
      forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.assocs
      forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID [UID])
traceTable

    refByUIDs :: Map UID ([a], [UID])
refByUIDs = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
      forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(UID
x, [UID]
y) -> (UID
x, ([], [UID]
y)))
      forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.assocs
      forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID [UID])
refbyTable

    nestNum :: Int
nestNum = Int
30

-- ** 'UID' Manipulation
-- | Creates a table of all UIDs and their "highest" recorded level of information. See 'mkListShowUsedUIDs'
-- for more details.
renderUsedUIDs :: [(UID, String)] -> Doc
renderUsedUIDs :: [(UID, String)] -> Doc
renderUsedUIDs [(UID, String)]
chs = Doc -> Doc
header (String -> Doc
text String
"UIDs" Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
40 (String -> Doc
text String
"Associated Chunks"))
  Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map forall {a}. Show a => (a, String) -> Doc
renderUsedUID [(UID, String)]
chs)
  where
    renderUsedUID :: (a, String) -> Doc
renderUsedUID (a
u, String
chks) = String -> Doc
text (forall a. Show a => a -> String
show a
u) Doc -> Doc -> Doc
$$ Int -> Doc -> Doc
nest Int
40 (String -> Doc
text String
chks)

-- | For the last section of the log output. Shows which chunk UID is being used at which stage.
-- Note that chunks used at a "higher stage" (like 'Concept's and 'QuantityDict's) will still be built off of the
-- more basic types (like 'IdeaDict's), they are just not explicitly used in that manner.
-- Also, some chunks may have been "downgraded" when put into the database (for example, mapping a
-- 'QuantityDict' wrapper onto things like Constrained and Unital chunks happens often).
mkListShowUsedUIDs :: PrintingInformation -> [(UID, String)]
mkListShowUsedUIDs :: PrintingInformation -> [(UID, String)]
mkListShowUsedUIDs PI { _ckdb :: PrintingInformation -> ChunkDB
_ckdb = ChunkDB
db } = forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a b. (a, b) -> a
fst)
  forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second [String] -> String
stringList)
  forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.toList
  forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith forall a. [a] -> [a] -> [a]
(++)
  forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (QuantityDict, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (QuantityDict, Int))
x, [String
"QuantityDict"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB -> SymbolMap
symbolTable ChunkDB
db)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (IdeaDict, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (IdeaDict, Int))
x, [String
"IdeaDict"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB -> TermMap
termTable ChunkDB
db)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (ConceptChunk, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (ConceptChunk, Int))
x, [String
"ConceptChunk"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB -> ConceptMap
defTable ChunkDB
db)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (UnitDefn, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (UnitDefn, Int))
x, [String
"UnitDefn"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (UnitDefn, Int))
unitTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (DataDefinition, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (DataDefinition, Int))
x, [String
"DataDefinition"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (DataDefinition, Int))
dataDefnTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (InstanceModel, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (InstanceModel, Int))
x, [String
"InstanceModel"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (InstanceModel, Int))
insmodelTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map
    (\(UID, (GenDefn, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (GenDefn, Int))
x, [String
"GeneralDefinition"]))
    (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (GenDefn, Int))
gendefTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (TheoryModel, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (TheoryModel, Int))
x, [String
"TheoryModel"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (TheoryModel, Int))
theoryModelTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map
    (\(UID, (ConceptInstance, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (ConceptInstance, Int))
x, [String
"ConceptInstance"]))
    (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (ConceptInstance, Int))
conceptinsTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (Section, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (Section, Int))
x, [String
"Section"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (Section, Int))
sectionTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map
    (\(UID, (LabelledContent, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (LabelledContent, Int))
x, [String
"LabelledContent"]))
    (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (LabelledContent, Int))
labelledcontentTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(UID, (Reference, Int))
x -> (forall a b. (a, b) -> a
fst (UID, (Reference, Int))
x, [String
"Reference"])) (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (Reference, Int))
refTable)

-- Currently Unused
-- | Get all 'UID's from a database ('ChunkDB').
mkListAll :: ChunkDB -> [UID]
mkListAll :: ChunkDB -> [UID]
mkListAll ChunkDB
db = forall a. Eq a => [a] -> [a]
nub
  forall a b. (a -> b) -> a -> b
$ forall a. Ord a => [a] -> [a]
sort
  forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB -> SymbolMap
symbolTable ChunkDB
db)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB -> TermMap
termTable ChunkDB
db)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB -> ConceptMap
defTable ChunkDB
db)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (UnitDefn, Int))
unitTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID [UID])
traceTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID [UID])
refbyTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (DataDefinition, Int))
dataDefnTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (InstanceModel, Int))
insmodelTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (GenDefn, Int))
gendefTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (TheoryModel, Int))
theoryModelTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (ConceptInstance, Int))
conceptinsTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (Section, Int))
sectionTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (LabelledContent, Int))
labelledcontentTable)
  forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k a. Map k a -> [(k, a)]
Map.assocs forall a b. (a -> b) -> a -> b
$ ChunkDB
db forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (Map UID (Reference, Int))
refTable)