Skip to content

Instantly share code, notes, and snippets.

@TOTBWF
Created August 21, 2020 01:32
Show Gist options
  • Save TOTBWF/7640f8e961dccb27853fc2d0113982fd to your computer and use it in GitHub Desktop.
Save TOTBWF/7640f8e961dccb27853fc2d0113982fd to your computer and use it in GitHub Desktop.
A weird case where destructuring a tuple causes type errors
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Lib where
import Data.Functor.Const
type family NonInjective env a = result | result -> env where
NonInjective env (a -> r) = [env] -> NonInjective env r
NonInjective env a = [env]
type family NonInjectiveT env a = result | result -> env a where
NonInjectiveT env (a -> r) = [Const env a] -> NonInjectiveT env r
NonInjectiveT env a = [Const env a]
problem :: a -> (NonInjective env a, NonInjectiveT env a')
problem = undefined
-- ================== Fails to Compile with following error =============================
-- src/Lib.hs:38:1-25: error: …
-- • Couldn't match expected type ‘forall env.
-- [env] -> [env] -> [env]’
-- with actual type ‘[env0] -> [env0] -> [env0]’
-- • When checking that the inferred type
-- uh :: [env0] -> [env0] -> [env0]
-- is as general as its signature
-- uh :: forall env. [env] -> [env] -> [env]
-- src/Lib.hs:38:13-25: error: …
-- • Couldn't match type ‘NonInjective env0 a0’ with ‘[env0]’
-- Expected type: ([env0] -> [env0] -> [env0],
-- [Const env0 Int] -> [Const env0 Int] -> [Const env0 Int])
-- Actual type: (NonInjective env0 (a0 -> a0 -> a0),
-- NonInjectiveT env0 (Int -> Int -> Int))
-- The type variables ‘a0’, ‘env0’ are ambiguous
-- • In the expression: problem ((+))
-- In a pattern binding: (uh, uhh) = problem ((+))
uh :: [env] -> [env] -> [env]
uhh :: [Const env Int] -> [Const env Int] -> [Const env Int]
(uh, uhh) = problem ((+))
-- Type Applications seem to fix it
huh :: [env] -> [env] -> [env]
hmm :: [Const env Int] -> [Const env Int] -> [Const env Int]
(huh, hmm) = problem ((+) @Double)
-- What is goin on GHC???
what :: ([env] -> [env] -> [env], [Const env Int] -> [Const env Int] -> [Const env Int])
what = problem (+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment