forked from nsmryan/HEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplex.hs
More file actions
50 lines (39 loc) · 1.47 KB
/
Multiplex.hs
File metadata and controls
50 lines (39 loc) · 1.47 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
module Multiplex (
countCorrect,
multicases,
register,
registers,
ifthenOP,
notOP,
orOP,
andOP
) where
import Operators
import Data.List
addrs 0 = [[]]
addrs k = [True:ad | ad <- ads] ++ [False:ad | ad <- ads] where
ads = addrs (k-1)
answer k bs = bs !! (k + toInt (take k bs))
toInt bs = sum $ zipWith (\index i -> if i then 2^index else 0) [0..length bs] bs
multicases k = map (\bs -> (bs, answer k bs)) (addrs (k+2^k))
countCorrect _ worst Nothing = return worst
countCorrect tests _ (Just f) = return $ foldl' evalcase 0 tests where
evalcase fit (bs, b) = if f bs == b then fit+1 else fit
register i = OP {eats=0, leaves=1, applyOp=((!! i):), name=show i}
registers k = map register [0..k-1]
type Multiplexer = [Bool] -> Bool
ifthen' :: Multiplexer -> Multiplexer -> Multiplexer -> [Bool] -> Bool
ifthen' b f f' bs = if b bs then f bs else f' bs
not' :: Multiplexer -> [Bool] -> Bool
not' f bs = not (f bs)
or' :: Multiplexer -> Multiplexer -> [Bool] -> Bool
or' f f' bs = f bs || f' bs
and' :: Multiplexer -> Multiplexer -> [Bool] -> Bool
and' f f' bs = f bs && f' bs
--wrap1 f (a:as) = f a:as
--wrap2 f (a:a':as) = f a a':as
--wrap3 f (a:a':a'':as) = f a a' a'':as
ifthenOP = OP {eats=3, leaves=1, applyOp=wrap3 ifthen', name="ifthen"}
notOP = OP {eats=1, leaves=1, applyOp=wrap1 not', name="not" }
orOP = OP {eats=2, leaves=1, applyOp=wrap2 or', name="or" }
andOP = OP {eats=2, leaves=1, applyOp=wrap2 and', name="and" }