{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
module Language.Futhark.Syntax
(
module Language.Futhark.Core
, Uniqueness(..)
, IntType(..)
, FloatType(..)
, PrimType(..)
, ArrayDim (..)
, DimDecl (..)
, ShapeDecl (..)
, shapeRank
, stripDims
, unifyShapes
, TypeName(..)
, typeNameFromQualName
, qualNameFromTypeName
, TypeBase(..)
, TypeArg(..)
, TypeExp(..)
, TypeArgExp(..)
, RecordArrayElemTypeBase(..)
, ArrayElemTypeBase(..)
, CompType
, PatternType
, StructType
, Diet(..)
, TypeDeclBase (..)
, IntValue(..)
, FloatValue(..)
, PrimValue(..)
, IsPrimValue(..)
, Value(..)
, BinOp (..)
, IdentBase (..)
, Inclusiveness(..)
, DimIndexBase(..)
, ExpBase(..)
, FieldBase(..)
, CaseBase(..)
, LoopFormBase (..)
, PatternBase(..)
, StreamForm(..)
, SpecBase(..)
, SigExpBase(..)
, TypeRefBase(..)
, SigBindBase(..)
, ModExpBase(..)
, ModBindBase(..)
, ModParamBase(..)
, DocComment(..)
, ValBindBase(..)
, Liftedness(..)
, TypeBindBase(..)
, TypeParamBase(..)
, typeParamName
, ProgBase(..)
, DecBase(..)
, NoInfo(..)
, Info(..)
, Alias(..)
, Aliasing
, QualName(..)
)
where
import Control.Applicative
import Control.Monad
import Data.Array
import Data.Bifoldable
import Data.Bifunctor
import Data.Bitraversable
import Data.Foldable
import Data.Loc
import qualified Data.Map.Strict as M
import Data.Monoid
import Data.Ord
import qualified Data.Set as S
import Data.Traversable
import Data.List
import Prelude
import Futhark.Representation.Primitive (FloatType (..),
FloatValue (..),
IntType (..), IntValue (..))
import Futhark.Util.Pretty
import Language.Futhark.Core
class (Show vn,
Show (f VName),
Show (f Diet),
Show (f String),
Show (f [VName]),
Show (f PatternType),
Show (f CompType),
Show (f (TypeBase () ())),
Show (f Int),
Show (f [TypeBase () ()]),
Show (f StructType),
Show (f (Aliasing, StructType)),
Show (f ([TypeBase () ()], PatternType)),
Show (f (M.Map VName VName)),
Show (f [RecordArrayElemTypeBase ()]),
Show (f Uniqueness),
Show (f ([CompType], CompType))) => Showable f vn where
data NoInfo a = NoInfo
deriving (Eq, Ord, Show)
instance Show vn => Showable NoInfo vn where
instance Functor NoInfo where
fmap _ NoInfo = NoInfo
instance Foldable NoInfo where
foldr _ b NoInfo = b
instance Traversable NoInfo where
traverse _ NoInfo = pure NoInfo
newtype Info a = Info { unInfo :: a }
deriving (Eq, Ord, Show)
instance Show vn => Showable Info vn where
instance Functor Info where
fmap f (Info x) = Info $ f x
instance Foldable Info where
foldr f b (Info x) = f x b
instance Traversable Info where
traverse f (Info x) = Info <$> f x
data PrimType = Signed IntType
| Unsigned IntType
| FloatType FloatType
| Bool
deriving (Eq, Ord, Show)
data PrimValue = SignedValue !IntValue
| UnsignedValue !IntValue
| FloatValue !FloatValue
| BoolValue !Bool
deriving (Eq, Ord, Show)
class IsPrimValue v where
primValue :: v -> PrimValue
instance IsPrimValue Int where
primValue = SignedValue . Int32Value . fromIntegral
instance IsPrimValue Int8 where
primValue = SignedValue . Int8Value
instance IsPrimValue Int16 where
primValue = SignedValue . Int16Value
instance IsPrimValue Int32 where
primValue = SignedValue . Int32Value
instance IsPrimValue Int64 where
primValue = SignedValue . Int64Value
instance IsPrimValue Word8 where
primValue = UnsignedValue . Int8Value . fromIntegral
instance IsPrimValue Word16 where
primValue = UnsignedValue . Int16Value . fromIntegral
instance IsPrimValue Word32 where
primValue = UnsignedValue . Int32Value . fromIntegral
instance IsPrimValue Word64 where
primValue = UnsignedValue . Int64Value . fromIntegral
instance IsPrimValue Float where
primValue = FloatValue . Float32Value
instance IsPrimValue Double where
primValue = FloatValue . Float64Value
instance IsPrimValue Bool where
primValue = BoolValue
class (Eq dim, Ord dim) => ArrayDim dim where
unifyDims :: dim -> dim -> Maybe dim
instance ArrayDim () where
unifyDims () () = Just ()
data DimDecl vn = NamedDim (QualName vn)
| ConstDim Int
| AnyDim
deriving (Eq, Ord, Show)
instance Functor DimDecl where
fmap = fmapDefault
instance Foldable DimDecl where
foldMap = foldMapDefault
instance Traversable DimDecl where
traverse f (NamedDim qn) = NamedDim <$> traverse f qn
traverse _ (ConstDim x) = pure $ ConstDim x
traverse _ AnyDim = pure AnyDim
instance (Eq vn, Ord vn) => ArrayDim (DimDecl vn) where
unifyDims AnyDim y = Just y
unifyDims x AnyDim = Just x
unifyDims (NamedDim x) (NamedDim y) | x == y = Just $ NamedDim x
unifyDims (ConstDim x) (ConstDim y) | x == y = Just $ ConstDim x
unifyDims _ _ = Nothing
newtype ShapeDecl dim = ShapeDecl { shapeDims :: [dim] }
deriving (Eq, Ord, Show)
instance Foldable ShapeDecl where
foldr f x (ShapeDecl ds) = foldr f x ds
instance Traversable ShapeDecl where
traverse f (ShapeDecl ds) = ShapeDecl <$> traverse f ds
instance Functor ShapeDecl where
fmap f (ShapeDecl ds) = ShapeDecl $ map f ds
instance Semigroup (ShapeDecl dim) where
ShapeDecl l1 <> ShapeDecl l2 = ShapeDecl $ l1 ++ l2
instance Monoid (ShapeDecl dim) where
mempty = ShapeDecl []
shapeRank :: ShapeDecl dim -> Int
shapeRank = length . shapeDims
stripDims :: Int -> ShapeDecl dim -> Maybe (ShapeDecl dim)
stripDims i (ShapeDecl l)
| i < length l = Just $ ShapeDecl $ drop i l
| otherwise = Nothing
unifyShapes :: ArrayDim dim => ShapeDecl dim -> ShapeDecl dim -> Maybe (ShapeDecl dim)
unifyShapes (ShapeDecl xs) (ShapeDecl ys) = do
guard $ length xs == length ys
ShapeDecl <$> zipWithM unifyDims xs ys
data TypeName = TypeName { typeQuals :: [VName], typeLeaf :: VName }
deriving (Show)
instance Eq TypeName where
TypeName _ x == TypeName _ y = x == y
instance Ord TypeName where
TypeName _ x `compare` TypeName _ y = x `compare` y
typeNameFromQualName :: QualName VName -> TypeName
typeNameFromQualName (QualName qs x) = TypeName qs x
qualNameFromTypeName :: TypeName -> QualName VName
qualNameFromTypeName (TypeName qs x) = QualName qs x
data RecordArrayElemTypeBase dim =
RecordArrayElem (ArrayElemTypeBase dim)
| RecordArrayArrayElem (ArrayElemTypeBase dim) (ShapeDecl dim)
deriving (Eq, Show)
instance Traversable RecordArrayElemTypeBase where
traverse f (RecordArrayElem t) = RecordArrayElem <$> traverse f t
traverse f (RecordArrayArrayElem a shape) =
RecordArrayArrayElem <$> traverse f a <*> traverse f shape
instance Functor RecordArrayElemTypeBase where
fmap = fmapDefault
instance Foldable RecordArrayElemTypeBase where
foldMap = foldMapDefault
data ArrayElemTypeBase dim =
ArrayPrimElem PrimType
| ArrayPolyElem TypeName [TypeArg dim]
| ArrayRecordElem (M.Map Name (RecordArrayElemTypeBase dim))
| ArrayEnumElem [Name]
deriving (Eq, Show)
instance Traversable ArrayElemTypeBase where
traverse _ (ArrayPrimElem t) =
pure $ ArrayPrimElem t
traverse f (ArrayPolyElem t args) =
ArrayPolyElem t <$> traverse (traverse f) args
traverse f (ArrayRecordElem fs) =
ArrayRecordElem <$> traverse (traverse f) fs
traverse _ (ArrayEnumElem cs) =
pure $ ArrayEnumElem cs
instance Functor ArrayElemTypeBase where
fmap = fmapDefault
instance Foldable ArrayElemTypeBase where
foldMap = foldMapDefault
data TypeBase dim as = Prim PrimType
| Enum [Name]
| Array as Uniqueness (ArrayElemTypeBase dim) (ShapeDecl dim)
| Record (M.Map Name (TypeBase dim as))
| TypeVar as Uniqueness TypeName [TypeArg dim]
| Arrow as (Maybe VName) (TypeBase dim as) (TypeBase dim as)
deriving (Show)
instance (Eq dim, Eq as) => Eq (TypeBase dim as) where
Prim x1 == Prim y1 = x1 == y1
Array x1 y1 z1 v1 == Array x2 y2 z2 v2 = x1 == x2 && y1 == y2 && z1 == z2 && v1 == v2
Record x1 == Record x2 = x1 == x2
TypeVar _ u1 x1 y1 == TypeVar _ u2 x2 y2 = u1 == u2 && x1 == x2 && y1 == y2
Arrow _ _ x1 y1 == Arrow _ _ x2 y2 = x1 == x2 && y1 == y2
Enum ns1 == Enum ns2 = sort ns1 == sort ns2
_ == _ = False
instance Bitraversable TypeBase where
bitraverse _ _ (Prim t) = pure $ Prim t
bitraverse f g (Array a u t shape) =
Array <$> g a <*> pure u <*> traverse f t <*> traverse f shape
bitraverse f g (Record fs) = Record <$> traverse (bitraverse f g) fs
bitraverse f g (TypeVar als u t args) =
TypeVar <$> g als <*> pure u <*> pure t <*> traverse (traverse f) args
bitraverse f g (Arrow als v t1 t2) =
Arrow <$> g als <*> pure v <*> bitraverse f g t1 <*> bitraverse f g t2
bitraverse _ _ (Enum n) = pure $ Enum n
instance Bifunctor TypeBase where
bimap = bimapDefault
instance Bifoldable TypeBase where
bifoldMap = bifoldMapDefault
data TypeArg dim = TypeArgDim dim SrcLoc
| TypeArgType (TypeBase dim ()) SrcLoc
deriving (Eq, Show)
instance Traversable TypeArg where
traverse f (TypeArgDim v loc) = TypeArgDim <$> f v <*> pure loc
traverse f (TypeArgType t loc) = TypeArgType <$> bitraverse f pure t <*> pure loc
instance Functor TypeArg where
fmap = fmapDefault
instance Foldable TypeArg where
foldMap = foldMapDefault
data Alias = AliasBound { aliasVar :: VName }
| AliasFree { aliasVar :: VName }
deriving (Eq, Ord, Show)
type Aliasing = S.Set Alias
type CompType = TypeBase () Aliasing
type PatternType = TypeBase (DimDecl VName) Aliasing
data TypeExp vn = TEVar (QualName vn) SrcLoc
| TETuple [TypeExp vn] SrcLoc
| TERecord [(Name, TypeExp vn)] SrcLoc
| TEArray (TypeExp vn) (DimDecl vn) SrcLoc
| TEUnique (TypeExp vn) SrcLoc
| TEApply (TypeExp vn) (TypeArgExp vn) SrcLoc
| TEArrow (Maybe vn) (TypeExp vn) (TypeExp vn) SrcLoc
| TEEnum [Name] SrcLoc
deriving (Eq, Show)
instance Located (TypeExp vn) where
locOf (TEArray _ _ loc) = locOf loc
locOf (TETuple _ loc) = locOf loc
locOf (TERecord _ loc) = locOf loc
locOf (TEVar _ loc) = locOf loc
locOf (TEUnique _ loc) = locOf loc
locOf (TEApply _ _ loc) = locOf loc
locOf (TEArrow _ _ _ loc) = locOf loc
locOf (TEEnum _ loc) = locOf loc
data TypeArgExp vn = TypeArgExpDim (DimDecl vn) SrcLoc
| TypeArgExpType (TypeExp vn)
deriving (Eq, Show)
instance Located (TypeArgExp vn) where
locOf (TypeArgExpDim _ loc) = locOf loc
locOf (TypeArgExpType t) = locOf t
type StructType = TypeBase (DimDecl VName) ()
data TypeDeclBase f vn =
TypeDecl { declaredType :: TypeExp vn
, expandedType :: f StructType
}
deriving instance Showable f vn => Show (TypeDeclBase f vn)
instance Located (TypeDeclBase f vn) where
locOf = locOf . declaredType
data Diet = RecordDiet (M.Map Name Diet)
| FuncDiet Diet Diet
| Consume
| Observe
deriving (Eq, Show)
data Value = PrimValue !PrimValue
| ArrayValue !(Array Int Value) (TypeBase () ())
deriving (Eq, Show)
data IdentBase f vn = Ident { identName :: vn
, identType :: f CompType
, identSrcLoc :: SrcLoc
}
deriving instance Showable f vn => Show (IdentBase f vn)
instance Eq vn => Eq (IdentBase ty vn) where
x == y = identName x == identName y
instance Ord vn => Ord (IdentBase ty vn) where
compare = comparing identName
instance Located (IdentBase ty vn) where
locOf = locOf . identSrcLoc
data BinOp = Backtick
| Plus
| Minus
| Pow
| Times
| Divide
| Mod
| Quot
| Rem
| ShiftR
| ShiftL
| Band
| Xor
| Bor
| LogAnd
| LogOr
| Equal
| NotEqual
| Less
| Leq
| Greater
| Geq
| PipeRight
| PipeLeft
deriving (Eq, Ord, Show, Enum, Bounded)
data Inclusiveness a = DownToExclusive a
| ToInclusive a
| UpToExclusive a
deriving (Eq, Ord, Show)
instance Located a => Located (Inclusiveness a) where
locOf (DownToExclusive x) = locOf x
locOf (ToInclusive x) = locOf x
locOf (UpToExclusive x) = locOf x
instance Functor Inclusiveness where
fmap = fmapDefault
instance Foldable Inclusiveness where
foldMap = foldMapDefault
instance Traversable Inclusiveness where
traverse f (DownToExclusive x) = DownToExclusive <$> f x
traverse f (ToInclusive x) = ToInclusive <$> f x
traverse f (UpToExclusive x) = UpToExclusive <$> f x
data DimIndexBase f vn = DimFix (ExpBase f vn)
| DimSlice (Maybe (ExpBase f vn))
(Maybe (ExpBase f vn))
(Maybe (ExpBase f vn))
deriving instance Showable f vn => Show (DimIndexBase f vn)
data QualName vn = QualName { qualQuals :: ![vn]
, qualLeaf :: !vn
}
deriving (Eq, Ord, Show)
instance Functor QualName where
fmap = fmapDefault
instance Foldable QualName where
foldMap = foldMapDefault
instance Traversable QualName where
traverse f (QualName qs v) = QualName <$> traverse f qs <*> f v
data ExpBase f vn =
Literal PrimValue SrcLoc
| IntLit Integer (f (TypeBase () ())) SrcLoc
| FloatLit Double (f (TypeBase () ())) SrcLoc
| Parens (ExpBase f vn) SrcLoc
| QualParens (QualName vn) (ExpBase f vn) SrcLoc
| TupLit [ExpBase f vn] SrcLoc
| RecordLit [FieldBase f vn] SrcLoc
| ArrayLit [ExpBase f vn] (f CompType) SrcLoc
| Range (ExpBase f vn) (Maybe (ExpBase f vn)) (Inclusiveness (ExpBase f vn)) (f CompType) SrcLoc
| Var (QualName vn) (f PatternType) SrcLoc
| Ascript (ExpBase f vn) (TypeDeclBase f vn) SrcLoc
| LetPat [TypeParamBase vn] (PatternBase f vn) (ExpBase f vn) (ExpBase f vn) SrcLoc
| LetFun vn ([TypeParamBase vn], [PatternBase f vn], Maybe (TypeExp vn), f StructType, ExpBase f vn)
(ExpBase f vn) SrcLoc
| If (ExpBase f vn) (ExpBase f vn) (ExpBase f vn) (f CompType) SrcLoc
| Apply (ExpBase f vn) (ExpBase f vn) (f Diet) (f PatternType) SrcLoc
| Negate (ExpBase f vn) SrcLoc
| Lambda [TypeParamBase vn] [PatternBase f vn] (ExpBase f vn)
(Maybe (TypeDeclBase f vn)) (f (Aliasing, StructType)) SrcLoc
| OpSection (QualName vn) (f PatternType) SrcLoc
| OpSectionLeft (QualName vn) (f PatternType)
(ExpBase f vn) (f StructType, f StructType) (f PatternType) SrcLoc
| OpSectionRight (QualName vn) (f PatternType)
(ExpBase f vn) (f StructType, f StructType) (f PatternType) SrcLoc
| ProjectSection [Name] (f PatternType) SrcLoc
| IndexSection [DimIndexBase f vn] (f PatternType) SrcLoc
| DoLoop
[TypeParamBase vn]
(PatternBase f vn)
(ExpBase f vn)
(LoopFormBase f vn)
(ExpBase f vn)
SrcLoc
| BinOp (QualName vn) (f PatternType)
(ExpBase f vn, f StructType) (ExpBase f vn, f StructType)
(f PatternType) SrcLoc
| Project Name (ExpBase f vn) (f CompType) SrcLoc
| LetWith (IdentBase f vn) (IdentBase f vn)
[DimIndexBase f vn] (ExpBase f vn)
(ExpBase f vn) SrcLoc
| Index (ExpBase f vn) [DimIndexBase f vn] (f CompType) SrcLoc
| Update (ExpBase f vn) [DimIndexBase f vn] (ExpBase f vn) SrcLoc
| RecordUpdate (ExpBase f vn) [Name] (ExpBase f vn) (f PatternType) SrcLoc
| Map (ExpBase f vn) (ExpBase f vn) (f CompType) SrcLoc
| Reduce Commutativity (ExpBase f vn) (ExpBase f vn) (ExpBase f vn) SrcLoc
| GenReduce (ExpBase f vn) (ExpBase f vn) (ExpBase f vn)
(ExpBase f vn) (ExpBase f vn) SrcLoc
| Scan (ExpBase f vn) (ExpBase f vn) (ExpBase f vn) SrcLoc
| Filter (ExpBase f vn) (ExpBase f vn) SrcLoc
| Partition Int (ExpBase f vn) (ExpBase f vn) SrcLoc
| Stream (StreamForm f vn) (ExpBase f vn) (ExpBase f vn) SrcLoc
| Zip Int (ExpBase f vn) [ExpBase f vn] (f CompType) SrcLoc
| Unzip (ExpBase f vn) [f CompType] SrcLoc
| Unsafe (ExpBase f vn) SrcLoc
| Assert (ExpBase f vn) (ExpBase f vn) (f String) SrcLoc
| VConstr0 Name (f CompType) SrcLoc
| Match (ExpBase f vn) [CaseBase f vn] (f CompType) SrcLoc
deriving instance Showable f vn => Show (ExpBase f vn)
data StreamForm f vn = MapLike StreamOrd
| RedLike StreamOrd Commutativity (ExpBase f vn)
deriving instance Showable f vn => Show (StreamForm f vn)
instance Located (ExpBase f vn) where
locOf (Literal _ loc) = locOf loc
locOf (IntLit _ _ loc) = locOf loc
locOf (FloatLit _ _ loc) = locOf loc
locOf (Parens _ loc) = locOf loc
locOf (QualParens _ _ loc) = locOf loc
locOf (TupLit _ pos) = locOf pos
locOf (RecordLit _ pos) = locOf pos
locOf (Project _ _ _ pos) = locOf pos
locOf (ArrayLit _ _ pos) = locOf pos
locOf (Range _ _ _ _ pos) = locOf pos
locOf (BinOp _ _ _ _ _ pos) = locOf pos
locOf (If _ _ _ _ pos) = locOf pos
locOf (Var _ _ loc) = locOf loc
locOf (Ascript _ _ loc) = locOf loc
locOf (Negate _ pos) = locOf pos
locOf (Apply _ _ _ _ pos) = locOf pos
locOf (LetPat _ _ _ _ pos) = locOf pos
locOf (LetFun _ _ _ loc) = locOf loc
locOf (LetWith _ _ _ _ _ pos) = locOf pos
locOf (Index _ _ _ loc) = locOf loc
locOf (Update _ _ _ pos) = locOf pos
locOf (RecordUpdate _ _ _ _ pos) = locOf pos
locOf (Map _ _ _ loc) = locOf loc
locOf (Reduce _ _ _ _ pos) = locOf pos
locOf (GenReduce _ _ _ _ _ pos) = locOf pos
locOf (Zip _ _ _ _ loc) = locOf loc
locOf (Unzip _ _ pos) = locOf pos
locOf (Scan _ _ _ pos) = locOf pos
locOf (Filter _ _ pos) = locOf pos
locOf (Partition _ _ _ loc) = locOf loc
locOf (Lambda _ _ _ _ _ loc) = locOf loc
locOf (OpSection _ _ loc) = locOf loc
locOf (OpSectionLeft _ _ _ _ _ loc) = locOf loc
locOf (OpSectionRight _ _ _ _ _ loc) = locOf loc
locOf (ProjectSection _ _ loc) = locOf loc
locOf (IndexSection _ _ loc) = locOf loc
locOf (DoLoop _ _ _ _ _ pos) = locOf pos
locOf (Stream _ _ _ pos) = locOf pos
locOf (Unsafe _ loc) = locOf loc
locOf (Assert _ _ _ loc) = locOf loc
locOf (VConstr0 _ _ loc) = locOf loc
locOf (Match _ _ _ loc) = locOf loc
data FieldBase f vn = RecordFieldExplicit Name (ExpBase f vn) SrcLoc
| RecordFieldImplicit vn (f CompType) SrcLoc
deriving instance Showable f vn => Show (FieldBase f vn)
instance Located (FieldBase f vn) where
locOf (RecordFieldExplicit _ _ loc) = locOf loc
locOf (RecordFieldImplicit _ _ loc) = locOf loc
data CaseBase f vn = CasePat (PatternBase f vn) (ExpBase f vn) SrcLoc
deriving instance Showable f vn => Show (CaseBase f vn)
instance Located (CaseBase f vn) where
locOf (CasePat _ _ loc) = locOf loc
data LoopFormBase f vn = For (IdentBase f vn) (ExpBase f vn)
| ForIn (PatternBase f vn) (ExpBase f vn)
| While (ExpBase f vn)
deriving instance Showable f vn => Show (LoopFormBase f vn)
data PatternBase f vn = TuplePattern [PatternBase f vn] SrcLoc
| RecordPattern [(Name, PatternBase f vn)] SrcLoc
| PatternParens (PatternBase f vn) SrcLoc
| Id vn (f PatternType) SrcLoc
| Wildcard (f PatternType) SrcLoc
| PatternAscription (PatternBase f vn) (TypeDeclBase f vn) SrcLoc
| PatternLit (ExpBase f vn) (f PatternType) SrcLoc
deriving instance Showable f vn => Show (PatternBase f vn)
instance Located (PatternBase f vn) where
locOf (TuplePattern _ loc) = locOf loc
locOf (RecordPattern _ loc) = locOf loc
locOf (PatternParens _ loc) = locOf loc
locOf (Id _ _ loc) = locOf loc
locOf (Wildcard _ loc) = locOf loc
locOf (PatternAscription _ _ loc) = locOf loc
locOf (PatternLit _ _ loc) = locOf loc
data DocComment = DocComment String SrcLoc
deriving (Show)
instance Located DocComment where
locOf (DocComment _ loc) = locOf loc
data ValBindBase f vn = ValBind { valBindEntryPoint :: Bool
, valBindName :: vn
, valBindRetDecl :: Maybe (TypeExp vn)
, valBindRetType :: f StructType
, valBindTypeParams :: [TypeParamBase vn]
, valBindParams :: [PatternBase f vn]
, valBindBody :: ExpBase f vn
, valBindDoc :: Maybe DocComment
, valBindLocation :: SrcLoc
}
deriving instance Showable f vn => Show (ValBindBase f vn)
instance Located (ValBindBase f vn) where
locOf = locOf . valBindLocation
data TypeBindBase f vn = TypeBind { typeAlias :: vn
, typeParams :: [TypeParamBase vn]
, typeExp :: TypeDeclBase f vn
, typeDoc :: Maybe DocComment
, typeBindLocation :: SrcLoc
}
deriving instance Showable f vn => Show (TypeBindBase f vn)
instance Located (TypeBindBase f vn) where
locOf = locOf . typeBindLocation
data Liftedness = Unlifted
| Lifted
deriving (Eq, Ord, Show)
data TypeParamBase vn = TypeParamDim vn SrcLoc
| TypeParamType Liftedness vn SrcLoc
deriving (Eq, Show)
instance Functor TypeParamBase where
fmap = fmapDefault
instance Foldable TypeParamBase where
foldMap = foldMapDefault
instance Traversable TypeParamBase where
traverse f (TypeParamDim v loc) = TypeParamDim <$> f v <*> pure loc
traverse f (TypeParamType l v loc) = TypeParamType l <$> f v <*> pure loc
instance Located (TypeParamBase vn) where
locOf (TypeParamDim _ loc) = locOf loc
locOf (TypeParamType _ _ loc) = locOf loc
typeParamName :: TypeParamBase vn -> vn
typeParamName (TypeParamDim v _) = v
typeParamName (TypeParamType _ v _) = v
data SpecBase f vn = ValSpec { specName :: vn
, specTypeParams :: [TypeParamBase vn]
, specType :: TypeDeclBase f vn
, specDoc :: Maybe DocComment
, specLocation :: SrcLoc
}
| TypeAbbrSpec (TypeBindBase f vn)
| TypeSpec Liftedness vn [TypeParamBase vn] (Maybe DocComment) SrcLoc
| ModSpec vn (SigExpBase f vn) (Maybe DocComment) SrcLoc
| IncludeSpec (SigExpBase f vn) SrcLoc
deriving instance Showable f vn => Show (SpecBase f vn)
instance Located (SpecBase f vn) where
locOf (ValSpec _ _ _ _ loc) = locOf loc
locOf (TypeAbbrSpec tbind) = locOf tbind
locOf (TypeSpec _ _ _ _ loc) = locOf loc
locOf (ModSpec _ _ _ loc) = locOf loc
locOf (IncludeSpec _ loc) = locOf loc
data SigExpBase f vn = SigVar (QualName vn) SrcLoc
| SigParens (SigExpBase f vn) SrcLoc
| SigSpecs [SpecBase f vn] SrcLoc
| SigWith (SigExpBase f vn) (TypeRefBase f vn) SrcLoc
| SigArrow (Maybe vn) (SigExpBase f vn) (SigExpBase f vn) SrcLoc
deriving instance Showable f vn => Show (SigExpBase f vn)
data TypeRefBase f vn = TypeRef (QualName vn) [TypeParamBase vn] (TypeDeclBase f vn) SrcLoc
deriving instance Showable f vn => Show (TypeRefBase f vn)
instance Located (TypeRefBase f vn) where
locOf (TypeRef _ _ _ loc) = locOf loc
instance Located (SigExpBase f vn) where
locOf (SigVar _ loc) = locOf loc
locOf (SigParens _ loc) = locOf loc
locOf (SigSpecs _ loc) = locOf loc
locOf (SigWith _ _ loc) = locOf loc
locOf (SigArrow _ _ _ loc) = locOf loc
data SigBindBase f vn = SigBind { sigName :: vn
, sigExp :: SigExpBase f vn
, sigDoc :: Maybe DocComment
, sigLoc :: SrcLoc
}
deriving instance Showable f vn => Show (SigBindBase f vn)
instance Located (SigBindBase f vn) where
locOf = locOf . sigLoc
data ModExpBase f vn = ModVar (QualName vn) SrcLoc
| ModParens (ModExpBase f vn) SrcLoc
| ModImport FilePath (f FilePath) SrcLoc
| ModDecs [DecBase f vn] SrcLoc
| ModApply (ModExpBase f vn) (ModExpBase f vn) (f (M.Map VName VName)) (f (M.Map VName VName)) SrcLoc
| ModAscript (ModExpBase f vn) (SigExpBase f vn) (f (M.Map VName VName)) SrcLoc
| ModLambda (ModParamBase f vn)
(Maybe (SigExpBase f vn, f (M.Map VName VName)))
(ModExpBase f vn)
SrcLoc
deriving instance Showable f vn => Show (ModExpBase f vn)
instance Located (ModExpBase f vn) where
locOf (ModVar _ loc) = locOf loc
locOf (ModParens _ loc) = locOf loc
locOf (ModImport _ _ loc) = locOf loc
locOf (ModDecs _ loc) = locOf loc
locOf (ModApply _ _ _ _ loc) = locOf loc
locOf (ModAscript _ _ _ loc) = locOf loc
locOf (ModLambda _ _ _ loc) = locOf loc
data ModBindBase f vn =
ModBind { modName :: vn
, modParams :: [ModParamBase f vn]
, modSignature :: Maybe (SigExpBase f vn, f (M.Map VName VName))
, modExp :: ModExpBase f vn
, modDoc :: Maybe DocComment
, modLocation :: SrcLoc
}
deriving instance Showable f vn => Show (ModBindBase f vn)
instance Located (ModBindBase f vn) where
locOf = locOf . modLocation
data ModParamBase f vn = ModParam { modParamName :: vn
, modParamType :: SigExpBase f vn
, modParamAbs :: f [VName]
, modParamLocation :: SrcLoc
}
deriving instance Showable f vn => Show (ModParamBase f vn)
instance Located (ModParamBase f vn) where
locOf = locOf . modParamLocation
data DecBase f vn = ValDec (ValBindBase f vn)
| TypeDec (TypeBindBase f vn)
| SigDec (SigBindBase f vn)
| ModDec (ModBindBase f vn)
| OpenDec (ModExpBase f vn) SrcLoc
| LocalDec (DecBase f vn) SrcLoc
| ImportDec FilePath (f FilePath) SrcLoc
deriving instance Showable f vn => Show (DecBase f vn)
instance Located (DecBase f vn) where
locOf (ValDec d) = locOf d
locOf (TypeDec d) = locOf d
locOf (SigDec d) = locOf d
locOf (ModDec d) = locOf d
locOf (OpenDec _ loc) = locOf loc
locOf (LocalDec _ loc) = locOf loc
locOf (ImportDec _ _ loc) = locOf loc
data ProgBase f vn = Prog { progDoc :: Maybe DocComment
, progDecs :: [DecBase f vn]
}
deriving instance Showable f vn => Show (ProgBase f vn)
instance Pretty PrimType where
ppr (Unsigned Int8) = text "u8"
ppr (Unsigned Int16) = text "u16"
ppr (Unsigned Int32) = text "u32"
ppr (Unsigned Int64) = text "u64"
ppr (Signed t) = ppr t
ppr (FloatType t) = ppr t
ppr Bool = text "bool"
instance Pretty BinOp where
ppr Backtick = text "``"
ppr Plus = text "+"
ppr Minus = text "-"
ppr Pow = text "**"
ppr Times = text "*"
ppr Divide = text "/"
ppr Mod = text "%"
ppr Quot = text "//"
ppr Rem = text "%%"
ppr ShiftR = text ">>"
ppr ShiftL = text "<<"
ppr Band = text "&"
ppr Xor = text "^"
ppr Bor = text "|"
ppr LogAnd = text "&&"
ppr LogOr = text "||"
ppr Equal = text "=="
ppr NotEqual = text "!="
ppr Less = text "<"
ppr Leq = text "<="
ppr Greater = text ">"
ppr Geq = text ">="
ppr PipeLeft = text "<|"
ppr PipeRight = text "|>"