programing tip

ghci desugar 유형은 왜 패밀리를 나열하고 유형합니까?

itbloger 2020. 8. 31. 07:32
반응형

ghci desugar 유형은 왜 패밀리를 나열하고 유형합니까? 선택적으로 비활성화 할 수 있습니까?


내 라이브러리의 유형 ghci 디스플레이를 가능한 한 직관적으로 만들려고 노력하고 있지만 더 고급 유형 기능을 사용할 때 많은 어려움이 있습니다.

파일에이 코드가 있다고 가정 해 보겠습니다.

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

import GHC.TypeLits

data Container (xs::[*]) = Container

ghci에서로드 한 다음 다음 명령을 입력합니다.

ghci> :t undefined :: Container '[String,String,String,String,String]

불행히도 ghci는 나에게 다소 추한 모습을 보여줍니다.

:: Container
       ((':)
          *
          String
          ((':)
             * String ((':) * String ((':) * String ((':) * String ('[] *))))))

ghci는 유형 수준 문자열의 설탕을 제거했습니다. ghci가 이것을 방지하고 나에게 예쁜 버전을주는 것을 방지하는 방법이 있습니까?


관련 메모에서 유형 수준 Replicate함수를 생성한다고 가정 해 보겠습니다.

data Nat1 = Zero | Succ Nat1

type family Replicate (n::Nat1) x :: [*]
type instance Replicate Zero x = '[]
type instance Replicate (Succ n) x = x ': (Replicate n x)

type LotsOfStrings = Replicate (Succ (Succ (Succ (Succ (Succ Zero))))) String

이제 ghci에게 다음을 사용하여 유형을 요청할 때 LotsOfStrings:

ghci> :t undefined :: Container LotsOfStrings

ghci는 훌륭하고 예쁜 결과를 제공합니다.

undefined :: Container LotsOfStrings

하지만 Replicated 버전을 요청하면

ghci> :t undefined :: Container (Replicate (Succ (Succ (Succ (Succ (Succ Zero))))) String)

ghci는 유형 동의어를 사용하지 않았을 때 유형 계열을 in으로 대체합니다.

:: Container
       ((':)
          *
          [Char]
          ((':)
             * [Char] ((':) * [Char] ((':) * [Char] ((':) * [Char] ('[] *))))))

ghci가 유형 계열을 대체하지만 유형 동의어가 아닌 이유는 무엇입니까? ghci가 언제 대체를 수행할지 제어하는 ​​방법이 있습니까?


내가 아는 해결 방법은 : kind를 사용하는 것입니다. 예를 들어

ghci> : kind (컨테이너 '[문자열, 문자열, 문자열, 문자열, 문자열])

제공 :

(컨테이너 '[문자열, 문자열, 문자열, 문자열, 문자열]) :: *

동안

ghci> : 종류! (컨테이너 '[문자열, 문자열, 문자열, 문자열, 문자열])

다음과 같이 인쇄됩니다.

컨테이너

(( ':)

  *
  [Char]
  ((':)
     * [Char] ((':) * [Char] ((':) * [Char] ((':) * [Char] ('[] *))))))

물론 공식적으로는 ghci에게 다른 질문을 던지지 kind만 작동합니다. undefined ::어쨌든 사용 은 일종의 해결 방법이므로 이것이 충분할 것이라고 생각했습니다.


이는 곧 출시 될 GHC 7.8에서 수정되었습니다.

GHC 7.6 prints kinds if a datatype uses PolyKinds. So you see (':) * String ('[] *) instead of just (':) String '[].

In GHC 7.8, kinds are no longer shown by default and your datatype is pretty printed as a list, as you would expect. You can use the new flag -fprint-explicit-kinds to see explicit kinds as in GHC 7.6. I don't know the reasons for this, presumably explicit kinds were meant to be an aid for understanding PolyKinds.


import GHC.TypeLits

data Container (xs::[*]) = Container

I load it up in ghci, then I type the following command:

:t undefined :: Container '[String,String,String,String,String]

참고URL : https://stackoverflow.com/questions/16346293/why-does-ghci-desugar-type-lists-and-type-families-can-this-be-selectively-disa

반응형