Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Sandra Dylus
advanced-programming19
Commits
c8deea71
Commit
c8deea71
authored
Dec 16, 2019
by
Sandra Dylus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add raw code and GHCi output of lecture on polymorphism
parent
1028aa63
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
516 additions
and
0 deletions
+516
-0
Functional/Lecture/Poly.hs
Functional/Lecture/Poly.hs
+222
-0
Functional/Lecture/PolyGHCI.txt
Functional/Lecture/PolyGHCI.txt
+294
-0
No files found.
Functional/Lecture/Poly.hs
0 → 100644
View file @
c8deea71
{-# LANGUAGE GADTSyntax #-}
module
Functional.Lecture.Poly
where
{-
> data CoordMap where
> EmptyC :: CoordMap
> ACoord :: Coordinate -> CoordMap -> CoordMap
> data Row where
> EmptyR :: Row
> ARow :: Token -> Row -> Row
> data Field where
> EmptyF :: Field
> AField :: Row -> Field -> Field
> data IntList where
> Nil :: IntList -- nil; nihil (nothing)
> Cons :: Integer -> IntList -> IntList
-- type Field = [[Token]]
-}
data
BoolList
where
EmptyB
::
BoolList
-- SingletonB :: Bool -> BoolList
ABool
::
Bool
->
BoolList
->
BoolList
-- here, `a` is type variable
-- similar to generics in Java List<A>
data
List
a
where
Nil
::
List
a
Cons
::
a
->
List
a
->
List
a
deriving
Show
-- something like a "type function" (type constructor)
-- List :: TYPE -> TYPE
-- Bool :: TYPE
-- Integer :: TYPE
const43
::
Integer
const43
=
43
intList
::
(
List
Integer
)
intList
=
Cons
42
(
Cons
12
Nil
)
intList3
::
Integer
->
(
List
Integer
)
intList3
n
=
Cons
n
(
Cons
n
(
Cons
n
Nil
))
boolList
::
List
Bool
boolList
=
Cons
True
(
Cons
False
(
Cons
True
Nil
))
lengthList
::
List
a
->
Integer
lengthList
Nil
=
0
lengthList
(
Cons
val
list
)
=
1
+
lengthList
list
-- mapCoordMap :: (Coordinate -> Coordinate) -> CoordMap -> CoordMap
-- mapList :: (a -> a ) -> List a -> List a
-- mapCoordToInt :: (Coordinate -> Integer) -> CoordMap -> IntList
mapList
::
(
a
->
b
)
->
List
a
->
List
b
mapList
f
Nil
=
Nil
mapList
f
(
Cons
elem
list
)
=
Cons
(
f
elem
)
(
mapList
f
list
)
-- mapList (\n -> even n) (Cons 13 intList)
-- (Cons 13 intList) :: List Integer
-- even :: Integer -> Bool
-- mapList :: (a -> b) -> List a -> List b
-- because of the usage of `Cons 13 intList`, I know the second argument needs to be
-- of type `List Integer`
-- mapList :: (a -> b) -> List a -> List b { List a -> List Integer, a -> Integer }
-- mapList :: (Integer -> b) -> List Integer -> List b
-- because of the usage `\n -> even n`, I know the function we use as first argument
-- needs to be of type `Integer -> Bool`
-- mapList :: (Integer -> b) -> List Integer -> List b { b -> Bool }
-- mapList :: (Integer -> Bool) -> List Integer -> List Bool
{-
λ> mapList (\b -> if b then 42 else 0) boolList
Cons 42 (Cons 0 (Cons 42 Nil))
λ> boolList
Cons True (Cons False (Cons True Nil))
λ> mapList (\n -> even n) intList
Cons True (Cons True Nil)
λ> intList
Cons 42 (Cons 12 Nil)
λ> mapList (\n -> even n) (Cons 13 intList)
Cons False (Cons True (Cons True Nil))
-}
-- can we have a even more generic type signature?
-- the only valid definition of `mapList2`
mapList2
::
(
a
->
b
)
->
List
c
->
List
d
mapList2
fAtoB
Nil
=
Nil
mapList2
fAtoB
(
Cons
cVal
cList
)
=
Nil
-- fAtoB :: a -> b
-- cVal :: c
-- cList :: List c
-- wrongList :: List Integer
-- wrongList = Cons False (Cons 42 Nil)
-- ^^^^^^^^^^^^^^^^^^^^^^
-- :: List Bool
-- False :: Bool
-- (Cons 42 Nil) :: List Bool <- no, it's `List Integer`
-- filterCoordMap :: (Coordinate -> Bool) -> CoordMap -> CoordMap
filterList
::
(
a
->
Bool
)
->
List
a
->
List
a
filterList
pred
Nil
=
Nil
filterList
pred
(
Cons
elem
list
)
=
if
pred
elem
then
Cons
elem
(
filterList
pred
list
)
else
filterList
pred
list
-- filterList (\b -> b) (Cons True (Cons False (Cons True Nil)))
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- = if (\b -> b) True
-- then Cons True (filterList (\b -> b) (Cons False (Cons True Nil)))
-- else filterList (\b -> b) (Cons False (Cons True Nil))
-- = Cons True (filterList (\b -> b) (Cons False (Cons True Nil)))
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- = Cons True (if (\b -> b) False
-- then Cons False (filterList (\b -> b) (Cons True Nil))
-- else filterList (\b -> b) (Cons True Nil))
-- = Cons True (filterList (\b -> b) (Cons True Nil))
-- = Cons True (Cons True (filterList (\b -> b) Nil))
-- = Cons True (Cons True Nil)
{-
λ> filterList (\b -> b) boolList
Cons True (Cons True Nil)
λ> (\b -> b) 42
42
λ> (\b -> b) True
True
λ> (\b -> b) (Cons 42 Nil)
Cons 42 Nil
λ> even 42
True
λ> (\b x y -> if b then x else y) True 42 12
42
λ> (\b x y -> if b then x else y) False 42 12
12
λ> (\b -> b) False
False
λ> filterList (\n -> n > 12) intList
Cons 42 Nil
λ> intList
Cons 42 (Cons 12 Nil)
-}
-- In Haskell the list data type actually looks like the following
--
-- data [] a where
-- [] :: [] a -- like `Nil`
-- (:) :: a -> [] a -> [] a -- like `Cons`
-- `map` (and `filter`) are also predefined in Haskell
-- map :: (a -> b) -> [] a -> [] b
-- map f [] = []
-- map f ((:) elem list) = (:) (f elem) (map f list)
-- map f (elem : list) = (f elem) : (map f list)
-- also allowed, instead of writing `[] a`, we can write `[a]`
-- map :: (a -> b) -> [a] -> [b]
-- Cons True (Cons True (Cons False Nil)) :: List Bool
-- True : (True : (False : [])) :: [] Bool
-- [True, True, False]
-- data Coordinate where
-- XYAxis :: Integer -> Integer -> Coordinate
--
-- xCoord :: Coordinate -> Integer
-- xCoord (XYAxis x y) = x
--
-- yCoord :: Coordinate -> Integer
-- yCoord (XYAxis x y) = y
-- predefined in Haskell
--
-- data (,) a b where
-- (,) :: a -> b -> (,) a b
--
-- (,) :: TYPE -> TYPE -> TYPE
-- I can also write it `(a,b)` instead of `(,) a b` (on type-level)
-- I can also write `(True, 42)` insteaf of `(,) True 42` (on value-level)
type
Coordinate
=
(,)
Integer
Integer
type
CoordMap
=
[
Coordinate
]
-- `token` is a type variable
type
Field
token
=
[[
token
]]
-- Field :: TYPE -> TYPE
-- fst :: (a,b) -> a
-- fst (x,y) = x
--
-- snd :: (a,b) -> b
-- snd (x,y) = y
-- predefined in Haskell
--
-- data Either a b where
-- Left :: a -> Either a b
-- Right :: b -> Either a b
intOrBool1
::
Either
Integer
Bool
intOrBool1
=
Left
42
intOrBool2
::
Either
Integer
Bool
intOrBool2
=
Right
True
Functional/Lecture/PolyGHCI.txt
0 → 100644
View file @
c8deea71
Hours of hacking await!
If I break, you can:
1. Restart: M-x haskell-process-restart
2. Configure logging: C-h v haskell-process-log (useful for debugging)
3. General config: M-x customize-mode
4. Hide these tips: C-h v haskell-process-show-debug-tips
Changed directory: /Users/sad/Documents/Teaching/advanced-programming-ws19/
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
/Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:(23,1)-(26,39): error:
• Illegal generalised algebraic data declaration for ‘BoolList’
(Enable the GADTs extension to allow this)
• In the data declaration for ‘BoolList’
|
23 | data BoolList where
| ^^^^^^^^^^^^^^^^^^^...
Failed, modules loaded: none.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> intList
<interactive>:8:1-7: error:
• No instance for (Show (List Integer))
arising from a use of ‘print’
• In a stmt of an interactive GHCi command: print it
λ> :R
unknown command ':R'
use :? for help.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> intList
Cons 42 (Cons 12 Nil)
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> intList 4
<interactive>:13:1-9: error:
• Couldn't match expected type ‘Integer -> t’
with actual type ‘List Integer’
• The function ‘intList’ is applied to one argument,
but its type ‘List Integer’ has none
In the expression: intList 4
In an equation for ‘it’: it = intList 4
• Relevant bindings include it :: t (bound at <interactive>:13:1)
λ> intList3 4
Cons 4 (Cons 4 (Cons 4 Nil))
λ> boolList
Cons True (Cons False (Cons True Nil))
λ> Cons False boolList
Cons False (Cons True (Cons False (Cons True Nil)))
λ> Cons False (Cons 42 Nil)
<interactive>:17:18-19: error:
• No instance for (Num Bool) arising from the literal ‘42’
• In the first argument of ‘Cons’, namely ‘42’
In the second argument of ‘Cons’, namely ‘(Cons 42 Nil)’
In the expression: Cons False (Cons 42 Nil)
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
/Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:54:30-31: error:
• No instance for (Num Bool) arising from the literal ‘42’
• In the first argument of ‘Cons’, namely ‘42’
In the second argument of ‘Cons’, namely ‘(Cons 42 Nil)’
In the expression: Cons False (Cons 42 Nil)
|
54 | wrongList = Cons False (Cons 42 Nil)
| ^^
Failed, modules loaded: none.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
/Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:54:13-36: error:
• Couldn't match type ‘Bool’ with ‘Integer’
Expected type: List Integer
Actual type: List Bool
• In the expression: Cons False (Cons 42 Nil)
In an equation for ‘wrongList’:
wrongList = Cons False (Cons 42 Nil)
|
54 | wrongList = Cons False (Cons 42 Nil)
| ^^^^^^^^^^^^^^^^^^^^^^^^
Failed, modules loaded: none.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
/Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:55:30-35: error:
Ambiguous occurrence ‘length’
It could refer to either ‘Prelude.length’,
imported from ‘Prelude’ at /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:3:8-30
(and originally defined in ‘Data.Foldable’)
or ‘Functional.Lecture.Poly.length’,
defined at /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:54:1
|
55 | length (Cons val list) = 1 + length list
| ^^^^^^
Failed, modules loaded: none.
λ> :R
unknown command ':R'
use :? for help.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> lengthList boolList
3
λ> lengthList intList
2
λ> lengthList (intList3 5)
3
λ> lengthList (Cons 42 (Cons 24 (intList3 5)))
5
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> mapList (\n -> n + 1) intList
Cons 43 (Cons 13 Nil)
λ> intList
Cons 42 (Cons 12 Nil)
λ> mapList (\b -> not b) boolList
Cons False (Cons True (Cons False Nil))
λ> boolList
Cons True (Cons False (Cons True Nil))
λ> not True
False
λ> not False
True
λ> mapList (\b -> if b then 42 else 0) boolList
<interactive>:36:26-27: error:
• No instance for (Num Bool) arising from the literal ‘42’
• In the expression: 42
In the expression: if b then 42 else 0
In the first argument of ‘mapList’, namely
‘(\ b -> if b then 42 else 0)’
λ> mapList (\n -> even n) intList
<interactive>:37:24-30: error:
• Couldn't match type ‘Integer’ with ‘Bool’
Expected type: List Bool
Actual type: List Integer
• In the second argument of ‘mapList’, namely ‘intList’
In the expression: mapList (\ n -> even n) intList
In an equation for ‘it’: it = mapList (\ n -> even n) intList
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> mapList (\b -> if b then 42 else 0) boolList
Cons 42 (Cons 0 (Cons 42 Nil))
λ> boolList
Cons True (Cons False (Cons True Nil))
λ> mapList (\n -> even n) intList
Cons True (Cons True Nil)
λ> intList
Cons 42 (Cons 12 Nil)
λ> mapList (\n -> even n) (Cons 13 intList)
Cons False (Cons True (Cons True Nil))
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> boolList
Cons True (Cons False (Cons True Nil))
λ> filterList (\b -> b) boolList
Cons True (Cons True Nil)
λ> (\b -> b) 42
42
λ> (\b -> b) True
True
λ> (\b -> b) (Cons 42 Nil)
Cons 42 Nil
λ> even True
<interactive>:50:1-9: error:
• No instance for (Integral Bool) arising from a use of ‘even’
• In the expression: even True
In an equation for ‘it’: it = even True
λ> even 42
True
λ> (\b x y -> if b then x else y) True 42 12
42
λ> (\b x y -> if b then x else y) False 42 12
12
λ> (\b -> b) False
False
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> filterList (\n -> n > 12) intList
Cons 42 Nil
λ> intList
Cons 42 (Cons 12 Nil)
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> True : True : False : []
[True,True,False]
λ> [12,42,123]
[12,42,123]
λ> 1 : [12, 42, 123]
[1,12,42,123]
λ> 1 : [12, 42, 123]
[1,12,42,123]
λ> [1, 12, 42, 123]
[1,12,42,123]
λ> map (\b -> b) [True, False, True]
[True,False,True]
λ> map (\b -> not b) [True, False, True]
[False,True,False]
λ> map (\b -> or b True) [True, False, True]
<interactive>:66:12-20: error:
• Couldn't match expected type ‘Bool -> b’ with actual type ‘Bool’
• The function ‘or’ is applied to two arguments,
but its type ‘[Bool] -> Bool’ has only one
In the expression: or b True
In the first argument of ‘map’, namely ‘(\ b -> or b True)’
• Relevant bindings include it :: [b] (bound at <interactive>:66:1)
<interactive>:66:24-27: error:
• Couldn't match expected type ‘[Bool]’ with actual type ‘Bool’
• In the expression: True
In the second argument of ‘map’, namely ‘[True, False, True]’
In the expression: map (\ b -> or b True) [True, False, True]
<interactive>:66:30-34: error:
• Couldn't match expected type ‘[Bool]’ with actual type ‘Bool’
• In the expression: False
In the second argument of ‘map’, namely ‘[True, False, True]’
In the expression: map (\ b -> or b True) [True, False, True]
<interactive>:66:37-40: error:
• Couldn't match expected type ‘[Bool]’ with actual type ‘Bool’
• In the expression: True
In the second argument of ‘map’, namely ‘[True, False, True]’
In the expression: map (\ b -> or b True) [True, False, True]
λ> map (\b -> b || True) [True, False, True]
[True,True,True]
λ> map (\n -> even n) [12, 123,14]
[True,False,True]
λ> filter (\n -> even n) [12, 123,14]
[12,14]
λ> filter (\n -> odd n) [12, 123,14]
[123]
λ> :t (,)
(,) :: a -> b -> (a, b)
λ> (,) True 42
(True,42)
λ> fst (True,42)
True
λ> snd (True,42)
42
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
/Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:209:15-28: error:
• Expecting one more argument to ‘Either Integer’
Expected a type, but ‘Either Integer’ has kind ‘* -> *’
• In the type signature: intOrBool1 :: Either Integer
|
209 | intOrBool1 :: Either Integer
| ^^^^^^^^^^^^^^
Failed, modules loaded: none.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
/Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs:210:14-15: error:
• No instance for (Num (Either Integer Bool))
arising from the literal ‘42’
• In the expression: 42
In an equation for ‘intOrBool1’: intOrBool1 = 42
|
210 | intOrBool1 = 42
| ^^
Failed, modules loaded: none.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> :r
[1 of 1] Compiling Functional.Lecture.Poly ( /Users/sad/Documents/Teaching/advanced-programming-ws19/Functional/Lecture/Poly.hs, interpreted )
Ok, modules loaded: Functional.Lecture.Poly.
λ> [[1,2] , [2,3]]
[[1,2],[2,3]]
λ> [(1,2) , (2,3)]
[(1,2),(2,3)]
λ>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment