Skip to content

Commit 5f15c86

Browse files
Gabriella439mergify[bot]
authored andcommitted
List dependencies in "post-order" (#1539)
* List dependencies in "post-order" Related to https://github.com/dhall-lang/dhall-lang/issues/823 This lists dependencies starting from the leaves and concluding with the root import, same as how `nix-store --query --requisites` does Example output: ```bash $ dhall resolve --no-cache --transitive-dependencies <<< 'https://prelude.dhall-lang.org/package.dhall' https://prelude.dhall-lang.org/Bool/and https://prelude.dhall-lang.org/Bool/build https://prelude.dhall-lang.org/Bool/even https://prelude.dhall-lang.org/Bool/fold https://prelude.dhall-lang.org/Bool/not https://prelude.dhall-lang.org/Bool/odd https://prelude.dhall-lang.org/Bool/or https://prelude.dhall-lang.org/Bool/show https://prelude.dhall-lang.org/Bool/package.dhall https://prelude.dhall-lang.org/Double/show https://prelude.dhall-lang.org/Double/package.dhall https://prelude.dhall-lang.org/Function/compose https://prelude.dhall-lang.org/Function/package.dhall https://prelude.dhall-lang.org/Integer/show https://prelude.dhall-lang.org/Integer/toDouble https://prelude.dhall-lang.org/Integer/package.dhall https://prelude.dhall-lang.org/List/all https://prelude.dhall-lang.org/List/any https://prelude.dhall-lang.org/List/build https://prelude.dhall-lang.org/List/concat https://prelude.dhall-lang.org/List/concatMap https://prelude.dhall-lang.org/List/default https://prelude.dhall-lang.org/List/empty https://prelude.dhall-lang.org/List/filter https://prelude.dhall-lang.org/List/fold https://prelude.dhall-lang.org/List/generate https://prelude.dhall-lang.org/List/head https://prelude.dhall-lang.org/List/indexed https://prelude.dhall-lang.org/List/iterate https://prelude.dhall-lang.org/List/last https://prelude.dhall-lang.org/List/length https://prelude.dhall-lang.org/List/map https://prelude.dhall-lang.org/List/null https://prelude.dhall-lang.org/List/partition https://prelude.dhall-lang.org/List/replicate https://prelude.dhall-lang.org/List/reverse https://prelude.dhall-lang.org/List/shifted https://prelude.dhall-lang.org/List/unzip https://prelude.dhall-lang.org/List/package.dhall https://prelude.dhall-lang.org/Location/Type https://prelude.dhall-lang.org/Location/package.dhall https://prelude.dhall-lang.org/Map/Type https://prelude.dhall-lang.org/Map/Entry https://prelude.dhall-lang.org/Map/empty https://prelude.dhall-lang.org/Map/keyText https://prelude.dhall-lang.org/Map/keyValue https://prelude.dhall-lang.org/Map/keys https://prelude.dhall-lang.org/Map/map https://prelude.dhall-lang.org/Map/values https://prelude.dhall-lang.org/Map/package.dhall https://prelude.dhall-lang.org/Monoid https://prelude.dhall-lang.org/Natural/build https://prelude.dhall-lang.org/Natural/enumerate https://prelude.dhall-lang.org/Natural/even https://prelude.dhall-lang.org/Natural/fold https://prelude.dhall-lang.org/Natural/isZero https://prelude.dhall-lang.org/Natural/odd https://prelude.dhall-lang.org/Natural/product https://prelude.dhall-lang.org/Natural/sum https://prelude.dhall-lang.org/Natural/show https://prelude.dhall-lang.org/Natural/toDouble https://prelude.dhall-lang.org/Natural/toInteger https://prelude.dhall-lang.org/Natural/lessThanEqual https://prelude.dhall-lang.org/Natural/greaterThanEqual https://prelude.dhall-lang.org/Natural/lessThan https://prelude.dhall-lang.org/Natural/equal https://prelude.dhall-lang.org/Natural/greaterThan https://prelude.dhall-lang.org/Natural/min https://prelude.dhall-lang.org/Natural/max https://prelude.dhall-lang.org/Optional/map https://prelude.dhall-lang.org/Natural/listMin https://prelude.dhall-lang.org/Natural/listMax https://prelude.dhall-lang.org/Natural/sort https://prelude.dhall-lang.org/Natural/subtract https://prelude.dhall-lang.org/Natural/package.dhall https://prelude.dhall-lang.org/Optional/all https://prelude.dhall-lang.org/Optional/any https://prelude.dhall-lang.org/Optional/build https://prelude.dhall-lang.org/Optional/concat https://prelude.dhall-lang.org/Optional/default https://prelude.dhall-lang.org/Optional/filter https://prelude.dhall-lang.org/Optional/fold https://prelude.dhall-lang.org/Optional/head https://prelude.dhall-lang.org/Optional/last https://prelude.dhall-lang.org/Optional/length https://prelude.dhall-lang.org/Optional/null https://prelude.dhall-lang.org/Optional/toList https://prelude.dhall-lang.org/Optional/unzip https://prelude.dhall-lang.org/Optional/package.dhall https://prelude.dhall-lang.org/JSON/Type https://prelude.dhall-lang.org/JSON/Nesting https://prelude.dhall-lang.org/JSON/Tagged https://prelude.dhall-lang.org/JSON/keyText https://prelude.dhall-lang.org/JSON/keyValue https://prelude.dhall-lang.org/JSON/string https://prelude.dhall-lang.org/JSON/number https://prelude.dhall-lang.org/JSON/object https://prelude.dhall-lang.org/JSON/array https://prelude.dhall-lang.org/JSON/bool https://prelude.dhall-lang.org/JSON/null https://prelude.dhall-lang.org/Text/concatMapSep https://prelude.dhall-lang.org/JSON/render https://prelude.dhall-lang.org/JSON/package.dhall https://prelude.dhall-lang.org/Text/concat https://prelude.dhall-lang.org/Text/concatMap https://prelude.dhall-lang.org/Text/concatSep https://prelude.dhall-lang.org/Text/default https://prelude.dhall-lang.org/Text/defaultMap https://prelude.dhall-lang.org/Text/show https://prelude.dhall-lang.org/Text/package.dhall https://prelude.dhall-lang.org/XML/Type https://prelude.dhall-lang.org/XML/attribute https://prelude.dhall-lang.org/XML/render https://prelude.dhall-lang.org/XML/element https://prelude.dhall-lang.org/XML/leaf https://prelude.dhall-lang.org/XML/text https://prelude.dhall-lang.org/XML/emptyAttributes https://prelude.dhall-lang.org/XML/package.dhall https://prelude.dhall-lang.org/package.dhall ``` * Document order in `dhall resolve --help` output ... as suggested by @sjakobi * Fix dhall-lsp-server build
1 parent 619788f commit 5f15c86

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

dhall-lsp-server/src/Dhall/LSP/Backend/Dhall.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import qualified Dhall.TypeCheck as Dhall
2929
import qualified Data.Graph as Graph
3030
import qualified Data.Map.Strict as Map
3131
import qualified Data.Set as Set
32+
import qualified Dhall.Map
3233
import qualified Network.URI as URI
3334
import qualified Language.Haskell.LSP.Types as LSP.Types
3435
import qualified Data.Text as Text
@@ -75,19 +76,19 @@ type ImportGraph = [Dhall.Depends]
7576

7677
-- | A cache maps Dhall imports to fully normalised expressions. By reusing
7778
-- caches we can speeds up diagnostics etc. significantly!
78-
data Cache = Cache ImportGraph (Map.Map Dhall.Chained Dhall.ImportSemantics)
79+
data Cache = Cache ImportGraph (Dhall.Map.Map Dhall.Chained Dhall.ImportSemantics)
7980

8081
-- | The initial cache.
8182
emptyCache :: Cache
82-
emptyCache = Cache [] Map.empty
83+
emptyCache = Cache [] Dhall.Map.empty
8384

8485
-- | Invalidate any _unhashed_ imports of the given file. Hashed imports are
8586
-- kept around as per
8687
-- https://github.com/dhall-lang/dhall-lang/blob/master/standard/imports.md.
8788
-- Transitively invalidates any imports depending on the changed file.
8889
invalidate :: FileIdentifier -> Cache -> Cache
8990
invalidate (FileIdentifier chained) (Cache dependencies cache) =
90-
Cache dependencies' $ Map.withoutKeys cache invalidImports
91+
Cache dependencies' $ Dhall.Map.withoutKeys cache invalidImports
9192
where
9293
imports = map Dhall.parent dependencies ++ map Dhall.child dependencies
9394

dhall/src/Dhall/Import.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ import qualified Data.ByteString.Lazy
189189
import qualified Data.CaseInsensitive
190190
import qualified Data.Foldable
191191
import qualified Data.List.NonEmpty as NonEmpty
192-
import qualified Data.Map.Strict as Map
193192
import qualified Data.Text.Encoding
194193
import qualified Data.Text as Text
195194
import qualified Data.Text.IO
@@ -479,11 +478,12 @@ chainImport (Chained parent) child =
479478
loadImport :: Chained -> StateT Status IO ImportSemantics
480479
loadImport import_ = do
481480
Status {..} <- State.get
482-
case Map.lookup import_ _cache of
481+
482+
case Dhall.Map.lookup import_ _cache of
483483
Just importSemantics -> return importSemantics
484484
Nothing -> do
485485
importSemantics <- loadImportWithSemanticCache import_
486-
zoom cache (State.modify (Map.insert import_ importSemantics))
486+
zoom cache (State.modify (Dhall.Map.insert import_ importSemantics))
487487
return importSemantics
488488

489489
-- | Load an import from the 'semantic cache'. Defers to

dhall/src/Dhall/Import/Types.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Control.Exception (Exception)
1010
import Control.Monad.Trans.State.Strict (StateT)
1111
import Data.Dynamic
1212
import Data.List.NonEmpty (NonEmpty)
13-
import Data.Map (Map)
13+
import Dhall.Map (Map)
1414
import Data.Semigroup ((<>))
1515
import Data.Text.Prettyprint.Doc (Pretty(..))
1616
import Data.Void (Void)
@@ -35,7 +35,7 @@ import Lens.Family (LensLike')
3535
import System.FilePath (isRelative, splitDirectories)
3636

3737
import qualified Dhall.Context
38-
import qualified Data.Map as Map
38+
import qualified Dhall.Map as Map
3939
import qualified Data.Text
4040

4141
-- | A fully 'chained' import, i.e. if it contains a relative path that path is

dhall/src/Dhall/Main.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import qualified Dhall.Freeze
6363
import qualified Dhall.Import
6464
import qualified Dhall.Import.Types
6565
import qualified Dhall.Lint
66+
import qualified Dhall.Map
6667
import qualified Dhall.Tags
6768
import qualified Dhall.Pretty
6869
import qualified Dhall.Repl
@@ -307,7 +308,7 @@ parseMode =
307308
Options.Applicative.flag' (Just ListTransitiveDependencies)
308309
( Options.Applicative.long "transitive-dependencies"
309310
<> Options.Applicative.help
310-
"List transitive import dependencies"
311+
"List transitive import dependencies in post-order"
311312
)
312313
<|> pure Nothing
313314

@@ -570,7 +571,8 @@ command (Options {..}) = do
570571
. Dhall.Core.importType
571572
. Dhall.Core.importHashed
572573
. Dhall.Import.chainedImport )
573-
. Data.Map.keys
574+
. reverse
575+
. Dhall.Map.keys
574576
$ _cache
575577

576578
Resolve { resolveMode = Nothing, ..} -> do

dhall/src/Dhall/Map.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Dhall.Map
1313
Map
1414

1515
-- * Construction
16+
, empty
1617
, singleton
1718
, fromList
1819
, fromListWithKey
@@ -33,6 +34,7 @@ module Dhall.Map
3334
, delete
3435
, filter
3536
, restrictKeys
37+
, withoutKeys
3638
, mapMaybe
3739

3840
-- * Query
@@ -161,6 +163,10 @@ instance Ord k => GHC.Exts.IsList (Map k v) where
161163

162164
toList = Dhall.Map.toList
163165

166+
-- | Create an empty `Map`
167+
empty :: Ord k => Map k v
168+
empty = mempty
169+
164170
{-| Create a `Map` from a single key-value pair
165171
166172
>>> singleton "A" 1
@@ -369,6 +375,23 @@ restrictKeys (Map m ks) s = Map m' ks'
369375
ks' = filterKeys (\k -> Data.Set.member k s) ks
370376
{-# INLINABLE restrictKeys #-}
371377

378+
{-| Remove all keys in a @"Data.Set".'Set'@ from a 'Map'
379+
380+
>>> withoutKeys (fromList [("A",1),("B",2)]) (Data.Set.fromList ["A"])
381+
fromList [("B",2)]
382+
-}
383+
withoutKeys :: Ord k => Map k a -> Data.Set.Set k -> Map k a
384+
withoutKeys (Map m ks) s = Map m' ks'
385+
where
386+
#if MIN_VERSION_containers(0,5,8)
387+
m' = Data.Map.withoutKeys m s
388+
#else
389+
m' = Data.Map.filterWithKey (\k _ -> Data.Set.notMember k s) m
390+
#endif
391+
392+
ks' = filterKeys (\k -> Data.Set.notMember k s) ks
393+
{-# INLINABLE withoutKeys #-}
394+
372395
{-| Transform all values in a `Map` using the supplied function, deleting the
373396
key if the function returns `Nothing`
374397

0 commit comments

Comments
 (0)