A question about run-time errors when class members are undefined

Anthony Clayden anthony_clayden at clear.net.nz
Sun Oct 14 03:20:01 UTC 2018


Thank you Carlos, but oh dear

this is fast becoming as exasperating as the github rounds.

We've been talking about modular/scoped instances.
All of a sudden you've introduced MPTCs, which nobody even mentioned.
And you have a definition of "reachable" which is not GHC's definition:
'"reachable" is a conservative approximation to "functionally dependent".'
https://downloads.haskell.org/~ghc/7.6.3/docs/html/users_guide/other-type-extensions.html
(That's from an old version of the User's Guide. It works as an
approximation because it assumes a) Functional Dependencies are declared;
b) all methods observe the FunDeps declared for their class; c) all
instances are consistence with the FunDeps. These days GHC has a
better-structured approach, which SPJ explained in his comment on github
last year.)

Are your 1. and 2. below orthogonal? [Good] Then discuss them orthogonally.
Does one build on the other? [Good] Then discuss the logically first one
first.
Does each have independent motivations, and then it's the combination of
them that gives a quantum leap in expressivity? [Maybe good] Then discuss
them independently at first. (Maybe two distinct proposals?)
Are they all tangled together in a mess? [Not good] Then please try to
untangle them before presenting a proposal.

Carter is right, and I take the point.
Haskell-prime is for considering changes to the language standard.
It expects any proposal to be already well-established and stable in at
least one version of Haskell. [**]
The place to discuss 'speculative' proposals is nowadays github.
Even on github, it would strengthen a proposal's appeal if there's an
already-developed prototype, possibly not in Haskell but some related
language. Proofs of useful properties (like Principal typing) add to the
appeal, but are not strongly persuasive without lots of use cases.

> Comments, corrections etc. are welcome. If the ideas are welcome or lead
to a related welcome proposal, ...

Possibly the place to try to clarify the ideas is the cafe. Or maybe if
it's leading towards a proposal, a github 'Issue' rather than 'Pull
request'. (That has the advantage of a more permanent record, and more
markup/formatting than email without being as awkward to format as rst.)

[**] You'd expect by the 'well-established' criterion, MPTCs would be a
strong candidate for considering in Haskell-prime: already anticipated in
1988; two implementations, each from nearly 20 years ago, stable since
about 2006. But there's a whole swirl of difficulties around them, which I
tried to summarise here
https://mail.haskell.org/pipermail/haskell-prime/2018-October/004367.html
It's not exactly MPTCs, but how they interact with other typeclass/instance
features.

In none of the discussion on those github rounds did Carlos/Rodrigo seem to
be aware of those difficulties -- which are well-known and well-documented.

Then to come back to the issue of modular/scoped instances -- i.e. the
infamous 'orphan instances'. For the record [***] let's explain Carter's

>> local scoping for type classes is flat out not gonna happen in the haskell
language standard any time soon.

A. local scoping breaks referential transparency.
   I can't replace a function by its definition, and get the same behaviour.
B. local scoping breaks parametricity.
   The behaviour of a function depends not only on its arguments (value and
type)
   but also on some invisible typing baked into the function from what was
in scope at its definition site.

Referential transparency and Parametricity are powerful, simple principles
for reasoning about programs.
Anything that breaks them is immediately not "simple" IMO.

C. local scoping also breaks something related; I'm not sure if there's a
technical term: 'transparency of type improvement'?
   What gets baked in is an instance selection that includes type
improvement.
   So even if I replace a function by its definition, **and** give it
exactly the type signature from its definition site,
   I still get different behaviour -- specifically different type
improvement.

With C. we threaten type soundness: the compiler would be entitled to use
either of the definition site's type improvement or the usage site's or
both. And then infer different types for the same expression. And then get
a segfault in the executable. If Carlos/team haven't experienced that yet
in their prototypes, that's down to pure luck and GHC being so
well-structured. (I'd guess GHC would catch it at the core lint typecheck,
as a last resort.) The threat to soundness is Edward K's concern here
https://mail.haskell.org/pipermail/haskell-prime/2018-April/004358.html



[***] I say "for the record", partly to give a heads up to Juriaan, who
started this thread, for educational/learning purposes;
and partly because it's surprising how often 'local instances' come up on
Haskell-prime. Where too often = more than never.


AntC


On Sat, 13 Oct 2018 at 4:04 AM, <camarao at dcc.ufmg.br> wrote:

