Resolve all imports within an expression
Moduledhall-1.42.3Haskell2010
Dhall.Import
Dhall lets you import external expressions located either in local files or hosted on network endpoints.
To import a local file as an expression, just insert the path to the file,
prepending a ./ if the path is relative to the current directory. For
example, if you create a file named id with the following contents:
$ cat id
λ(a : Type) → λ(x : a) → xThen you can use the file directly within a dhall program just by
referencing the file's path:
$ dhall
./id Bool True
<Ctrl-D>
Bool
TrueImported expressions may contain imports of their own, too, which will continue to be resolved. However, Dhall will prevent cyclic imports. For example, if you had these two files:
$ cat foo
./bar$ cat bar
./foo... Dhall would throw the following exception if you tried to import foo:
$ dhall
./foo
^D
↳ ./foo
↳ ./bar
Cyclic import: ./fooYou can also import expressions hosted on network endpoints. Just use the URL
http://host[:port]/pathThe compiler expects the downloaded expressions to be in the same format as local files, specifically UTF8-encoded source code text.
For example, if our id expression were hosted at http://example.com/id,
then we would embed the expression within our code using:
http://example.com/idYou can also import expressions stored within environment variables using
env:NAME, where NAME is the name of the environment variable. For
example:
$ export FOO=1
$ export BAR='"Hi"'
$ export BAZ='λ(x : Bool) → x == False'
$ dhall <<< "{ foo = env:FOO , bar = env:BAR , baz = env:BAZ }"
{ bar : Text, baz : ∀(x : Bool) → Bool, foo : Integer }
{ bar = "Hi", baz = λ(x : Bool) → x == False, foo = 1 }If you wish to import the raw contents of an impoert as Text then add
as Text to the end of the import:
$ dhall <<< "http://example.com as Text"
Text
"<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta
charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html
; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width,
initial-scale=1\" />\n <style type=\"text/css\">\n body {\n backgro
und-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-famil
y: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n
}\n div {\n width: 600px;\n margin: 5em auto;\n paddi
ng: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n
a:link, a:visited {\n color: #38488f;\n text-decoration: none;
\n }\n @media (max-width: 700px) {\n body {\n background
-color: #fff;\n }\n div {\n width: auto;\n m
argin: 0 auto;\n border-radius: 0;\n padding: 1em;\n
}\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</
h1>\n <p>This domain is established to be used for illustrative examples in d
ocuments. You may use this\n domain in examples without prior coordination or
asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/exampl
e\">More information...</a></p>\n</div>\n</body>\n</html>\n"- 17 types
- 31 values
- Packagedhall-1.42.3
- Exports48
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceImport.hs
Import
48 declarationsSee load.
Resolve all imports within an expression, importing relative to the given directory.
See loadRelativeTo.
Construct the file path corresponding to a local import. If the import is _relative_ then the resulting path is also relative.
Hash a fully resolved expression
Convenience utility to hash a fully resolved expression and return the
base-16 encoded hash with the sha256: prefix
In other words, the output of this function can be pasted into Dhall source code to add an integrity check to an import
Ensure that the given expression is present in the semantic cache. The given expression should be alpha-beta-normal.
Assert than an expression is import-free
Shared state for HTTP requests
The default HTTP Manager
Used internally to track whether or not we've already warned the user about caching issues
State threaded throughout the import process
Constructors
Status_stack :: NonEmpty ChainedStack of Imports that we've imported along the way to get to the current point
_graph :: [Depends]Graph of all the imports visited so far, represented by a list of import dependencies.
_cache :: Map Chained ImportSemanticsCache of imported expressions with their node id in order to avoid importing the same expression twice with different values
_newManager :: IO Manager_manager :: Maybe ManagerUsed to cache the Manager when making multiple requests
_loadOriginHeaders :: StateT Status IO OriginHeadersLoad the origin headers from environment or configuration file. After loading once, further evaluations return the cached version.
_remote :: URL -> StateT Status IO TextThe remote resolver, fetches the content at the given URL.
_remoteBytes :: URL -> StateT Status IO ByteString_substitutions :: Substitutions Src Void_normalizer :: Maybe (ReifiedNormalizer Void)_startingContext :: Context (Expr Src Void)_semanticCacheMode :: SemanticCacheMode_cacheWarning :: CacheWarningRecords whether or not we already warned the user about issues with cache directory
This enables or disables the semantic cache for imports protected by integrity checks
Instances1Eq
Eq SemanticCacheModeDefined in dhall-1.42.3 · Dhall.Import.Types
A fully "chained" import, i.e. if it contains a relative path that path
is relative to the current directory. If it is a remote import with headers
those are well-typed (either of type `List { header : Text, value Text}` or
`List { mapKey : Text, mapValue Text})` and in normal form. These
invariants are preserved by the API exposed by Dhall.Import.
The underlying import
Adjust the import mode of a chained import
Default starting Status, importing relative to the given directory.
See emptyStatus
Load headers only from the environment (used in tests)
See emptyStatus.
See remoteStatus
Fetch the text contents of a URL
Given a well-typed (of type `List { header : Text, value Text }` or `List { mapKey : Text, mapValue Text }`) headers expressions in normal form construct the corresponding binary http headers; otherwise return the empty list.
Lens from a Status to its _substitutions field
Lens from a Status to its _normalizer field
Lens from a Status to its _startingContext field
Chain imports, also typecheck and normalize headers if applicable.
This function is used by the --transitive option of the
dhall {freeze,format,lint} subcommands to determine which dependencies
to descend into
dependencyToFile (emptyStatus ".") Import{ importHashed = ImportHashed{ hash = Nothing, importType = Local Here (File (Directory []) "foo") }, importMode = Code }Just "./foo"
dependencyToFile (emptyStatus "./foo") Import{ importHashed = ImportHashed{ hash = Nothing, importType = Local Here (File (Directory []) "bar") }, importMode = Code }Just "./foo/bar"
dependencyToFile (emptyStatus "./foo") Import{ importHashed = ImportHashed{ hash = Nothing, importType = Remote (URL HTTPS "example.com" (File (Directory []) "") Nothing Nothing) }, importMode = Code }Nothing
dependencyToFile (emptyStatus ".") Import{ importHashed = ImportHashed{ hash = Nothing, importType = Env "foo" }, importMode = Code }Nothing
An import that has been fully interpeted
HTTP headers
An import failed because of a cycle in the import graph
Constructors
CyclecyclicImport :: ImportThe offending cyclic import
Dhall tries to ensure that all expressions hosted on network endpoints are weakly referentially transparent, meaning roughly that any two clients will compile the exact same result given the same URL.
To be precise, a strong interpretaton of referential transparency means that if you compiled a URL you could replace the expression hosted at that URL with the compiled result. Let's call this "static linking". Dhall (very intentionally) does not satisfy this stronger interpretation of referential transparency since "statically linking" an expression (i.e. permanently resolving all imports) means that the expression will no longer update if its dependencies change.
In general, either interpretation of referential transparency is not enforceable in a networked context since one can easily violate referential transparency with a custom DNS, but Dhall can still try to guard against common unintentional violations. To do this, Dhall enforces that a non-local import may not reference a local import.
Local imports are defined as:
A file
A URL with a host of
localhostor127.0.0.1
All other imports are defined to be non-local
Constructors
ReferentiallyOpaqueopaqueImport :: ImportThe offending opaque import
Instances2Show, Exception
Show ReferentiallyOpaqueDefined in dhall-1.42.3 · Dhall.ImportException ReferentiallyOpaqueDefined in dhall-1.42.3 · Dhall.Import
Extend another exception with the current import stack
Constructors
ImportedimportStack :: NonEmpty ChainedImports resolved so far, in reverse order
nested :: eThe nested exception
A call to assertNoImports failed because there was at least one import
Instances2Show, Exception
Show ImportResolutionDisabledDefined in dhall-1.42.3 · Dhall.ImportException ImportResolutionDisabledDefined in dhall-1.42.3 · Dhall.Import
Wrapper around HttpExceptions with a prettier Show instance
In order to keep the library API constant even when the with-http Cabal
flag is disabled the pretty error message is pre-rendered and the real
HttpException is stored in a Dynamic
Constructors
Instances2Show, Exception
Show PrettyHttpExceptionDefined in dhall-1.42.3 · Dhall.Import.TypesException PrettyHttpExceptionDefined in dhall-1.42.3 · Dhall.Import.Types
Exception thrown when an imported file is missing
Constructors
Instances2Show, Exception
Show MissingFileDefined in dhall-1.42.3 · Dhall.ImportException MissingFileDefined in dhall-1.42.3 · Dhall.Import
Exception thrown when an environment variable is missing
Constructors
Instances2Show, Exception
Show MissingEnvironmentVariableDefined in dhall-1.42.3 · Dhall.ImportException MissingEnvironmentVariableDefined in dhall-1.42.3 · Dhall.Import
List of Exceptions we encounter while resolving Import Alternatives
Constructors
Instances2Show, Exception
Show MissingImportsDefined in dhall-1.42.3 · Dhall.ImportException MissingImportsDefined in dhall-1.42.3 · Dhall.Import
Exception thrown when an integrity check fails
Constructors
Instances2Show, Exception
Show HashMismatchDefined in dhall-1.42.3 · Dhall.ImportException HashMismatchDefined in dhall-1.42.3 · Dhall.Import