<jupyter_start><jupyter_text>3 Types and classes This IHaskell notebook outlines a chapter from the 2nd edition of *Programming in Haskell* by Prof. <NAME>,demonstrating examples and summarising some of the sections.  3.1 Basic concpets A *type* is a collection of related values.<jupyter_code>:type False
:type True
:type not
:type not False
:type not True
:type not (not False)<jupyter_output><empty_output><jupyter_text>In Haskell, every expression must have a type, which is calculated priror to evaluating the expression by a process called *type inference*.$\displaystyle\frac{\mathtt{f ~::~ A \to B}\qquad\mathtt{e ~::~ A}}{\mathtt{f~~e ~::~ B}}$<jupyter_code>:type not 3<jupyter_output><empty_output><jupyter_text>3.2 Basic types  Boollogical values  Charsingle characters<jupyter_code>'a'
'A'
'3'
'_'
'\n'
'\t'
'가' -- 유니코드 문자 지원<jupyter_output><empty_output><jupyter_text>Stringstring of characters<jupyter_code>"abc"
"1+2=3"
""<jupyter_output><empty_output><jupyter_text>Intfixed-precision integers<jupyter_code>-100 :: Int
0 :: Int
999 :: Int
2^63 - 1 :: Int
2^63 :: Int
-2^63 :: Int
-2^63 - 1 :: Int<jupyter_output><empty_output><jupyter_text>Integerarbitrary-precision integers<jupyter_code>2^63 :: Integer
- 2^63 -1 :: Integer 
product [1..70] :: Integer<jupyter_output><empty_output><jupyter_text>Floatsingle-precision floating-point numbers<jupyter_code>sqrt 2 :: Float
sqrt 99999 :: Float<jupyter_output><empty_output><jupyter_text>Doubledouble-precision floating-point numbers<jupyter_code>sqrt 2 :: Double
sqrt 99999 :: Double<jupyter_output><empty_output><jupyter_text>A number may have more than one numeric type.<jupyter_code>3 :: Int
:type it

3 :: Integer
:type it

3 :: Float
:type it

3 :: Double
:type it

3
:type it<jupyter_output><empty_output><jupyter_text>3.3 List types A *list* is a sequence of *elements* of the same type.<jupyter_code>[False,True,False] :: [Bool]
['a','b','c','d'] :: [Char]
["One","Two","Three"] :: [String]
:type [False,True,False]
:type ['a','b','c','d']
:type ["One","Two","Three"]
:type [False]
:type ['a']
:type [[]]
:type []
:type [False,True]
:type [False,True,False]
:type [['a','b'],['c','d','e']]<jupyter_output><empty_output><jupyter_text>There is no restriction that a list must have a finite length.  3.4 Tuple types A *tuple* is a finite sequence of *components* of possibly differrent types.<jupyter_code>:type (False, True)
:type (False, 'a', True)
:type ("Yes", True, 'a')
:type ('a', (False, 'b'))
:type (['a','b'], [False,True])
:type [('a',False), ('b',True)]<jupyter_output><empty_output><jupyter_text>3.5 Function types<jupyter_code>:type not
:type even
:type (even :: Int -> Bool)
:type (even :: Float -> Bool) -- type error
add :: (Int,Int) -> Int
add (x, y) = x + y

zeroto :: Int -> [Int]
zeroto n = [0..n]
add (3,4)
zeroto 10
:type add
:type zeroto<jupyter_output><empty_output><jupyter_text>There is no restriction that functions must be *total* on their argument type.<jupyter_code>head [1,2,3,5]
head [1]
head []<jupyter_output><empty_output><jupyter_text>3.6 Curried functions<jupyter_code>add :: (Int,Int) -> Int
add (x, y) = x + y

add' :: Int -> (Int -> Int)
add' x y = x + y<jupyter_output><empty_output><jupyter_text>```haskellInt -> (Int -> Int)```is same as```haskellInt -> Int -> Int```<jupyter_code>:type add'
:type (add' 2)
(add' 2) 3 == add' 2 3
mult :: Int -> (Int -> (Int -> Int))
mult x y z = x * y * z<jupyter_output><empty_output><jupyter_text>```haskellInt -> (Int -> (Int -> Int))```is same as```haskellInt -> Int -> Int -> Int```<jupyter_code>:type mult
((mult 2) 3) 4 == mult 2 3 4<jupyter_output><empty_output><jupyter_text>3.7 Polymorphic types<jupyter_code>:type [1,3,5,7]
:type ["Yes","No"]
:type [sin,cos,tan]
length [1,3,5,7]
length ["Yes","No"]
length [sin,cos,tan]
:type length
:type (length :: [a] -> Int)
:type fst
:type snd
:type head
:type take
:type zip
:type id
id 3
id "abc"
id True<jupyter_output><empty_output><jupyter_text>3.8 Overloaded types<jupyter_code>1 + 2
1.0 + 2.0
:type (+)<jupyter_output><empty_output><jupyter_text>`Num a` is a *class constraint* that constrains `a` to be any instance of the class `Num` of numeric types.A type that contains one or more class constraints is called *overloaded*, as is an expression with such a type.<jupyter_code>:type (*)
:type negate
:type abs
:type 3<jupyter_output><empty_output><jupyter_text>3.9 Basic classes A *class* is a collection of types that support certain overloaded operations called *methods*.  Eqequality types<jupyter_code>True == False
False == False
'a' == 'b'
'a' == 'a'
"abc" == "abc"
"abc" == "abcd"
[1,2] == [1,2]
[1,2] == [1,2,3]
[sin,cos] == [tan,sin]
('a',False) == ('a',False)
('a',True) == ('a',False)
('a',True) == ('b',True)
(sin,False) == (cos,True)<jupyter_output><empty_output><jupyter_text>Ordordered types<jupyter_code>False < True
min 'a' 'b'<jupyter_output><empty_output><jupyter_text>Strings, lists, and tuples are ordered *lexicographically*.<jupyter_code>"elegant" < "elephant"
[1,2,3] < [1,2]
('a',2) < ('b',1)
('a',2) < ('a',1)<jupyter_output><empty_output><jupyter_text>Showshowable types  Readreadable types  Numnumeric types  Integralintegral types  Fractionalfractional types ----<jupyter_code>:opt no-pager -- to inline :info
-- :info Eq
-- :info Ord
-- :info Show
-- :info Read
-- :info Num
-- :info Integral
-- :info Fractional<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17708333333333334, 'fraction_numerical': 0.023809523809523808, 'mean_word_length': 6.13129973474801, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '337330', 'license': 'MIT', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2068, 'n_words': 540, 'path': 'pih2ed/3 Types and Classes.ipynb', 'repo_name': 'hnu-pl/compiler2019fall', 'size': '5383'}