Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active June 22, 2021 08:29
Show Gist options
  • Save nattybear/c685608b045eea5acf919bcc2b3e103c to your computer and use it in GitHub Desktop.
Save nattybear/c685608b045eea5acf919bcc2b3e103c to your computer and use it in GitHub Desktop.
하스켈 확장 InstanceSigs

이 글은 책 Haskell Programming from First Principles를 읽고 일부 내용을 정리한 것이다.

인스턴스에 타입 적기

인스턴스를 구현할 때 메소드의 타입은 보통 적지 않아도 된다. 정확히는 적고 싶어도 적을 수 없다. 적으면 에러가 난다.

예를 들어 인스턴스 구현에 타입을 적으려고 하면

instance Functor (Reader r) where
  fmap :: (a -> b)
       -> Reader r a
       -> Reader r b
  fmap f ra =
    Reader $ f . ra

에러가 난다.

Illegal type signature in instance declaration

그리고 친절하게도 그렇게 하고 싶으면 어떤 확장을 쓰라고 알려준다.

Use InstanceSigs to allow this

확장 InstanceSigs를 추가하면 이제 인스턴스 구현 할 때 타입을 명시적으로 적을 수 있게 된다.

{-# LANGUAGE InstanceSigs #-}

instance Functor (Reader r) where
  fmap :: (a -> b)
       -> Reader r a
       -> Reader r b
  fmap f ra =
    Reader $ f . ra

그래서 이걸 어디에 쓰냐? 공부할 때 쓴다!ㅎ

대문 링크

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