Discussion:
[Agda] I want implicit coercions in Agda
Martin Escardo
2018-11-08 20:16:03 UTC
Permalink
... and I am sure many other people do too.

Many other proof assistants have them.

Is there a philosophical reason for that, or is this something that just
hasn't been done yet?

Real-life mathematics has them in abundance, and it would be nice to
reflect this in Agda.

I would like to press for a design and implementation of this in Agda. :-)

Best,
Martin
Jesper Cockx
2018-11-09 08:37:14 UTC
Permalink
I think this is related to the problem of subtyping / cumulativity. The
reasons why we don't have it are also similar: a combination of

1. Lack of manpower
2. A rather large codebase which makes many implicit assumptions about the
internal state, so any major change usually requires a lot of refactoring
3. Existing literature on these subjects often omits details crucial to the
implementation, such as how it interacts with metavariables and constraint
solving
4. Concerns about adding too much feature bloat to Agda, making it harder
to maintain and making the move towards an eventual core language more
difficult

That being said, having implicit coercions would certainly be very nice in
many situations, so it would be great if someone would do the experiment to
add them to Agda.

An interesting comment in the Agda source code (TypeChecking.Conversion):

-- | @coerce v a b@ coerces @v : a@ to type @b@, returning a @v' : b@
> -- with maybe extra hidden applications or hidden abstractions.
> --
> -- In principle, this function can host coercive subtyping, but
> -- currently it only tries to fix problems with hidden function types.
>

If someone is interested in giving it a try then that'd maybe be a good
place to start.

-- Jesper

On Thu, Nov 8, 2018 at 9:16 PM Martin Escardo <***@cs.bham.ac.uk>
wrote:

> ... and I am sure many other people do too.
>
> Many other proof assistants have them.
>
> Is there a philosophical reason for that, or is this something that just
> hasn't been done yet?
>
> Real-life mathematics has them in abundance, and it would be nice to
> reflect this in Agda.
>
> I would like to press for a design and implementation of this in Agda. :-)
>
> Best,
> Martin
> _______________________________________________
> Agda mailing list
> ***@lists.chalmers.se
> https://lists.chalmers.se/mailman/listinfo/agda
>
Miëtek Bak
2018-11-17 00:23:31 UTC
Permalink
Martin Odersky is proposing to get rid of implicits in Scala 3. I would like to draw your attention to the following quote:

“… most applications of implicit conversions have turned out to be of dubious value. The problem is that many newcomers to the language start with defining implicit conversions since they are easy to understand and seem powerful and convenient. … But the problem remains that syntactically, conversions and values just look too similar for comfort.”

https://github.com/lampepfl/dotty/pull/5458

Personally, I found implicit conversions greatly increase the difficulty of understanding other people’s code, and I would be happier if they did not make their way into Agda.

--
(mb)


On Fri, Nov 9, 2018, at 08:37, Jesper Cockx wrote:
> I think this is related to the problem of subtyping / cumulativity. The
> reasons why we don't have it are also similar: a combination of
>
> 1. Lack of manpower
> 2. A rather large codebase which makes many implicit assumptions about the
> internal state, so any major change usually requires a lot of refactoring
> 3. Existing literature on these subjects often omits details crucial to the
> implementation, such as how it interacts with metavariables and constraint
> solving
> 4. Concerns about adding too much feature bloat to Agda, making it harder
> to maintain and making the move towards an eventual core language more
> difficult
>
> That being said, having implicit coercions would certainly be very nice in
> many situations, so it would be great if someone would do the experiment to
> add them to Agda.
>
> An interesting comment in the Agda source code (TypeChecking.Conversion):
>
> -- | @coerce v a b@ coerces @v : a@ to type @b@, returning a @v' : b@
> > -- with maybe extra hidden applications or hidden abstractions.
> > --
> > -- In principle, this function can host coercive subtyping, but
> > -- currently it only tries to fix problems with hidden function types.
> >
>
> If someone is interested in giving it a try then that'd maybe be a good
> place to start.
>
> -- Jesper
>
> On Thu, Nov 8, 2018 at 9:16 PM Martin Escardo <***@cs.bham.ac.uk>
> wrote:
>
> > ... and I am sure many other people do too.
> >
> > Many other proof assistants have them.
> >
> > Is there a philosophical reason for that, or is this something that just
> > hasn't been done yet?
> >
> > Real-life mathematics has them in abundance, and it would be nice to
> > reflect this in Agda.
> >
> > I would like to press for a design and implementation of this in Agda. :-)
> >
> > Best,
> > Martin
> > _______________________________________________
> > Agda mailing list
> > ***@lists.chalmers.se
> > https://lists.chalmers.se/mailman/listinfo/agda
> >
> _______________________________________________
> Agda mailing list
> ***@lists.chalmers.se
> https://lists.chalmers.se/mailman/listinfo/agda
Sergei Meshveliani
2018-11-17 10:29:53 UTC
Permalink
On Sat, 2018-11-17 at 00:23 +0000, Miëtek Bak wrote:
> Martin Odersky is proposing to get rid of implicits in Scala 3.
> I would like to draw your attention to the following quote:
>
> “… most applications of implicit conversions have turned out to be of dubious value.
> The problem is that many newcomers to the language start with defining
> implicit conversions since they are easy to understand and seem
> powerful and convenient. … But the problem remains that syntactically,
> conversions and values just look too similar for comfort.”
>
> https://github.com/lampepfl/dotty/pull/5458
>
> Personally, I found implicit conversions greatly increase the difficulty of understanding
> other people’s code, and I would be happier if they did not make
> their way into Agda.
>

Consider the two examples.

Example I.
Defining multiplication in the direct product of two semigroups

_∙_ : Op₂ (A × B)
(x , y) ∙ (x' , y') = (x ∙₁ x' , y ∙₂ y') --(I.1)

-- (x ∙ x' , y ∙ y') --(I.2)

Do the subscripts in ∙₁, ∙₂ make the program (I.1) easier to
read/understand than (I.2) ? Is implicit conversion desirable here?
I doubt.

