{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances, StandaloneDeriving #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
-- | Futhark core language skeleton.  Concrete representations further
-- extend this skeleton by defining a "lore", which specifies concrete
-- annotations ("Futhark.Representation.AST.Annotations") and
-- semantics.
module Futhark.Representation.AST.Syntax
  (
    module Language.Futhark.Core
  , module Futhark.Representation.AST.Annotations
  , module Futhark.Representation.AST.Syntax.Core

  -- * Types
  , Uniqueness(..)
  , NoUniqueness(..)
  , Rank(..)
  , ArrayShape(..)
  , Space (..)
  , TypeBase(..)
  , Diet(..)

  -- * Abstract syntax tree
  , Ident (..)
  , SubExp(..)
  , PatElem
  , PatElemT (..)
  , PatternT (..)
  , Pattern
  , StmAux(..)
  , Stm(..)
  , Stms
  , Result
  , BodyT(..)
  , Body
  , BasicOp (..)
  , UnOp (..)
  , BinOp (..)
  , CmpOp (..)
  , ConvOp (..)
  , DimChange (..)
  , ShapeChange
  , ExpT(..)
  , Exp
  , LoopForm (..)
  , IfAttr (..)
  , IfSort (..)
  , Safety (..)
  , LambdaT(..)
  , Lambda

  -- * Definitions
  , Param (..)
  , FParam
  , LParam
  , FunDef (..)
  , EntryPoint
  , EntryPointType(..)
  , Prog(..)

  -- * Utils
  , oneStm
  , stmsFromList
  , stmsToList
  , stmsHead
  )
  where

import Data.Foldable
import Data.Loc
import qualified Data.Sequence as Seq

import Language.Futhark.Core
import Futhark.Representation.AST.Annotations
import Futhark.Representation.AST.Syntax.Core

-- | A type alias for namespace control.
type PatElem lore = PatElemT (LetAttr lore)

-- | A pattern is conceptually just a list of names and their types.
data PatternT attr =
  Pattern { PatternT attr -> [PatElemT attr]
patternContextElements :: [PatElemT attr]
            -- ^ existential context (sizes and memory blocks)
          , PatternT attr -> [PatElemT attr]
patternValueElements   :: [PatElemT attr]
            -- ^ "real" values
          }
  deriving (Eq (PatternT attr)
Eq (PatternT attr)
-> (PatternT attr -> PatternT attr -> Ordering)
-> (PatternT attr -> PatternT attr -> Bool)
-> (PatternT attr -> PatternT attr -> Bool)
-> (PatternT attr -> PatternT attr -> Bool)
-> (PatternT attr -> PatternT attr -> Bool)
-> (PatternT attr -> PatternT attr -> PatternT attr)
-> (PatternT attr -> PatternT attr -> PatternT attr)
-> Ord (PatternT attr)
PatternT attr -> PatternT attr -> Bool
PatternT attr -> PatternT attr -> Ordering
PatternT attr -> PatternT attr -> PatternT attr
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall attr. Ord attr => Eq (PatternT attr)
forall attr. Ord attr => PatternT attr -> PatternT attr -> Bool
forall attr. Ord attr => PatternT attr -> PatternT attr -> Ordering
forall attr.
Ord attr =>
PatternT attr -> PatternT attr -> PatternT attr
min :: PatternT attr -> PatternT attr -> PatternT attr
$cmin :: forall attr.
Ord attr =>
PatternT attr -> PatternT attr -> PatternT attr
max :: PatternT attr -> PatternT attr -> PatternT attr
$cmax :: forall attr.
Ord attr =>
PatternT attr -> PatternT attr -> PatternT attr
>= :: PatternT attr -> PatternT attr -> Bool
$c>= :: forall attr. Ord attr => PatternT attr -> PatternT attr -> Bool
> :: PatternT attr -> PatternT attr -> Bool
$c> :: forall attr. Ord attr => PatternT attr -> PatternT attr -> Bool
<= :: PatternT attr -> PatternT attr -> Bool
$c<= :: forall attr. Ord attr => PatternT attr -> PatternT attr -> Bool
< :: PatternT attr -> PatternT attr -> Bool
$c< :: forall attr. Ord attr => PatternT attr -> PatternT attr -> Bool
compare :: PatternT attr -> PatternT attr -> Ordering
$ccompare :: forall attr. Ord attr => PatternT attr -> PatternT attr -> Ordering
$cp1Ord :: forall attr. Ord attr => Eq (PatternT attr)
Ord, Int -> PatternT attr -> ShowS
[PatternT attr] -> ShowS
PatternT attr -> String
(Int -> PatternT attr -> ShowS)
-> (PatternT attr -> String)
-> ([PatternT attr] -> ShowS)
-> Show (PatternT attr)
forall attr. Show attr => Int -> PatternT attr -> ShowS
forall attr. Show attr => [PatternT attr] -> ShowS
forall attr. Show attr => PatternT attr -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PatternT attr] -> ShowS
$cshowList :: forall attr. Show attr => [PatternT attr] -> ShowS
show :: PatternT attr -> String
$cshow :: forall attr. Show attr => PatternT attr -> String
showsPrec :: Int -> PatternT attr -> ShowS
$cshowsPrec :: forall attr. Show attr => Int -> PatternT attr -> ShowS
Show, PatternT attr -> PatternT attr -> Bool
(PatternT attr -> PatternT attr -> Bool)
-> (PatternT attr -> PatternT attr -> Bool) -> Eq (PatternT attr)
forall attr. Eq attr => PatternT attr -> PatternT attr -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PatternT attr -> PatternT attr -> Bool
$c/= :: forall attr. Eq attr => PatternT attr -> PatternT attr -> Bool
== :: PatternT attr -> PatternT attr -> Bool
$c== :: forall attr. Eq attr => PatternT attr -> PatternT attr -> Bool
Eq)

