-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.Relations.lhs
More file actions
58 lines (40 loc) · 1.37 KB
/
3.Relations.lhs
File metadata and controls
58 lines (40 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
The prolog program below:
% Normally we wouldn't define numbers in terms of their even/oddness,
% but use that information to define a relation:
even(zero,true).
even(succ(N),B) :- odd(N, B).
odd(zero, false).
odd(succ(N),B) :- even(N, B).
can be directly translated to type-level Haskell (with some extensions)
> {-# LANGUAGE MultiParamTypeClasses #-}
> {-# LANGUAGE FlexibleInstances #-}
> import Prelude hiding (even, odd)
Now we need some boolean types
> data True
> data False
And our old natural numbers
> data Zero
> data Succ n
> type Three = Succ (Succ (Succ Zero))
Again we include the operations even though the types are what matter
> class Even n b where
> even :: n -> b -- Only because we must!
> class Odd n b where
> odd :: n -> b -- C'est la vie.
> instance Even Zero True
> instance Odd n b => Even (Succ n) b
> instance Odd Zero False
> instance Even n b => Odd (Succ n) b
We can check this in ghci by issuing commands like the following:
:type odd (undefined :: Three) :: True
:type odd (undefined :: Three) :: False
:type even (undefined :: Three) :: False
This is just to get the interactive panel working:
> instance Show Zero where
> show _ = "Success!"
> instance Show (Succ n) where
> show _ = "Success"
> instance Show True where
> show _ = "'True"
> instance Show False where
> show _ = "'False"