-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypes.hs
More file actions
116 lines (86 loc) · 2.75 KB
/
Copy pathTypes.hs
File metadata and controls
116 lines (86 loc) · 2.75 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Types where
import Data.List (intersperse)
-- Predicate
data PredicateArgs = StringArg String | IntArg Int | PredicateArg Predicate
deriving (Eq)
instance Show PredicateArgs where
show (IntArg i) = show i
show (PredicateArg p) = show p
show (StringArg a) = a
data Predicate = Predicate { predicateAlias :: Maybe String,
predicateNegated :: Bool,
predicateName :: String,
predicateArgs :: [PredicateArgs]
} deriving (Eq)
instance Show Predicate where
show (Predicate alias negated name args) =
let showNeg = if negated then "\\" else ""
in showNeg ++ name ++ showAlias alias ++ "(" ++ myShowList args ++ ")"
showAlias :: Maybe String -> String
showAlias Nothing = ""
showAlias (Just a) = "/" ++ a
myShowList :: Show a => [a] -> String
myShowList [] = ""
myShowList [arg] = show arg
myShowList (arg:args) = show arg ++ ", " ++ myShowList args
instance Ord Predicate where
compare p1 p2 = compare (predicateName p1) (predicateName p2)
emptyPredicate :: Predicate
emptyPredicate = Predicate {
predicateAlias = Nothing,
predicateNegated = False,
predicateName = "",
predicateArgs = []
}
-- Type
data Type = Str | N | P [String]
deriving (Eq)
instance Show Type where
show Str = "Str"
show N = "Int"
show (P args) = unwords $ intersperse "|" args
data TypeDef = TypeDef { typeName :: String,
typeArgs :: [Type]
} deriving (Eq)
emptyTypeDef :: TypeDef
emptyTypeDef = TypeDef {
typeName = "",
typeArgs = []
}
instance Ord TypeDef where
compare td1 td2 = compare (typeName td1) (typeName td2)
instance Show TypeDef where
show (TypeDef name args) =
name ++ "(" ++ myShowList args ++ ")"
-- Rules
type Conjunction = [Predicate]
data Rule = Rule { premises :: Conjunction,
consequences :: Conjunction
} deriving (Eq)
-- Show instance for Rule with proper formatting
instance Show Rule where
show (Rule prem cons) =
"Rule { " ++ myShowList prem ++ " -> " ++ myShowList cons ++ " }\n"
-- Expr
data Expr = ExprPredicate Predicate
| ExprTypeDef TypeDef
| ExprRule Rule
| ExprCommand Command
| ExprQuestion Conjunction
deriving (Show, Eq)
-- Commands
data Command = ShowFacts
| ShowDeclaredTypes
| ShowRules
| ForwardChaining
| StrategyRecent
| StrategyFirst
| Quit
deriving (Eq, Show)
-- Key
class Key a where
key :: a -> String
instance Key Predicate where
key = predicateName
instance Key TypeDef where
key = typeName