instance Functor PatternT where
  fmap :: (a -> b) -> PatternT a -> PatternT b
fmap a -> b
f (Pattern [PatElemT a]
ctx [PatElemT a]
val) = [PatElemT b] -> [PatElemT b] -> PatternT b
forall attr. [PatElemT attr] -> [PatElemT attr] -> PatternT attr
Pattern ((PatElemT a -> PatElemT b) -> [PatElemT a] -> [PatElemT b]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> b) -> PatElemT a -> PatElemT b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) [PatElemT a]
ctx) ((PatElemT a -> PatElemT b) -> [PatElemT a] -> [PatElemT b]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> b) -> PatElemT a -> PatElemT b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) [PatElemT a]
val)

instance Semigroup (PatternT attr) where
  Pattern [PatElemT attr]
cs1 [PatElemT attr]
vs1 <> :: PatternT attr -> PatternT attr -> PatternT attr
<> Pattern [PatElemT attr]
cs2 [PatElemT attr]
vs2 = [PatElemT attr] -> [PatElemT attr] -> PatternT attr
forall attr. [PatElemT attr] -> [PatElemT attr] -> PatternT attr
Pattern ([PatElemT attr]
cs1[PatElemT attr] -> [PatElemT attr] -> [PatElemT attr]
forall a. [a] -> [a] -> [a]
++[PatElemT attr]
cs2) ([PatElemT attr]
vs1[PatElemT attr] -> [PatElemT attr] -> [PatElemT attr]
forall a. [a] -> [a] -> [a]
++[PatElemT attr]
vs2)

instance Monoid (PatternT attr) where
  mempty :: PatternT attr
mempty = [PatElemT attr] -> [PatElemT attr] -> PatternT attr
forall attr. [PatElemT attr] -> [PatElemT attr] -> PatternT attr
Pattern [] []

-- | A type alias for namespace control.
type Pattern lore = PatternT (LetAttr lore)

-- | Auxilliary Information associated with a statement.
data StmAux attr = StmAux { StmAux attr -> Certificates
stmAuxCerts :: !Certificates
                          , StmAux attr -> attr
stmAuxAttr :: attr
                          }
                  deriving (Eq (StmAux attr)
Eq (StmAux attr)
-> (StmAux attr -> StmAux attr -> Ordering)
-> (StmAux attr -> StmAux attr -> Bool)
-> (StmAux attr -> StmAux attr -> Bool)
-> (StmAux attr -> StmAux attr -> Bool)
-> (StmAux attr -> StmAux attr -> Bool)
-> (StmAux attr -> StmAux attr -> StmAux attr)
-> (StmAux attr -> StmAux attr -> StmAux attr)
-> Ord (StmAux attr)
StmAux attr -> StmAux attr -> Bool
StmAux attr -> StmAux attr -> Ordering
StmAux attr -> StmAux attr -> StmAux attr
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall attr. Ord attr => Eq (StmAux attr)
forall attr. Ord attr => StmAux attr -> StmAux attr -> Bool
forall attr. Ord attr => StmAux attr -> StmAux attr -> Ordering
forall attr. Ord attr => StmAux attr -> StmAux attr -> StmAux attr
min :: StmAux attr -> StmAux attr -> StmAux attr
$cmin :: forall attr. Ord attr => StmAux attr -> StmAux attr -> StmAux attr
max :: StmAux attr -> StmAux attr -> StmAux attr
$cmax :: forall attr. Ord attr => StmAux attr -> StmAux attr -> StmAux attr
>= :: StmAux attr -> StmAux attr -> Bool
$c>= :: forall attr. Ord attr => StmAux attr -> StmAux attr -> Bool
> :: StmAux attr -> StmAux attr -> Bool
$c> :: forall attr. Ord attr => StmAux attr -> StmAux attr -> Bool
<= :: StmAux attr -> StmAux attr -> Bool
$c<= :: forall attr. Ord attr => StmAux attr -> StmAux attr -> Bool
< :: StmAux attr -> StmAux attr -> Bool
$c< :: forall attr. Ord attr => StmAux attr -> StmAux attr -> Bool
compare :: StmAux attr -> StmAux attr -> Ordering
$ccompare :: forall attr. Ord attr => StmAux attr -> StmAux attr -> Ordering
$cp1Ord :: forall attr. Ord attr => Eq (StmAux attr)
Ord, Int -> StmAux attr -> ShowS
[StmAux attr] -> ShowS
StmAux attr -> String
(Int -> StmAux attr -> ShowS)
-> (StmAux attr -> String)
-> ([StmAux attr] -> ShowS)
-> Show (StmAux attr)
forall attr. Show attr => Int -> StmAux attr -> ShowS
forall attr. Show attr => [StmAux attr] -> ShowS
forall attr. Show attr => StmAux attr -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StmAux attr] -> ShowS
$cshowList :: forall attr. Show attr => [StmAux attr] -> ShowS
show :: StmAux attr -> String
$cshow :: forall attr. Show attr => StmAux attr -> String
showsPrec :: Int -> StmAux attr -> ShowS
$cshowsPrec :: forall attr. Show attr => Int -> StmAux attr -> ShowS
Show, StmAux attr -> StmAux attr -> Bool
(StmAux attr -> StmAux attr -> Bool)
-> (StmAux attr -> StmAux attr -> Bool) -> Eq (StmAux attr)
forall attr. Eq attr => StmAux attr -> StmAux attr -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StmAux attr -> StmAux attr -> Bool
$c/= :: forall attr. Eq attr => StmAux attr -> StmAux attr -> Bool
== :: StmAux attr -> StmAux attr -> Bool
$c== :: forall attr. Eq attr => StmAux attr -> StmAux attr -> Bool
Eq)

-- | A local variable binding.
data Stm lore = Let { Stm lore -> Pattern lore
stmPattern :: Pattern lore
                    , Stm lore -> StmAux (ExpAttr lore)
stmAux :: StmAux (ExpAttr lore)
                    , Stm lore -> Exp lore
stmExp :: Exp lore
                    }

deriving instance Annotations lore => Ord (Stm lore)
deriving instance Annotations lore => Show (Stm lore)
deriving instance Annotations lore => Eq (Stm lore)

-- | A sequence of statements.
type Stms lore = Seq.Seq (Stm lore)

oneStm :: Stm lore -> Stms lore
oneStm :: Stm lore -> Stms lore
oneStm = Stm lore -> Stms lore
forall a. a -> Seq a
Seq.singleton

stmsFromList :: [Stm lore] -> Stms lore
stmsFromList :: [Stm lore] -> Stms lore
stmsFromList = [Stm lore] -> Stms lore
forall a. [a] -> Seq a
Seq.fromList

stmsToList :: Stms lore -> [Stm lore]
stmsToList :: Stms lore -> [Stm lore]
stmsToList = Stms lore -> [Stm lore]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

stmsHead :: Stms lore -> Maybe (Stm lore, Stms lore)
stmsHead :: Stms lore -> Maybe (Stm lore, Stms lore)
stmsHead Stms lore
stms = case Stms lore -> ViewL (Stm lore)
forall a. Seq a -> ViewL a
Seq.viewl Stms lore
stms of Stm lore
stm Seq.:< Stms lore
stms' -> (Stm lore, Stms lore) -> Maybe (Stm lore, Stms lore)
forall a. a -> Maybe a
Just (Stm lore
stm, Stms lore
stms')
                                       ViewL (Stm lore)
Seq.EmptyL       -> Maybe (Stm lore, Stms lore)
forall a. Maybe a
Nothing

-- | The result of a body is a sequence of subexpressions.
type Result = [SubExp]

-- | A body consists of a number of bindings, terminating in a result
-- (essentially a tuple literal).
data BodyT lore = Body { BodyT lore -> BodyAttr lore
bodyAttr :: BodyAttr lore
                       , BodyT lore -> Stms lore
bodyStms :: Stms lore
                       , BodyT lore -> Result
bodyResult :: Result
                       }

deriving instance Annotations lore => Ord (BodyT lore)
deriving instance Annotations lore => Show (BodyT lore)
deriving instance Annotations lore => Eq (BodyT lore)

-- | Type alias for namespace reasons.
type Body = BodyT

-- | The new dimension in a 'Reshape'-like operation.  This allows us to
-- disambiguate "real" reshapes, that change the actual shape of the
-- array, from type coercions that are just present to make the types
-- work out.  The two constructors are considered equal for purposes of 'Eq'.
data DimChange d = DimCoercion d
                   -- ^ The new dimension is guaranteed to be numerically
                   -- equal to the old one.
                 | DimNew d
                   -- ^ The new dimension is not necessarily numerically
                   -- equal to the old one.
                 deriving (Eq (DimChange d)
Eq (DimChange d)
-> (DimChange d -> DimChange d -> Ordering)
-> (DimChange d -> DimChange d -> Bool)
-> (DimChange d -> DimChange d -> Bool)
-> (DimChange d -> DimChange d -> Bool)
-> (DimChange d -> DimChange d -> Bool)
-> (DimChange d -> DimChange d -> DimChange d)
-> (DimChange d -> DimChange d -> DimChange d)
-> Ord (DimChange d)
DimChange d -> DimChange d -> Bool
DimChange d -> DimChange d -> Ordering
DimChange d -> DimChange d -> DimChange d
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall d. Ord d => Eq (DimChange d)
forall d. Ord d => DimChange d -> DimChange d -> Bool
forall d. Ord d => DimChange d -> DimChange d -> Ordering
forall d. Ord d => DimChange d -> DimChange d -> DimChange d
min :: DimChange d -> DimChange d -> DimChange d
$cmin :: forall d. Ord d => DimChange d -> DimChange d -> DimChange d
max :: DimChange d -> DimChange d -> DimChange d
$cmax :: forall d. Ord d => DimChange d -> DimChange d -> DimChange d
>= :: DimChange d -> DimChange d -> Bool
$c>= :: forall d. Ord d => DimChange d -> DimChange d -> Bool
> :: DimChange d -> DimChange d -> Bool
$c> :: forall d. Ord d => DimChange d -> DimChange d -> Bool
<= :: DimChange d -> DimChange d -> Bool
$c<= :: forall d. Ord d => DimChange d -> DimChange d -> Bool
< :: DimChange d -> DimChange d -> Bool
$c< :: forall d. Ord d => DimChange d -> DimChange d -> Bool
compare :: DimChange d -> DimChange d -> Ordering
$ccompare :: forall d. Ord d => DimChange d -> DimChange d -> Ordering
$cp1Ord :: forall d. Ord d => Eq (DimChange d)
Ord, Int -> DimChange d -> ShowS
[DimChange d] -> ShowS
DimChange d -> String
(Int -> DimChange d -> ShowS)
-> (DimChange d -> String)
-> ([DimChange d] -> ShowS)
-> Show (DimChange d)
forall d. Show d => Int -> DimChange d -> ShowS
forall d. Show d => [DimChange d] -> ShowS
forall d. Show d => DimChange d -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DimChange d] -> ShowS
$cshowList :: forall d. Show d => [DimChange d] -> ShowS
show :: DimChange d -> String
$cshow :: forall d. Show d => DimChange d -> String
showsPrec :: Int -> DimChange d -> ShowS
$cshowsPrec :: forall d. Show d => Int -> DimChange d -> ShowS
Show)

instance Eq d => Eq (DimChange d) where
  DimCoercion d
x == :: DimChange d -> DimChange d -> Bool
== DimNew d
y = d
x d -> d -> Bool
forall a. Eq a => a -> a -> Bool
== d
y
  DimCoercion d
x == DimCoercion d
y = d
x d -> d -> Bool
forall a. Eq a => a -> a -> Bool
== d
y
  DimNew d
x == DimCoercion d
y = d
x d -> d -> Bool
forall a. Eq a => a -> a -> Bool
== d
y
  DimNew d
x == DimNew d
y = d
x d -> d -> Bool
forall a. Eq a => a -> a -> Bool
== d
y

instance Functor DimChange where
  fmap :: (a -> b) -> DimChange a -> DimChange b
fmap a -> b
f (DimCoercion a
d) = b -> DimChange b
forall d. d -> DimChange d
DimCoercion (b -> DimChange b) -> b -> DimChange b
forall a b. (a -> b) -> a -> b
$ a -> b
f a
d
  fmap a -> b
f (DimNew      a
d) = b -> DimChange b
forall d. d -> DimChange d
DimNew (b -> DimChange b) -> b -> DimChange b
forall a b. (a -> b) -> a -> b
$ a -> b
f a
d

instance Foldable DimChange where
  foldMap :: (a -> m) -> DimChange a -> m
foldMap a -> m
f (DimCoercion a
d) = a -> m
f a
d
  foldMap a -> m
f (DimNew      a
d) = a -> m
f a
d

instance Traversable DimChange where
  traverse :: (a -> f b) -> DimChange a -> f (DimChange b)
traverse a -> f b
f (DimCoercion a
d) = b -> DimChange b
forall d. d -> DimChange d
DimCoercion (b -> DimChange b) -> f b -> f (DimChange b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
d
  traverse a -> f b
f (DimNew      a
d) = b -> DimChange b
forall d. d -> DimChange d
DimNew (b -> DimChange b) -> f b -> f (DimChange b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
d

-- | A list of 'DimChange's, indicating the new dimensions of an array.
type ShapeChange d = [DimChange d]

-- | A primitive operation that returns something of known size and
-- does not itself contain any bindings.
data BasicOp lore
  = SubExp SubExp
    -- ^ A variable or constant.

  | Opaque SubExp
    -- ^ Semantically and operationally just identity, but is
    -- invisible/impenetrable to optimisations (hopefully).  This is
    -- just a hack to avoid optimisation (so, to work around compiler
    -- limitations).

  | ArrayLit  [SubExp] Type
    -- ^ Array literals, e.g., @[ [1+x, 3], [2, 1+4] ]@.
    -- Second arg is the element type of the rows of the array.
    -- Scalar operations

  | UnOp UnOp SubExp
    -- ^ Unary operation.

  | BinOp BinOp SubExp SubExp
    -- ^ Binary operation.

  | CmpOp CmpOp SubExp SubExp
    -- ^ Comparison - result type is always boolean.

  | ConvOp ConvOp SubExp
    -- ^ Conversion "casting".

  | Assert SubExp (ErrorMsg SubExp) (SrcLoc, [SrcLoc])
  -- ^ Turn a boolean into a certificate, halting the program with the
  -- given error message if the boolean is false.

  -- Primitive array operations

  | Index VName (Slice SubExp)
  -- ^ The certificates for bounds-checking are part of the 'Stm'.

  | Update VName (Slice SubExp) SubExp
  -- ^ An in-place update of the given array at the given position.
  -- Consumes the array.

  | Concat Int VName [VName] SubExp
  -- ^ @concat@0([1],[2, 3, 4]) = [1, 2, 3, 4]@.

  | Copy VName
  -- ^ Copy the given array.  The result will not alias anything.

  | Manifest [Int] VName
  -- ^ Manifest an array with dimensions represented in the given
  -- order.  The result will not alias anything.

  -- Array construction.
  | Iota SubExp SubExp SubExp IntType
  -- ^ @iota(n, x, s) = [x,x+s,..,x+(n-1)*s]@.
  --
  -- The 'IntType' indicates the type of the array returned and the
  -- offset/stride arguments, but not the length argument.

  | Replicate Shape SubExp
  -- ^ @replicate([3][2],1) = [[1,1], [1,1], [1,1]]@

  | Repeat [Shape] Shape VName
  -- ^ Repeat each dimension of the input array some number of times,
  -- given by the corresponding shape.  For an array of rank @k@, the
  -- list must contain @k@ shapes.  A shape may be empty (in which
  -- case the dimension is not repeated, but it is still present).
  -- The last shape indicates the amount of extra innermost
  -- dimensions.  All other extra dimensions are added *before* the original dimension.

  | Scratch PrimType [SubExp]
  -- ^ Create array of given type and shape, with undefined elements.

  -- Array index space transformation.
  | Reshape (ShapeChange SubExp) VName
   -- ^ 1st arg is the new shape, 2nd arg is the input array *)

  | Rearrange [Int] VName
  -- ^ Permute the dimensions of the input array.  The list
  -- of integers is a list of dimensions (0-indexed), which
  -- must be a permutation of @[0,n-1]@, where @n@ is the
  -- number of dimensions in the input array.

  | Rotate [SubExp] VName
  -- ^ Rotate the dimensions of the input array.  The list of
  -- subexpressions specify how much each dimension is rotated.  The
  -- length of this list must be equal to the rank of the array.
  deriving (BasicOp lore -> BasicOp lore -> Bool
(BasicOp lore -> BasicOp lore -> Bool)
-> (BasicOp lore -> BasicOp lore -> Bool) -> Eq (BasicOp lore)
forall lore. BasicOp lore -> BasicOp lore -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BasicOp lore -> BasicOp lore -> Bool
$c/= :: forall lore. BasicOp lore -> BasicOp lore -> Bool
== :: BasicOp lore -> BasicOp lore -> Bool
$c== :: forall lore. BasicOp lore -> BasicOp lore -> Bool
Eq, Eq (BasicOp lore)
Eq (BasicOp lore)
-> (BasicOp lore -> BasicOp lore -> Ordering)
-> (BasicOp lore -> BasicOp lore -> Bool)
-> (BasicOp lore -> BasicOp lore -> Bool)
-> (BasicOp lore -> BasicOp lore -> Bool)
-> (BasicOp lore -> BasicOp lore -> Bool)
-> (BasicOp lore -> BasicOp lore -> BasicOp lore)
-> (BasicOp lore -> BasicOp lore -> BasicOp lore)
-> Ord (BasicOp lore)
BasicOp lore -> BasicOp lore -> Bool
BasicOp lore -> BasicOp lore -> Ordering
BasicOp lore -> BasicOp lore -> BasicOp lore
forall lore. Eq (BasicOp lore)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall lore. BasicOp lore -> BasicOp lore -> Bool
forall lore. BasicOp lore -> BasicOp lore -> Ordering
forall lore. BasicOp lore -> BasicOp lore -> BasicOp lore
min :: BasicOp lore -> BasicOp lore -> BasicOp lore
$cmin :: forall lore. BasicOp lore -> BasicOp lore -> BasicOp lore
max :: BasicOp lore -> BasicOp lore -> BasicOp lore
$cmax :: forall lore. BasicOp lore -> BasicOp lore -> BasicOp lore
>= :: BasicOp lore -> BasicOp lore -> Bool
$c>= :: forall lore. BasicOp lore -> BasicOp lore -> Bool
> :: BasicOp lore -> BasicOp lore -> Bool
$c> :: forall lore. BasicOp lore -> BasicOp lore -> Bool
<= :: BasicOp lore -> BasicOp lore -> Bool
$c<= :: forall lore. BasicOp lore -> BasicOp lore -> Bool
< :: BasicOp lore -> BasicOp lore -> Bool
$c< :: forall lore. BasicOp lore -> BasicOp lore -> Bool
compare :: BasicOp lore -> BasicOp lore -> Ordering
$ccompare :: forall lore. BasicOp lore -> BasicOp lore -> Ordering
$cp1Ord :: forall lore. Eq (BasicOp lore)
Ord, Int -> BasicOp lore -> ShowS
[BasicOp lore] -> ShowS
BasicOp lore -> String
(Int -> BasicOp lore -> ShowS)
-> (BasicOp lore -> String)
-> ([BasicOp lore] -> ShowS)
-> Show (BasicOp lore)
forall lore. Int -> BasicOp lore -> ShowS
forall lore. [BasicOp lore] -> ShowS
forall lore. BasicOp lore -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BasicOp lore] -> ShowS
$cshowList :: forall lore. [BasicOp lore] -> ShowS
show :: BasicOp lore -> String
$cshow :: forall lore. BasicOp lore -> String
showsPrec :: Int -> BasicOp lore -> ShowS
$cshowsPrec :: forall lore. Int -> BasicOp lore -> ShowS
Show)

-- | The root Futhark expression type.  The 'Op' constructor contains
-- a lore-specific operation.  Do-loops, branches and function calls
-- are special.  Everything else is a simple 'BasicOp'.
data ExpT lore
  = BasicOp (BasicOp lore)
    -- ^ A simple (non-recursive) operation.

  | Apply  Name [(SubExp, Diet)] [RetType lore] (Safety, SrcLoc, [SrcLoc])

  | If     SubExp (BodyT lore) (BodyT lore) (IfAttr (BranchType lore))

  | DoLoop [(FParam lore, SubExp)] [(FParam lore, SubExp)] (LoopForm lore) (BodyT lore)
    -- ^ @loop {a} = {v} (for i < n|while b) do b@.  The merge
    -- parameters are divided into context and value part.

  | Op (Op lore)

deriving instance Annotations lore => Eq (ExpT lore)
deriving instance Annotations lore => Show (ExpT lore)
deriving instance Annotations lore => Ord (ExpT lore)

-- | Whether something is safe or unsafe (mostly function calls, and
-- in the context of whether operations are dynamically checked).
-- When we inline an 'Unsafe' function, we remove all safety checks in
-- its body.  The 'Ord' instance picks 'Unsafe' as being less than
-- 'Safe'.
data Safety = Unsafe | Safe deriving (Safety -> Safety -> Bool
(Safety -> Safety -> Bool)
-> (Safety -> Safety -> Bool) -> Eq Safety
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Safety -> Safety -> Bool
$c/= :: Safety -> Safety -> Bool
== :: Safety -> Safety -> Bool
$c== :: Safety -> Safety -> Bool
Eq, Eq Safety
Eq Safety
-> (Safety -> Safety -> Ordering)
-> (Safety -> Safety -> Bool)
-> (Safety -> Safety -> Bool)
-> (Safety -> Safety -> Bool)
-> (Safety -> Safety -> Bool)
-> (Safety -> Safety -> Safety)
-> (Safety -> Safety -> Safety)
-> Ord Safety
Safety -> Safety -> Bool
Safety -> Safety -> Ordering
Safety -> Safety -> Safety
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Safety -> Safety -> Safety
$cmin :: Safety -> Safety -> Safety
max :: Safety -> Safety -> Safety
$cmax :: Safety -> Safety -> Safety
>= :: Safety -> Safety -> Bool
$c>= :: Safety -> Safety -> Bool
> :: Safety -> Safety -> Bool
$c> :: Safety -> Safety -> Bool
<= :: Safety -> Safety -> Bool
$c<= :: Safety -> Safety -> Bool
< :: Safety -> Safety -> Bool
$c< :: Safety -> Safety -> Bool
compare :: Safety -> Safety -> Ordering
$ccompare :: Safety -> Safety -> Ordering
$cp1Ord :: Eq Safety
Ord, Int -> Safety -> ShowS
[Safety] -> ShowS
Safety -> String
(Int -> Safety -> ShowS)
-> (Safety -> String) -> ([Safety] -> ShowS) -> Show Safety
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Safety] -> ShowS
$cshowList :: [Safety] -> ShowS
show :: Safety -> String
$cshow :: Safety -> String
showsPrec :: Int -> Safety -> ShowS
$cshowsPrec :: Int -> Safety -> ShowS
Show)

-- | For-loop or while-loop?
data LoopForm lore = ForLoop VName IntType SubExp [(LParam lore,VName)]
                   | WhileLoop VName

deriving instance Annotations lore => Eq (LoopForm lore)
deriving instance Annotations lore => Show (LoopForm lore)
deriving instance Annotations lore => Ord (LoopForm lore)

-- | Data associated with a branch.
data IfAttr rt = IfAttr { IfAttr rt -> [rt]
ifReturns :: [rt]
                        , IfAttr rt -> IfSort
ifSort :: IfSort
                        }
                 deriving (IfAttr rt -> IfAttr rt -> Bool
(IfAttr rt -> IfAttr rt -> Bool)
-> (IfAttr rt -> IfAttr rt -> Bool) -> Eq (IfAttr rt)
forall rt. Eq rt => IfAttr rt -> IfAttr rt -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IfAttr rt -> IfAttr rt -> Bool
$c/= :: forall rt. Eq rt => IfAttr rt -> IfAttr rt -> Bool
== :: IfAttr rt -> IfAttr rt -> Bool
$c== :: forall rt. Eq rt => IfAttr rt -> IfAttr rt -> Bool
Eq, Int -> IfAttr rt -> ShowS
[IfAttr rt] -> ShowS
IfAttr rt -> String
(Int -> IfAttr rt -> ShowS)
-> (IfAttr rt -> String)
-> ([IfAttr rt] -> ShowS)
-> Show (IfAttr rt)
forall rt. Show rt => Int -> IfAttr rt -> ShowS
forall rt. Show rt => [IfAttr rt] -> ShowS
forall rt. Show rt => IfAttr rt -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IfAttr rt] -> ShowS
$cshowList :: forall rt. Show rt => [IfAttr rt] -> ShowS
show :: IfAttr rt -> String
$cshow :: forall rt. Show rt => IfAttr rt -> String
showsPrec :: Int -> IfAttr rt -> ShowS
$cshowsPrec :: forall rt. Show rt => Int -> IfAttr rt -> ShowS
Show, Eq (IfAttr rt)
Eq (IfAttr rt)
-> (IfAttr rt -> IfAttr rt -> Ordering)
-> (IfAttr rt -> IfAttr rt -> Bool)
-> (IfAttr rt -> IfAttr rt -> Bool)
-> (IfAttr rt -> IfAttr rt -> Bool)
-> (IfAttr rt -> IfAttr rt -> Bool)
-> (IfAttr rt -> IfAttr rt -> IfAttr rt)
-> (IfAttr rt -> IfAttr rt -> IfAttr rt)
-> Ord (IfAttr rt)
IfAttr rt -> IfAttr rt -> Bool
IfAttr rt -> IfAttr rt -> Ordering
IfAttr rt -> IfAttr rt -> IfAttr rt
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall rt. Ord rt => Eq (IfAttr rt)
forall rt. Ord rt => IfAttr rt -> IfAttr rt -> Bool
forall rt. Ord rt => IfAttr rt -> IfAttr rt -> Ordering
forall rt. Ord rt => IfAttr rt -> IfAttr rt -> IfAttr rt
min :: IfAttr rt -> IfAttr rt -> IfAttr rt
$cmin :: forall rt. Ord rt => IfAttr rt -> IfAttr rt -> IfAttr rt
max :: IfAttr rt -> IfAttr rt -> IfAttr rt
$cmax :: forall rt. Ord rt => IfAttr rt -> IfAttr rt -> IfAttr rt
>= :: IfAttr rt -> IfAttr rt -> Bool
$c>= :: forall rt. Ord rt => IfAttr rt -> IfAttr rt -> Bool
> :: IfAttr rt -> IfAttr rt -> Bool
$c> :: forall rt. Ord rt => IfAttr rt -> IfAttr rt -> Bool
<= :: IfAttr rt -> IfAttr rt -> Bool
$c<= :: forall rt. Ord rt => IfAttr rt -> IfAttr rt -> Bool
< :: IfAttr rt -> IfAttr rt -> Bool
$c< :: forall rt. Ord rt => IfAttr rt -> IfAttr rt -> Bool
compare :: IfAttr rt -> IfAttr rt -> Ordering
$ccompare :: forall rt. Ord rt => IfAttr rt -> IfAttr rt -> Ordering
$cp1Ord :: forall rt. Ord rt => Eq (IfAttr rt)
Ord)

data IfSort = IfNormal -- ^ An ordinary branch.
            | IfFallback -- ^ A branch where the "true" case is what
                         -- we are actually interested in, and the
                         -- "false" case is only present as a fallback
                         -- for when the true case cannot be safely
                         -- evaluated.  the compiler is permitted to
                         -- optimise away the branch if the true case
                         -- contains only safe statements.
            deriving (IfSort -> IfSort -> Bool
(IfSort -> IfSort -> Bool)
-> (IfSort -> IfSort -> Bool) -> Eq IfSort
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IfSort -> IfSort -> Bool
$c/= :: IfSort -> IfSort -> Bool
== :: IfSort -> IfSort -> Bool
$c== :: IfSort -> IfSort -> Bool
Eq, Int -> IfSort -> ShowS
[IfSort] -> ShowS
IfSort -> String
(Int -> IfSort -> ShowS)
-> (IfSort -> String) -> ([IfSort] -> ShowS) -> Show IfSort
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IfSort] -> ShowS
$cshowList :: [IfSort] -> ShowS
show :: IfSort -> String
$cshow :: IfSort -> String
showsPrec :: Int -> IfSort -> ShowS
$cshowsPrec :: Int -> IfSort -> ShowS
Show, Eq IfSort
Eq IfSort
-> (IfSort -> IfSort -> Ordering)
-> (IfSort -> IfSort -> Bool)
-> (IfSort -> IfSort -> Bool)
-> (IfSort -> IfSort -> Bool)
-> (IfSort -> IfSort -> Bool)
-> (IfSort -> IfSort -> IfSort)
-> (IfSort -> IfSort -> IfSort)
-> Ord IfSort
IfSort -> IfSort -> Bool
IfSort -> IfSort -> Ordering
IfSort -> IfSort -> IfSort
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: IfSort -> IfSort -> IfSort
$cmin :: IfSort -> IfSort -> IfSort
max :: IfSort -> IfSort -> IfSort
$cmax :: IfSort -> IfSort -> IfSort
>= :: IfSort -> IfSort -> Bool
$c>= :: IfSort -> IfSort -> Bool
> :: IfSort -> IfSort -> Bool
$c> :: IfSort -> IfSort -> Bool
<= :: IfSort -> IfSort -> Bool
$c<= :: IfSort -> IfSort -> Bool
< :: IfSort -> IfSort -> Bool
$c< :: IfSort -> IfSort -> Bool
compare :: IfSort -> IfSort -> Ordering
$ccompare :: IfSort -> IfSort -> Ordering
$cp1Ord :: Eq IfSort
Ord)

-- | A type alias for namespace control.
type Exp = ExpT

-- | Anonymous function for use in a SOAC.
data LambdaT lore = Lambda { LambdaT lore -> [LParam lore]
lambdaParams     :: [LParam lore]
                           , LambdaT lore -> BodyT lore
lambdaBody       :: BodyT lore
                           , LambdaT lore -> [Type]
lambdaReturnType :: [Type]
                           }

deriving instance Annotations lore => Eq (LambdaT lore)
deriving instance Annotations lore => Show (LambdaT lore)
deriving instance Annotations lore => Ord (LambdaT lore)

-- | Type alias for namespacing reasons.
type Lambda = LambdaT

type FParam lore = Param (FParamAttr lore)

type LParam lore = Param (LParamAttr lore)

-- | Function Declarations
data FunDef lore = FunDef { FunDef lore -> Maybe EntryPoint
funDefEntryPoint :: Maybe EntryPoint
                            -- ^ Contains a value if this function is
                            -- an entry point.
                          , FunDef lore -> Name
funDefName :: Name
                          , FunDef lore -> [RetType lore]
funDefRetType :: [RetType lore]
                          , FunDef lore -> [FParam lore]
funDefParams :: [FParam lore]
                          , FunDef lore -> BodyT lore
funDefBody :: BodyT lore
                          }

deriving instance Annotations lore => Eq (FunDef lore)
deriving instance Annotations lore => Show (FunDef lore)
deriving instance Annotations lore => Ord (FunDef lore)

-- | Information about the parameters and return value of an entry
-- point.  The first element is for parameters, the second for return
-- value.
type EntryPoint = ([EntryPointType], [EntryPointType])

-- | Every entry point argument and return value has an annotation
-- indicating how it maps to the original source program type.
data EntryPointType = TypeUnsigned
                      -- ^ Is an unsigned integer or array of unsigned
                      -- integers.
                    | TypeOpaque String Int
                      -- ^ A black box type comprising this many core
                      -- values.  The string is a human-readable
                      -- description with no other semantics.
                    | TypeDirect
                      -- ^ Maps directly.
                    deriving (EntryPointType -> EntryPointType -> Bool
(EntryPointType -> EntryPointType -> Bool)
-> (EntryPointType -> EntryPointType -> Bool) -> Eq EntryPointType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EntryPointType -> EntryPointType -> Bool
$c/= :: EntryPointType -> EntryPointType -> Bool
== :: EntryPointType -> EntryPointType -> Bool
$c== :: EntryPointType -> EntryPointType -> Bool
Eq, Int -> EntryPointType -> ShowS
[EntryPointType] -> ShowS
EntryPointType -> String
(Int -> EntryPointType -> ShowS)
-> (EntryPointType -> String)
-> ([EntryPointType] -> ShowS)
-> Show EntryPointType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EntryPointType] -> ShowS
$cshowList :: [EntryPointType] -> ShowS
show :: EntryPointType -> String
$cshow :: EntryPointType -> String
showsPrec :: Int -> EntryPointType -> ShowS
$cshowsPrec :: Int -> EntryPointType -> ShowS
Show, Eq EntryPointType
Eq EntryPointType
-> (EntryPointType -> EntryPointType -> Ordering)
-> (EntryPointType -> EntryPointType -> Bool)
-> (EntryPointType -> EntryPointType -> Bool)
-> (EntryPointType -> EntryPointType -> Bool)
-> (EntryPointType -> EntryPointType -> Bool)
-> (EntryPointType -> EntryPointType -> EntryPointType)
-> (EntryPointType -> EntryPointType -> EntryPointType)
-> Ord EntryPointType
EntryPointType -> EntryPointType -> Bool
EntryPointType -> EntryPointType -> Ordering
EntryPointType -> EntryPointType -> EntryPointType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: EntryPointType -> EntryPointType -> EntryPointType
$cmin :: EntryPointType -> EntryPointType -> EntryPointType
max :: EntryPointType -> EntryPointType -> EntryPointType
$cmax :: EntryPointType -> EntryPointType -> EntryPointType
>= :: EntryPointType -> EntryPointType -> Bool
$c>= :: EntryPointType -> EntryPointType -> Bool
> :: EntryPointType -> EntryPointType -> Bool
$c> :: EntryPointType -> EntryPointType -> Bool
<= :: EntryPointType -> EntryPointType -> Bool
$c<= :: EntryPointType -> EntryPointType -> Bool
< :: EntryPointType -> EntryPointType -> Bool
$c< :: EntryPointType -> EntryPointType -> Bool
compare :: EntryPointType -> EntryPointType -> Ordering
$ccompare :: EntryPointType -> EntryPointType -> Ordering
$cp1Ord :: Eq EntryPointType
Ord)

-- | An entire Futhark program.
data Prog lore = Prog
  { Prog lore -> Stms lore
progConsts :: Stms lore
    -- ^ Top-level constants that are computed at program startup, and
    -- which are in scope inside all functions.

  , Prog lore -> [FunDef lore]
progFuns :: [FunDef lore]
    -- ^ The functions comprising the program.  All funtions are also
    -- available in scope in the definitions of the constants, so be
    -- careful not to introduce circular dependencies (not currently
    -- checked).
  } deriving (Prog lore -> Prog lore -> Bool
(Prog lore -> Prog lore -> Bool)
-> (Prog lore -> Prog lore -> Bool) -> Eq (Prog lore)
forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Prog lore -> Prog lore -> Bool
$c/= :: forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
== :: Prog lore -> Prog lore -> Bool
$c== :: forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
Eq, Eq (Prog lore)
Eq (Prog lore)
-> (Prog lore -> Prog lore -> Ordering)
-> (Prog lore -> Prog lore -> Bool)
-> (Prog lore -> Prog lore -> Bool)
-> (Prog lore -> Prog lore -> Bool)
-> (Prog lore -> Prog lore -> Bool)
-> (Prog lore -> Prog lore -> Prog lore)
-> (Prog lore -> Prog lore -> Prog lore)
-> Ord (Prog lore)
Prog lore -> Prog lore -> Bool
Prog lore -> Prog lore -> Ordering
Prog lore -> Prog lore -> Prog lore
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall lore. Annotations lore => Eq (Prog lore)
forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
forall lore. Annotations lore => Prog lore -> Prog lore -> Ordering
forall lore.
Annotations lore =>
Prog lore -> Prog lore -> Prog lore
min :: Prog lore -> Prog lore -> Prog lore
$cmin :: forall lore.
Annotations lore =>
Prog lore -> Prog lore -> Prog lore
max :: Prog lore -> Prog lore -> Prog lore
$cmax :: forall lore.
Annotations lore =>
Prog lore -> Prog lore -> Prog lore
>= :: Prog lore -> Prog lore -> Bool
$c>= :: forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
> :: Prog lore -> Prog lore -> Bool
$c> :: forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
<= :: Prog lore -> Prog lore -> Bool
$c<= :: forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
< :: Prog lore -> Prog lore -> Bool
$c< :: forall lore. Annotations lore => Prog lore -> Prog lore -> Bool
compare :: Prog lore -> Prog lore -> Ordering
$ccompare :: forall lore. Annotations lore => Prog lore -> Prog lore -> Ordering
$cp1Ord :: forall lore. Annotations lore => Eq (Prog lore)
Ord, Int -> Prog lore -> ShowS
[Prog lore] -> ShowS
Prog lore -> String
(Int -> Prog lore -> ShowS)
-> (Prog lore -> String)
-> ([Prog lore] -> ShowS)
-> Show (Prog lore)
forall lore. Annotations lore => Int -> Prog lore -> ShowS
forall lore. Annotations lore => [Prog lore] -> ShowS
forall lore. Annotations lore => Prog lore -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Prog lore] -> ShowS
$cshowList :: forall lore. Annotations lore => [Prog lore] -> ShowS
show :: Prog lore -> String
$cshow :: forall lore. Annotations lore => Prog lore -> String
showsPrec :: Int -> Prog lore -> ShowS
$cshowsPrec :: forall lore. Annotations lore => Int -> Prog lore -> ShowS
Show)