Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Last active September 23, 2022 16:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Icelandjack/aeda8e98214cc52c96230af7b8724d25 to your computer and use it in GitHub Desktop.
Save Icelandjack/aeda8e98214cc52c96230af7b8724d25 to your computer and use it in GitHub Desktop.
(#14860) QuantifiedConstraints: Can't quantify constraint involving type family

For the Trac ticket #14860.

All these types are valid for h, but its principal type involves -XQuantifiedConstraints

{-# Language FlexibleInstances, MultiParamTypeClasses, GADTs, QuantifiedConstraints #-}

import Data.Kind

type family FB (a::Type) (b::Type) :: Type where
  FB Int _ = Bool
  FB [a] b = FB a b

data Bar a where
  K :: a -> xx -> Bar a

h :: Bar     Int     -> Bool
h :: Bar    [Int]    -> Bool
h :: Bar   [[Int]]   -> Bool
h :: Bar  [[[Int]]]  -> Bool
h :: Bar [[[[Int]]]] -> Bool
h (K a xx) = not (fb a xx)

fb :: a -> b -> FB a b
fb = undefined 

We really want: h :: forall a. (forall b. FB a b ~ Bool) => Bar a -> Bool.

@Icelandjack
Copy link
Author

Icelandjack commented Apr 2, 2018

This is possible with the following steps:

  1. Class alias for FB a b ~ Bool

    class    FB a b ~ Bool => FB_Bool a b
    instance FB a b ~ Bool => FB_Bool a b
  2. Quantify over alias

    h :: forall a. (forall xx. FB_Bool a xx) => Bar a -> Bool
  3. Helper function that converts (a -> xx -> FB a xx) into (a -> xx -> Bool) given a witness of FB_Bool a xx

    h :: forall a. (forall xx. FB_Bool a xx) => Bar a -> Bool
    h (K a xx) = go Dict fb a xx where
    
      go :: Dict (FB_Bool a xx) -> (a -> xx -> FB a xx) -> (a -> xx -> Bool)
      go Dict k = k

where we can instantiate h

h :: Bar     Int     -> Bool
h :: Bar    [Int]    -> Bool
h :: Bar   [[Int]]   -> Bool
h :: Bar  [[[Int]]]  -> Bool
h :: Bar [[[[Int]]]] -> Bool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment