Last active
June 22, 2019 12:39
-
-
Save meganehouser/decbc69c10d3a368495061393f0fb8cf to your computer and use it in GitHub Desktop.
Meguro.LYAHFGG #4 - https://megurolyahfgg.connpass.com/event/133793/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Type variables (型変数)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>head :: forall a. [a] -> a</span>" | |
], | |
"text/plain": [ | |
"head :: forall a. [a] -> a" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t head" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 型定義にて小文字で始まる名称の型は型変数となる\n", | |
" - 他の言語のgenericsに似ているがより強力\n", | |
"- 型変数を持つ関数を多相的関数(polymorphic function)という\n", | |
"- 型変数の名前は複数文字でも良いが、通例として一文字の型変数名が使われている" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>fst :: forall a b. (a, b) -> a</span>" | |
], | |
"text/plain": [ | |
"fst :: forall a b. (a, b) -> a" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t fst" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 関数で複数の異なる型を表す場合は異なる型変数名を与える" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Typeclasses 101\n", | |
"### 型クラス(typeclass)\n", | |
"- 振る舞いを定義するインターフェースの一種" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>(==) :: forall a. Eq a => a -> a -> Bool</span>" | |
], | |
"text/plain": [ | |
"(==) :: forall a. Eq a => a -> a -> Bool" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t (==)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 例えば == 関数はEqを型クラスとして持つ型を2個を引数にとる\n", | |
"- (関数定義では=>の左側に型制約を定義する)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Note:*\n", | |
"- =,+,-,/などの記号で構成された関数名の関数は中値関数となる\n", | |
"- 前置関数として呼び出す場合は関数名を`()`で囲んで使用する" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>elem :: forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool</span>" | |
], | |
"text/plain": [ | |
"elem :: forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t elem" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- elem関数はlistの中に探している値と等しい値があるかチェックするので引数にEq型クラスの実装を必要とする" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 基本的な型クラス" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Eq" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"5 == 5\n", | |
"5 /= 5\n", | |
"'a' == 'a'\n", | |
"\"Ho Ho\" == \"Ho HO\"\n", | |
"3.432 == 3.432" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Eqは2個の値が等しいかどうかテストするインタフェースを提供する型クラス\n", | |
"- `Eq`型クラスのメンバは`==`と`/=`を実装する" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Ord" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>(>) :: forall a. Ord a => a -> a -> Bool</span>" | |
], | |
"text/plain": [ | |
"(>) :: forall a. Ord a => a -> a -> Bool" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t (>)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Ordは順序を持つ型のための型クラス\n", | |
"- `>`, `<`, `>=`, `<`などの一般的な比較関数を実装する" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"LT" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"GT" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"\"Abrakatabra\" < \"Zebra\"\n", | |
"\"Abrakatabra\" `compare` \"Zebra\"\n", | |
"5 >= 2\n", | |
"5 `compare` 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>compare :: forall a. Ord a => a -> a -> Ordering</span>" | |
], | |
"text/plain": [ | |
"compare :: forall a. Ord a => a -> a -> Ordering" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t compare" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- compare関数は2個の同じOrd型クラスのメンバ型の変数を取りOrdering型の値を返す(OrderingはGT, LT, EQの型)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Show" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"3\"" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"\"5.334\"" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"\"True\"" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"show 3\n", | |
"show 5.334\n", | |
"show True" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Showは値を文字列として表現するインターフェースを提供する型クラス\n", | |
"- ここまで出てきた関数を除く全ての型はShowのメンバ型である\n", | |
"- メンバー型は、値を文字列で返すshow関数を実装する" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"\\\"abc\\\"\"" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"show \"abc\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 文字列をshowするとダブルクォーテーションで囲まれた文字列が返る" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Read" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"12.0" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[1,2,3,4,3]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"read \"True\" || False\n", | |
"read \"8.2\" + 3.8 \n", | |
"read \"5\" - 2 \n", | |
"read \"[1,2,3,4]\" ++ [3]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Readは文字列からRead型クラスのメンバ型の値を返すread関数を実装する" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='err-msg'>Prelude.read: no parse</span>" | |
], | |
"text/plain": [ | |
"Prelude.read: no parse" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"read \"4\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>read :: forall a. Read a => String -> a</span>" | |
], | |
"text/plain": [ | |
"read :: forall a. Read a => String -> a" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t read" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"read関数にて、戻り値の型が推論できない場合はエラーとなる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"5.0" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"20.0" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[1,2,3,4]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(3,'a')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"read \"5\" :: Int\n", | |
"read \"5\" :: Float\n", | |
"(read \"5\" :: Float) * 4\n", | |
"read \"[1,2,3,4]\" :: [Int]\n", | |
"read \"(3, 'a')\" :: (Int, Char)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 型注釈(type annotation)で戻り値の型を明示することができる" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Enum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"abcde\"" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[LT,EQ,GT]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[3,4,5]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"'C'" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"'A'" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"['a' .. 'e']\n", | |
"[LT .. GT]\n", | |
"[3 .. 5]\n", | |
"succ 'B'\n", | |
"pred 'B'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- `Enum`型クラスのメンバ型は、連続的な順序を持つ\n", | |
" - `Enum`型クラスのメンバ型は`list range`や`succ`, `pred`関数を使用可能\n", | |
" - `()`, `Bool`, `Char`, `Ordering`, `Int`, `Integer`, `Float`, `Double`等がEnum型クラスを実装している" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Bounded" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(minBound :: Int)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">minBound :: Int</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(maxBound :: Char)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">maxBound :: Char</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(maxBound :: Bool)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">maxBound :: Bool</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(minBound :: Bool)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">minBound :: Bool</div></div>" | |
], | |
"text/plain": [ | |
"Line 1: Redundant bracket\n", | |
"Found:\n", | |
"(minBound :: Int)\n", | |
"Why not:\n", | |
"minBound :: IntLine 1: Redundant bracket\n", | |
"Found:\n", | |
"(maxBound :: Char)\n", | |
"Why not:\n", | |
"maxBound :: CharLine 1: Redundant bracket\n", | |
"Found:\n", | |
"(maxBound :: Bool)\n", | |
"Why not:\n", | |
"maxBound :: BoolLine 1: Redundant bracket\n", | |
"Found:\n", | |
"(minBound :: Bool)\n", | |
"Why not:\n", | |
"minBound :: Bool" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"-9223372036854775808" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"'\\1114111'" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"(minBound :: Int)\n", | |
"(maxBound :: Char)\n", | |
"(maxBound :: Bool)\n", | |
"(minBound :: Bool)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- `Bounded`型クラスは上限と下限の制限範囲を持つ\n", | |
" - `Bounded`型クラスのメンバ型は`minBound`と`maxBound`関数で制限範囲を確認できる\n", | |
"\n", | |
"*Note:*\n", | |
"- IHaskellのバグで()をつけて呼び出さないとエラーになる\n", | |
"- [The type signature for ‘minBound’ lacks an accompanying binding (The type signature must be given where ‘minBound’ is declared) · Issue #509 · gibiansky/IHaskell](https://github.com/gibiansky/IHaskell/issues/509)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>minBound :: forall a. Bounded a => a</span>" | |
], | |
"text/plain": [ | |
"minBound :: forall a. Bounded a => a" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t minBound" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- `minBound`や`maxBound`等の引数を取らない関数を多相定数という" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(maxBound :: (Bool, Int, Char))</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">maxBound :: (Bool, Int, Char)</div></div>" | |
], | |
"text/plain": [ | |
"Line 1: Redundant bracket\n", | |
"Found:\n", | |
"(maxBound :: (Bool, Int, Char))\n", | |
"Why not:\n", | |
"maxBound :: (Bool, Int, Char)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(True,9223372036854775807,'\\1114111')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"(maxBound :: (Bool, Int, Char))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- tupleもBouded型クラスを実装している" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Num" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>20 :: forall p. Num p => p</span>" | |
], | |
"text/plain": [ | |
"20 :: forall p. Num p => p" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t 20" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Numは数の型クラス。メンバ型は数として扱える属性を持つ" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"20" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"20" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"20.0" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"20.0" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"20 :: Int\n", | |
"20 :: Integer\n", | |
"20 :: Float\n", | |
"20 :: Double" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 全ての数値は多相定数でもある\n", | |
"- `Num`型クラスに所属しているメンバ型は`Show`型クラスと`Eq`型クラスも所属していることになる" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*[補足]*\n", | |
"- Haskell 2010 の仕様では、上に書いたように `Eq` と `Show` が `Num` のスーパークラスとなっている。しかし、最近の GHC ではこれらは `Num` のスーパークラスではなくなっている\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Integral\n", | |
"- `Integral`も数の型クラス。`Num`型クラスは全ての数字(実数と整数)`Integral`型クラスは整数のみ。\n", | |
" - `Int`と`Integer`がメンバ型" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Floating\n", | |
"- `Floating`型クラスは`Float`と`Double`などの実数のみ。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style>/* Styles used for the Hoogle display in the pager */\n", | |
".hoogle-doc {\n", | |
"display: block;\n", | |
"padding-bottom: 1.3em;\n", | |
"padding-left: 0.4em;\n", | |
"}\n", | |
".hoogle-code {\n", | |
"display: block;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"}\n", | |
".hoogle-text {\n", | |
"display: block;\n", | |
"}\n", | |
".hoogle-name {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-head {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-sub {\n", | |
"display: block;\n", | |
"margin-left: 0.4em;\n", | |
"}\n", | |
".hoogle-package {\n", | |
"font-weight: bold;\n", | |
"font-style: italic;\n", | |
"}\n", | |
".hoogle-module {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".hoogle-class {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".get-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"white-space: pre-wrap;\n", | |
"}\n", | |
".show-type {\n", | |
"color: green;\n", | |
"font-weight: bold;\n", | |
"font-family: monospace;\n", | |
"margin-left: 1em;\n", | |
"}\n", | |
".mono {\n", | |
"font-family: monospace;\n", | |
"display: block;\n", | |
"}\n", | |
".err-msg {\n", | |
"color: red;\n", | |
"font-style: italic;\n", | |
"font-family: monospace;\n", | |
"white-space: pre;\n", | |
"display: block;\n", | |
"}\n", | |
"#unshowable {\n", | |
"color: red;\n", | |
"font-weight: bold;\n", | |
"}\n", | |
".err-msg.in.collapse {\n", | |
"padding-top: 0.7em;\n", | |
"}\n", | |
".highlight-code {\n", | |
"white-space: pre;\n", | |
"font-family: monospace;\n", | |
"}\n", | |
".suggestion-warning { \n", | |
"font-weight: bold;\n", | |
"color: rgb(200, 130, 0);\n", | |
"}\n", | |
".suggestion-error { \n", | |
"font-weight: bold;\n", | |
"color: red;\n", | |
"}\n", | |
".suggestion-name {\n", | |
"font-weight: bold;\n", | |
"}\n", | |
"</style><span class='get-type'>fromIntegral :: forall a b. (Integral a, Num b) => a -> b</span>" | |
], | |
"text/plain": [ | |
"fromIntegral :: forall a b. (Integral a, Num b) => a -> b" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t fromIntegral" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"7.2" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"fromIntegral (length [1, 2, 3, 4]) + 3.2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 数値を扱う場合`fromIntegral :: (Num b, Integral a) => a -> b` 関数が便利\n", | |
" - 例えばlength関数はIntを返すがこれはFloatと足すことができない。fromIntegralでFloatに変換できる。" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Haskell", | |
"language": "haskell", | |
"name": "haskell" | |
}, | |
"language_info": { | |
"codemirror_mode": "ihaskell", | |
"file_extension": ".hs", | |
"name": "haskell", | |
"pygments_lexer": "Haskell", | |
"version": "8.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment