{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
module Jikka.Core.Convert.BubbleLet
( run,
rule,
)
where
import Jikka.Common.Alpha
import Jikka.Common.Error
import Jikka.Core.Language.BuiltinPatterns
import Jikka.Core.Language.Expr
import Jikka.Core.Language.FreeVars
import Jikka.Core.Language.Lint
import Jikka.Core.Language.RewriteRules
rule :: MonadAlpha m => RewriteRule m
rule :: RewriteRule m
rule =
let go :: Expr -> (Expr -> Expr) -> m (Maybe Expr)
go Expr
f Expr -> Expr
cont = case Expr
f of
Lam VarName
x Type
t (Let VarName
y Type
t' Expr
e Expr
body) | VarName
x VarName -> Expr -> Bool
`isUnusedVar` Expr
e -> Maybe Expr -> m (Maybe Expr)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Expr -> m (Maybe Expr))
-> (Expr -> Maybe Expr) -> Expr -> m (Maybe Expr)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr -> Maybe Expr
forall a. a -> Maybe a
Just (Expr -> m (Maybe Expr)) -> Expr -> m (Maybe Expr)
forall a b. (a -> b) -> a -> b
$ VarName -> Type -> Expr -> Expr -> Expr
Let VarName
y Type
t' Expr
e (Expr -> Expr
cont (VarName -> Type -> Expr -> Expr
Lam VarName
x Type
t Expr
body))
Expr
_ -> Maybe Expr -> m (Maybe Expr)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Expr
forall a. Maybe a
Nothing
in ([(VarName, Type)] -> Expr -> m (Maybe Expr)) -> RewriteRule m
forall (m :: * -> *).
([(VarName, Type)] -> Expr -> m (Maybe Expr)) -> RewriteRule m
RewriteRule (([(VarName, Type)] -> Expr -> m (Maybe Expr)) -> RewriteRule m)
-> ([(VarName, Type)] -> Expr -> m (Maybe Expr)) -> RewriteRule m
forall a b. (a -> b) -> a -> b
$ \[(VarName, Type)]
_ -> \case
Iterate' Type
t Expr
k Expr
f Expr
x -> Expr -> (Expr -> Expr) -> m (Maybe Expr)
forall (m :: * -> *).
Monad m =>
Expr -> (Expr -> Expr) -> m (Maybe Expr)
go Expr
f (\Expr
f -> Type -> Expr -> Expr -> Expr -> Expr
Iterate' Type
t Expr
k Expr
f Expr
x)
Foldl' Type
t1 Type
t2 Expr
f Expr
init Expr
xs -> Expr -> (Expr -> Expr) -> m (Maybe Expr)
forall (m :: * -> *).
Monad m =>
Expr -> (Expr -> Expr) -> m (Maybe Expr)
go Expr
f (\Expr
f -> Type -> Type -> Expr -> Expr -> Expr -> Expr
Foldl' Type
t1 Type
t2 Expr
f Expr
init Expr
xs)
Build' Type
t Expr
f Expr
xs Expr
n -> Expr -> (Expr -> Expr) -> m (Maybe Expr)
forall (m :: * -> *).
Monad m =>
Expr -> (Expr -> Expr) -> m (Maybe Expr)
go Expr
f (\Expr
f -> Type -> Expr -> Expr -> Expr -> Expr
Build' Type
t Expr
f Expr
xs Expr
n)
Map' Type
t1 Type
t2 Expr
f Expr
xs -> Expr -> (Expr -> Expr) -> m (Maybe Expr)
forall (m :: * -> *).
Monad m =>
Expr -> (Expr -> Expr) -> m (Maybe Expr)
go Expr
f (\Expr
f -> Type -> Type -> Expr -> Expr -> Expr
Map' Type
t1 Type
t2 Expr
f Expr
xs)
Filter' Type
t Expr
f Expr
xs -> Expr -> (Expr -> Expr) -> m (Maybe Expr)
forall (m :: * -> *).
Monad m =>
Expr -> (Expr -> Expr) -> m (Maybe Expr)
go Expr
f (\Expr
f -> Type -> Expr -> Expr -> Expr
Filter' Type
t Expr
f Expr
xs)
Expr
_ -> Maybe Expr -> m (Maybe Expr)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Expr
forall a. Maybe a
Nothing
runProgram :: (MonadAlpha m, MonadError Error m) => Program -> m Program
runProgram :: Program -> m Program
runProgram = RewriteRule m -> Program -> m Program
forall (m :: * -> *).
MonadError Error m =>
RewriteRule m -> Program -> m Program
applyRewriteRuleProgram' RewriteRule m
forall (m :: * -> *). MonadAlpha m => RewriteRule m
rule
run :: (MonadAlpha m, MonadError Error m) => Program -> m Program
run :: Program -> m Program
run Program
prog = String -> m Program -> m Program
forall (m :: * -> *) a. MonadError Error m => String -> m a -> m a
wrapError' String
"Jikka.Core.Convert.BubbleLet" (m Program -> m Program) -> m Program -> m Program
forall a b. (a -> b) -> a -> b
$ do
m () -> m ()
forall (m :: * -> *) a. MonadError Error m => m a -> m a
precondition (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Program -> m ()
forall (m :: * -> *). MonadError Error m => Program -> m ()
ensureWellTyped Program
prog
Program
prog <- Program -> m Program
forall (m :: * -> *).
(MonadAlpha m, MonadError Error m) =>
Program -> m Program
runProgram Program
prog
m () -> m ()
forall (m :: * -> *) a. MonadError Error m => m a -> m a
postcondition (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Program -> m ()
forall (m :: * -> *). MonadError Error m => Program -> m ()
ensureWellTyped Program
prog
Program -> m Program
forall (m :: * -> *) a. Monad m => a -> m a
return Program
prog