@@ -28,28 +28,30 @@ import Data.Bifunctor (first)
2828import Data.List.NonEmpty (NonEmpty ((:|) ))
2929import Data.Text (Text )
3030import Data.Void (Void )
31+ import Dhall (EvaluateSettings )
3132import Network.URI (URI )
3233import System.FilePath
3334 ( splitDirectories
3435 , takeDirectory
3536 , takeFileName
3637 )
3738
38- import qualified Data.Graph as Graph
39- import qualified Data.Map.Strict as Map
40- import qualified Data.Set as Set
41- import qualified Data.Text as Text
42- import qualified Dhall.Core as Dhall
43- import qualified Dhall.Import as Dhall
39+ import qualified Data.Graph as Graph
40+ import qualified Data.Map.Strict as Map
41+ import qualified Data.Set as Set
42+ import qualified Data.Text as Text
43+ import qualified Dhall
44+ import qualified Dhall.Core as Dhall
45+ import qualified Dhall.Import as Import
4446import qualified Dhall.Map
45- import qualified Dhall.Parser as Dhall
46- import qualified Dhall.TypeCheck as Dhall
47+ import qualified Dhall.Parser as Dhall
48+ import qualified Dhall.TypeCheck as Dhall
4749import qualified Language.LSP.Protocol.Types as LSP.Types
48- import qualified Network.URI as URI
50+ import qualified Network.URI as URI
4951
5052
5153-- | A @FileIdentifier@ represents either a local file or a remote url.
52- newtype FileIdentifier = FileIdentifier Dhall . Chained
54+ newtype FileIdentifier = FileIdentifier Import . Chained
5355
5456-- | Construct a FileIdentifier from a local file path.
5557fileIdentifierFromFilePath :: FilePath -> FileIdentifier
@@ -58,7 +60,7 @@ fileIdentifierFromFilePath path =
5860 directory = takeDirectory path
5961 components = map Text. pack . reverse . splitDirectories $ directory
6062 file = Dhall. File (Dhall. Directory components) filename
61- in FileIdentifier $ Dhall . chainedFromLocalHere Dhall. Absolute file Dhall. Code
63+ in FileIdentifier $ Import . chainedFromLocalHere Dhall. Absolute file Dhall. Code
6264
6365-- | Construct a FileIdentifier from a given URI. Supports only "file:" URIs.
6466fileIdentifierFromURI :: URI -> Maybe FileIdentifier
@@ -76,11 +78,11 @@ newtype WellTyped = WellTyped {fromWellTyped :: Expr Src Void}
7678newtype Normal = Normal { fromNormal :: Expr Src Void }
7779
7880-- An import graph, represented by list of import dependencies.
79- type ImportGraph = [Dhall . Depends ]
81+ type ImportGraph = [Import . Depends ]
8082
8183-- | A cache maps Dhall imports to fully normalised expressions. By reusing
8284-- caches we can speeds up diagnostics etc. significantly!
83- data Cache = Cache ImportGraph (Dhall.Map. Map Dhall . Chained Dhall . ImportSemantics )
85+ data Cache = Cache ImportGraph (Dhall.Map. Map Import . Chained Import . ImportSemantics )
8486
8587-- | The initial cache.
8688emptyCache :: Cache
@@ -94,11 +96,11 @@ invalidate :: FileIdentifier -> Cache -> Cache
9496invalidate (FileIdentifier chained) (Cache dependencies cache) =
9597 Cache dependencies' $ Dhall.Map. withoutKeys cache invalidImports
9698 where
97- imports = map Dhall . parent dependencies ++ map Dhall . child dependencies
99+ imports = map Import . parent dependencies ++ map Import . child dependencies
98100
99101 adjacencyLists = foldr
100102 -- add reversed edges to adjacency lists
101- (\ (Dhall . Depends parent child) -> Map. adjust (parent : ) child)
103+ (\ (Import . Depends parent child) -> Map. adjust (parent : ) child)
102104 -- starting from the discrete graph
103105 (Map. fromList [ (i,[] ) | i <- imports])
104106 dependencies
@@ -112,18 +114,18 @@ invalidate (FileIdentifier chained) (Cache dependencies cache) =
112114 do vertex <- vertexFromImport import_
113115 return (Graph. reachable graph vertex)
114116
115- codeImport = Dhall . chainedChangeMode Dhall. Code chained
116- textImport = Dhall . chainedChangeMode Dhall. RawText chained
117+ codeImport = Import . chainedChangeMode Dhall. Code chained
118+ textImport = Import . chainedChangeMode Dhall. RawText chained
117119 invalidImports = Set. fromList $ codeImport : reachableImports codeImport
118120 ++ textImport : reachableImports textImport
119121
120- dependencies' = filter (\ (Dhall . Depends parent child) -> Set. notMember parent invalidImports
122+ dependencies' = filter (\ (Import . Depends parent child) -> Set. notMember parent invalidImports
121123 && Set. notMember child invalidImports) dependencies
122124
123125-- | A Dhall error. Covers parsing, resolving of imports, typechecking and
124126-- normalisation.
125127data DhallError = ErrorInternal SomeException
126- | ErrorImportSourced (Dhall. SourcedException Dhall . MissingImports )
128+ | ErrorImportSourced (Dhall. SourcedException Import . MissingImports )
127129 | ErrorTypecheck (Dhall. TypeError Src Void )
128130 | ErrorParse Dhall. ParseError
129131
@@ -137,38 +139,50 @@ parseWithHeader :: Text -> Either DhallError (Dhall.Header, Expr Src Dhall.Impor
137139parseWithHeader = first ErrorParse . Dhall. exprAndHeaderFromText " "
138140
139141-- | Resolve all imports in an expression.
140- load :: FileIdentifier -> Expr Src Dhall. Import -> Cache ->
141- IO (Either DhallError (Cache , Expr Src Void ))
142- load (FileIdentifier chained) expr (Cache graph cache) = do
143- let emptyStatus = Dhall. emptyStatus " "
144- status = -- reuse cache and import graph
145- set Dhall. cache cache .
146- set Dhall. graph graph .
142+ load
143+ :: EvaluateSettings
144+ -> FileIdentifier
145+ -> Expr Src Dhall. Import
146+ -> Cache
147+ -> IO (Either DhallError (Cache , Expr Src Void ))
148+ load settings (FileIdentifier chained) expr (Cache graph cache) = do
149+ let emptyStatus =
150+ set Import. substitutions (view Dhall. substitutions settings)
151+ . set Import. normalizer (view Dhall. normalizer settings)
152+ . set Import. startingContext (view Dhall. startingContext settings)
153+ $ Import. emptyStatusWithManager (view Dhall. newManager settings) " "
154+
155+ let status = -- reuse cache and import graph
156+ set Import. cache cache .
157+ set Import. graph graph .
147158 -- set "root import"
148- set Dhall . stack (chained :| [] )
159+ set Import . stack (chained :| [] )
149160 $ emptyStatus
150- (do (expr', status') <- runStateT (Dhall . loadWith expr) status
151- let cache' = view Dhall . cache status'
152- graph' = view Dhall . graph status'
161+ (do (expr', status') <- runStateT (Import . loadWith expr) status
162+ let cache' = view Import . cache status'
163+ graph' = view Import . graph status'
153164 return . Right $ (Cache graph' cache', expr'))
154165 `catch` (\ e -> return . Left $ ErrorImportSourced e)
155166 `catch` (\ e -> return . Left $ ErrorInternal e)
156167
157168-- | Typecheck a fully resolved expression. Returns a certification that the
158169-- input was well-typed along with its (well-typed) type.
159- typecheck :: Expr Src Void -> Either DhallError (WellTyped , WellTyped )
160- typecheck expr = case Dhall. typeOf expr of
170+ typecheck
171+ :: EvaluateSettings
172+ -> Expr Src Void
173+ -> Either DhallError (WellTyped , WellTyped )
174+ typecheck settings expr = case Dhall. typeWith (view Dhall. startingContext settings) expr of
161175 Left err -> Left $ ErrorTypecheck err
162176 Right typ -> Right (WellTyped expr, WellTyped typ)
163177
164178-- | Normalise a well-typed expression.
165- normalize :: WellTyped -> Normal
166- normalize (WellTyped expr) = Normal $ Dhall. normalize expr
179+ normalize :: EvaluateSettings -> WellTyped -> Normal
180+ normalize settings (WellTyped expr) = Normal $ Dhall. normalizeWith (view Dhall. normalizer settings) expr
167181
168182-- | Given a normal expression compute the hash (using the default standard
169183-- version) of its alpha-normal form. Returns the hash in the format used in
170184-- Dhall's hash annotations (prefixed by "sha256:" and base-64 encoded).
171185hashNormalToCode :: Normal -> Text
172186hashNormalToCode (Normal expr) =
173- Dhall . hashExpressionToCode (Dhall. denote alphaNormal)
187+ Import . hashExpressionToCode (Dhall. denote alphaNormal)
174188 where alphaNormal = Dhall. alphaNormalize expr
0 commit comments