Haskell(GHC)のimport/module構文に表れるのtypeキーワードについて(ExplicitNamespaces)
例によるGHC Haskellの中の概念の紹介記事。
GHCのHaskellでは(おそらく主に)DataKinds拡張を用いる場合にて、以下のように
import/module構文内でtypeキーワードが表れる場合がある。
Main.hs
{-# LANGUAGE ExplicitNamespaces #-}
import Test (type (==>))
main :: IO ()
main = return ()Test.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Test
( type (==>)
) where
type family (x :: Bool) ==> (y :: Bool) :: Bool where
False ==> _ = True
True ==> False = False
True ==> True = True これは厳密にはExplicitNamespaces拡張によって提供されている構文で、Main.hsとTest.hsを以下のように
改変した場合にわかりやすい。
Main.hs
{-# LANGUAGE ExplicitNamespaces #-}
import Test (type (==>), (==>))
main :: IO ()
main = print $ False ==> TrueTest.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Test
( type (==>)
, (==>)
) where
type family (x :: Bool) ==> (y :: Bool) :: Bool where
False ==> _ = True
True ==> False = False
True ==> True = True
(==>) :: Bool -> Bool -> Bool
False ==> _ = True
True ==> False = False
True ==> True = True つまるところ、Main.hsでtype familyの型演算子==>をimportときにtypeキーワードが必要になる。
具体的には、以下はコンパイルエラーになる。
Main.hs
{-# LANGUAGE ExplicitNamespaces #-}
import Test ((==>))
main :: IO ()
main = return ()Test.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Test
( (==>)
) where
type family (x :: Bool) ==> (y :: Bool) :: Bool where
False ==> _ = True
True ==> False = False
True ==> True = Trueコンパイルエラー
Test.hs:8:5: error:
Not in scope: ‘==>’
Perhaps you meant ‘==’ (imported from Prelude)Main.hs:3:14: error: Module ‘Test’ does not export ‘(==>)’
