LoginSignup
0
2

More than 5 years have passed since last update.

PureScript by Example の 6.1, 6.2, 6.3 を読む

Last updated at Posted at 2016-12-19

この記事は (bouzuya) PureScript Advent Calendar 2016 の 19 日目。 (bouzuya) PureScript Advent Calendar 2016 は bouzuya の PureScript 学習の記録だ。

概要

PureScript by ExamplePureScript Language GuidePursuit を見ながら進めていく。

今日は 6.1, 6.2, 6.3 を読む。ソースコードの例は [purescript-book の exercises/chapter6][purescript-book-chapter-6] にある。

※注意事項: 英語と日本語訳では大きな差異がある。0.10.x に対応している英語の側で進める。

PureScript by Example 6.1

この章では型クラス (type classes) を扱うらしい。

PureScript by Example 6.2

この章のための package は次のとおり。ソースコードは上記のは [purescript-book の exercises/chapter6][purescript-book-chapter-6] にある。

  • purescript-maybe
  • purescript-tuples
  • purescript-either
  • purescript-strings
  • purescript-functions

名前からはとても基本っぽい package が並んでいるように思える。

PureScript by Example 6.3

型クラス Show とそのインスタンス Show Boolean が挙げられている。実際のコードは purescript-prelude パッケージの https://github.com/purescript/purescript-prelude/blob/970637279c95bf4a4c371aaac921cc42d5c6137d/src/Data/Show.purs#L9-L14

class Show a where
  show :: a -> String

instance showBoolean :: Show Boolean where
  show true = "true"
  show false = "false"

Show2016-12-04 以降に何度も出てきた show を適用できることを表す型クラスだ。2016-12-07 でも触れたとおり Show インスタンスは show を適用できる。 show :: a -> String から分かるとおり、任意の型から文字列に変換する関数だ。 JavaScript で言うところの Object.prototype.toString() のようなものだ。

class / instance は初出だ。型クラスを定義するキーワードと、インスタンスを定義するキーワードだ。

型クラス・インスタンスは Java などにおけるクラス・インスタンスとは違う点に注意。ぼくは型におけるクラスとそのインスタンスとしての型……のイメージで見ている。型クラスは Java で言うとインタフェースが近いように思う。

Data.TupleData.Either に対しての show を試す例が続く。注目したいのは show (Left 10) におけるエラーだろうか。

> import Prelude
> import Data.Either
> show (Left 10)
Error found:
in module $PSCI

  The inferred type

    forall t2. (Show t2) => String

  has type variables which are not mentioned in the body of the type. Consider adding a type annotation.

in value declaration it

See https://github.com/purescript/purescript/wiki/Error-Code-AmbiguousTypeVariables for more information,
or to contribute content related to this error.

これは Left 10 から型を推論できないことによるもの。 Either a b のうち aInt だと分かるが b が分からない。

こういう場合には :: 演算子を使う。

> show (Left 10 :: Either Int String)
"(Left 10)"

関数などが Show インスタンスでないことにも触れられている。

> import Prelude
> show (\n -> n + 1)
Error found:
in module $PSCI
at  line 1, column 1 - line 1, column 18

  No type class instance was found for

    Data.Show.Show (Int -> Int)


while inferring the type of show (\n ->
                                    (add n) 1
                                 )
while applying a function show
  of type forall a. (Show a) => a -> String
  to argument \n ->
                (add n) 1
in value declaration it

See https://github.com/purescript/purescript/wiki/Error-Code-NoInstanceFound for more information,
or to contribute content related to this error.

なるほど。

まとめ

とりあえず今日は Show だけ。

次回以降の TODO

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2