[Haskell-cafe] Syntax extension - adding import support to let/where bindings

Tikhon Jelvis tikhon at jelv.is
Wed Aug 5 18:11:44 UTC 2015


If you want a direct experience report, I've written a fair amount of OCaml
which supports pretty much exactly this feature[1]:

    let open List in ...

They even have a weird concise version of the syntax:

    List.(...)

This is quite useful in practice, and doesn't seem to cause any problems.
However, OCaml has different norms around external modules, so it might not
translate to Haskell the same way.

In OCaml, every module is automatically imported qualified. There's no list
of import statements at the top. Importing ("opening") a module is
equivalent to an *unqualified* import in Haskell and happens rarely.
Moreover, it can appear in any part of the file. In practice this doesn't
seem to be a problem, but that could be because nobody's depending on
having all the imports conveniently declared up-front. Personally, I think
this trade-off is perfectly acceptable: I'd rather have life be better for
programmers and worse for tools than vice-versa.

OCaml also doesn't have typeclasses or the open world assumption which
would lead to confusing behavior one way or another.

With all that in mind, OCaml's feature feels closer to Elliot's suggestion:
it's more about locally *dequalifying* a module than importing it. Doing
just that also fits better with Haskell's current import and typeclass
system. However, I'm not sure how to design a local dequalifying statement
in a way that's not confusing.

[1]: http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual021.html#toc77

On Wed, Aug 5, 2015 at 10:46 AM, Elliot Cameron <
elliot.cameron at covenanteyes.com> wrote:

> My initial attempt seems to have failed:
>
>
>
> +1 on the idea.
>
>
>
> I wonder if full-blown “import” is overkill for the desired effect. Many
> languages simply allow you to de-qualify a namespace within a smaller
> scope. I’m thinking of C++ at the moment: { using namespace std; … }
>
>
>
> I think this would be preferable because it would still require that a
> module declare its “import dependencies” in a known place. I’m imagining
> chaos from large source files with several dozen import dependencies, but
> only a few of them defined in the “normal” place. Not to mention this would
> solve some of the worries about tooling, etc.
>
>
>
> That said, there is syntactical boon from re-using the “import” keyword.
> Yet I don’t think it’s a stretch to make inlined imports be constrained by
> the module’s imports. It’s a simple compiler error: “Foo cannot be imported
> inline because it is not imported by the module”.
>
>
>
> With or without the constraint, this would be an excellent feature.
>
>
>
> Elliot
>
>
>
>
>
> *From:* Haskell-Cafe [mailto:haskell-cafe-bounces at haskell.org] *On Behalf
> Of *Oliver Charles
> *Sent:* Wednesday, August 5, 2015 12:56 PM
> *To:* Evan Laforge
> *Cc:* Haskell Cafe
> *Subject:* Re: [Haskell-cafe] Syntax extension - adding import support to
> let/where bindings
>
>
>
>
>
> On Wed, Aug 5, 2015 at 5:43 PM Evan Laforge <qdunkan at gmail.com> wrote:
>
> > In practice I use a lot more than just two symbols. The point is the
> > repeated qualification quickly introduces more noise and obscures the
> intent
> > of the code.
>
> Well, qualification is only necessary for the symbols that conflict,
> right?  It seems to me that if you want an EDSL with a certain
> prelude, you have to make sure the prelude symbols are all distinct.
> If you want to compose two DSLs, then you could make a third prelude
> that imports the other two, but renames colliding symbols.
>
>
>
> At this point I am working for the compiler (and in this case doing a lot
> of work!), but it should be the other way round - this makes me sad.
>
>
>
> Unless there are many collisions... in which case, maybe don't define
> your EDSLs like that in the first place?
>
>
>
> My EDSLs for HTML and CSS are meant to reflect those actual languages as
> closely as possible. If I start renaming things just to "fit in" with other
> symbols, then I've started adding burden on my users.
>
>
>
> Also, let's not forget this proposal is useful for more than just EDSLs,
> so lets not get too caught up on that - for example, one might wish to
> import Data.Text.Lazy or Data.Text in different locations depending on what
> they are working with. There are many packages out there with conflicting
> symbols that have fairly "localised" use sites, but at a granularity of a
> top-level definition rather than a module.
>
>
>
> Currently if you want to  figure out all imports you parse the top of
> the file and can stop at the first definition.  But with this feature
> you have to parse the whole file and thus understand all haskell
> grammar, including all extensions in use.  I'd have to give up on my
> fast deps chaser and switch to slow ghc -M... which is maybe the right
> way anyway, I don't know.
>
> Ok, to be fair, I wouldn't, because I could choose to not use that
> feature, but in *theory* :)  And while "you don't have to use it" is
> always brought up, it seems to me the more successful the feature is
> the more likely you do have to use it.
>
>
>
> It makes me sad if we can't progress the language on the grounds that
> people's attempts at parsing the source code themselves would break. If you
> want to know all the imports, then we should be providing this information
> through tools for people to consume.
>
>
>
> On the other hand, lots of languages have a "local open" feature like
> this.  I think many of them make you first import the module, and then
> you can "open" it in a local scope.  This would address both my "parse
> the whole file for imports" objection and the "what about instances",
> because module importing would be unchanged.
>
>
>
> Indeed, this could be a path forward. I'm not really familiar with any
> languages that do this, could you link to some examples of how this works
> in other languages?
>
>
>
> *ocharles*
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150805/97a12a77/attachment.html>


More information about the Haskell-Cafe mailing list