Example II.
homomorphismPreservesInversion
(monoidHomo ((f , f-cong) , f∙homo) f-preserves-ε) x =
begin≈'
f x' ≈'[ ≈'sym (∙ε' fx') ]
fx' ∙' ε' ≈'[ ∙'cong2 (≈'sym (x∙'x⁻¹ fx)) ]
fx' ∙' (fx ∙' fx ⁻¹') ≈'[ ≈'sym (≈'assoc fx' fx (fx ⁻¹')) ]
(fx' ∙' fx) ∙' fx ⁻¹' ≈'[ ∙'cong1 (≈'sym (f∙homo x' x)) ]
f (x' ∙ x) ∙' fx ⁻¹' ≈'[ ∙'cong1 (f-cong (x⁻¹∙x x)) ]
f ε ∙' fx ⁻¹' ≈'[ ∙'cong1 f-preserves-ε ]
ε' ∙' fx ⁻¹' ≈'[ ε'∙ (fx ⁻¹') ]
(f x) ⁻¹'
end≈'
where
x' = x ⁻¹; fx = f x; fx' = f x'

Here the carriers of G and G' are C and C',
≈ is on C, ≈' is on C' (by renaming),
_∙_ on C, _∙'_ on C',
ε is of G, ε' of G',
_⁻¹ is of G, _⁻¹' of G',
and so on.

It is desirable to make the code more readable by canceling some of the
above renaming. For example, to replace ε' with ε and _∙'_ with _∙_
-- at least in the right hand column.

Another point
-------------
In textbooks on mathematics they most often write things like

f(x+y) = f(x) + f(y)

(the definition of f : A -> B being a homomorphism)
with `+' defined on two different domains.
Also they write
[m] * [n] =def [m * n]

as a definition for the product of residues, where [k] denotes the
residue class of an integer k modulo some p.

There are many other examples.

Generally, we need to respect mathematical style.
It is based on experience of many centuries.
Most of the mathematical community really understands which book is
easier to read.
I tend to trust this approach more.

But there is a fashion for denotation in science, and it changes.
But the implicit conversion style remains stable. I think this will
remain.

Similarly as a reader understands implicit conversion in a mathematical
book, a compiler and a reader of a program need to understand implicit
conversion in a program.

On the other hand, programs have more difficulties with implicit
conversion than mathematical texts.
I observe this on my experience with Haskell and Agda.
For example, several years ago I was stuck with about 500 times slow
down in type checking in my program due to a single usage of "open Foo
{{...}}".

May be, the reason for this is that compilers are not enough wise, it
needs a great effort to make them wiser.

I think that
a) compilers need to understand implicit conversion as much as possible,
and future compilers will do this,
b) a programmer has to choose wisely where to apply implicit conversion,
c) for each compiler system being developed there is a priority in
developing features, for example, I cannot advise about the priority
of implicit conversion in the Agda type checker, let the Agda team
decide.

Regards,

------
Sergei
Martin Escardo
2018-11-17 11:40:11 UTC
Permalink
On 17/11/2018 10:29, ***@botik.ru wrote:

> Generally, we need to respect mathematical style.
> It is based on experience of many centuries.
> Most of the mathematical community really understands which book is
> easier to read.
> I tend to trust this approach more.

Exactly. I fully agree.

If implicit coercions are ever implemented, I propose that the emacs
mode and the html rendering highlight the implicitly coerced things with
a different color so that the reader can see that there is a coercion
being applied.

Martin
Miëtek Bak
2018-11-17 15:25:38 UTC
Permalink
On Sat, Nov 17, 2018, at 11:40, Martin Escardo wrote:
> On 17/11/2018 10:29, ***@botik.ru wrote:
>
> > Generally, we need to respect mathematical style.
> > It is based on experience of many centuries.
> > Most of the mathematical community really understands which book is
> > easier to read.
> > I tend to trust this approach more.
>
> Exactly. I fully agree.

I completely disagree. Ambiguous notation is one of the gravest sins that continue to be inflicted upon students of mathematics.

Leslie Lamport writes:

“Mathematicians think that the logic of the proofs they write is completely obvious, but our examination … shows that they are wrong. Students are expected to learn how to write logically correct proofs from examples that, when read literally, are illogical. … It is little wonder that so few of them succeed.”

“Is it crazy to think that students who cannot learn to write proofs in prose can learn to write them in an unfamiliar formal language and get a computer to check them? Anyone who finds it crazy should consider how many students learn to write programs in unfamiliar formal languages and get a computer to execute them, and how few now learn to write proofs.”

> If implicit coercions are ever implemented, I propose that the emacs
> mode and the html rendering highlight the implicitly coerced things with
> a different color so that the reader can see that there is a coercion
> being applied.

Your proposal recognises that implicit coercions are detrimental to understanding. Highlighting implicit coercions in colour would still not help the reader resolve which definitions are actually being used. I would prefer for Agda programs to remain independently checkable, whether they are written by hand, printed out, or displayed on screen.

--
(mb)
Martin Escardo
2018-11-17 15:53:46 UTC
Permalink
If you want mathematicians to use proof assistants, you have to
implement the notations they use, not the notations that computer
scientists / engineers find convenient to write computer programs. Martin

On 17/11/2018 15:25, ***@bak.io wrote:
> On Sat, Nov 17, 2018, at 11:40, Martin Escardo wrote:
>> On 17/11/2018 10:29, ***@botik.ru wrote:
>>
>>> Generally, we need to respect mathematical style.
>>> It is based on experience of many centuries.
>>> Most of the mathematical community really understands which book is
>>> easier to read.
>>> I tend to trust this approach more.
>>
>> Exactly. I fully agree.
>
> I completely disagree. Ambiguous notation is one of the gravest sins that continue to be inflicted upon students of mathematics.
>
> Leslie Lamport writes:
>
> “Mathematicians think that the logic of the proofs they write is completely obvious, but our examination … shows that they are wrong. Students are expected to learn how to write logically correct proofs from examples that, when read literally, are illogical. … It is little wonder that so few of them succeed.”
>
> “Is it crazy to think that students who cannot learn to write proofs in prose can learn to write them in an unfamiliar formal language and get a computer to check them? Anyone who finds it crazy should consider how many students learn to write programs in unfamiliar formal languages and get a computer to execute them, and how few now learn to write proofs.”
>
>> If implicit coercions are ever implemented, I propose that the emacs
>> mode and the html rendering highlight the implicitly coerced things with
>> a different color so that the reader can see that there is a coercion
>> being applied.
>
> Your proposal recognises that implicit coercions are detrimental to understanding. Highlighting implicit coercions in colour would still not help the reader resolve which definitions are actually being used. I would prefer for Agda programs to remain independently checkable, whether they are written by hand, printed out, or displayed on screen.
>

--
Martin Escardo
http://www.cs.bham.ac.uk/~mhe
Peter Hancock
2018-11-17 16:54:12 UTC
Permalink
On 17/11/2018 15:53, Martin Escardo wrote:
> If you want mathematicians to use proof assistants, you have to
> implement the notations they use, not the notations that computer
> scientists / engineers find convenient to write computer programs.
> Martin

You might as well say that if you want mathematicians to use
type-setting software you have to implement the notations they use.
The mathematicians I know seem to find their way pretty well around LaTeX, etc,
for all its barbarity.

Perhaps one can think of mathematical notation as a matter of DSLs.
LaTeX is full of DSL-like apparatus for things like category theory diagrams, etc.
I think the support in Agda for equational reasoning might be
a nice example of a DSL.

Is there a general definition of coercion? Is there a connection with identity types?

Is changing coordinates from Cartesian to Polar something a coercion might do?
Sergei Meshveliani
2018-11-17 18:14:00 UTC
Permalink
On Sat, 2018-11-17 at 16:54 +0000, Peter Hancock wrote:
> On 17/11/2018 15:53, Martin Escardo wrote:
> > If you want mathematicians to use proof assistants, you have to
> > implement the notations they use, not the notations that computer
> > scientists / engineers find convenient to write computer programs.
> > Martin
>
> You might as well say that if you want mathematicians to use
> type-setting software you have to implement the notations they use.
> The mathematicians I know seem to find their way pretty well around LaTeX, etc,
> for all its barbarity.
>
> Perhaps one can think of mathematical notation as a matter of DSLs.
> LaTeX is full of DSL-like apparatus for things like category theory diagrams, etc.
> I think the support in Agda for equational reasoning might be
> a nice example of a DSL.
>
> Is there a general definition of coercion? Is there a connection with identity types?
>
> [..]

Coercion has not sense in general.
But mathematicians (S.Lang and Van der Waerden, and many others) write
that a group homomorphism is a map f that satisfies
the equality
f(xy) = f(x)f(y),

that a ring homomorphism needs to satisfy

f(x+y) = f(x) + f(y),
and so on.
These books write so, because this way it is easier to explain and to
understand. This has been tested by thousands of professors and millions
of students.

Generally, compilers are forced to follow science (mathematics).

* For example, in early days programs used numbers instead of names.
Then they have improved.
* They used different functions to add polynomials over integers,
to add polynomials over rationals, over polynomial, and so on.
Then, they have improved, and introduced generic programming -- this
follows mathematics.
* In mathematics, domains depend on a value, and this value can be
computed dynamically, and a function can return a domain.
Compilers could not handle this.
But they have improved, and now they can.

* Similar it is (or will be) with implicit conversion.

Science has more sense than programming, science is wiser than a
technology, it is fundamental. And programming has to follow science.

Regards,

------
Sergei
Apostolis Xekoukoulotakis
2018-11-17 18:58:36 UTC
Permalink
Is there a way to inspect a term in the context of a function? To find its
type and the type of the variables it depends on?
It seems that all you can do is be directed to the definition of a function
and nothing more.

Holes are very useful when you write a proof. As soon as you have written
the proof , you lose all information about it except from what you can see
in the text.
So here is an oxymoron. It is easier to write proofs than to understand
already written ones.
I too believe that the future is the use of interactive tools to inspect
the code.

As far as replacing hand written proofs all together , I am not so sure it
is possible. I hope that one day we get there , though.

On Sat, Nov 17, 2018 at 8:14 PM Sergei Meshveliani <***@botik.ru> wrote:

> On Sat, 2018-11-17 at 16:54 +0000, Peter Hancock wrote:
> > On 17/11/2018 15:53, Martin Escardo wrote:
> > > If you want mathematicians to use proof assistants, you have to
> > > implement the notations they use, not the notations that computer
> > > scientists / engineers find convenient to write computer programs.
> > > Martin
> >
> > You might as well say that if you want mathematicians to use
> > type-setting software you have to implement the notations they use.
> > The mathematicians I know seem to find their way pretty well around
> LaTeX, etc,
> > for all its barbarity.
> >
> > Perhaps one can think of mathematical notation as a matter of DSLs.
> > LaTeX is full of DSL-like apparatus for things like category theory
> diagrams, etc.
> > I think the support in Agda for equational reasoning might be
> > a nice example of a DSL.
> >
> > Is there a general definition of coercion? Is there a connection with
> identity types?
> >
> > [..]
>
> Coercion has not sense in general.
> But mathematicians (S.Lang and Van der Waerden, and many others) write
> that a group homomorphism is a map f that satisfies
> the equality
> f(xy) = f(x)f(y),
>
> that a ring homomorphism needs to satisfy
>
> f(x+y) = f(x) + f(y),
> and so on.
> These books write so, because this way it is easier to explain and to
> understand. This has been tested by thousands of professors and millions
> of students.
>
> Generally, compilers are forced to follow science (mathematics).
>
> * For example, in early days programs used numbers instead of names.
> Then they have improved.
> * They used different functions to add polynomials over integers,
> to add polynomials over rationals, over polynomial, and so on.
> Then, they have improved, and introduced generic programming -- this
> follows mathematics.
> * In mathematics, domains depend on a value, and this value can be
> computed dynamically, and a function can return a domain.
> Compilers could not handle this.
> But they have improved, and now they can.
>
> * Similar it is (or will be) with implicit conversion.
>
> Science has more sense than programming, science is wiser than a
> technology, it is fundamental. And programming has to follow science.
>
> Regards,
>
> ------
> Sergei
>
> _______________________________________________
> Agda mailing list
> ***@lists.chalmers.se
> https://lists.chalmers.se/mailman/listinfo/agda
>
Sergei Meshveliani
2018-11-18 09:00:03 UTC
Permalink
On Sat, 2018-11-17 at 16:54 +0000, Peter Hancock wrote:
> On 17/11/2018 15:53, Martin Escardo wrote:
> > If you want mathematicians to use proof assistants, you have to
> > implement the notations they use, not the notations that computer
> > scientists / engineers find convenient to write computer programs.
> > Martin
>
> You might as well say that if you want mathematicians to use
> type-setting software you have to implement the notations they use.
> The mathematicians I know seem to find their way pretty well around LaTeX, etc,
> for all its barbarity.
>
> Perhaps one can think of mathematical notation as a matter of DSLs.
> LaTeX is full of DSL-like apparatus for things like category theory diagrams, etc.
> I think the support in Agda for equational reasoning might be
> a nice example of a DSL.
>
> Is there a general definition of coercion? Is there a connection with identity types?
>



> Is changing coordinates from Cartesian to Polar something a coercion might do?


Coercion (if implemented) can do all transformations between all types.
It depends on which coercion instances are declared in a program.
We can set

record Coerce {α β} {A : Set α} {B : Set β} : Set (α ⊔ β) where
field
coerce : A → B
--
-- a general notion of coercion

and define

instance
Cartesian→Polar : Coerce {Cartesian} {Polar}
Cartesian→Polar =
record{ coerce = transform }
where
transform : Cartesian → Polar
transform cart =
by the known formulae

Having types and classes in a program, and having a set of Coercion
instances in a program, and an expression E : T,
the type checker needs to solve: what sequence of coercions to apply to
which part in E so that E to have a type T and the operations in E to
agree with the types.

For example:

f : Cartesian → Rational
f v = (norm v) + (radius v)

v is a vector represented by a list of rational numbers, and
(norm v) : Rational.
Suppose that radius is an operation of the class related to Polar.
But v is not of Polar.
The type checker sees the coercion instance Cartesian→Polar. It has to
apply it and see whether the result agrees with types.
And the program is converted internally to

f v = (norm v) +q (radius v$$Polar),

where _+q_ is _+_ instanced from implicit to addition on Rational,
and
v$$Polar = coerce {Cartesian} {Polar} v.



> Is there a general definition of coercion?

The above discourse is something like a general definition of coercion.
I expect that coercion solutions are found by some kind of term
unification tactic. There may be many solutions found, and problems ...
Still, I expect, the tool is practicable.

Implicit instance resolution looks like a special case of implicit
coercion resolution.
I think that I need first to try implicit instances, as they are already
given in Agda.
There my be overlapping instances, and their related problems.
But the programmer always can set enough of explicit things to obtain a
unique resolution.

The problem of powerful implicit instance resolution is difficult for
implementation. But Agda already has a certain resolution.
We need to see what real possibilities does it present, and what
problems it causes.

Regards,

------
Sergei
Jon Sterling
2018-11-17 16:32:50 UTC
Permalink
I'm a little sympathetic to Mietek's point of view here, because I've been in the boat where I found for the longest time (and still do in many cases) the conventions of mathematical writing to be just totally perplexing. Now that I'm more used to it, I find it easier going, but even so, the boundary between sensible overloading and perverse overloading is a very fine one, and rarely do mathematical texts actually walk that boundary in a satisfactory way.

But the thing that convinces me of the importance of *some* way to resolve syntactically ambiguous notations (regardless of whether it is realized through implicit coercion, unification hints, type classes, etc.) is that when building hierarchies of structures, the specifics of how you cooked up the hierarchy (and there are always a million different ways to do this) have a huge effect on what the fully-explicit form of some operator will be. Another point worth mentioning is that even within a *single* discipline for mathematical hierarchies, there may be multiple (commuting) ways to access the same operator, so it is really not so good to be forced to choose one of those in the user-level code.

Some kind of implicit resolution of these things can do a lot to grease the wheels. With that said, I've been unsatisfied and frustrated with all attempts at such implicit resolution that I've used. But I haven't used all of them :)

---

I do not believe that one should necessarily be able to grasp everything that is happening by looking at textual code. Far from being a weakness, the fact that we can (in theory) query our IDE to show us more and more information iteratively is a *strength* of digital presentation formats for mathematics. Back in the paleolithic period, the 1980s, the proof assistant Nuprl introduced a very flexible form of notation extension which did not even require all the constituents of a piece of syntax to be represented in the displayed form -- this was possible because Nuprl used a structure editor with a lossy renderer. The idea was that you could point and click, or use some keystrokes, to explore a term, and explode it to expose more and more information, etc.

A more moderate (and possibly more sensible) version of this interaction style is within our grasp today, where we just omit whatever can be resolved by our most powerful elaborators and unification engines -- and this style is compatible with purely textual formats, not requiring any "rendering" of some more detailed format. Being able to inspect the type of something in Agda's emacs mode is only the beginning. I believe that Idris's emacs mode contains more sophisticated features along these lines, where you can even inspect code that appears quoted in an error message! (Maybe Agda has this now too, but I haven't checked.)

My experience with formalizing mathematics is that one needs to be able to interact with the artifact both at a high level (eyes squinted) and at a low level, seeing every minute detail. We should take advantage of the fact that our editing interfaces are interactive, rather than requiring all this detail to be inlined into the textual representation.

Best,
Jon


On Sat, Nov 17, 2018, at 7:25 AM, Miëtek Bak wrote:
> On Sat, Nov 17, 2018, at 11:40, Martin Escardo wrote:
> > On 17/11/2018 10:29, ***@botik.ru wrote:
> >
> > > Generally, we need to respect mathematical style.
> > > It is based on experience of many centuries.
> > > Most of the mathematical community really understands which book is
> > > easier to read.
> > > I tend to trust this approach more.
> >
> > Exactly. I fully agree.
>
> I completely disagree. Ambiguous notation is one of the gravest sins
> that continue to be inflicted upon students of mathematics.
>
> Leslie Lamport writes:
>
> “Mathematicians think that the logic of the proofs they write is
> completely obvious, but our examination … shows that they are wrong.
> Students are expected to learn how to write logically correct proofs
> from examples that, when read literally, are illogical. … It is little
> wonder that so few of them succeed.”
>
> “Is it crazy to think that students who cannot learn to write proofs in
> prose can learn to write them in an unfamiliar formal language and get a
> computer to check them? Anyone who finds it crazy should consider how
> many students learn to write programs in unfamiliar formal languages and
> get a computer to execute them, and how few now learn to write proofs.”
>
> > If implicit coercions are ever implemented, I propose that the emacs
> > mode and the html rendering highlight the implicitly coerced things with
> > a different color so that the reader can see that there is a coercion
> > being applied.
>
> Your proposal recognises that implicit coercions are detrimental to
> understanding. Highlighting implicit coercions in colour would still
> not help the reader resolve which definitions are actually being used.
> I would prefer for Agda programs to remain independently checkable,
> whether they are written by hand, printed out, or displayed on screen.
>
> --
> (mb)
>
> _______________________________________________
> Agda mailing list
> ***@lists.chalmers.se
> https://lists.chalmers.se/mailman/listinfo/agda
Martin Escardo
2018-11-18 20:11:57 UTC
Permalink
On 17/11/2018 16:32, ***@jonmsterling.com wrote:
> the boundary between sensible overloading and perverse overloading is a very fine one, and rarely do mathematical texts actually walk that boundary in a satisfactory way.

Indeed, it is an art to write mathematics well (or to write anything at
all well). Most people don't.

It would be very surprising if legislation by the proof-assistant
community would elevate mathematical writing to the levels of, say, Gian
Carlo Rota.

Please let the proof assistants have the modes of expression that
mathematicians do use in practice, and the mathematical community to
judge (as it has done for centuries and millennia) the qualities of the
writings of each mathematician.

You will not solve the shortcomings of my mathematical style by refusing
to implement implicit coercion in Agda.

Best,
Martin
Ulf Norell
2018-11-18 20:35:33 UTC
Permalink
On Sun, Nov 18, 2018 at 9:12 PM Martin Escardo <***@cs.bham.ac.uk>
wrote:

>
> You will not solve the shortcomings of my mathematical style by refusing
> to implement implicit coercion in Agda.
>

I feel that the discussion has derailed a little at this point. The issue
isn't that some people
are stubbornly refusing to implement this one simple thing that would make
life easier for
the practising mathematician. The problem is that implicit coercions are
very difficult to get
right. To move this discussion forward I suggest that you give some of the
following:

- concrete examples of implicit coercions that you'd like to see
- a rough sketch of how they are to be solved
- references to the proof assistants that (in your opinion) have gotten
implicit coercions right
- some notes on why instance arguments are not a good replacement for
implicit coercions

/ Ulf
Martin Escardo
2018-11-18 20:43:44 UTC
Permalink
On 18/11/2018 20:35, ***@gmail.com wrote:
> On Sun, Nov 18, 2018 at 9:12 PM Martin Escardo <***@cs.bham.ac.uk
> <mailto:***@cs.bham.ac.uk>> wrote:
>
>
> You will not solve the shortcomings of my mathematical style by
> refusing
> to implement implicit coercion in Agda.
>
>
> I feel that the discussion has derailed a little at this point. The
> issue isn't that some people
> are stubbornly refusing to implement this

This reply was not directed to the Agda developers, but instead to the
people who ranted against these features.

I will be happy to give a list of things for which implicit coercions
are used by mathematicians, including those of my own Agda developments.

Martin
Jason -Zhong Sheng- Hu
2018-11-18 21:32:30 UTC
Permalink
Since Coq has coersion, I use this functionality there. I assume that the use of coersion is not limited to the agda program text, but should also be presented in the interaction, namely in the proof window, the coersion functions would also be hidden. In this situation, another request would probably follow immediately after coersion is implemented in Agda: add a switch to (temporarily) turn it off.

Sometimes coersion can be confusing. I am not against it (since I use it), but I would say that this functionality could be very misleading if it overshoots too much, so the design of it is very important. I think the reason of this disagreement here is because there is only one view of the program text, so we either have a very high level view but abstract, or a very low level view but too verbose. If we can have a hierarchy of views, which allows us to zoom in and out of mathematics/proofs/programs, I think coersion can have way more value.

Sincerely Yours,

Jason Hu

________________________________
From: Agda <agda-***@lists.chalmers.se> on behalf of Martin Escardo <***@cs.bham.ac.uk>
Sent: November 18, 2018 3:43 PM
To: ***@gmail.com
Cc: Agda mailing list
Subject: Re: [Agda] I want implicit coercions in Agda



On 18/11/2018 20:35, ***@gmail.com wrote:
> On Sun, Nov 18, 2018 at 9:12 PM Martin Escardo <***@cs.bham.ac.uk
> <mailto:***@cs.bham.ac.uk>> wrote:
>
>
> You will not solve the shortcomings of my mathematical style by
> refusing
> to implement implicit coercion in Agda.
>
>
> I feel that the discussion has derailed a little at this point. The
> issue isn't that some people
> are stubbornly refusing to implement this

This reply was not directed to the Agda developers, but instead to the
people who ranted against these features.

I will be happy to give a list of things for which implicit coercions
are used by mathematicians, including those of my own Agda developments.

Martin
Peter Hancock
2018-11-19 04:44:59 UTC
Permalink
On 18/11/2018 21:32, Jason -Zhong Sheng- Hu wrote:
> If we can have a hierarchy of views, which allows us to zoom in and
> out of mathematics/proofs/programs, I think coersion can have way
> more value.

Someone who's opinion is worth having about the presentation of
mathematical material is Lamport. I am thinking about his
specification language TLA+, and its proof system TLP (I think).

These formalisms were designed to be used by engineers, who I suppose
are mathematicians of some kind. Stylistically, they resemble COBOL,
with lots of upper-case keywords, etc. IMHO, they are readable, largely
because notational fuss is kept to a minimum. Things are designed to be
emailable in ASCII.

Most interesting is that TLP is designed for writing informal proofs.
At any point, you can put in an informal "handwave" (a bit like a hole).
A "folding" metaphor is used to browse TLP proofs. You can choose to
hide uninteresting portions of a proof, and drill down to
particular steps in full gory detail. Lamport says one should
write a proof in small enough steps that everything is completely obvious,
then provide one more level of formal proof.

I presume coercions are injective functions that resolve type-clashes between
things like natural numbers and signed integers, or fractions, etc.
There are times when we want to see these things, and there are times
when we would like them to be hidden.
Could one have a "pedantry" knob when editing or browsing agda?
At pedantry level 11 absolutely nothing would be hidden....

Sergie's gave some examples of using "+", "*" etc to mean different
operations in different rings. Is this an example of coercion?
I'm inclined to call it overloading. (Which also helps to dispense with
pedantic crap.)
Apostolis Xekoukoulotakis
2018-11-19 06:08:16 UTC
Permalink
I wonder what is more confusing : Using "+" and "*_i" to denote addition
for Nat and addition for Integers
or "+" and "+" to denote addition for both Nat and Integers.

Using correct symbols that do not confuse is a responsibility that cannot
be avoided even without coercions.

If we are able to inspect terms of a proof , any confusion is removed
entirely, while at the same time simplifying our proof.



On Mon, Nov 19, 2018 at 6:45 AM Peter Hancock <***@fastmail.fm> wrote:

> On 18/11/2018 21:32, Jason -Zhong Sheng- Hu wrote:
> > If we can have a hierarchy of views, which allows us to zoom in and
> > out of mathematics/proofs/programs, I think coersion can have way
> > more value.
>
> Someone who's opinion is worth having about the presentation of
> mathematical material is Lamport. I am thinking about his
> specification language TLA+, and its proof system TLP (I think).
>
> These formalisms were designed to be used by engineers, who I suppose
> are mathematicians of some kind. Stylistically, they resemble COBOL,
> with lots of upper-case keywords, etc. IMHO, they are readable, largely
> because notational fuss is kept to a minimum. Things are designed to be
> emailable in ASCII.
>
> Most interesting is that TLP is designed for writing informal proofs.
> At any point, you can put in an informal "handwave" (a bit like a hole).
> A "folding" metaphor is used to browse TLP proofs. You can choose to
> hide uninteresting portions of a proof, and drill down to
> particular steps in full gory detail. Lamport says one should
> write a proof in small enough steps that everything is completely obvious,
> then provide one more level of formal proof.
>
> I presume coercions are injective functions that resolve type-clashes
> between
> things like natural numbers and signed integers, or fractions, etc.
> There are times when we want to see these things, and there are times
> when we would like them to be hidden.
> Could one have a "pedantry" knob when editing or browsing agda?
> At pedantry level 11 absolutely nothing would be hidden....
>
> Sergie's gave some examples of using "+", "*" etc to mean different
> operations in different rings. Is this an example of coercion?
> I'm inclined to call it overloading. (Which also helps to dispense with
> pedantic crap.)
>
>
>
> _______________________________________________
> Agda mailing list
> ***@lists.chalmers.se
> https://lists.chalmers.se/mailman/listinfo/agda
>
Thorsten Altenkirch
2018-11-19 09:01:20 UTC
Permalink
Another source of coercions are surjections from record subtyping, e.g.
every monoid is a group etc.

On 19/11/2018, 04:44, "Agda on behalf of Peter Hancock"
<agda-***@lists.chalmers.se on behalf of ***@fastmail.fm> wrote:

>I presume coercions are injective functions that resolve type-clashes
>between
>things like natural numbers and signed integers, or fractions, etc.
>There are times when we want to see these things, and there are times
>when we would like them to be hidden.
>Could one have a "pedantry" knob when editing or browsing agda?
>At pedantry level 11 absolutely nothing would be hidden....
>




This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please contact the sender and delete the email and
attachment.

Any views or opinions expressed by the author of this email do not
necessarily reflect the views of the University of Nottingham. Email
communications with the University of Nottingham may be monitored
where permitted by law.
Thorsten Altenkirch
2018-11-19 09:05:26 UTC
Permalink
I meant every group is a monoid...

On 19/11/2018, 09:01, "Agda on behalf of Thorsten Altenkirch"
<agda-***@lists.chalmers.se on behalf of
***@nottingham.ac.uk> wrote:

>Another source of coercions are surjections from record subtyping, e.g.
>every monoid is a group etc.
>
>On 19/11/2018, 04:44, "Agda on behalf of Peter Hancock"
><agda-***@lists.chalmers.se on behalf of ***@fastmail.fm> wrote:
>
>>I presume coercions are injective functions that resolve type-clashes
>>between
>>things like natural numbers and signed integers, or fractions, etc.
>>There are times when we want to see these things, and there are times
>>when we would like them to be hidden.
>>Could one have a "pedantry" knob when editing or browsing agda?
>>At pedantry level 11 absolutely nothing would be hidden....
>>
>
>
>
>
>This message and any attachment are intended solely for the addressee
>and may contain confidential information. If you have received this
>message in error, please contact the sender and delete the email and
>attachment.
>
>Any views or opinions expressed by the author of this email do not
>necessarily reflect the views of the University of Nottingham. Email
>communications with the University of Nottingham may be monitored
>where permitted by law.
>
>
>
>
>_______________________________________________
>Agda mailing list
>***@lists.chalmers.se
>https://lists.chalmers.se/mailman/listinfo/agda




This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please contact the sender and delete the email and
attachment.

Any views or opinions expressed by the author of this email do not
necessarily reflect the views of the University of Nottingham. Email
communications with the University of Nottingham may be monitored
where permitted by law.
Sergei Meshveliani
2018-11-19 11:41:40 UTC
Permalink
On Mon, 2018-11-19 at 04:44 +0000, Peter Hancock wrote:

> [..]
>
> I presume coercions are injective functions that resolve type-clashes between
> things like natural numbers and signed integers, or fractions, etc.


Just any function, not only injective.
Any finite set of such declared in a particular program, similar as with
class instances.
But they can be composed in the process of searching of appropriate
coercion composition.

For example, in

[2] : Z/<3> -- domain of integers modulo 3
a : Z/<3>
a = (5 + 3) + [2]

Here the second + is on the domain Z/<3>. So 5 + 3 -> 8 is expected to
coerce to [8] : Z/<3>, and it equals [2] by taking the remainder by
3, and the whole expression coerces to

[2] + [2]

> Sergie's gave some examples of using "+", "*" etc to mean different
> operations in different rings. Is this an example of coercion?
> I'm inclined to call it overloading. (Which also helps to dispense with
> pedantic crap.)

Also the first `+' is on Nat (or may be Integer), and the last `+' is on
Z/<3>. The method of searching for coercion includes searching for
appropriate operation (class) instances. It this example there are found
two instances of Semigroup -- for _+_.

A finite set of coercion declarations is set in the user program.
This is set similar as class instances.

Can it be called overloading? I do not know.

I know the word `coercion' from the materials of the Axiom library for
scientific computation (it started in about 1980).

I think that coercion has an essential part of instance resolution,
and also specially, for the instances for the class

record Coerce {α β} {A : Set α} {B : Set β} : Set (α ⊔ β) where
constructor coerce
field
$$ : A → B

The following example works. This example is contrived.
Suppose that the programmer puts that any x _can_ be coerced to
(x , x), and any Nat _can_ be coerced to Integer.
And there are also defined instances for some compositions of these
instances:

-------------------------------------------------------------
open import Level using (_⊔_)
open import Function using (_∘_; _$_; const)
open import Relation.Binary using (_Preserves_⟶_)
import Relation.Binary.EqReasoning as EqR
open import Data.Product using (_,_; proj₁; Σ)


record Coerce {α β} {A : Set α} {B : Set β} : Set (α ⊔ β) where
constructor coerce
field
$$ : A → B

open import Data.Nat using (ℕ)
import Data.Nat.Show as NatShow
open import Data.Integer using (ℤ; +_) renaming (show to showℤ)
open import Data.Product using (_×_; _,_)
open import Data.List using (List; []; _∷_)

instance
coerce-ℕ→ℤ : Coerce {A = ℕ} {B = ℤ}
coerce-ℕ→ℤ = coerce +_

coerceToPair : ∀ {α} {A : Set α} → Coerce {A = A} {B = A × A}
coerceToPair =
coerce (\x → (x , x))

coerce-ℕ→ℤ×ℤ : Coerce {A = ℕ} {B = ℤ × ℤ}
coerce-ℕ→ℤ×ℤ =
coerce ($$ {A = ℤ} ∘ $$ {B = ℤ})
where
open Coerce {{...}}

open Coerce {{...}}

f : List ℤ
f = (+ 2) ∷ ($$ 3) ∷ []

g : List (ℤ × ℤ)
g = $$ 1 ∷ $$ 2 ∷ []

h : ℤ × ℤ
h = $$ 2
------------------------------------------------------------------------

Also here h = $$ ($$ {B = ℤ} 2) -- (II)

is resolved by the current Agda, and

h = $$ ($$ 2) -- (III)
is not.

A real coercion will have to make $$ implicit.
Because in mathematics
2 ∷ 3 ∷ [] : List ℤ
can be coerced to
(+ 2) ∷ (+ 3) ∷ []
without showing $$.

May be this can be done as a library written in Agda, via Reflexion
feature, I do not know.

I expect that resolving a coercion for any expression E in a program is
something like finding of an appropriate sequence of class instances to
be composed.
It is needed a flexible way to define a set of coercions, wise options
for searching of a composition.

But I am not sure, I am not a specialist in dependent types resolution.

I wrote Algebra in Haskell, and used explicit $$ of the Coerce class
(do not recall its name), with defining composition instances. It was
useful.
But Haskell (Glasgow extension of the language)
can resolve a certain restricted case of _overlapping instances_,
with choosing the instance most special by substitution.
Though, I think that the feature remains experimental, difficult to
implement, to support.

But it is already useful for application, it looks like an interesting
field for implementors. And for theory too
(suppose that someone proves that a certain version of the feature is
fundamentally incorrect, etc.).

Regards,

------
Sergei
Guillaume Allais
2018-11-19 10:07:15 UTC
Permalink
I agree with Martin that it was important to make the point that
because a feature can be abused, that does not mean it shouldn't
be implemented. I can mimic some of Coq's canonical structures
mechanisms using REWRITE in a perverse way. That doesn't invalidate
all the practical use-cases for it.

Anyway, here is my use-case:

In agdarsec, I have a □ type constructor which corresponds to guarded
recursive calls. Users are given a fixpoint combinator but to be able
to guarantee that all parsers are total, they are only allowed to use
the recursive call in a guarded manner.

Various combinators' types make explicit the fact that some of their
arguments will be guarded. E.g. conjunction:
_<&>_ : ∀[ Parser P A ⇒ □ Parser P B ⇒ Parser P (A × B) ]

Now, that is both crucial and boring. Crucial for totality but boring
from the point of view of the computational behaviour of the function.
It is pretty annoying to have to explicitly wrap non-guarded values
in the guarded modality by using `box : ∀[ Parser P A ⇒ □ Parser P A ]`.

In Idris and Coq I declare `box` as an implicit coercion and whenever
the checker expects a `□ Parser P A` but got a `Parser P A` then it
inserts a call to `box` and keeps checking the rest of the term. This
allows me to write Haskell-looking parsers while getting Agda-strong
guarantees on totality.

IIRC it works well in Coq but I have gotten weird error messages in
Idris where the system was a bit too eager to introduce these and end
up with a type error in a state hard to understand. I wouldn't mind if,
as described informally earlier, they were only introduced if we *know*
they are going to make the term match (as a first approximation: there
are no open metas in the checked & inferred type).

As is the case in Coq & Idris, I would not expect coercions to be chained.
If there is more than one candidate, the coercion should fail (with maybe
a pragma to allow overlap).


On 18/11/2018 21:35, Ulf Norell wrote:
> On Sun, Nov 18, 2018 at 9:12 PM Martin Escardo <***@cs.bham.ac.uk>
> wrote:
>
>>
>> You will not solve the shortcomings of my mathematical style by refusing
>> to implement implicit coercion in Agda.
>>
>
> I feel that the discussion has derailed a little at this point. The issue
> isn't that some people
> are stubbornly refusing to implement this one simple thing that would make
> life easier for
> the practising mathematician. The problem is that implicit coercions are
> very difficult to get
> right. To move this discussion forward I suggest that you give some of the
> following:
>
> - concrete examples of implicit coercions that you'd like to see
> - a rough sketch of how they are to be solved
> - references to the proof assistants that (in your opinion) have gotten
> implicit coercions right
> - some notes on why instance arguments are not a good replacement for
> implicit coercions
>
> / Ulf
>
>
> _______________________________________________
> Agda mailing list
> ***@lists.chalmers.se
> https://lists.chalmers.se/mailman/listinfo/agda
>
Sergei Meshveliani
2018-11-19 12:22:59 UTC
Permalink
On Sun, 2018-11-18 at 21:35 +0100, Ulf Norell wrote:
> On Sun, Nov 18, 2018 at 9:12 PM Martin Escardo
> <***@cs.bham.ac.uk> wrote:
>
> You will not solve the shortcomings of my mathematical style
> by refusing
> to implement implicit coercion in Agda.
>
>
> I feel that the discussion has derailed a little at this point. The
> issue isn't that some people
> are stubbornly refusing to implement this one simple thing that would
> make life easier for
> the practising mathematician. The problem is that implicit coercions
> are very difficult to get
> right. To move this discussion forward I suggest that you give some of
> the following:
>
>
> - concrete examples of implicit coercions that you'd like to see
> - a rough sketch of how they are to be solved
> - references to the proof assistants that (in your opinion) have
> gotten implicit coercions right
> - some notes on why instance arguments are not a good replacement for
> implicit coercions
>

See my example with `record Coerce ...' in my last letter.
May be the first step can be

"record Coerce ..."
+
allow overlapping instances with choosing among the overlaps the
instance most special by substitution.
+
may be, trying to implement things in Agda as a library
(can Reflexion be used?).


About implicit instances:
I use

-------------------------------------------------------
open Group {{...}}
instance _ = G
_ = G'

preservesInversion :
(mHomo : MonoidHomomorphism)
(let f = MonoidHomomorphism.carrierMap mHomo) (x : Carrier) →
f (x ⁻¹) ≈ (f x) ⁻¹

preservesInversion (monoidHomo ((f , f-cong) , f∙homo) f-preserves-ε)
x =
begin
f x' ≈⟨ sym (∙ε fx') ⟩
fx' ∙ ε ≈⟨ ∙cong2 (sym (x∙x⁻¹ fx)) ⟩
fx' ∙ (fx ∙ fx ⁻¹) ≈⟨ sym (assoc fx' fx (fx ⁻¹)) ⟩
(fx' ∙ fx) ∙ (fx ⁻¹) ≈⟨ ∙cong1 (sym (f∙homo x' x)) ⟩
(f (x' ∙ x)) ∙ (fx ⁻¹) ≈⟨ ∙cong1 (f-cong (x⁻¹∙x x)) ⟩ -- 119
--
f ε ∙ (fx ⁻¹) ≈⟨ ∙cong1 f-preserves-ε ⟩
ε ∙ (fx ⁻¹) ≈⟨ ε∙ (fx ⁻¹) ⟩
(f x) ⁻¹

where
x' = x ⁻¹; fx = f x; fx' = f x'
---------------------------------------------------------

The example uses only Standard library for algebra.
Many operation occurrences here are overloaded,
and this removes a lot of garbage in denotation.

Currently I have a report
"(Group.Carrier G) !=< (Group.Carrier G') of type (Set α)
when checking that the expression x has type Group.Carrier G'
"
and hope to find the thing and to fix the program.

Regards,

------
Sergei
Sergei Meshveliani
2018-11-19 18:44:51 UTC
Permalink
On Sun, 2018-11-18 at 21:35 +0100, Ulf Norell wrote:

> [..]
> To move this discussion forward I suggest that you give some of the
> following:
>
>
> - concrete examples of implicit coercions that you'd like to see
> - a rough sketch of how they are to be solved
> - references to the proof assistants that (in your opinion) have
> gotten implicit coercions right
> - some notes on why instance arguments are not a good replacement for
> implicit coercions


1. Examples are given in the book about a library for computer algebra
"Axiom. 30 years horizon",
Chapter 2.7: coercion, conversion ... There are given examples.
It is on the Web.

2. Nicolas J. Doye.
Automated coercion for axiom. ACM journal, 1999.
It is on the Web.

This paper looks like a foundation for an algorithm to find coercion.
I never tried to understand it.

3. May be, algebraic libraries in Coq develop coercion.



> some notes on why instance arguments are not a good replacement for
> implicit coercions


Suppose that Rational is represented as n /' d, n, d : Integer
(let us forget here of d /= 0).
Consider
a : Rational
a = 2 + (1 /' 2) -- (E)

To which types each part of (E) needs to be mapped, and how,
so that to obtain a valid expression of type Rational
to be type-checked?

By implicit instances, _+_ can be resolved to _+r_ -- addition on
rational numbers.
But Agda will report an error, because

2 is not in Rational, 1 and 2 are not in Integer.

Some method needs to guess how to coerce (E) to the subsumed expression

((+ 2) /' 1) + ((+ 1) /' (+ 2))

And the rest is by instance resolution for _+_.
Here 2 first transforms to (+ 2), and then to (+ 2) /' (+ 1).

Mathematicians and engineers write (E) with subsuming this coercion.

The basic coercions are declared and defined in the user program.
For Agda, they can be set, say, as instances of the Coerce class,
why not?

In this example, basic coercions are
n : Nat -> (+ n) : Integer,
x : Integer -> x /' (+ 1) : Rational.

The solver needs to find a chain of basic coercions that satisfy the
goal type and also is type-checked.

Can implicit instances for _+_ be set so that the current Agda to
type-check (E) ?
I think, they cannot. So that implicit instance arguments do not cover
coercion.
Am I missing something?

Regards,

------
Sergei
Guillaume Brunerie
2018-11-19 19:18:12 UTC
Permalink
It seems like there are non-trivial interactions between implicit
coercions and instance arguments.
For instance if you have an implicit coercion inj : N -> Q, and n m :
N, then what is n + m : Q supposed to mean?
Is it inj (n +_N m) or is it (inj n) +_Q (inj m) ?
Of course in that case they are both (propositionally) equal, that’s
why a mathematician is happy with it, but which one should Agda
choose?

Best,
Guillaume
Den mån 19 nov. 2018 kl 19:44 skrev Sergei Meshveliani <***@botik.ru>:
>
> On Sun, 2018-11-18 at 21:35 +0100, Ulf Norell wrote:
>
> > [..]
> > To move this discussion forward I suggest that you give some of the
> > following:
> >
> >
> > - concrete examples of implicit coercions that you'd like to see
> > - a rough sketch of how they are to be solved
> > - references to the proof assistants that (in your opinion) have
> > gotten implicit coercions right
> > - some notes on why instance arguments are not a good replacement for
> > implicit coercions
>
>
> 1. Examples are given in the book about a library for computer algebra
> "Axiom. 30 years horizon",
> Chapter 2.7: coercion, conversion ... There are given examples.
> It is on the Web.
>
> 2. Nicolas J. Doye.
> Automated coercion for axiom. ACM journal, 1999.
> It is on the Web.
>
> This paper looks like a foundation for an algorithm to find coercion.
> I never tried to understand it.
>
> 3. May be, algebraic libraries in Coq develop coercion.
>
>
>
> > some notes on why instance arguments are not a good replacement for
> > implicit coercions
>
>
> Suppose that Rational is represented as n /' d, n, d : Integer
> (let us forget here of d /= 0).
> Consider
> a : Rational
> a = 2 + (1 /' 2) -- (E)
>
> To which types each part of (E) needs to be mapped, and how,
> so that to obtain a valid expression of type Rational
> to be type-checked?
>
> By implicit instances, _+_ can be resolved to _+r_ -- addition on
> rational numbers.
> But Agda will report an error, because
>
> 2 is not in Rational, 1 and 2 are not in Integer.
>
> Some method needs to guess how to coerce (E) to the subsumed expression
>
> ((+ 2) /' 1) + ((+ 1) /' (+ 2))
>
> And the rest is by instance resolution for _+_.
> Here 2 first transforms to (+ 2), and then to (+ 2) /' (+ 1).
>
> Mathematicians and engineers write (E) with subsuming this coercion.
>
> The basic coercions are declared and defined in the user program.
> For Agda, they can be set, say, as instances of the Coerce class,
> why not?
>
> In this example, basic coercions are
> n : Nat -> (+ n) : Integer,
> x : Integer -> x /' (+ 1) : Rational.
>
> The solver needs to find a chain of basic coercions that satisfy the
> goal type and also is type-checked.
>
> Can implicit instances for _+_ be set so that the current Agda to
> type-check (E) ?
> I think, they cannot. So that implicit instance arguments do not cover
> coercion.
> Am I missing something?
>
> Regards,
>
> ------
> Sergei
>
> _______________________________________________
> Agda mailing list
> ***@lists.chalmers.se
> https://lists.chalmers.se/mailman/listinfo/agda
Sergei Meshveliani
2018-11-19 20:36:58 UTC
Permalink
On Mon, 2018-11-19 at 20:18 +0100, Guillaume Brunerie wrote:
> It seems like there are non-trivial interactions between implicit
> coercions and instance arguments.
> For instance if you have an implicit coercion inj : N -> Q, and n m :
> N, then what is n + m : Q supposed to mean?
> Is it inj (n +_N m) or is it (inj n) +_Q (inj m) ?
(I)

> Of course in that case they are both (propositionally) equal, that’s
> why a mathematician is happy with it, but which one should Agda
> choose?
>

Different sequences of basic coercions having the same result.
This also means different evaluation _algorithms_ for run-time.
And these algorithms may have a very different _cost_.

For example, for n : Bin

(n * n) modulo 2 =mod_2= (n modulo 2) *m (n modulo 2), (II)

but the left-hand side method can be many times more expensive than the
right-hand side.

In classical mathematical books, authors do not care of the evaluation
cost.
But the programmer must care.
The programmer must understand the danger in each particular case, and
when there is a risk, one needs to set _explicit_ coercion in-place.

Thus, for (I), I would write n + m : Q

carelessly (is Q = Rational ?), and let it coerce as it can.
And for (II), I would write
noCoercion ((n mod 2) * (n mod 2))

(there can be a default agreement, or some small sign for denotation).

Also imagine that the interactive type checker could list the found
sequences of basic coercions, with suggesting to choose by pointing to
No.

Many years ago I programmed in Axiom interpreter and in the Spad
language of Axiom (it is a compiled language).
I was giving to Axiom expressions, and they where coerced. I do not
recall how precisely it was, studying coercion (there is also
"conversion") was not my subject. But my impression there was that
a) coercion helps, b) I always can avoid unneeded coercion.

I am not sure, probably coercion/conversion is not a part of the Spad
compiler. May be this is a part of the interactive shell of the Axiom
system.

I suspect that for Agda, it could be implemented as a part of a library
(I may mistake) written in Agda and being used in the interactive mode
and also in automatic type checking.

Regards,

------
Sergei
Nils Anders Danielsson
2018-11-20 09:12:44 UTC
Permalink
On 19/11/2018 20.18, Guillaume Brunerie wrote:
> Of course in that case they are both (propositionally) equal, that’s
> why a mathematician is happy with it, but which one should Agda
> choose?

A design choice of Agda is, roughly speaking, to never make an unforced
choice.

--
/NAD
Sergei Meshveliani
2018-11-20 11:41:48 UTC
Permalink
On Mon, 2018-11-19 at 21:44 +0300, Sergei Meshveliani wrote:
> On Sun, 2018-11-18 at 21:35 +0100, Ulf Norell wrote:
>
> > [..]
> > To move this discussion forward I suggest that you give some of the
> > following:
> >
> >
> > - concrete examples of implicit coercions that you'd like to see
> > - a rough sketch of how they are to be solved
> > - references to the proof assistants that (in your opinion) have
> > gotten implicit coercions right
> > - some notes on why instance arguments are not a good replacement for
> > implicit coercions
>

> [..]
> Suppose that Rational is represented as n /' d, n, d : Integer
> (let us forget here of d /= 0).
> Consider
> a : Rational
> a = 2 + (1 /' 2) -- (E)
>
> To which types each part of (E) needs to be mapped, and how,
> so that to obtain a valid expression of type Rational
> to be type-checked?
> [..]


Here is a more interesting example.
Let
Pol (R : CommutativeRing) (x : String)

be a type of polynomials in a variable x with coefficients in
C = Carrier R.

Put the basic coercions as

cToPol : C -> Pol R x for any R : CommutativeRing
cToPol c = c*x^0

polZ->polQ : Pol Integer x -> Pol Rational x
polZ->polQ =
map each coefficient as \c -> c /' 1

For x : String, 1, 2 : Integer, what does it mean

1 * x^3 + 1 /' 2 -- (E)
?
In this case the goal type is not declared.

The first summand belongs to Pol Integer x,
the second summand belongs to Rational.
Both summands need to be lifted to some `minimal' type where some
instance of _+_ is defined.

The coercer method finds that
* the first summand is coerced by polZ->polQ to Pol Rational x,
* the second summand is coerced by cToPol to Pol Rational x,
* the composition of these two coercions converts (E) to a valid
expression
(1 /' 2) * x^3 + (1 /' 2) * x^0 -- (E')

In real programs it can be written

x + 1 /' 2,

and it will be first converted to (+ 1) * x^0 + (+ 1) /' (+ 2).


Regards,

------
Sergei
Stefan Monnier
2018-11-17 21:21:54 UTC
Permalink
> I completely disagree. Ambiguous notation is one of the gravest sins that
> continue to be inflicted upon students of mathematics.

This question is not binary: Agda already has forms of
coercions/overloading (e.g. implicit arguments are a form of coercion
and type classes are a form of overloading, which happens to also handle
the kind of examples shown by Sergei who uses the term coercions).

So the question should maybe better be phrased as:
which kinds of coercions/overloading are more beneficial than harmful?

Stefan
Nils Anders Danielsson
2018-11-23 10:21:12 UTC
Permalink
On 23/11/2018 10.34, Nicolas Pouillard wrote:
> To me a concrete first step would build around a Coerce record and
> instance arguments plus some syntactic help to use it. Would it make
> sense to reuse idiom brackets?

Why do you need new syntax for this?

--
/NAD
Loading...