module Tools where pair :: a -> b -> (a,b) pair = (,) singleton :: a -> [a] singleton a = [a] dup :: (a,r) -> ((a,r),r) dup ~(a,r) = ((a,r),r) assocpairl :: ( (a, b), c) -> (a, (b, c)) assocpairl ~(~(a, b), c) = (a, (b, c)) assocpairr :: (a, (b, c)) -> ((a, b), c) assocpairr ~(a, ~(b, c)) = ((a, b), c) swap12 :: (s, (a, b)) -> (a, (s, b)) swap12 ~(s, ~(a, b)) = (a, (s, b)) swap23 :: ( (a, b), s) -> ((a, s), b) swap23 ~(~(a, b), s) = ((a, s), b) rotl :: ( (c, a), s) -> ((a, s), c) rotl ~(~(c, a), s) = ((a, s), c) rotr :: ( (a, s), c) -> ((c, a), s) rotr ~(~(a, s), c) = ((c, a), s) eitherout :: (Either a a',s) -> Either (a,s) (a',s) eitherout ~(x,s) = either (Left . (`pair` s)) (Right . (`pair` s)) x eitherin :: Either (b,s) (b',s) -> (Either b b',s) eitherin = either (mapFst Left) (mapFst Right) mapFst :: (a->b) -> (a,c) -> (b,c) mapFst f = f `seq` \(~(a, t)) -> (f a, t) mapSnd :: (a->b) -> (c,a) -> (c,b) mapSnd f = f `seq` \(~(t, a)) -> (t, f a) swap :: (a,b) -> (b,a) swap ~(x,y) = (y,x) bool2Either :: Bool -> a -> Either a a bool2Either b = if b then Left else Right -- strict composition comp :: (a->b) -> (b->c) -> (a->c) comp f g = f `seq` g `seq` (g.f)