{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}

-- | A sequential representation.
module Futhark.IR.Seq
  ( Seq,

    -- * Simplification
    simplifyProg,

    -- * Module re-exports
    module Futhark.IR.Prop,
    module Futhark.IR.Traversals,
    module Futhark.IR.Pretty,
    module Futhark.IR.Syntax,
  )
where

import Futhark.Builder
import Futhark.Construct
import Futhark.IR.Pretty
import Futhark.IR.Prop
import Futhark.IR.Syntax
import Futhark.IR.Traversals
import qualified Futhark.Optimise.Simplify as Simplify
import qualified Futhark.Optimise.Simplify.Engine as Engine
import Futhark.Optimise.Simplify.Rules
import Futhark.Pass
import qualified Futhark.TypeCheck as TypeCheck

-- | The phantom type for the Seq representation.
data Seq

instance RepTypes Seq where
  type Op Seq = ()

instance ASTRep Seq where
  expTypesFromPat :: Pat Seq -> m [BranchType Seq]
expTypesFromPat = [ExtType] -> m [ExtType]
forall (m :: * -> *) a. Monad m => a -> m a
return ([ExtType] -> m [ExtType])
-> (PatT Type -> [ExtType]) -> PatT Type -> m [ExtType]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatT Type -> [ExtType]
forall dec. Typed dec => PatT dec -> [ExtType]
expExtTypesFromPat

instance TypeCheck.CheckableOp Seq where
  checkOp :: OpWithAliases (Op Seq) -> TypeM Seq ()
checkOp = OpWithAliases (Op Seq) -> TypeM Seq ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure

instance TypeCheck.Checkable Seq

instance Buildable Seq where
  mkBody :: Stms Seq -> Result -> Body Seq
mkBody = BodyDec Seq -> Stms Seq -> Result -> Body Seq
forall rep. BodyDec rep -> Stms rep -> Result -> BodyT rep
Body ()
  mkExpPat :: [Ident] -> Exp Seq -> Pat Seq
mkExpPat [Ident]
idents Exp Seq
_ = [Ident] -> PatT Type
basicPat [Ident]
idents
  mkExpDec :: Pat Seq -> Exp Seq -> ExpDec Seq
mkExpDec Pat Seq
_ Exp Seq
_ = ()
  mkLetNames :: [VName] -> Exp Seq -> m (Stm Seq)
mkLetNames = [VName] -> Exp Seq -> m (Stm Seq)
forall rep (m :: * -> *).
(ExpDec rep ~ (), LetDec rep ~ Type, MonadFreshNames m,
 TypedOp (Op rep), HasScope rep m) =>
[VName] -> Exp rep -> m (Stm rep)
simpleMkLetNames

instance BuilderOps Seq

instance PrettyRep Seq

instance BuilderOps (Engine.Wise Seq)

simpleSeq :: Simplify.SimpleOps Seq
simpleSeq :: SimpleOps Seq
simpleSeq = SimplifyOp Seq (Op Seq) -> SimpleOps Seq
forall rep.
(SimplifiableRep rep, Buildable rep) =>
SimplifyOp rep (Op rep) -> SimpleOps rep
Simplify.bindableSimpleOps (SimpleM Seq ((), Stms (Wise Seq))
-> () -> SimpleM Seq ((), Stms (Wise Seq))
forall a b. a -> b -> a
const (SimpleM Seq ((), Stms (Wise Seq))
 -> () -> SimpleM Seq ((), Stms (Wise Seq)))
-> SimpleM Seq ((), Stms (Wise Seq))
-> ()
-> SimpleM Seq ((), Stms (Wise Seq))
forall a b. (a -> b) -> a -> b
$ ((), Stms (Wise Seq)) -> SimpleM Seq ((), Stms (Wise Seq))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((), Stms (Wise Seq)
forall a. Monoid a => a
mempty))

-- | Simplify a sequential program.
simplifyProg :: Prog Seq -> PassM (Prog Seq)
simplifyProg :: Prog Seq -> PassM (Prog Seq)
simplifyProg = SimpleOps Seq
-> RuleBook (Wise Seq)
-> HoistBlockers Seq
-> Prog Seq
-> PassM (Prog Seq)
forall rep.
SimplifiableRep rep =>
SimpleOps rep
-> RuleBook (Wise rep)
-> HoistBlockers rep
-> Prog rep
-> PassM (Prog rep)
Simplify.simplifyProg SimpleOps Seq
simpleSeq RuleBook (Wise Seq)
forall rep. (BuilderOps rep, Aliased rep) => RuleBook rep
standardRules HoistBlockers Seq
forall rep. HoistBlockers rep
blockers
  where
    blockers :: HoistBlockers rep
blockers = HoistBlockers rep
forall rep. HoistBlockers rep
Engine.noExtraHoistBlockers