Constraint implication

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Constraint implication

Ryan Reich
The Constraint kind appears to lack an interface to an important capability that is already part of the type checker: constraint implication.  Namely, the ability to provide a witness for the statement "constraint c1 implies constraint c2" or, more importantly, "for all a, constraint (c1 a) implies constraint (c2 a)", where c1 and c2 are now constraint-valued type functions (and possibly even for constraint functions with multiple parameters).  It seems to me that this can follow the pattern of the "magic" Coercible type class and the (non-magic) Coercion data type; it provides the programmer with an analogous value to this example that can be obtained in apparently no other way.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Constraint implication

Edward Kmett-2
Just a few old observations I've made from implementing these things into type systems of my own:

1.) An internal hom for the category of constraints is admissible:

(|-) :: Constraint -> Constraint -> Constraint

models entailment, and effectively brings into scope a local rule, but global instance resolution isn't lost if this is only produced from existing instances.

To your point, this is analogous to the (:-) :: Constraint -> Constraint -> * external hom for the category of constraints provided by my constraints package, but it is internal, with all of the appropriate CCC operations.

2.) Quantification over constraints is also admissible.

Neither one compromises the "thinness" of the category of constraints that provides us global coherence of instance resolution

In this case the property that if D is thin, so is [C,D]. forall here can quantify over any other kind you want.

Unfortunately, neither implemented in Haskell.

You'd need both of them to be able to talk about constraints like (forall a. Eq a |- Eq (f a)). 

Together could eliminate, morally, the entire Data.Functor.Classes mess. (That said, as implemented those classes are a bit stronger than the quantified form)

3.) Constraint also admits a sum type, (\/) but it acts more like a least upper bound than an either.

It is much easier to talk about in the category of constraints using the first part above.

Given (p |- r, q |- r), (p \/ q) |- r and vice versa.

The key to keeping the category of constraints thin is that you can't case analyze on it, its more like if you look at, say, Ord [a] \/ Eq a you can get to any constraint that you could get to given the intersection of both, not using the particulars of either. e.g. its morally as powerful as Eq [a] in this case.

Getting this stuff into GHC is the tricky part!

-Edward

On Wed, Dec 27, 2017 at 2:50 AM, Ryan Reich <[hidden email]> wrote:
The Constraint kind appears to lack an interface to an important capability that is already part of the type checker: constraint implication.  Namely, the ability to provide a witness for the statement "constraint c1 implies constraint c2" or, more importantly, "for all a, constraint (c1 a) implies constraint (c2 a)", where c1 and c2 are now constraint-valued type functions (and possibly even for constraint functions with multiple parameters).  It seems to me that this can follow the pattern of the "magic" Coercible type class and the (non-magic) Coercion data type; it provides the programmer with an analogous value to this example that can be obtained in apparently no other way.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries



_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Constraint implication

Ryan Reich
Your 1) is a very erudite way of saying "you forgot about my 'constraints' package" :)  Which I did.  Although it's just a little more awkward syntactically to write `(a :- b) -> whatever` instead of `(a |- b) => whatever`, which is only possible with a GHC extension, it obviously solves the problem of using constraints of the form `c1 implies c2`, i.e. 0-ary constraint functions.  If I understand you correctly, you are saying that an internal entailment operator is consistent with the other workings of constraints, and pointing out the concepts that need to be handled in implementing this in GHC?

Re 2): I am probably missing something when I think that `type (c1 :: * -> Constraint) ::- (c2 :: * -> Constraint) = forall a. (c1 a :- c2 a)` works for 1-ary entailment; for instance `Sub Dict :: Ord ::- Eq` appears not to bother ghci.

3) It would be neat, for sure, to have this sum type.  Is that the full extent of boolean logic you can define on Constraint without losing thinness?

On Wed, Dec 27, 2017 at 11:17 AM, Edward Kmett <[hidden email]> wrote:
Just a few old observations I've made from implementing these things into type systems of my own:

1.) An internal hom for the category of constraints is admissible:

(|-) :: Constraint -> Constraint -> Constraint

models entailment, and effectively brings into scope a local rule, but global instance resolution isn't lost if this is only produced from existing instances.

To your point, this is analogous to the (:-) :: Constraint -> Constraint -> * external hom for the category of constraints provided by my constraints package, but it is internal, with all of the appropriate CCC operations.

2.) Quantification over constraints is also admissible.

Neither one compromises the "thinness" of the category of constraints that provides us global coherence of instance resolution

In this case the property that if D is thin, so is [C,D]. forall here can quantify over any other kind you want.

