Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Add language support for alternative imports #163

Closed
Gabriella439 opened this issue May 26, 2018 · 3 comments · Fixed by #172
Closed

Discussion: Add language support for alternative imports #163

Gabriella439 opened this issue May 26, 2018 · 3 comments · Fixed by #172

Comments

@Gabriella439
Copy link
Contributor

I've gotten a couple of requests for supporting mirrors (i.e. alternative ways to retrieve an import) and I wanted to propose a more general solution for addressing this and other issues.

We could add a new ? operator to separate two arbitrary expressions (not necessarily imports) where:

e₀ ? e₁

... means try to resolve expression e₀ and if that fails then resolve expression e₁. You would also be able to chain these operators and the parentheses would be optional since this fallback behavior is associative:

(e₀ ? e₁) ? e₂ = e₀ ? (e₁ ? e₂)

There would also be a corresponding identity of the ? operator called missing that is an import that always fails to resolve:

missing ? e = e

e ? missing = e

One motivation for providing ? is to improve resilience, such as in #162 where @f-f mentioned using GitLab as a mirror for the Prelude in case GitHub goes down. Using this ? operator users wouldn't need to modify their code whenever GitHub goes down if they specify their import like this:

    let sum
          = https://raw.githubusercontent.com/dhall-lang/Prelude/e44284bc37a5808861dacd4c8bd13d18411cb961/Natural/sum
          ? https://gitlab.com/dhall-lang/Prelude/raw/e44284bc37a5808861dacd4c8bd13d18411cb961/Natural/sum

in ...

This would also solve another feature request in #70 where a person could provide an environment variable fallback if a URL was not available:

http://some.svc/ ? env:SERVICE_FALLBACK

... where SERVICE_FALLBACK might contain a Dhall expression or a new URL to import

You could also use the same operator to fall back to a pure expression. For example, if you wanted to gracefully fall back if an environment variable is missing, you could make the environment variable Optional, like this:

[ env:USER as Text ] : Optional Text ? [] : Optional Text
@f-f
Copy link
Member

f-f commented May 26, 2018

This sounds like a really good idea that would fix a bunch of problems at once, nice 👏

Some questions:

We could add a new ? operator to separate two arbitrary expressions (not necessarily imports)

As I understand it, we want <|> but at the "import resolution" stage, which is "before" compile time.
So if we chain N operations with ?, the first N - 1 should be imports, otherwise the first "pure" expression would shortcut, and this might lead to unintended bugs (OTOH messing up the order of any alternative selection leads to bugs..)

Unrelated: how about the proposal from #70 of having <|> for Bool? Would it make sense to reuse ? (from the semantics and UX points of view)?

@quasicomputational
Copy link
Collaborator

Unrelated: how about the proposal from #70 of having <|> for Bool? Would it make sense to reuse ? (from the semantics and UX points of view)?

I don't think reusing ? would be a good idea because ? can be lazy and avoid hitting mirrors if the primary source succeeds, but <|> would presumably not be able to do that without being a phase violation.

@Gabriella439
Copy link
Contributor Author

@f-f: Yeah, this would be like <|> in the import resolution phase. This also implies that any expression free of imports would short-circuit the chain, but I think that is the desired behavior.

I think whether or not to add (<|>) is an orthogonal decision

@f-f f-f mentioned this issue Jun 9, 2018
@f-f f-f closed this as completed in #172 Jun 13, 2018
f-f added a commit that referenced this issue Jun 13, 2018
This adds support for specifying alternative for imports, as detailed in #163.
We add:
- a `?` operator for specifying alternatives
- a `missing` keyword for impossible imports (identity for the above operator)

Fix #163, fix #70
f-f added a commit to dhall-lang/dhall-haskell that referenced this issue Jun 28, 2018
This implements dhall-lang/dhall-lang#172, by adding
"alternative imports", as detailed in dhall-lang/dhall-lang#163.

We add:
- a `?` operator for specifying alternatives for Imports that fail to resolve
- a `missing` keyword for impossible imports (identity for the above operator)

Also adds tests for all of the above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants