1 {-# OPTIONS -XRank2Types -XCPP -XScopedTypeVariables #-}
4 import qualified Data.IntMap as IntMap
7 import System.IO.Unsafe
9 import Control.Exception
11 data Nat = S Nat | Z deriving (Eq)
13 instance Show Nat where
16 instance Num Nat where
17 (+) = error "No operators defined for Nat"
18 (*) = error "No operators defined for Nat"
19 abs = error "No operators defined for Nat"
20 signum = error "No operators defined for Nat"
21 fromInteger n | n < 0 = error "Nat cannot be negative"
24 toNat x = if x == 0 then
30 fromNat (S x) = 1 + fromNat x
32 fromDistinctList = IntMap.fromList
35 -> (forall a. [a] -> [a])
36 -> (Nat -> Nat -> Maybe Nat)
39 gen_put_bias bias get sput s v =
40 do { let ls = length s
41 ; let g = fromDistinctList (zip (bias ls) s)
42 ; l' <- maybe (fail "...")
44 (sput (toNat ls) (toNat (length v)))
45 ; let t = bias (fromNat l')
46 ; let h = fromDistinctList (zip (get t) v)
47 ; let h'= IntMap.union h g
48 ; return (map (flip IntMap.lookup h') t) }
50 withDefaultBias put bias d s v =
51 do { s' <- put bias s v
52 ; return (map (maybe d id) s') }
54 withDefault put d s v =
56 ; return (map (maybe d id) s') }
58 gen_put_dbias :: Bias -> (forall a. [a] -> [a])
59 -> (Nat -> Nat -> Maybe Nat)
60 -> a -> [a] -> [a] -> Maybe [a]
61 gen_put_dbias bias get sput d s v =
62 do { s' <- gen_put_bias bias get sput s v
63 ; return (map (maybe d id) s') }
65 castError :: a -> Maybe a
66 castError f = unsafePerformIO $
67 do { r <- try (evaluate f)
69 #if __GLASGOW_HASKELL__ >= 610
70 Left (e::SomeException) -> return $ Nothing
72 Left e -> return $ Nothing
74 Right r -> return $ Just $ r }
76 type Bias = Int -> [ Int ]
77 rear l = [ 0 .. l - 1 ]
78 front l = reverse [ 0 .. l - 1 ]
79 middle l = [1,3..l] ++ (reverse [2,4..l])
80 borders l = (reverse [1,3..l])++[2,4..l]