[GHC] #14822: -XQuantifiedConstraints: Turn term-level entailments (:-) into constraints (=>)

#14822: -XQuantifiedConstraints: Turn term-level entailments (:-) into constraints (=>) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.5 Keywords: | Operating System: Unknown/Multiple QuantifiedConstraints, wipT2893 | Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- This code works fine: {{{#!hs {-# Language TypeOperators, RankNTypes, KindSignatures, GADTs, DataKinds, MultiParamTypeClasses, FlexibleInstances, ConstraintKinds, ScopedTypeVariables #-} import Data.Kind -- 'constraints' machinery data Dict c where Dict :: c => Dict c newtype a :- b = Sub (a => Dict b) -- entailment -- 'singletons' machinery data SBool :: Bool -> Type where SFalse :: SBool 'False STrue :: SBool 'True class SBoolI (bool::Bool) where sbool :: SBool bool instance SBoolI 'False where sbool = SFalse instance SBoolI 'True where sbool = STrue -- VVV Example VVV class Funny (b::Bool) (b'::Bool) instance Funny 'False b' instance Funny 'True 'True instance Funny 'True 'False proof :: forall b b'. (SBoolI b, SBoolI b') :- Funny b b' proof = Sub (case (sbool :: SBool b, sbool :: SBool b') of (SFalse, _) -> Dict (STrue, SFalse) -> Dict (STrue, STrue) -> Dict) -- ^^^ Example ^^^ }}} What I'm interested in is the entailment: Singletons for `b` and `b'` entail `Funny b b'`. This is witnessed by `proof`. Given that we have a branch for `-XQuantifiedConstraints`, it is tantalizing to convert this into an implication constraint `(SBoolI b, SBoolI b') => Funny b b'`. Is such a thing sensible (wrt coherence) and if so is it at all possible to do this on WIPT2893? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14822 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14822: -XQuantifiedConstraints: Turn term-level entailments (:-) into constraints (=>) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.5 Resolution: | Keywords: | QuantifiedConstraints, wipT2893 Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by kcsongor): What would the conditions of generating such an implication constraint be? To me it seems like this would make use of the fact that the kind `Bool` is closed, and in theory, a constraint `forall (b :: Bool). C b` could be discharged by having an instance for `C 'True` and `C 'False`. Or in terms of implication constraints, that `forall (b :: Bool). C b => D b` could be built from the individual constituents. If my understanding is correct as written above, then I don't think it would make sense. When I see a quantified implication constraint, like `forall a. C a => D a`, I expect it to be parametric in `a`, which means that the solution of that constraint should be a function that uses the dictionary for `C a` to build the dictionary for `D a`, regardless of what `a` is instantiated to. (This expectation is grounded in the open world assumption.) For what it's worth, I think the question can be reformulated as whether the constraint `(forall a b. Funny a b)` should be solved based on your provided instances (to which I would answer no). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14822#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14822: -XQuantifiedConstraints: Turn term-level entailments (:-) into constraints (=>) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.5 Resolution: invalid | Keywords: | QuantifiedConstraints, wipT2893 Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => closed * resolution: => invalid Comment: I fully agree with kcsongor's analysis. This is essentially asking for a way to violate the open-world assumption, which isn't going to fly. If you want to use `(SBoolI b, SBoolI b') => Funny b b'` as a quantified constraint, then write an instance for it. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14822#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14822: -XQuantifiedConstraints: Turn term-level entailments (:-) into constraints (=>) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.5 Resolution: invalid | Keywords: | QuantifiedConstraints, wipT2893 Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): I don't make use of `Bool` being closed. I don't seek to discharge `forall b. C b` as you claim, rather `pi b. C b`. I found a way to emulate what I want with `unsafeCoerce`. Here is an example for the open kind `Type`: {{{#!hs {-# Language KindSignatures, GADTs, ConstraintKinds, QuantifiedConstraints, TypeOperators, MultiParamTypeClasses, FlexibleInstances, UndecidableInstances, AllowAmbiguousTypes, TypeApplications, ScopedTypeVariables, RankNTypes #-} import Data.Kind import Unsafe.Coerce -- constraints data Dict :: Constraint -> Type where Dict :: c => Dict c class (a => b) => Implies a b instance (a => b) => Implies a b type a |- b = Dict (a `Implies` b) -- Representation of two types: Int and Bool data S :: Type -> Type where SInt :: S Int SBool :: S Bool -- Can be passed explicitly class SI a where s :: S a instance SI Int where s = SInt instance SI Bool where s = SBool -- Can be eliminated like with an if-then-else sElim :: S a -> (Int ~ a => res) -> (Bool ~ a => res) -> res sElim SInt i _ = i sElim SBool _ b = b -- (SI a) entails (Str a) class Str a where str :: String instance Str Int where str = "INT" instance Str Bool where str = "BOOL" newtype Wrap a = Wrap a instance SI a => Str (Wrap a) where str = sElim (s @a) (str @a) (str @a) wit :: forall ty. SI ty |- Str ty wit = wrapElim Dict where wrapElim :: (SI ty |- Str (Wrap ty)) -> (SI ty |- Str ty) wrapElim = unsafeCoerce -- >> siStr @Int -- "INT!" -- >> siStr @Bool -- "BOOL!" siStr :: forall ty. SI ty => String siStr = go wit where go :: SI ty => (SI ty |- Str ty) -> String go Dict = str @ty ++ "!" }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14822#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14822: -XQuantifiedConstraints: Turn term-level entailments (:-) into constraints (=>) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.5 Resolution: invalid | Keywords: | QuantifiedConstraints, wipT2893 Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): Using #14292 this can be `coerce`d rather than `unsafeCoerce`d if only we turn on `-XIncoherentInstances` and mark some parameters `representational` {{{#!hs type role Implies nominal representational type role Str representational }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14822#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC