diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-04-25 15:15:56 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-04-25 15:15:56 +0200 |
commit | a42869d36c1dc85c2f41d405a5800e2e6992a2df (patch) | |
tree | 25cbb0b87b90759298447f0dd912baa665cbfac4 | |
parent | 580b55955fc972c928b244f3086c7d4094b4693f (diff) | |
download | uni-a42869d36c1dc85c2f41d405a5800e2e6992a2df.tar uni-a42869d36c1dc85c2f41d405a5800e2e6992a2df.tar.gz uni-a42869d36c1dc85c2f41d405a5800e2e6992a2df.tar.bz2 uni-a42869d36c1dc85c2f41d405a5800e2e6992a2df.tar.xz uni-a42869d36c1dc85c2f41d405a5800e2e6992a2df.zip |
FSV 02 H2-1
-rw-r--r-- | ss2016/fsv/02/H2-1.hs | 76 | ||||
-rw-r--r-- | ss2016/fsv/02/manifest | 1 |
2 files changed, 77 insertions, 0 deletions
diff --git a/ss2016/fsv/02/H2-1.hs b/ss2016/fsv/02/H2-1.hs new file mode 100644 index 0000000..8f83caf --- /dev/null +++ b/ss2016/fsv/02/H2-1.hs | |||
@@ -0,0 +1,76 @@ | |||
1 | import qualified Data.Map as Map | ||
2 | import qualified Data.Set as Set | ||
3 | import Data.Map (Map) | ||
4 | import Data.Set (Set) | ||
5 | import Data.Maybe | ||
6 | |||
7 | data Formula = Or [Formula] | ||
8 | | Not Formula | ||
9 | | Var Char | ||
10 | |||
11 | type Assignment = Map Char Bool | ||
12 | |||
13 | type CNFFormula = [Assignment -> Bool] | ||
14 | |||
15 | satisfies :: Assignment -> CNFFormula -> Bool | ||
16 | satisfies = all . flip ($) | ||
17 | |||
18 | asCNF :: [Formula] -> CNFFormula | ||
19 | asCNF = map asMap | ||
20 | where | ||
21 | -- asMap :: Formula -> (Assignment -> Bool) | ||
22 | asMap (Or xs) = \a -> any (($ a) . asMap) xs | ||
23 | asMap (Not x) = \a -> not $ (asMap x) a | ||
24 | asMap (Var x) = \a -> fromMaybe (error $ "Key " ++ [x] ++ " not in assignment") $ Map.lookup x a | ||
25 | |||
26 | keys :: Formula -> Set Char | ||
27 | keys (Or xs) = Set.unions $ map keys xs | ||
28 | keys (Not x) = keys x | ||
29 | keys (Var c) = Set.singleton c | ||
30 | |||
31 | doDPLL :: CNFFormula -> [Char] -> Maybe Assignment | ||
32 | doDPLL [] _ = Just Map.empty | ||
33 | doDPLL _ [] = Just Map.empty | ||
34 | doDPLL f keys = doDPLL' as f | ||
35 | where | ||
36 | as = map (Map.fromList . zip keys) . takeWhile ((== length keys) . length) . dropWhile ((< length keys) . length) $ bs | ||
37 | bs = [[True], [False]] ++ [ head : tail | tail <- bs, head <- [True, False] ] | ||
38 | doDPLL' [] f = Nothing | ||
39 | doDPLL' (a:as) f | ||
40 | | a `satisfies` f = Just a | ||
41 | | otherwise = doDPLL' as f | ||
42 | |||
43 | main = do | ||
44 | let | ||
45 | fs = [ Var 'D' | ||
46 | , Or [ Not $ Var 'D' | ||
47 | , Not $ Var 'A' | ||
48 | , Var 'B' | ||
49 | ] | ||
50 | , Or [ Not $ Var 'B' | ||
51 | , Not $ Var 'E' | ||
52 | ] | ||
53 | , Or [ Var 'E' | ||
54 | , Var 'A' | ||
55 | , Not $ Var 'D' | ||
56 | ] | ||
57 | , Or [ Not $ Var 'F' | ||
58 | , Var 'G' | ||
59 | ] | ||
60 | , Or [ Not $ Var 'G' | ||
61 | , Not $ Var 'D' | ||
62 | , Not $ Var 'C' | ||
63 | ] | ||
64 | , Or [ Var 'C' | ||
65 | , Not $ Var 'H' | ||
66 | ] | ||
67 | , Or [ Var 'H' | ||
68 | , Var 'F' | ||
69 | ] | ||
70 | , Or [ Var 'D' | ||
71 | , Var 'A' | ||
72 | , Var 'B' | ||
73 | ] | ||
74 | ] | ||
75 | print $ doDPLL (asCNF fs) (Set.toList . Set.unions $ map keys fs) | ||
76 | -- Just (fromList [('A',False),('B',False),('C',True),('D',True),('E',True),('F',False),('G',False),('H',True)]) | ||
diff --git a/ss2016/fsv/02/manifest b/ss2016/fsv/02/manifest new file mode 100644 index 0000000..51f6d31 --- /dev/null +++ b/ss2016/fsv/02/manifest | |||
@@ -0,0 +1 @@ | |||
H2-1.hs \ No newline at end of file | |||