{-# LANGUAGE ExistentialQuantification, PatternGuards, Rank2Types #-}
module Lambdabot.Plugin.Haskell.Pl.Rules (RewriteRule(..), fire, rules) where
import Lambdabot.Plugin.Haskell.Pl.Common
import Lambdabot.Plugin.Haskell.Pl.RuleLib
import Lambdabot.Plugin.Haskell.Pl.Names
collapseLists :: Expr -> Maybe Expr
collapseLists (Var _ "++" `App` e1 `App` e2)
| (xs,x) <- getList e1, x==nil,
(ys,y) <- getList e2, y==nil = Just $ makeList $ xs ++ ys
collapseLists _ = Nothing
data Binary = forall a b c. (Read a, Show a, Read b, Show b, Read c, Show c) => BA (a -> b -> c)
evalBinary :: [(String, Binary)] -> Expr -> Maybe Expr
evalBinary fs (Var _ f' `App` Var _ x' `App` Var _ y')
| Just (BA f) <- lookup f' fs = (Var Pref . show) `fmap` liftM2 f (readM x') (readM y')
evalBinary _ _ = Nothing
data Unary = forall a b. (Read a, Show a, Read b, Show b) => UA (a -> b)
evalUnary :: [(String, Unary)] -> Expr -> Maybe Expr
evalUnary fs (Var _ f' `App` Var _ x')
| Just (UA f) <- lookup f' fs = (Var Pref . show . f) `fmap` readM x'
evalUnary _ _ = Nothing
assocR, assocL, assoc :: [String] -> Expr -> Maybe Expr
assocR ops (Var f1 op1 `App` (Var f2 op2 `App` e1 `App` e2) `App` e3)
| op1 == op2 && op1 `elem` ops
= Just (Var f1 op1 `App` e1 `App` (Var f2 op2 `App` e2 `App` e3))
assocR _ _ = Nothing
assocL ops (Var f1 op1 `App` e1 `App` (Var f2 op2 `App` e2 `App` e3))
| op1 == op2 && op1 `elem` ops
= Just (Var f1 op1 `App` (Var f2 op2 `App` e1 `App` e2) `App` e3)
assocL _ _ = Nothing
assoc ops (Var _ "." `App` (Var f1 op1 `App` e1) `App` (Var f2 op2 `App` e2))
| op1 == op2 && op1 `elem` ops
= Just (Var f1 op1 `App` (Var f2 op2 `App` e1 `App` e2))
assoc _ _ = Nothing
commutative :: [String] -> Expr -> Maybe Expr
commutative ops (Var f op `App` e1 `App` e2)
| op `elem` ops = Just (Var f op `App` e2 `App` e1)
commutative ops (Var _ "flip" `App` e@(Var _ op)) | op `elem` ops = Just e
commutative _ _ = Nothing
{-# INLINE simplifies #-}
simplifies :: RewriteRule
simplifies = Or [
rr0 (\f g x -> (f `c` g) `a` x)
(\f g x -> f `a` (g `a` x)),
rr0 (\x -> idE `a` x)
(\x -> x),
rr (\x -> flipE `a` (flipE `a` x))
(\x -> x),
rr0 (\f x -> (flipE `a` idE `a` x) `c` f)
(\f x -> flipE `a` f `a` x),
rr0 (\f -> idE `c` f)
(\f -> f),
rr0 (\f -> f `c` idE)
(\f -> f),
rr0 (\x y -> constE `a` x `a` y)
(\x _ -> x),
rr (\x -> notE `a` (notE `a` x))
(\x -> x),
rr (\x y -> fstE `a` (commaE `a` x `a` y))
(\x _ -> x),
rr (\x y -> sndE `a` (commaE `a` x `a` y))
(\_ y -> y),
rr (\x xs -> headE `a` (consE `a` x `a` xs))
(\x _ -> x),
rr (\x xs -> tailE `a` (consE `a` x `a` xs))
(\_ xs -> xs),
rr1 (\f x y -> uncurryE `a` f `a` (commaE `a` x `a` y))
(\f x y -> f `a` x `a` y),
rr (uncurryE `a` commaE)
(idE),
rr1 (\f g -> (uncurryE `a` f) `c` (sE `a` commaE `a` g))
(\f g -> sE `a` f `a` g),
rr (curryE `a` fstE) (constE),
rr (curryE `a` sndE) (constE `a` idE),
rr0 (\f g x -> sE `a` f `a` g `a` x)
(\f g x -> f `a` x `a` (g `a` x)),
rr0 (\f x y -> flipE `a` f `a` x `a` y)
(\f x y -> f `a` y `a` x),
rr0 (flipE `a` extE)
bindE,
rr (fmapE `a` idE)
(idE),
rr (mapE `a` idE)
(idE),
rr0 (\f g h -> (f `c` g) `c` h)
(\f g h -> f `c` (g `c` h)),
rr0 (\f g -> fmapE `a` f `c` fmapE `a` g)
(\f g -> fmapE `a` (f `c` g)),
rr0 (\f g -> mapE `a` f `c` mapE `a` g)
(\f g -> mapE `a` (f `c` g))
]
onceRewrites :: RewriteRule
onceRewrites = Hard $ Or [
rr0 (dollarE)
idE,
rr concatMapE extE,
rr concatE joinE,
rr liftME fmapE,
rr mapE fmapE,
rr subtractE
(flipE `a` minusE)
]
rules :: RewriteRule
rules = Or [
Hard $
rr (\f g x -> f `a` (g `a` x))
(\f g x -> (f `c` g) `a` x),
Hard $
rr bindE
(flipE `a` extE),
rr (compE `a` idE)
idE,
rr (\x -> appendE `a` (consE `a` x `a` nilE))
(\x -> consE `a` x),
rr (extE `a` returnE)
idE,
rr (\f x -> extE `a` f `a` (returnE `a` x))
(\f x -> f `a` x),
rr (\f g -> extE `a` ((extE `a` f) `c` g))
(\f g -> (extE `a` f) `c` (extE `a` g)),
Hard $
rr (\f g -> flipE `a` (f `c` g))
(\f g -> (flipE `a` compE `a` g) `c` (flipE `a` f)),
rr (\f -> (flipE `a` compE `a` f) `c` (flipE `a` idE))
(\f -> flipE `a` f),
rr (\f -> (flipE `a` compE `a` f) `c` (flipE `a` flipE))
(\f -> flipE `a` (flipE `c` f)),
rr1 (\f g -> flipE `a` (flipE `a` (flipE `c` f) `a` g))
(\f g -> flipE `a` (flipE `c` flipE `a` f) `a` g),
rr (flipE `a` compE `a` idE)
idE,
rr (compE `c` (flipE `a` idE))
(flipE `a` flipE),
rr (\x y -> sE `a` constE `a` x `a` y)
(\_ y -> y),
rr1 (\f g -> sE `a` (constE `c` f) `a` g)
(\f _ -> f),
rr (\f -> sE `a` (constE `a` f))
(\f -> compE `a` f),
rr (\f g h -> (flipE `a` sE `a` f) `c` (flipE `a` compE `a` g) `c` constE `c` h)
(\f _ h -> (flipE `a` compE `a` f) `c` h),
rr (\f -> sE `a` (f `c` fstE) `a` sndE)
(\f -> uncurryE `a` f),
rr (\x -> fstE `a` (joinE `a` commaE `a` x))
(\x -> x),
rr (\x -> sndE `a` (joinE `a` commaE `a` x))
(\x -> x),
rr (\f -> curryE `a` (uncurryE `a` f))
(\f -> f),
rr (\f -> uncurryE `a` (curryE `a` f))
(\f -> f),
rr (\f -> (constE `a` idE) `c` f)
(\_ -> constE `a` idE),
rr (\x f -> constE `a` x `c` f)
(\x _ -> constE `a` x),
rr (\f -> (flipE `a` compE `a` f) `c` constE)
(\_ -> constE),
rr (\f g -> (flipE `a` compE `a` f) `c` constE `c` g)
(\_ g -> constE `c` g),
Hard $
rr0 (\f -> fixE `a` f)
(\f -> f `a` (fixE `a` f)),
Hard $
rr0 (\f -> f `a` (fixE `a` f))
(\f -> fixE `a` f),
Hard $
rr0 (\f -> fixE `a` f)
(\f -> f `a` (f `a` (fixE `a` f))),
rr (\f -> fixE `a` (constE `a` f))
(\f -> f),
rr (\x -> flipE `a` constE `a` x)
(\_ -> idE),
Hard $
rr (\f -> constE `c` f)
(\f -> flipE `a` (constE `a` f)),
rr2 (\x y -> notE `a` (equalsE `a` x `a` y))
(\x y -> nequalsE `a` x `a` y),
rr2 (\x y -> notE `a` (nequalsE `a` x `a` y))
(\x y -> equalsE `a` x `a` y),
If (Or [rr plusE plusE, rr minusE minusE, rr multE multE]) $ down $ Or [
rr (\x -> plusE `a` zeroE `a` x)
(\x -> x),
rr (\x -> multE `a` zeroE `a` x)
(\_ -> zeroE),
rr (\x -> multE `a` oneE `a` x)
(\x -> x),
rr (\x -> minusE `a` x `a` x)
(\_ -> zeroE),
rr (\y x -> plusE `a` (minusE `a` x `a` y) `a` y)
(\_ x -> x),
rr (\y x -> minusE `a` (plusE `a` x `a` y) `a` y)
(\_ x -> x),
rr (\x y z -> plusE `a` x `a` (minusE `a` y `a` z))
(\x y z -> minusE `a` (plusE `a` x `a` y) `a` z),
rr (\x y z -> minusE `a` x `a` (plusE `a` y `a` z))
(\x y z -> minusE `a` (minusE `a` x `a` y) `a` z),
rr (\x y z -> minusE `a` x `a` (minusE `a` y `a` z))
(\x y z -> minusE `a` (plusE `a` x `a` y) `a` z)
],
Hard onceRewrites,
rr (\f x -> joinE `a` (fmapE `a` f `a` x))
(\f x -> extE `a` f `a` x),
rr (extE `a` idE) joinE,
Hard $
rr joinE (extE `a` idE),
rr (\x -> joinE `a` (returnE `a` x))
(\x -> x),
rr (\f m -> extE `a` (returnE `c` f) `a` m)
(\f m -> fmapIE `a` f `a` m),
rr (\f x -> bindE `a` x `c` (compE `a` returnE) `c` f)
(\f x -> flipE `a` (fmapIE `c` f) `a` x),
rr (\f -> bindE `a` (returnE `a` f))
(\f -> flipE `a` idE `a` f),
Hard $
rr (\f x -> liftM2E `a` f `a` x)
(\f x -> apE `a` (fmapIE `a` f `a` x)),
rr (\f x -> liftM2E `a` f `a` (returnE `a` x))
(\f x -> fmapIE `a` (f `a` x)),
rr (\f x -> fmapE `a` f `a` (returnE `a` x))
(\f x -> returnE `a` (f `a` x)),
Hard $
rr (\f -> extE `c` flipE `a` (fmapE `c` f))
(\f -> flipE `a` liftM2E `a` f),
Hard $
rr compE fmapE,
Hard $
rr (\f xs ys -> mapE `a` f `a` (zipE `a` xs `a` ys))
(\f xs ys -> zipWithE `a` (curryE `a` f) `a` xs `a` ys),
rr (zipWithE `a` commaE) zipE,
Hard $
rr (\f -> allE `a` f)
(\f -> andE `c` mapE `a` f),
rr (\f -> andE `c` mapE `a` f)
(\f -> allE `a` f),
Hard $
rr (\f -> anyE `a` f)
(\f -> orE `c` mapE `a` f),
rr (\f -> orE `c` mapE `a` f)
(\f -> anyE `a` f),
rr (\f x -> apE `a` (returnE `a` f) `a` x)
(\f x -> fmapIE `a` f `a` x),
rr (\f x -> apE `a` (fmapIE `a` f `a` x))
(\f x -> liftM2E `a` f `a` x),
Hard $
rr (\f x -> apE `a` f `a` x)
(\f x -> extE `a` (flipE `a` fmapIE `a` x) `a` f),
rr (\f x -> extE `a` (flipE `a` fmapIE `a` x) `a` f)
(\f x -> apE `a` f `a` x),
rr (\f x -> bindE `a` x `c` flipE `a` (fmapE `c` f))
(\f x -> liftM2E `a` f `a` x),
rr0 (\f m x -> extE `a` f `a` m `a` x)
(\f m x -> f `a` (m `a` x) `a` x),
rr0 (\f g x -> fmapE `a` f `a` g `a` x)
(\f g x -> f `a` (g `a` x)),
rr (\y x -> returnE `a` x `a` y)
(\y _ -> y),
rr0 (\f g h x -> liftM2E `a` f `a` g `a` h `a` x)
(\f g h x -> f `a` (g `a` x) `a` (h `a` x)),
rr (\f -> apE `a` f `a` idE)
(\f -> joinE `a` f),
Hard $
rr (\q p -> extE `a` (constE `a` q) `a` p)
(\q p -> seqME `a` p `a` q),
Hard $
rr (\p q -> seqME `a` p `a` q)
(\p q -> extE `a` (constE `a` q) `a` p),
rr (\f g -> uncurryE `a` ((flipE `a` compE `a` g) `c` commaE `c` f))
(\f g -> crossE `a` f `a` g),
rr (\f -> uncurryE `a` (commaE `c` f))
(\f -> firstE `a` f),
rr (\g -> uncurryE `a` ((flipE `a` compE `a` g) `c` commaE))
(\g -> secondE `a` g),
rr (\f -> uncurryE `a` (constE `a` f))
(\f -> f `c` sndE),
rr (uncurryE `a` constE)
(fstE),
rr (\f -> uncurryE `a` (constE `c` f))
(\f -> f `c` fstE),
Hard $
rr (\x -> consE `a` x `a` nilE)
(\x -> returnE `a` x),
Hard $
If (Or [rr consE consE, rr nilE nilE]) $ Or [
down $ Or [
rr (lengthE `a` nilE)
zeroE,
rr (\x xs -> lengthE `a` (consE `a` x `a` xs))
(\_ xs -> plusE `a` oneE `a` (lengthE `a` xs))
],
down $ Or [
rr (\f x xs -> mapE `a` f `a` (consE `a` x `a` xs))
(\f x xs -> consE `a` (f `a` x) `a` (mapE `a` f `a` xs)),
rr (\f x xs -> fmapE `a` f `a` (consE `a` x `a` xs))
(\f x xs -> consE `a` (f `a` x) `a` (fmapE `a` f `a` xs)),
rr (\f -> mapE `a` f `a` nilE)
(\_ -> nilE),
rr (\f -> fmapE `a` f `a` nilE)
(\_ -> nilE)
],
down $ Or [
rr (\f x xs z -> (foldrE `a` f `a` z) `a` (consE `a` x `a` xs))
(\f x xs z -> (f `a` x) `a` (foldrE `a` f `a` z `a` xs)),
rr (\f z -> foldrE `a` f `a` z `a` nilE)
(\_ z -> z)
],
down $ Opt (CRR $ assocL ["."]) `Then` Or [
rr (\xs -> sumE `a` xs)
(\xs -> foldlE `a` plusE `a` zeroE `a` xs),
rr (\xs -> productE `a` xs)
(\xs -> foldlE `a` multE `a` oneE `a` xs),
rr (\f x xs -> foldl1E `a` f `a` (consE `a` x `a` xs))
(\f x xs -> foldlE `a` f `a` x `a` xs),
rr (\f z x xs -> (foldlE `a` f `a` z) `a` (consE `a` x `a` xs))
(\f z x xs -> foldlE `a` f `a` (f `a` z `a` x) `a` xs),
rr (\f z -> foldlE `a` f `a` z `a` nilE)
(\_ z -> z),
rr (\f z x -> foldlE `a` f `a` z `a` (returnE `a` x))
(\f z x -> f `a` z `a` x),
rr (\f z x -> foldlE `a` f `a` z `a` (consE `a` x `a` nilE))
(\f z x -> f `a` z `a` x)
] `OrElse` (
Opt (rr0 (\x -> consE `a` x)
(\x -> appendE `a` (consE `a` x `a` nilE))) `Then`
up (rr0 (\x ys -> (consE `a` x) `c` (appendE `a` ys))
(\x ys -> appendE `a` (consE `a` x `a` ys)))
)
],
CRR (collapseLists),
up $ Or [CRR (evalUnary unaryBuiltins), CRR (evalBinary binaryBuiltins)],
up $ CRR (assoc assocOps),
up $ CRR (assocL assocOps),
up $ CRR (assocR assocOps),
Up (CRR (commutative commutativeOps)) $ down $ Or [CRR $ assocL assocLOps,
CRR $ assocR assocROps],
Hard $ simplifies
] `Then` Opt (up simplifies)
assocLOps, assocROps, assocOps :: [String]
assocLOps = ["+", "*", "&&", "||", "max", "min"]
assocROps = [".", "++"]
assocOps = assocLOps ++ assocROps
commutativeOps :: [String]
commutativeOps = ["*", "+", "==", "/=", "max", "min"]
unaryBuiltins :: [(String,Unary)]
unaryBuiltins = [
("not", UA (not :: Bool -> Bool)),
("negate", UA (negate :: Integer -> Integer)),
("signum", UA (signum :: Integer -> Integer)),
("abs", UA (abs :: Integer -> Integer))
]
binaryBuiltins :: [(String,Binary)]
binaryBuiltins = [
("+", BA ((+) :: Integer -> Integer -> Integer)),
("-", BA ((-) :: Integer -> Integer -> Integer)),
("*", BA ((*) :: Integer -> Integer -> Integer)),
("^", BA ((^) :: Integer -> Integer -> Integer)),
("<", BA ((<) :: Integer -> Integer -> Bool)),
(">", BA ((>) :: Integer -> Integer -> Bool)),
("==", BA ((==) :: Integer -> Integer -> Bool)),
("/=", BA ((/=) :: Integer -> Integer -> Bool)),
("<=", BA ((<=) :: Integer -> Integer -> Bool)),
(">=", BA ((>=) :: Integer -> Integer -> Bool)),
("div", BA (div :: Integer -> Integer -> Integer)),
("mod", BA (mod :: Integer -> Integer -> Integer)),
("max", BA (max :: Integer -> Integer -> Integer)),
("min", BA (min :: Integer -> Integer -> Integer)),
("&&", BA ((&&) :: Bool -> Bool -> Bool)),
("||", BA ((||) :: Bool -> Bool -> Bool))
]