Unfortunately, neither implemented in Haskell.

You'd need both of them to be able to talk about constraints like (forall a. Eq a |- Eq (f a)). 

Together could eliminate, morally, the entire Data.Functor.Classes mess. (That said, as implemented those classes are a bit stronger than the quantified form)

3.) Constraint also admits a sum type, (\/) but it acts more like a least upper bound than an either.

It is much easier to talk about in the category of constraints using the first part above.

Given (p |- r, q |- r), (p \/ q) |- r and vice versa.

The key to keeping the category of constraints thin is that you can't case analyze on it, its more like if you look at, say, Ord [a] \/ Eq a you can get to any constraint that you could get to given the intersection of both, not using the particulars of either. e.g. its morally as powerful as Eq [a] in this case.

Getting this stuff into GHC is the tricky part!

-Edward

On Wed, Dec 27, 2017 at 2:50 AM, Ryan Reich <[hidden email]> wrote:
The Constraint kind appears to lack an interface to an important capability that is already part of the type checker: constraint implication.  Namely, the ability to provide a witness for the statement "constraint c1 implies constraint c2" or, more importantly, "for all a, constraint (c1 a) implies constraint (c2 a)", where c1 and c2 are now constraint-valued type functions (and possibly even for constraint functions with multiple parameters).  It seems to me that this can follow the pattern of the "magic" Coercible type class and the (non-magic) Coercion data type; it provides the programmer with an analogous value to this example that can be obtained in apparently no other way.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries




_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Constraint implication

Andrew Martin
In reply to this post by Ryan Reich
I agree that this would be cool. Here is some other discussion of this topic:
Many people seem to want this, but to my knowledge, no one has any plans to work on it any time soon. I would not be surprised if it actually ended up getting implemented one day. It could replace everything in Data.Functor.Classes as well as half of the stuff from Data.Exists in my `quantification` package.

On Wed, Dec 27, 2017 at 2:50 AM, Ryan Reich <[hidden email]> wrote:
The Constraint kind appears to lack an interface to an important capability that is already part of the type checker: constraint implication.  Namely, the ability to provide a witness for the statement "constraint c1 implies constraint c2" or, more importantly, "for all a, constraint (c1 a) implies constraint (c2 a)", where c1 and c2 are now constraint-valued type functions (and possibly even for constraint functions with multiple parameters).  It seems to me that this can follow the pattern of the "magic" Coercible type class and the (non-magic) Coercion data type; it provides the programmer with an analogous value to this example that can be obtained in apparently no other way.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries




--
-Andrew Thaddeus Martin

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Constraint implication

Edward Kmett-2
In reply to this post by Ryan Reich
On Thu, Dec 28, 2017 at 2:44 AM, Ryan Reich <[hidden email]> wrote:
Your 1) is a very erudite way of saying "you forgot about my 'constraints' package" :)  Which I did. 

Not exactly. The constraints package can construct 

(:-) :: Constraint -> Constraint -> *

but

(|-) :: Constraint -> Constraint -> Constraint

would really need compiler support to work correctly. That said, it might be able to be written as a GHC plugin, now that I think about it. (3) could also most likely be handled that way.
 
Although it's just a little more awkward syntactically to write `(a :- b) -> whatever` instead of `(a |- b) => whatever`, which is only possible with a GHC extension, it obviously solves the problem of using constraints of the form `c1 implies c2`, i.e. 0-ary constraint functions.  If I understand you correctly, you are saying that an internal entailment operator is consistent with the other workings of constraints, and pointing out the concepts that need to be handled in implementing this in GHC?
 
Yep.
 
Re 2): I am probably missing something when I think that `type (c1 :: * -> Constraint) ::- (c2 :: * -> Constraint) = forall a. (c1 a :- c2 a)` works for 1-ary entailment; for instance `Sub Dict :: Ord ::- Eq` appears not to bother ghci.

data Jet f a = a :- Jet f (f a)

is an example of a data type for which show, (==), compare, etc. are a pain in the neck to write.

For that you really want something like

instance (Ord a, forall x. Ord x |- Ord (f x)) => Ord (Jet f a)

In the constraints vocabulary today this particular usecase can sort of be faked by using Lifting:

instance (Lifting Ord f, Ord a) => Ord (Jet f a)

but GHC doesn't know enough to automatically bring the right stuff into scope to make it automatically work, you have to write a crapload of manual Lifting instances you might not expect, and you have to open the dictionary from Lifting by hand, and its generally a mess to use.

3) It would be neat, for sure, to have this sum type.  Is that the full extent of boolean logic you can define on Constraint without losing thinness?

Technically it is a Heyting algebra. You can go a bit crazier with the type theory for the category of constraints and show that its locally cartesian closed, which seems to admit some funny interpretations of MLTT in it as well.

-Edward

On Wed, Dec 27, 2017 at 11:17 AM, Edward Kmett <[hidden email]> wrote:
Just a few old observations I've made from implementing these things into type systems of my own:

1.) An internal hom for the category of constraints is admissible:

(|-) :: Constraint -> Constraint -> Constraint

models entailment, and effectively brings into scope a local rule, but global instance resolution isn't lost if this is only produced from existing instances.

To your point, this is analogous to the (:-) :: Constraint -> Constraint -> * external hom for the category of constraints provided by my constraints package, but it is internal, with all of the appropriate CCC operations.

2.) Quantification over constraints is also admissible.

Neither one compromises the "thinness" of the category of constraints that provides us global coherence of instance resolution

In this case the property that if D is thin, so is [C,D]. forall here can quantify over any other kind you want.

Unfortunately, neither implemented in Haskell.

You'd need both of them to be able to talk about constraints like (forall a. Eq a |- Eq (f a)). 

Together could eliminate, morally, the entire Data.Functor.Classes mess. (That said, as implemented those classes are a bit stronger than the quantified form)

3.) Constraint also admits a sum type, (\/) but it acts more like a least upper bound than an either.

It is much easier to talk about in the category of constraints using the first part above.

Given (p |- r, q |- r), (p \/ q) |- r and vice versa.

The key to keeping the category of constraints thin is that you can't case analyze on it, its more like if you look at, say, Ord [a] \/ Eq a you can get to any constraint that you could get to given the intersection of both, not using the particulars of either. e.g. its morally as powerful as Eq [a] in this case.

Getting this stuff into GHC is the tricky part!

-Edward

On Wed, Dec 27, 2017 at 2:50 AM, Ryan Reich <[hidden email]> wrote:
The Constraint kind appears to lack an interface to an important capability that is already part of the type checker: constraint implication.  Namely, the ability to provide a witness for the statement "constraint c1 implies constraint c2" or, more importantly, "for all a, constraint (c1 a) implies constraint (c2 a)", where c1 and c2 are now constraint-valued type functions (and possibly even for constraint functions with multiple parameters).  It seems to me that this can follow the pattern of the "magic" Coercible type class and the (non-magic) Coercion data type; it provides the programmer with an analogous value to this example that can be obtained in apparently no other way.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries





_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

RE: Constraint implication

Haskell - Libraries mailing list
In reply to this post by Edward Kmett-2

Getting this stuff into GHC is the tricky part!

Indeed… but step 1 is to articulate a well-formed specification.  I’m not close enough to this conversation to understand all the details, let alone form a specification.  But perhaps some of you are.

 

I think (2) is described here: https://ghc.haskell.org/trac/ghc/wiki/QuantifiedContexts.  I have been keen on this proposal for years, just lacking time to execute on it.

 

Simon

 

From: Libraries [mailto:[hidden email]] On Behalf Of Edward Kmett
Sent: 27 December 2017 19:18
To: Ryan Reich <[hidden email]>
Cc: Haskell Libraries <[hidden email]>
Subject: Re: Constraint implication

 

Just a few old observations I've made from implementing these things into type systems of my own:

 

1.) An internal hom for the category of constraints is admissible:

 

(|-) :: Constraint -> Constraint -> Constraint

 

models entailment, and effectively brings into scope a local rule, but global instance resolution isn't lost if this is only produced from existing instances.

 

To your point, this is analogous to the (:-) :: Constraint -> Constraint -> * external hom for the category of constraints provided by my constraints package, but it is internal, with all of the appropriate CCC operations.

 

2.) Quantification over constraints is also admissible.

 

Neither one compromises the "thinness" of the category of constraints that provides us global coherence of instance resolution

 

In this case the property that if D is thin, so is [C,D]. forall here can quantify over any other kind you want.

 

Unfortunately, neither implemented in Haskell.

 

You'd need both of them to be able to talk about constraints like (forall a. Eq a |- Eq (f a)). 

 

Together could eliminate, morally, the entire Data.Functor.Classes mess. (That said, as implemented those classes are a bit stronger than the quantified form)

 

3.) Constraint also admits a sum type, (\/) but it acts more like a least upper bound than an either.

 

It is much easier to talk about in the category of constraints using the first part above.

 

Given (p |- r, q |- r), (p \/ q) |- r and vice versa.

 

