-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnify.hs
More file actions
78 lines (57 loc) · 1.63 KB
/
Unify.hs
File metadata and controls
78 lines (57 loc) · 1.63 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Unify (Subst (S), sub, emp, get, Substitutable, Unifiable, subst, unify, cmb)
where
import Data.Maybe
import Data.List
--Part f
--Problem 1
--Part a
data Subst a = S [(String, a)]
deriving (Show, Eq)
--Part b
emp = S []
sub :: String -> a -> Subst a
sub str x = S [(str, x)]
--Part c
get :: String -> Subst a -> Maybe a
get str (emp) = Nothing
get str (S ((str1, x):xs))
|str == str1 = Just x
|otherwise = get str (S xs)
--Part d
class Substitutable a where
subst :: Subst a -> a -> a
--Part e
class (Eq a, Substitutable a) => Unifiable a where
unify :: a -> a -> Maybe (Subst a)
--Problem 3
--Part a
--added qualifyer "Eq a"
unresolved :: Eq a => [(String, a)] -> Maybe (String, a, a)
unresolved [] = Nothing
unresolved list
|length l1 == 0 = unresolved (tail list)
|(head l2) == (last l2) = Nothing
|otherwise = Just ((fst (head list), head l2, last l2))
where
l1 = [x| x <- (tail list), (fst x) == (fst (head list))]
l2 = snd (head list) : [snd y| y <- l1, snd y /= snd (head list)]
--Part b
resolve :: Unifiable a => Subst a -> Maybe (Subst a)
resolve (S subList)
|unresolved subList == Nothing = Just (S subList)
|unify b c == Nothing = Nothing
|otherwise = resolve newList
where
(a, b, c) = fromJust (unresolved subList)
S (r) = fromJust (unify b c)
temp = delete (a, b) subList
newList = S (r ++ subList)
--Part c
cmb:: Unifiable a => Maybe (Subst a) -> Maybe (Subst a) -> Maybe (Subst a)
cmb _ (Nothing) = Nothing
cmb (Nothing) _ = Nothing
cmb (Nothing) (Nothing) = Nothing
cmb x y = resolve (S (sub1 ++ sub2))
where
(S sub1) = fromJust x
(S sub2) = fromJust y