Repository navigation
Expand file tree
/
Copy pathSat.hs
More file actions
67 lines (51 loc) · 1.75 KB
/
Copy pathSat.hs
File metadata and controls
67 lines (51 loc) · 1.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
-- | Example of a simple SAT solver (simply constructs the truth table) in
-- Copilot.
-- {-# LANGUAGE RebindableSyntax #-}
module Sat where
import Language.Copilot
import qualified Prelude as P
import Control.Monad (foldM_)
---------------------------------------------------------------------------------
-- | Number of sat variables and a Boolean function over those variables.
type SatFunc = (Int, [Stream Bool] -> Stream Bool)
-- Takes a Sat function and returns the stream of variables and a SAT function.
sat :: SatFunc -> ([Stream Bool], Stream Bool)
sat (i,f) = (xs, res)
where
-- If it becomes true, it stays true.
res = [False] ++ (f xs || res)
xs = xs' i
xs' 0 = []
xs' n = clk (period $ 2 P.^ n) (phase (0 :: Int)) : xs' (n P.- 1)
satSpec :: SatFunc -> Spec
satSpec f = do
observer "sat" (snd $ sat f)
observer "done" done
foldM_ mkObs (0 :: Int) xs
where
xs = fst $ sat f
cnt :: Stream Word64 -- Would need to be extended depending on number of
-- variables.
cnt = [1] ++ cnt + 1
done = cnt == (2 P.^ length xs)
mkObs idx strm = do observer ("obs" P.++ show idx) strm
return (idx P.+ 1)
f0 :: SatFunc
--f0 = (3, \[a,b,c] -> a && (b || (not c)))
f0 = (3, f)
where
f [a,b,c] = a && (b || (not c))
f _ = error "Bad SAT function."
f1 :: SatFunc
f1 = (9, f')
where
f' [a,b,c,d,e,f,g,h,i] =
a && b && c && d && e &&
f && g && h && i && i && not i
f' _ = error "Bad SAT function."
-- | Run the interpreter long enough to get an answer.
runFunc :: SatFunc -> IO ()
runFunc f = interpret (2 P.^ fst f) (satSpec f)
satExamples :: IO ()
satExamples = runFunc f0 >> runFunc f1
---------------------------------------------------------------------------------