> Hi.
>
> A concise proposal for the introduction of MPTCs in Haskell follows.
> A similar ghc-proposal has been written before, but without success
> (certainly it would be better if some experimentation in ghc was done
> first, as Carter suggested). The proposal is based essentially on the
> following (1. crucial, 2. desirable):
>
>     1. Change the ambiguty rule.
>
>        Consider for example:
>
>        class F a b where f:: a → b
>        class X a   where x:: a
>        fx = f x
>
>        The type of fx, (F a b, X a) ⇒ b, should not be ambiguous: in
>        distinct contexts, fx can have distinct types (if ambiguity, and
>        'improvement', are as defined below). Note: agreeing with this
>        view can lead to far-reaching consequences, e.g. support of
>        overloaded record fields [1,Section 7], polymonads [2] etc.
>
>        Further examples can be discussed but this example conveys the
>        main idea that ambiguity should be changed; unlike the example
>        of (show . read), no type annotation can avoid ambiguity of
>        polymorphic fx in current Haskell.
>
>        Ambiguity should be a property of an expression, defined as
>        follows: if a constraint set C on the constrained type C∪D⇒t of
>        an expression has unreachable variables, then satisfiability is
>        tested for C (a definition of reachable and unreachable type
>        variables is at the end of this message), and:
>
>         - if there is a single unifying substitution of C with the set
>           of instances in the current context (module), then C can be
>           removed (overloading is resolved) and C∪D⇒t 'improved' to
>           D⇒t;
>
>         - otherwise there is a type error: ambiguity if there are two
>           or more unifying substitutions of C with the set of instances
>           in the current context (module), and unsatisfiability
>           otherwise.
>
>    2. Allow instances to be imported (all instances are assumed to be
>       exported):
>
>             import M (instance A τ₁ ⋯ τₙ , … )
>
>                specifies that the instance of τ₁ ⋯ τₙ for class A is
>                imported from M, in the module where the import clause
>                occurs.
>
>        In case of no explicit importation, all instances remain
>        imported, as currently in Haskell (in order that well-typed
>        programs remain well-typed).
>
> Comments, corrections etc. are welcome. If the ideas are welcome or
> lead to a related welcome proposal, then a detailed one, with changes
> to the Haskell report, can be worked out.
>
> (Definition: [Reachable and unreachable type variables in constraints]
> Consider a constrainted type C∪D⇒τ. A variable a ∈ tv(C) is reachable
> from V = tv(τ) if a ∈ V or if a ∈ tv(π) for some π ∈ C such that there
> exists b ∈ tv(π) such that b is reachable from V; otherwise it is
> unreachable.
> For example, in (F a b, X a) ⇒ b, type variable 'a' is reachable from
> { b }, because 'a' occurs in constraint F a b, and b is reachable.
> Similarly, if C = (F a b, G b c, X c), then c is reachable from {a}.)
>
> Kind regards,
>
> Carlos
>
> [1]  Optional Type Classes for Haskell,
>       Rodrigo Ribeiro, Carlos Camarão, Lucília Figueiredo, Cristiano
> Vasconcellos,
>       SBLP'2016 (20th Brazilian Symposium on Programming Languages),
>       Marília, SP, September 19-23, 2016.
>
> [2]
>
> https://github.com/carlos1camarao/ghc-proposals/blob/d81c1f26298961ac635ce0724bb76164b418866b/expression-ambiguity.rst
>
> ===
> Em 2018-10-10 22:16, Anthony Clayden escreveu:
> > On Mon, 8 Oct 2018 at 8:41 PM, Simon Peyton Jones  wrote
> >
> >> You may be interested in Carlos Camarao’s interesting work.  For a
> >> long time now he has advocated (in effect) making each function into
> >> its own type class, rather that grouping them into classes.
> >> Perhaps that is in line with your thinking.
> >
> >>
> >
> > Could I ask Simon direct, since it was he who introduced the topic.
> > When you say "interesting work", what is that evaluation based on? Is
> > there a paper or summary you've seen that expresses the ideas?
> > (Because despite the exhaustive and exhausting rounds on github last
> > year, and further comment on this thread, I just can't see anything
> > workable. And the papers that Carlos references ring alarm bells for
> > me, just looking at the Abstracts, let alone delving into the
> > impenetrable type theory.)
> >
> > And could I ask Carlos: are we allowed to know who peer-reviewed your
> > papers? Specifically, was it someone who's up with the state of the
> > art in GHC?
> >
> > Carlos/his team are making bold claims: to provide the functionality
> > of FunDeps/Type functions, Overlapping Instances/Closed Type Families,
> > to avoid the infamous `read . show` ambiguity, to avoid the equally
> > infamous 'orphan instances' incoherence, to preserve principal typing,
> > and to keep it "simple".
> >
> > Then I have to say that if there's evidence for those claims, it's not
> > appearing in the papers. Really the only example presented is `read .
> > show` (plus record field disambiguation). Yes I'd hope the approach
> > for a simple example is "simple". It doesn't seem any more simple than
> > putting an explicit type signature (or we could use type application
> > `@` these days). But I don't expect that would be the place to show
> > off the power/expressivity.
> >
> > Thank you
> > AntC
> > _______________________________________________
> > Haskell-prime mailing list
> > Haskell-prime at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-prime
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-prime/attachments/20181014/a8c83435/attachment-0001.html>


More information about the Haskell-prime mailing list