summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-05-14 18:30:04 +0200
committermanuel <manuel@mausz.at>2012-05-14 18:30:04 +0200
commitcf38b6a59a5f1d8d2db81cfb64d660d51de4697d (patch)
treec7e6ebcca0bf43fd2018ea60f7a8bd0a7e55ee9d
parent66e6cf52c24e0db7ab0fcba6c84cf9df65ab88e6 (diff)
downloadffp-cf38b6a59a5f1d8d2db81cfb64d660d51de4697d.tar.gz
ffp-cf38b6a59a5f1d8d2db81cfb64d660d51de4697d.tar.bz2
ffp-cf38b6a59a5f1d8d2db81cfb64d660d51de4697d.zip
implement yield_bt and some other minor stuff
-rw-r--r--AufgabeFFP6.hs92
1 files changed, 77 insertions, 15 deletions
diff --git a/AufgabeFFP6.hs b/AufgabeFFP6.hs
index 2e0a021..b4bb34b 100644
--- a/AufgabeFFP6.hs
+++ b/AufgabeFFP6.hs
@@ -5,39 +5,101 @@ where
5 5
6import Data.Array 6import Data.Array
7 7
8(+) :: Int -> Int -> Int 8type F = Array Int Int
9(+) = (Prelude.+) 9type Op = Int -> Int -> Int
10type G = Array Int (Op)
11type W = Int
10 12
11(-) :: Int -> Int -> Int 13--------------------------------------------------------------------------------
12(-) = (Prelude.-)
13
14(*) :: Int -> Int -> Int
15(*) = (Prelude.*)
16 14
17(./.) :: Int -> Int -> Int 15(./.) :: Int -> Int -> Int
18(./.) = div 16(./.) = div
19 17
20myfoldl :: [(a -> b -> a)] -> a -> [b] -> a 18myfoldl :: [(a -> b -> a)] -> a -> [b] -> a
21myfoldl _ z [] = z 19myfoldl _ f1 [] = f1
22myfoldl (f:fs) z (x:xs) = myfoldl fs (f z x) xs 20myfoldl (g:gs) f1 (f2:fs) = myfoldl gs (g f1 f2) fs
23 21
24myfoldl' :: [(a -> a -> a)] -> [a] -> a 22myfoldl' :: [(a -> a -> a)] -> [a] -> a
25myfoldl' f (x:xs) = myfoldl f x xs 23myfoldl' g (f:fs) = myfoldl g f fs
26 24
27eval :: Array Int Int -> Array Int (Int -> Int -> Int) -> Int 25eval :: F -> G -> W
28eval a b = myfoldl' (elems b) (elems a) 26eval f g = myfoldl' (elems g) (elems f)
29 27
30-------------------------------------------------------------------------------- 28--------------------------------------------------------------------------------
31 29
32-- yield :: Array Int Int -> Int -> [Array Int (Int -> Int -> Int)] 30yield :: F -> W -> [G]
33-- yield = yield_bt 31yield = yield_bt
34-- yield = yield_gtf 32-- yield = yield_gtf
35 33
36-- yield_bt :: Array Int Int -> Int -> [Array Int (Int -> Int -> Int)] 34--------------------------------------------------------------------------------
35
36type Node = ([Int], [Int], W, [Op])
37
38goal_yield_bt :: Node -> Bool
39goal_yield_bt (f, f', w, g)
40 | length g == 0 = False -- no operators
41 | length f' == 0 = eval (listArray (1, length f) f) (listArray (1, length g) g) == w
42 | otherwise = False
43
44succ_yield_bt :: Node -> [Node]
45succ_yield_bt (_, [], _, _) = []
46succ_yield_bt (f, f', w, g) = [(f_new, f'_new, w, g ++ [g_new]) | g_new <- ops]
47 where
48 f_new = f ++ [head f']
49 f'_new = tail f'
50 ops = [(+), (-), (*), (./.)]
51
52data Stack a = EmptyStk
53 | Stk a (Stack a)
54
55push :: a -> Stack a -> Stack a
56push x s = Stk x s
57
58pop :: Stack a -> Stack a
59pop EmptyStk = error "pop from an empty stack"
60pop (Stk _ s) = s
61
62top :: Stack a -> a
63top EmptyStk = error "top from an empty stack"
64top (Stk x _) = x
65
66emptyStack :: Stack a
67emptyStack = EmptyStk
68
69stackEmpty :: Stack a -> Bool
70stackEmpty EmptyStk = True
71stackEmpty _ = False
72
73searchDfs :: (node -> [node]) -> (node -> Bool) -> node -> [node]
74searchDfs succ goal x = search' (push x emptyStack)
75 where
76 search' s
77 | stackEmpty s = []
78 | goal (top s) = top s : search' (pop s)
79 | otherwise =
80 let x = top s
81 in search' (foldr push (pop s) (succ x))
82
83yield_bt :: F -> W -> [G]
84yield_bt f w = map (\(_, _, _, g) -> listArray (1, length g) g) nodes
85 where
86 f_elem1 = head (elems f)
87 f_tail = tail (elems f)
88 nodes = searchDfs succ_yield_bt goal_yield_bt ([f_elem1], f_tail, w, [])
89
90--------------------------------------------------------------------------------
91
37-- yield_gtf :: Array Int Int -> Int -> [Array Int (Int -> Int -> Int)] 92-- yield_gtf :: Array Int Int -> Int -> [Array Int (Int -> Int -> Int)]
38 93
39-------------------------------------------------------------------------------- 94--------------------------------------------------------------------------------
40 95
96instance Show (Integer -> Integer -> Integer) where
97 show op = case (3 `op` 3) of
98 6 -> "plus"
99 0 -> "minus"
100 9 -> "times"
101 1 -> "div"
102 _ -> "unknown"
41 103
42instance Show (Int -> Int -> Int) where 104instance Show (Int -> Int -> Int) where
43 show op = case (3 `op` 3) of 105 show op = case (3 `op` 3) of