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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
module Curry2SMT where
import Maybe ( fromMaybe )
import FlatCurry.Annotated.Goodies ( argTypes, resultType )
import FlatCurry.Annotated.Types
import BoolExp
import TypedFlatCurryGoodies
m1 :: IO ()
m1 = funcs2SMT [("Prelude","take")] >>= putStrLn
m2 :: IO ()
m2 = do let fnames = ["null","length","take"]
funcs2SMT (map pre fnames) >>= putStrLn
funcs2SMT :: [QName] -> IO String
funcs2SMT qns = do
funs <- getAllFunctions [] [] qns
return $ unlines (map ftype2SMT funs ++ [""] ++ map fdecl2SMT funs)
ftype2SMT :: TAFuncDecl -> String
ftype2SMT (AFunc qn _ _ texp _) =
asLisp ["declare-fun", transOpName qn,
asLisp (map (smtBE . type2SMTExp) (argTypes texp)),
smtBE (type2SMTExp (resultType texp))]
fdecl2SMT :: TAFuncDecl -> String
fdecl2SMT (AFunc qn _ _ _ rule) = unlines
["; define '" ++ showQName qn ++ "' by axiomatization of defining rules:",
smtBE (rule2SMT rule)]
where
rule2SMT (AExternal _ s) =
assertSMT $ bEqu (BTerm (transOpName qn) []) (BTerm ("External:" ++ s) [])
rule2SMT (ARule _ vs exp) =
assertSMT $ forallBinding (map (\ (v,t) -> (v, type2SMTExp t)) vs)
(if ndExpr exp
then exp2SMT (Just lhs) exp
else bEqu lhs (exp2SMT Nothing exp))
where
lhs = BTerm (transOpName qn) (map (BVar . fst) vs)
exp2SMT :: Maybe BoolExp -> TAExpr -> BoolExp
exp2SMT lhs exp = case exp of
AVar _ i -> makeRHS (BVar i)
ALit _ l -> makeRHS (lit2bool l)
AComb _ _ (qn,_) args ->
makeRHS (BTerm (transOpName qn) (map (exp2SMT Nothing) args))
ACase _ _ e brs -> let be = exp2SMT Nothing e
in branches2SMT be brs
ALet _ bs e -> letBinding (map (\ ((v,_),be) -> (v, exp2SMT Nothing be)) bs)
(exp2SMT lhs e)
ATyped _ e _ -> exp2SMT lhs e
AFree _ fvs e -> forallBinding (map (\ (v,t) -> (v, type2SMTExp t)) fvs)
(exp2SMT lhs e)
AOr _ e1 e2 -> Disj [exp2SMT lhs e1, exp2SMT lhs e2]
where
makeRHS rhs = maybe rhs (\l -> bEqu l rhs) lhs
branches2SMT _ [] = error "branches2SMT: empty branch list"
branches2SMT be [ABranch p e] = branch2SMT be p e
branches2SMT be (ABranch p e : brs@(_:_)) =
BTerm "ite" [bEqu be (pat2bool p), branch2SMT be p e,
branches2SMT be brs]
branch2SMT _ (ALPattern _ _) e = exp2SMT lhs e
branch2SMT be (APattern _ (qf,_) ps) e = case ps of
[] -> exp2SMT lhs e
_ -> letBinding (map (\ (s,v) -> (v, BTerm s [be]))
(zip (selectors qf) (map fst ps)))
(exp2SMT lhs e)
selectors :: QName -> [String]
selectors qf | qf == ("Prelude",":") = ["head","tail"]
| otherwise = error $ "Unknown selectors: " ++ snd qf
type2SMTExp :: TypeExpr -> BoolExp
type2SMTExp (TVar _) = BTerm "TVar" []
type2SMTExp (FuncType dom ran) = BTerm "->" (map type2SMTExp [dom,ran])
type2SMTExp (TCons (mn,tc) targs)
| mn=="Prelude" && length targs == 0 = BTerm tc []
| mn=="Prelude" && tc == "[]" && length targs == 1
= BTerm "List" [type2SMTExp (head targs)]
| otherwise = BTerm (mn ++ "." ++ tc) []
pat2bool :: TAPattern -> BoolExp
pat2bool (ALPattern _ l) = lit2bool l
pat2bool (APattern _ (qf,_) ps) = BTerm (transOpName qf) (map (BVar . fst) ps)
lit2bool :: Literal -> BoolExp
lit2bool (Intc i) = BTerm (show i) []
lit2bool (Floatc i) = BTerm (show i) []
lit2bool (Charc i) = BTerm (show i) []
transOpName :: QName -> String
transOpName (mn,fn)
| mn=="Prelude" = fromMaybe (mn ++ "_" ++ fn)
(lookup fn (primCons ++ preludePrimOps))
| otherwise = mn ++ "_" ++ fn
untransOpName :: String -> Maybe QName
untransOpName s = let (mn,ufn) = break (=='_') s in
if null ufn
then Nothing
else Just (mn, tail ufn)
|