-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSSP.hs
executable file
·122 lines (93 loc) · 3.39 KB
/
SSP.hs
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
117
118
119
120
121
--Structural Synthesis of Programs
--(c) Pavel Grigorenko
module Ssp where
import List (delete, intersect, nub)
--import Maybe (isJust, fromJust)
--import qualified Data.Set as Set
data Var = Var String deriving Eq
type Inputs = [Var]
type Output = Var
type Outputs = [Var]
data Subtask = Subtask Inputs Outputs
data Axiom = UCStm Inputs Output
| CStm [Subtask] Inputs Output
instance Show Var where
show (Var x) = x
instance Show Subtask where
show (Subtask is os) = "[" ++ showLst is ++ " -> " ++ showLst os ++ "]"
instance Show Axiom where
show (UCStm is o) = showLst is ++ " -> " ++ show o ++ "{f}"
show (CStm ss is o) = showLst ss ++ (if not $ null is then ", " ++ showLst is ++ " " else " ") ++ "-> " ++ show o ++ "{f}"
showLst :: Show a => [a] -> String
showLst = foldl (\acc l -> if acc == "" then show l else acc ++ ", " ++ show l) ""
tAxiom1 = print $ UCStm [Var "a", Var "x"] (Var "y")
tAxiom2 = print $ UCStm [] (Var "y")
tAxiom3 = print $ CStm [Subtask [Var "u", Var "v"] [Var "x", Var "y"]]
[Var "a", Var "b"]
(Var "c")
tAxiom4 = print $ CStm [Subtask [Var "u"] [Var "v"], Subtask [Var "x"] [Var "y"]]
[Var "a", Var "b"]
(Var "c")
tAxiom5 = print $ CStm [Subtask [Var "u"] [Var "v"], Subtask [Var "x"] [Var "y"]]
[]
(Var "c")
-- transform IL into LL
--collect all propostional atoms in a list without dublicates
atoms :: [Prop] -> [Var]
atoms = nub . concat . map help where
help (Atom a) = [Var a]
help (Conj l r) = help l ++ help r
help (Impl l r) = help l ++ help r
help _ = []
tAtoms = atoms [a*b, c*b, x, y, T, F]
transl p = (goal, as) where (goal, as, _) = tr p 0
tr :: Prop -> Int -> (Var, [Axiom], Int)
tr (Impl l r) c = (x, UCStm [x, lv] rv : CStm [Subtask [lv] [rv]] [] x : axioms, nc )
where (lv, rv, axioms, x, nc) = trHelp l r c
tr (Conj l r) c = (x, UCStm [lv, rv] x : UCStm [x] lv : UCStm [x] rv : axioms, nc)
where (lv, rv, axioms, x, nc) = trHelp l r c
tr (Atom p) c = (Var p, [], c)
trHelp l r c = (lv, rv, laxioms ++ raxioms, x, nc)
where x = makeVar nc
nc = rc + 1
(lv, laxioms, lc) = tr l c
(rv, raxioms, rc) = tr r lc
makeVar :: Int -> Var
makeVar c = Var ("X" ++ show c)
--tests
kripke = (((a --> b) --> a) --> a) --> b
kripke2 = ((((a --> b) --> a) --> a) --> b) --> b
testTransl f = show f ++ " -----> " ++ foldl (\acc l -> if null acc then show l else acc ++ "; " ++ show l) "" (snd $ transl f)
tt1 = testTransl kripke -- in CoCoViLa the goal is X4 -> b;
tt2 = testTransl kripke2 -- in CoCoViLa the goal is -> X5;
-- /transform IL into LL
----------Propositional logic
data Prop = Atom String |
Conj Prop Prop |
Impl Prop Prop |
T |
F
deriving (Eq,Ord)
isAtom (Atom _) = True
isAtom _ = False
instance Num Prop where
p * q = Conj p q
negate p = Impl p F
infixr 4 -->
p --> q = Impl p q
a = Atom "a"
b = Atom "b"
c = Atom "c"
p = Atom "p"
q = Atom "q"
r = Atom "r"
v = Atom "v"
x = Atom "x"
y = Atom "y"
z = Atom "z"
instance Show Prop where
show (Atom p) = p
show (Conj p q) = "(" ++ show p ++ "&" ++ show q ++ ")"
show (Impl p q) = "(" ++ show p ++ "->" ++ show q ++ ")"
show T = "T"
show F = "F"