{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

-- | Transform any SOACs to @for@-loops.
--
-- Example:
--
-- @
-- let ys = map (\x -> x + 2) xs
-- @
--
-- becomes something like:
--
-- @
-- let out = scratch n i32
-- let ys =
--   loop (ys' = out) for i < n do
--     let x = xs[i]
--     let y = x + 2
--     let ys'[i] = y
--     in ys'
-- @
module Futhark.Pass.FirstOrderTransform (firstOrderTransform) where

import Futhark.IR.SOACS (SOACS, scopeOf)
import Futhark.Pass
import Futhark.Transform.FirstOrderTransform (FirstOrderLore, transformConsts, transformFunDef)

-- | The first-order transformation pass.
firstOrderTransform :: FirstOrderLore lore => Pass SOACS lore
firstOrderTransform :: forall lore. FirstOrderLore lore => Pass SOACS lore
firstOrderTransform =
  String
-> String -> (Prog SOACS -> PassM (Prog lore)) -> Pass SOACS lore
forall fromlore tolore.
String
-> String
-> (Prog fromlore -> PassM (Prog tolore))
-> Pass fromlore tolore
Pass
    String
"first order transform"
    String
"Transform all SOACs to for-loops."
    ((Prog SOACS -> PassM (Prog lore)) -> Pass SOACS lore)
-> (Prog SOACS -> PassM (Prog lore)) -> Pass SOACS lore
forall a b. (a -> b) -> a -> b
$ (Stms SOACS -> PassM (Stms lore))
-> (Stms lore -> FunDef SOACS -> PassM (FunDef lore))
-> Prog SOACS
-> PassM (Prog lore)
forall fromlore tolore.
(Stms fromlore -> PassM (Stms tolore))
-> (Stms tolore -> FunDef fromlore -> PassM (FunDef tolore))
-> Prog fromlore
-> PassM (Prog tolore)
intraproceduralTransformationWithConsts
      Stms SOACS -> PassM (Stms lore)
forall (m :: * -> *) tolore.
(MonadFreshNames m, FirstOrderLore tolore) =>
Stms SOACS -> m (Stms tolore)
transformConsts
      (Scope lore -> FunDef SOACS -> PassM (FunDef lore)
forall (m :: * -> *) tolore.
(MonadFreshNames m, FirstOrderLore tolore) =>
Scope tolore -> FunDef SOACS -> m (FunDef tolore)
transformFunDef (Scope lore -> FunDef SOACS -> PassM (FunDef lore))
-> (Stms lore -> Scope lore)
-> Stms lore
-> FunDef SOACS
-> PassM (FunDef lore)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stms lore -> Scope lore
forall lore a. Scoped lore a => a -> Scope lore
scopeOf)