The key to keeping the category of constraints thin is that you can't case analyze on it, its more like if you look at, say, Ord [a] \/ Eq a you can get to any constraint that you could get to given the intersection of both, not using the particulars of either. e.g. its morally as powerful as Eq [a] in this case.

 

Getting this stuff into GHC is the tricky part!

 

-Edward

 

On Wed, Dec 27, 2017 at 2:50 AM, Ryan Reich <[hidden email]> wrote:

The Constraint kind appears to lack an interface to an important capability that is already part of the type checker: constraint implication.  Namely, the ability to provide a witness for the statement "constraint c1 implies constraint c2" or, more importantly, "for all a, constraint (c1 a) implies constraint (c2 a)", where c1 and c2 are now constraint-valued type functions (and possibly even for constraint functions with multiple parameters).  It seems to me that this can follow the pattern of the "magic" Coercible type class and the (non-magic) Coercion data type; it provides the programmer with an analogous value to this example that can be obtained in apparently no other way.


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

 


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

RE: Constraint implication

David Feuer
For whatever it's worth, it seems to me that the two next steps, in no particular order, should likely be

1. Add quantified contexts. These should obsolete Data.Constraint.Forall by offering a much nicer API (without needing to use `inst`, etc., manually).

2. Add the ability to at least express constraint implication at the constraint level, and propagate some of that information automatically. Trying to be too clever seems to run into backtracking, so it's not clear that we should try to jump straight to a full constraint implication system. If we could even get a version that requires manual guidance to use (like Data.Constraint.Forall does today), we'd surely be making some progress.

On Jan 2, 2018 5:08 AM, "Simon Peyton Jones via Libraries" <[hidden email]> wrote:

Getting this stuff into GHC is the tricky part!

Indeed… but step 1 is to articulate a well-formed specification.  I’m not close enough to this conversation to understand all the details, let alone form a specification.  But perhaps some of you are.

 

I think (2) is described here: https://ghc.haskell.org/trac/ghc/wiki/QuantifiedContexts.  I have been keen on this proposal for years, just lacking time to execute on it.

 

Simon

 

From: Libraries [mailto:[hidden email]] On Behalf Of Edward Kmett
Sent: 27 December 2017 19:18
To: Ryan Reich <[hidden email]>
Cc: Haskell Libraries <[hidden email]>
Subject: Re: Constraint implication

 

Just a few old observations I've made from implementing these things into type systems of my own:

 

1.) An internal hom for the category of constraints is admissible:

 

(|-) :: Constraint -> Constraint -> Constraint

 

models entailment, and effectively brings into scope a local rule, but global instance resolution isn't lost if this is only produced from existing instances.

 

To your point, this is analogous to the (:-) :: Constraint -> Constraint -> * external hom for the category of constraints provided by my constraints package, but it is internal, with all of the appropriate CCC operations.

 

2.) Quantification over constraints is also admissible.

 

Neither one compromises the "thinness" of the category of constraints that provides us global coherence of instance resolution

 

In this case the property that if D is thin, so is [C,D]. forall here can quantify over any other kind you want.

 

Unfortunately, neither implemented in Haskell.

 

You'd need both of them to be able to talk about constraints like (forall a. Eq a |- Eq (f a)). 

 

Together could eliminate, morally, the entire Data.Functor.Classes mess. (That said, as implemented those classes are a bit stronger than the quantified form)

 

3.) Constraint also admits a sum type, (\/) but it acts more like a least upper bound than an either.

 

It is much easier to talk about in the category of constraints using the first part above.

 

Given (p |- r, q |- r), (p \/ q) |- r and vice versa.

 

The key to keeping the category of constraints thin is that you can't case analyze on it, its more like if you look at, say, Ord [a] \/ Eq a you can get to any constraint that you could get to given the intersection of both, not using the particulars of either. e.g. its morally as powerful as Eq [a] in this case.

 

Getting this stuff into GHC is the tricky part!

 

-Edward

 

On Wed, Dec 27, 2017 at 2:50 AM, Ryan Reich <[hidden email]> wrote:

The Constraint kind appears to lack an interface to an important capability that is already part of the type checker: constraint implication.  Namely, the ability to provide a witness for the statement "constraint c1 implies constraint c2" or, more importantly, "for all a, constraint (c1 a) implies constraint (c2 a)", where c1 and c2 are now constraint-valued type functions (and possibly even for constraint functions with multiple parameters).  It seems to me that this can follow the pattern of the "magic" Coercible type class and the (non-magic) Coercion data type; it provides the programmer with an analogous value to this example that can be obtained in apparently no other way.


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

 


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries