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
|
module Language.Prolog.Goodies
( plList, isPlVar, rootOf, argsOf, goalVars, termVars, termsVars
, termVarOccs, goalConstrs, termConstrs )
where
import Data.List ( union )
import Language.Prolog.Types
plList :: [PlTerm] -> PlTerm
plList = foldr (\t ts -> PlStruct "." [t,ts]) (PlAtom "[]")
isPlVar :: PlTerm -> Bool
isPlVar pterm = case pterm of PlVar _ -> True
_ -> False
rootOf :: PlTerm -> (String,Int)
rootOf pterm = case pterm of
PlVar _ -> ("", 0)
PlInt i -> (show i, 0)
PlFloat x -> (show x, 0)
PlAtom a -> (a, 0)
PlStruct s args -> (s, length args)
argsOf :: PlTerm -> [PlTerm]
argsOf pterm = case pterm of
PlStruct _ args -> args
_ -> []
goalVars :: PlGoal -> [String]
goalVars pgoal = case pgoal of
PlLit _ ts -> termsVars ts
PlNeg goals -> unionMap goalVars goals
PlCond gs1 gs2 gs3 -> unionMap (unionMap goalVars) [gs1,gs2,gs3]
termVars :: PlTerm -> [String]
termVars pterm = case pterm of
PlVar v -> [v]
PlStruct _ ts -> termsVars ts
_ -> []
termsVars :: [PlTerm] -> [String]
termsVars = unionMap termVars
termVarOccs :: PlTerm -> [String]
termVarOccs pterm = case pterm of
PlVar v -> [v]
PlStruct _ ts -> concatMap termVars ts
_ -> []
goalConstrs :: PlGoal -> [(String,Int)]
goalConstrs pgoal = case pgoal of
PlLit _ ts -> unionMap termConstrs ts
PlNeg goals -> unionMap goalConstrs goals
PlCond gs1 gs2 gs3 -> unionMap (unionMap goalConstrs) [gs1,gs2,gs3]
termConstrs :: PlTerm -> [(String,Int)]
termConstrs pterm = case pterm of
PlAtom a -> [(a,0)]
PlStruct s ts -> union [(s, length ts)] (unionMap termConstrs ts)
_ -> []
unionMap :: Eq b => (a -> [b]) -> [a] -> [b]
unionMap f = foldr union [] . map f
|