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 ==> True

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

(==>) :: 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 ‘(==>)’

参考

筆者プロフィール

my-latest-logo

aiya000(あいや)

せつラボ 〜圏論の基本〜」 「せつラボ2~雲と天使と関手圏~」 「矢澤にこ先輩といっしょに代数!」を書いています!

強い静的型付けとテストを用いて、バグを防ぐのが好き。Haskell・TypeScript。