Conversation
948c509 to
ce992a2
Compare
JacquesCarette
left a comment
There was a problem hiding this comment.
This makes sense to me, but I also want @balacij 's review, to make sure this is going in the same direction that he's working on.
The merge-base changed after approval.
| -- | Helpers for extracting references ----------------------------------------- | ||
|
|
||
| -- | Generic traverse of all positions that could lead to /symbolic/ 'UID's from 'Sentence's. | ||
| getUIDs :: Sentence -> [UID] | ||
| getUIDs (Ch ShortStyle _ _) = [] | ||
| getUIDs (Ch TermStyle _ _) = [] | ||
| getUIDs (Ch PluralTerm _ _) = [] | ||
| getUIDs (SyCh a) = [a] | ||
| getUIDs Sy {} = [] | ||
| getUIDs NP {} = [] | ||
| getUIDs S {} = [] | ||
| getUIDs P {} = [] | ||
| getUIDs Ref {} = [] | ||
| getUIDs Percent = [] | ||
| getUIDs ((:+:) a b) = getUIDs a ++ getUIDs b | ||
| getUIDs (Quote a) = getUIDs a | ||
| getUIDs (E a) = meNames a | ||
| getUIDs EmptyS = [] | ||
|
|
||
| -- | Generic traverse of all positions that could lead to /symbolic/ and /abbreviated/ 'UID's from 'Sentence's | ||
| -- but doesn't go into expressions. | ||
| getUIDshort :: Sentence -> [UID] | ||
| getUIDshort (Ch ShortStyle _ a) = [a] | ||
| getUIDshort (Ch TermStyle _ _) = [] | ||
| getUIDshort (Ch PluralTerm _ _) = [] | ||
| getUIDshort SyCh {} = [] | ||
| getUIDshort Sy {} = [] | ||
| getUIDshort NP {} = [] | ||
| getUIDshort S {} = [] | ||
| getUIDshort Percent = [] | ||
| getUIDshort P {} = [] | ||
| getUIDshort Ref {} = [] | ||
| getUIDshort ((:+:) a b) = getUIDshort a ++ getUIDshort b | ||
| getUIDshort (Quote a) = getUIDshort a | ||
| getUIDshort E {} = [] | ||
| getUIDshort EmptyS = [] | ||
|
|
||
| ----------------------------------------------------------------------------- | ||
| -- And now implement the exported traversals all in terms of the above | ||
| -- | This is to collect /symbolic/ 'UID's that are printed out as a 'Symbol'. | ||
| sdep :: Sentence -> [UID] | ||
| sdep = nubOrd . getUIDs | ||
| {-# INLINE sdep #-} | ||
|
|
||
| -- This is to collect symbolic 'UID's that are printed out as an /abbreviation/. | ||
| shortdep :: Sentence -> [UID] | ||
| shortdep = nubOrd . getUIDshort | ||
| {-# INLINE shortdep #-} | ||
|
|
||
| -- | Generic traverse of all positions that could lead to /reference/ 'UID's from 'Sentence's. | ||
| lnames :: Sentence -> [UID] | ||
| lnames Ch {} = [] | ||
| lnames SyCh {} = [] | ||
| lnames Sy {} = [] | ||
| lnames NP {} = [] | ||
| lnames S {} = [] | ||
| lnames Percent = [] | ||
| lnames P {} = [] | ||
| lnames (Ref a _ _) = [a] | ||
| lnames ((:+:) a b) = lnames a ++ lnames b | ||
| lnames Quote {} = [] | ||
| lnames E {} = [] | ||
| lnames EmptyS = [] | ||
| {-# INLINE lnames #-} | ||
|
|
||
| -- | Get /reference/ 'UID's from 'Sentence's. | ||
| lnames' :: [Sentence] -> [UID] | ||
| lnames' = concatMap lnames | ||
| {-# INLINE lnames' #-} | ||
|
|
||
| sentenceRefs :: Sentence -> Set.Set UID | ||
| sentenceRefs sent = Set.fromList (lnames sent ++ sdep sent ++ shortdep sent) | ||
| {-# INLINE sentenceRefs #-} | ||
|
|
||
| instance HasChunkRefs Sentence where | ||
| chunkRefs = sentenceRefs | ||
| {-# INLINABLE chunkRefs #-} |
There was a problem hiding this comment.
This is great! Now that we have this, do we still need to exposesdep, shortdep, etc.?
There was a problem hiding this comment.
We're also now using a Set for chunkRefs. Should we change those functions to use sets, too? (This would need to be a separate PR if so)
There was a problem hiding this comment.
This is great! Now that we have this, do we still need to expose
sdep,shortdep, etc.?
Probably keep them for now. They’re still used outside this module (TraceTable, GetChunks, and re-exported via Development/Sentence.Extract).
There was a problem hiding this comment.
We're also now using a
SetforchunkRefs. Should we change those functions to use sets, too? (This would need to be a separate PR if so)
sdep/lnames' currently return [UID] and are used as lists in GetChunks.hs and TraceTable.hs.
Drasil/code/drasil-docLang/lib/Drasil/GetChunks.hs
Lines 19 to 21 in c3655dd
Drasil/code/drasil-docLang/lib/Drasil/GetChunks.hs
Lines 39 to 41 in c3655dd
Drasil/code/drasil-docLang/lib/Drasil/TraceTable.hs
Lines 37 to 38 in c3655dd
If
sdep/lnames' change to Set UID, those call sites would need to convert back to a list (e.g., Set.toAscList), right? Does the order matter for outputs?
There was a problem hiding this comment.
I think this code was all overwritten in another one of your PRs? This PR might be outdated now.
| {-# INLINE sentenceRefs #-} | ||
|
|
||
| instance HasChunkRefs Sentence where | ||
| chunkRefs = sentenceRefs |
There was a problem hiding this comment.
Can't we use getUIDs instead of sentenceRefs? This way we only need to traverse a Sentence once to get the same information?
There was a problem hiding this comment.
If not, seeing as sentenceRefs isn't exposed by the module, can we just inline it into the definition of chunkRefs?
There was a problem hiding this comment.
Can't we use
getUIDsinstead ofsentenceRefs? This way we only need to traverse aSentenceonce to get the same information?
getUIDs only collects symbolic UIDs (and from expressions). it ignores Ref and Ch ShortStyle. sentenceRefs was combining symbol + short + ref. So getUIDs isn’t equivalent. I added getAllUIDs that collects all three in one pass
|
When are you going to be able to get back to this @Xinlu-Y ? |
I’m working on it today |
| Ch :: SentenceStyle -> TermCapitalization -> UIDRef typ -> Sentence | ||
| -- | A branch of Ch dedicated to SymbolStyle only. | ||
| SyCh :: UID -> Sentence | ||
| SyCh :: UIDRef typ -> Sentence |
There was a problem hiding this comment.
This particular one was done in #4810.
It would be good to add constraints on what we expect from typ for the other things, but those should probably be split off into other PRs.
| -- | Helpers for extracting references ----------------------------------------- | ||
|
|
||
| -- | Generic traverse of all positions that could lead to /symbolic/ 'UID's from 'Sentence's. | ||
| getUIDs :: Sentence -> [UID] | ||
| getUIDs (Ch ShortStyle _ _) = [] | ||
| getUIDs (Ch TermStyle _ _) = [] | ||
| getUIDs (Ch PluralTerm _ _) = [] | ||
| getUIDs (SyCh a) = [a] | ||
| getUIDs Sy {} = [] | ||
| getUIDs NP {} = [] | ||
| getUIDs S {} = [] | ||
| getUIDs P {} = [] | ||
| getUIDs Ref {} = [] | ||
| getUIDs Percent = [] | ||
| getUIDs ((:+:) a b) = getUIDs a ++ getUIDs b | ||
| getUIDs (Quote a) = getUIDs a | ||
| getUIDs (E a) = meNames a | ||
| getUIDs EmptyS = [] | ||
|
|
||
| -- | Generic traverse of all positions that could lead to /symbolic/ and /abbreviated/ 'UID's from 'Sentence's | ||
| -- but doesn't go into expressions. | ||
| getUIDshort :: Sentence -> [UID] | ||
| getUIDshort (Ch ShortStyle _ a) = [a] | ||
| getUIDshort (Ch TermStyle _ _) = [] | ||
| getUIDshort (Ch PluralTerm _ _) = [] | ||
| getUIDshort SyCh {} = [] | ||
| getUIDshort Sy {} = [] | ||
| getUIDshort NP {} = [] | ||
| getUIDshort S {} = [] | ||
| getUIDshort Percent = [] | ||
| getUIDshort P {} = [] | ||
| getUIDshort Ref {} = [] | ||
| getUIDshort ((:+:) a b) = getUIDshort a ++ getUIDshort b | ||
| getUIDshort (Quote a) = getUIDshort a | ||
| getUIDshort E {} = [] | ||
| getUIDshort EmptyS = [] | ||
|
|
||
| ----------------------------------------------------------------------------- | ||
| -- And now implement the exported traversals all in terms of the above | ||
| -- | This is to collect /symbolic/ 'UID's that are printed out as a 'Symbol'. | ||
| sdep :: Sentence -> [UID] | ||
| sdep = nubOrd . getUIDs | ||
| {-# INLINE sdep #-} | ||
|
|
||
| -- This is to collect symbolic 'UID's that are printed out as an /abbreviation/. | ||
| shortdep :: Sentence -> [UID] | ||
| shortdep = nubOrd . getUIDshort | ||
| {-# INLINE shortdep #-} | ||
|
|
||
| -- | Generic traverse of all positions that could lead to /reference/ 'UID's from 'Sentence's. | ||
| lnames :: Sentence -> [UID] | ||
| lnames Ch {} = [] | ||
| lnames SyCh {} = [] | ||
| lnames Sy {} = [] | ||
| lnames NP {} = [] | ||
| lnames S {} = [] | ||
| lnames Percent = [] | ||
| lnames P {} = [] | ||
| lnames (Ref a _ _) = [a] | ||
| lnames ((:+:) a b) = lnames a ++ lnames b | ||
| lnames Quote {} = [] | ||
| lnames E {} = [] | ||
| lnames EmptyS = [] | ||
| {-# INLINE lnames #-} | ||
|
|
||
| -- | Get /reference/ 'UID's from 'Sentence's. | ||
| lnames' :: [Sentence] -> [UID] | ||
| lnames' = concatMap lnames | ||
| {-# INLINE lnames' #-} | ||
|
|
||
| sentenceRefs :: Sentence -> Set.Set UID | ||
| sentenceRefs sent = Set.fromList (lnames sent ++ sdep sent ++ shortdep sent) | ||
| {-# INLINE sentenceRefs #-} | ||
|
|
||
| instance HasChunkRefs Sentence where | ||
| chunkRefs = sentenceRefs | ||
| {-# INLINABLE chunkRefs #-} |
There was a problem hiding this comment.
I think this code was all overwritten in another one of your PRs? This PR might be outdated now.
| -- | Extract the 'UID' from a 'UIDRef'. | ||
| unRef :: UIDRef t -> UID | ||
| unRef (UIDRef u) = u |
| -- | Create a 'UIDRef' from a raw 'UID'. | ||
| uidRef :: UID -> UIDRef t | ||
| uidRef = UIDRef |
There was a problem hiding this comment.
I think we should try to avoid 'upgrading' UID references. I think hide is a bit better because it means that the specific chunk (and correct type) was in scope at some point.
| spec _ Percent = P.E $ P.MO P.Perc | ||
| spec _ (P s) = P.E $ symbol s | ||
| spec sm (SyCh s) = P.E $ symbol $ lookupC' sm s | ||
| spec sm (SyCh s) = P.E $ symbol $ lookupC' sm (unRef s) |
There was a problem hiding this comment.
Similarly, at 'get chunk' sites, we should be try to be using 'unhide' as much as possible, which knows about the correct type to be looking for from the ChunkDB. That being said, for this in particular, this is difficult. I had to resort to a fairly major hack in #4810.
Contributes to #4476
sdep/shortdep/lnameshelpers intoLanguage.Drasil.Sentence, derive a realHasChunkRefsinstance, and exportsentenceRefsso any chunk field of typeSentencereports itsUIDs.Language.Drasil.Sentence.Extractas a re-export shim so existing imports still build.This prepares the automation work in #4434 by turning
Sentenceinto a proper “chunk atom” fordeclareHasChunkRefs.