{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
module Language.Futhark.Attributes
(
Intrinsic(..)
, intrinsics
, maxIntrinsicTag
, namesToPrimTypes
, qualName
, qualify
, typeName
, valueType
, leadingOperator
, progImports
, decImports
, progModuleTypes
, identifierReference
, identifierReferences
, typeOf
, patternIdents
, patternType
, patternStructType
, patternParam
, patternOrderZero
, patternDimNames
, uniqueness
, unique
, aliases
, diet
, arrayRank
, nestedDims
, orderZero
, unfoldFunType
, foldFunType
, typeVars
, typeDimNames
, rank
, peelArray
, stripArray
, arrayOf
, toStructural
, toStruct
, fromStruct
, setAliases
, addAliases
, setUniqueness
, removeShapeAnnotations
, vacuousShapeAnnotations
, anyDimShapeAnnotations
, tupleRecord
, isTupleRecord
, areTupleFields
, tupleFieldNames
, sortFields
, sortConstrs
, isTypeParam
, combineTypeShapes
, unscopeType
, NoInfo(..)
, UncheckedType
, UncheckedTypeExp
, UncheckedIdent
, UncheckedTypeDecl
, UncheckedDimIndex
, UncheckedExp
, UncheckedModExp
, UncheckedSigExp
, UncheckedTypeParam
, UncheckedPattern
, UncheckedValBind
, UncheckedDec
, UncheckedProg
, UncheckedCase
)
where
import Control.Monad.Writer hiding (Sum)
import Data.Char
import Data.Foldable
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Data.List
import Data.Loc
import Data.Maybe
import Data.Ord
import Data.Bifunctor
import Data.Bifoldable
import Prelude
import Futhark.Util.Pretty
import Language.Futhark.Syntax
import qualified Futhark.Representation.Primitive as Primitive
arrayRank :: TypeBase dim as -> Int
arrayRank = shapeRank . arrayShape
arrayShape :: TypeBase dim as -> ShapeDecl dim
arrayShape (Array _ _ _ ds) = ds
arrayShape _ = mempty
nestedDims :: TypeBase (DimDecl VName) as -> [DimDecl VName]
nestedDims t =
case t of Array _ _ a ds ->
nub $ nestedDims (Scalar a) <> shapeDims ds
Scalar (Record fs) ->
nub $ fold $ fmap nestedDims fs
Scalar Prim{} ->
mempty
Scalar (Sum cs) ->
nub $ fold $ (fmap . concatMap) nestedDims cs
Scalar (Arrow _ v t1 t2) ->
filter (notV v) $ nestedDims t1 <> nestedDims t2
Scalar (TypeVar _ _ _ targs) ->
concatMap typeArgDims targs
where typeArgDims (TypeArgDim d _) = [d]
typeArgDims (TypeArgType at _) = nestedDims at
notV Unnamed = const True
notV (Named v) = (/=NamedDim (qualName v))
removeShapeAnnotations :: TypeBase (DimDecl vn) as -> TypeBase () as
removeShapeAnnotations = modifyShapeAnnotations $ const ()
vacuousShapeAnnotations :: TypeBase () as -> TypeBase (DimDecl vn) as
vacuousShapeAnnotations = modifyShapeAnnotations $ const AnyDim
anyDimShapeAnnotations :: TypeBase (DimDecl vn) as -> TypeBase (DimDecl vn) as
anyDimShapeAnnotations = modifyShapeAnnotations $ const AnyDim
modifyShapeAnnotations :: (oldshape -> newshape)
-> TypeBase oldshape as
-> TypeBase newshape as
modifyShapeAnnotations f = bimap f id
uniqueness :: TypeBase shape as -> Uniqueness
uniqueness (Array _ u _ _) = u
uniqueness (Scalar (TypeVar _ u _ _)) = u
uniqueness (Scalar (Sum ts)) = mconcat $ map (mconcat . map uniqueness) $ M.elems ts
uniqueness _ = Nonunique
unique :: TypeBase shape as -> Bool
unique = (==Unique) . uniqueness
aliases :: Monoid as => TypeBase shape as -> as
aliases = bifoldMap (const mempty) id
diet :: TypeBase shape as -> Diet
diet (Scalar (Record ets)) = RecordDiet $ fmap diet ets
diet (Scalar (Prim _)) = Observe
diet (Scalar TypeVar{}) = Observe
diet (Scalar (Arrow _ _ t1 t2)) = FuncDiet (diet t1) (diet t2)
diet (Array _ Unique _ _) = Consume
diet (Array _ Nonunique _ _) = Observe
diet (Scalar Sum{}) = Observe
toStructural :: TypeBase dim as
-> TypeBase () ()
toStructural = flip setAliases () . modifyShapeAnnotations (const ())
toStruct :: TypeBase dim as
-> TypeBase dim ()
toStruct t = t `setAliases` ()
fromStruct :: TypeBase dim as
-> TypeBase dim Aliasing
fromStruct t = t `setAliases` S.empty
peelArray :: Int -> TypeBase dim as -> Maybe (TypeBase dim as)
peelArray n (Array als u t shape)
| shapeRank shape == n =
Just $ Scalar t `addAliases` const als
| otherwise =
Array als u t <$> stripDims n shape
peelArray _ _ = Nothing
arrayOf :: Monoid as =>
TypeBase dim as
-> ShapeDecl dim
-> Uniqueness
-> TypeBase dim as
arrayOf t = arrayOfWithAliases (t `setUniqueness` Nonunique) mempty
arrayOfWithAliases :: Monoid as =>
TypeBase dim as
-> as
-> ShapeDecl dim
-> Uniqueness
-> TypeBase dim as
arrayOfWithAliases (Array as1 _ et shape1) as2 shape2 u =
Array (as1<>as2) u et (shape2 <> shape1)
arrayOfWithAliases (Scalar t) as shape u =
Array as u (bimap id (const ()) t) shape
stripArray :: Int -> TypeBase dim as -> TypeBase dim as
stripArray n (Array als u et shape)
| Just shape' <- stripDims n shape =
Array als u et shape'
| otherwise =
Scalar et `setUniqueness` u `setAliases` als
stripArray _ t = t
tupleRecord :: [TypeBase dim as] -> TypeBase dim as
tupleRecord = Scalar . Record . M.fromList . zip tupleFieldNames
isTupleRecord :: TypeBase dim as -> Maybe [TypeBase dim as]
isTupleRecord (Scalar (Record fs)) = areTupleFields fs
isTupleRecord _ = Nothing
areTupleFields :: M.Map Name a -> Maybe [a]
areTupleFields fs =
let fs' = sortFields fs
in if and $ zipWith (==) (map fst fs') tupleFieldNames
then Just $ map snd fs'
else Nothing
tupleFieldNames :: [Name]
tupleFieldNames = map (nameFromString . show) [(1::Int)..]
sortFields :: M.Map Name a -> [(Name,a)]
sortFields l = map snd $ sortOn fst $ zip (map (fieldish . fst) l') l'
where l' = M.toList l
fieldish s = case reads $ nameToString s of
[(x, "")] -> Left (x::Int)
_ -> Right s
sortConstrs :: M.Map Name a -> [(Name, a)]
sortConstrs cs = sortOn fst $ M.toList cs
isTypeParam :: TypeParamBase vn -> Bool
isTypeParam TypeParamType{} = True
isTypeParam TypeParamDim{} = False
combineTypeShapes :: (Monoid as, ArrayDim dim) =>
TypeBase dim as -> TypeBase dim as -> TypeBase dim as
combineTypeShapes (Scalar (Record ts1)) (Scalar (Record ts2))
| M.keys ts1 == M.keys ts2 =
Scalar $ Record $ M.map (uncurry combineTypeShapes) (M.intersectionWith (,) ts1 ts2)
combineTypeShapes (Array als1 u1 et1 shape1) (Array als2 _u2 et2 shape2)
| Just new_shape <- unifyShapes shape1 shape2 =
arrayOfWithAliases (combineTypeShapes (Scalar et1) (Scalar et2)
`setAliases` mempty)
(als1<>als2) new_shape u1
combineTypeShapes _ new_tp = new_tp
setUniqueness :: TypeBase dim as -> Uniqueness -> TypeBase dim as
setUniqueness (Array als _ et shape) u =
Array als u et shape
setUniqueness (Scalar (TypeVar als _ t targs)) u =
Scalar $ TypeVar als u t targs
setUniqueness (Scalar (Record ets)) u =
Scalar $ Record $ fmap (`setUniqueness` u) ets
setUniqueness (Scalar (Sum ets)) u =
Scalar $ Sum $ fmap (map (`setUniqueness` u)) ets
setUniqueness t _ = t
setAliases :: TypeBase dim asf -> ast -> TypeBase dim ast
setAliases t = addAliases t . const
addAliases :: TypeBase dim asf -> (asf -> ast)
-> TypeBase dim ast
addAliases t f = bimap id f t
intValueType :: IntValue -> IntType
intValueType Int8Value{} = Int8
intValueType Int16Value{} = Int16
intValueType Int32Value{} = Int32
intValueType Int64Value{} = Int64
floatValueType :: FloatValue -> FloatType
floatValueType Float32Value{} = Float32
floatValueType Float64Value{} = Float64
primValueType :: PrimValue -> PrimType
primValueType (SignedValue v) = Signed $ intValueType v
primValueType (UnsignedValue v) = Unsigned $ intValueType v
primValueType (FloatValue v) = FloatType $ floatValueType v
primValueType BoolValue{} = Bool
valueType :: Value -> TypeBase () ()
valueType (PrimValue bv) = Scalar $ Prim $ primValueType bv
valueType (ArrayValue _ t) = t
rank :: Int -> ShapeDecl ()
rank n = ShapeDecl $ replicate n ()
unscopeType :: S.Set VName -> PatternType -> PatternType
unscopeType bound_here t = modifyShapeAnnotations onDim $ t `addAliases` S.map unbind
where unbind (AliasBound v) | v `S.member` bound_here = AliasFree v
unbind a = a
onDim (NamedDim qn) | qualLeaf qn `S.member` bound_here = AnyDim
onDim d = d
typeOf :: ExpBase Info VName -> PatternType
typeOf (Literal val _) = Scalar $ Prim $ primValueType val
typeOf (IntLit _ (Info t) _) = t
typeOf (FloatLit _ (Info t) _) = t
typeOf (Parens e _) = typeOf e
typeOf (QualParens _ e _) = typeOf e
typeOf (TupLit es _) = tupleRecord $ map typeOf es
typeOf (RecordLit fs _) =
Scalar $ Record $ M.unions $ reverse $ map record fs
where record (RecordFieldExplicit name e _) = M.singleton name $ typeOf e
record (RecordFieldImplicit name (Info t) _) =
M.singleton (baseName name) $ t
`addAliases` S.insert (AliasBound name)
typeOf (ArrayLit _ (Info t) _) = t
typeOf (Range _ _ _ (Info t) _) = t
typeOf (BinOp _ _ _ _ (Info t) _) = t
typeOf (Project _ _ (Info t) _) = t
typeOf (If _ _ _ (Info t) _) = t
typeOf (Var _ (Info t) _) = t
typeOf (Ascript _ _ (Info t) _) = t
typeOf (Apply _ _ _ (Info t) _) = t
typeOf (Negate e _) = typeOf e
typeOf (LetPat _ _ _ (Info t) _) = t
typeOf (LetFun name _ body _) = unscopeType (S.singleton name) $ typeOf body
typeOf (LetWith _ _ _ _ _ (Info t) _) = t
typeOf (Index _ _ (Info t) _) = t
typeOf (Update e _ _ _) = typeOf e `setAliases` mempty
typeOf (RecordUpdate _ _ _ (Info t) _) = t
typeOf (Unsafe e _) = typeOf e
typeOf (Assert _ e _ _) = typeOf e
typeOf (DoLoop pat _ _ _ _) = patternType pat
typeOf (Lambda params _ _ (Info (als, t)) _) =
unscopeType bound_here $ foldr (arrow . patternParam) t params `setAliases` als
where bound_here = S.map identName (mconcat $ map patternIdents params)
arrow (px, tx) y = Scalar $ Arrow () px tx y
typeOf (OpSection _ (Info t) _) =
t
typeOf (OpSectionLeft _ _ _ (_, Info pt2) (Info ret) _) =
foldFunType [fromStruct pt2] ret
typeOf (OpSectionRight _ _ _ (Info pt1, _) (Info ret) _) =
foldFunType [fromStruct pt1] ret
typeOf (ProjectSection _ (Info t) _) = t
typeOf (IndexSection _ (Info t) _) = t
typeOf (Constr _ _ (Info t) _) = t
typeOf (Match _ cs (Info t) _) =
unscopeType (foldMap unscopeSet cs) t
where unscopeSet (CasePat p _ _) = S.map identName $ patternIdents p
foldFunType :: Monoid as => [TypeBase dim as] -> TypeBase dim as -> TypeBase dim as
foldFunType ps ret = foldr arrow ret ps
where arrow t1 t2 = Scalar $ Arrow mempty Unnamed t1 t2
unfoldFunType :: TypeBase dim as -> ([TypeBase dim as], TypeBase dim as)
unfoldFunType (Scalar (Arrow _ _ t1 t2)) =
let (ps, r) = unfoldFunType t2
in (t1 : ps, r)
unfoldFunType t = ([], t)
typeVars :: Monoid as => TypeBase dim as -> S.Set VName
typeVars t =
case t of
Scalar Prim{} -> mempty
Scalar (TypeVar _ _ tn targs) ->
mconcat $ typeVarFree tn : map typeArgFree targs
Scalar (Arrow _ _ t1 t2) -> typeVars t1 <> typeVars t2
Scalar (Record fields) -> foldMap typeVars fields
Scalar (Sum cs) -> mconcat $ (foldMap . fmap) typeVars cs
Array _ _ rt _ -> typeVars $ Scalar rt
where typeVarFree = S.singleton . typeLeaf
typeArgFree (TypeArgType ta _) = typeVars ta
typeArgFree TypeArgDim{} = mempty
orderZero :: TypeBase dim as -> Bool
orderZero Array{} = True
orderZero (Scalar (Prim _)) = True
orderZero (Scalar (Record fs)) = all orderZero $ M.elems fs
orderZero (Scalar TypeVar{}) = True
orderZero (Scalar Arrow{}) = False
orderZero (Scalar (Sum cs)) = all (all orderZero) cs
patternDimNames :: PatternBase Info VName -> S.Set VName
patternDimNames (TuplePattern ps _) = foldMap patternDimNames ps
patternDimNames (RecordPattern fs _) = foldMap (patternDimNames . snd) fs
patternDimNames (PatternParens p _) = patternDimNames p
patternDimNames (Id _ (Info tp) _) = typeDimNames tp
patternDimNames (Wildcard (Info tp) _) = typeDimNames tp
patternDimNames (PatternAscription p (TypeDecl _ (Info t)) _) =
patternDimNames p <> typeDimNames t
patternDimNames (PatternLit _ (Info tp) _) = typeDimNames tp
patternDimNames (PatternConstr _ _ ps _) = foldMap patternDimNames ps
typeDimNames :: TypeBase (DimDecl VName) als -> S.Set VName
typeDimNames = foldMap dimName . nestedDims
where dimName :: DimDecl VName -> S.Set VName
dimName (NamedDim qn) = S.singleton $ qualLeaf qn
dimName _ = mempty
patternOrderZero :: PatternBase Info vn -> Bool
patternOrderZero pat = case pat of
TuplePattern ps _ -> all patternOrderZero ps
RecordPattern fs _ -> all (patternOrderZero . snd) fs
PatternParens p _ -> patternOrderZero p
Id _ (Info t) _ -> orderZero t
Wildcard (Info t) _ -> orderZero t
PatternAscription p _ _ -> patternOrderZero p
PatternLit _ (Info t) _ -> orderZero t
PatternConstr _ _ ps _ -> all patternOrderZero ps
patternIdents :: (Functor f, Ord vn) => PatternBase f vn -> S.Set (IdentBase f vn)
patternIdents (Id v t loc) = S.singleton $ Ident v t loc
patternIdents (PatternParens p _) = patternIdents p
patternIdents (TuplePattern pats _) = mconcat $ map patternIdents pats
patternIdents (RecordPattern fs _) = mconcat $ map (patternIdents . snd) fs
patternIdents Wildcard{} = mempty
patternIdents (PatternAscription p _ _) = patternIdents p
patternIdents PatternLit{} = mempty
patternIdents (PatternConstr _ _ ps _ ) = mconcat $ map patternIdents ps
patternType :: PatternBase Info VName -> PatternType
patternType (Wildcard (Info t) _) = t
patternType (PatternParens p _) = patternType p
patternType (Id _ (Info t) _) = t
patternType (TuplePattern pats _) = tupleRecord $ map patternType pats
patternType (RecordPattern fs _) = Scalar $ Record $ patternType <$> M.fromList fs
patternType (PatternAscription p _ _) = patternType p
patternType (PatternLit _ (Info t) _) = t
patternType (PatternConstr _ (Info t) _ _) = t
patternStructType :: PatternBase Info VName -> StructType
patternStructType = toStruct . patternType
patternParam :: PatternBase Info VName -> (PName, StructType)
patternParam (PatternParens p _) =
patternParam p
patternParam (PatternAscription (Id v _ _) td _) =
(Named v, unInfo $ expandedType td)
patternParam (Id v (Info t) _) =
(Named v, toStruct t)
patternParam p =
(Unnamed, patternStructType p)
namesToPrimTypes :: M.Map Name PrimType
namesToPrimTypes = M.fromList
[ (nameFromString $ pretty t, t) |
t <- Bool :
map Signed [minBound..maxBound] ++
map Unsigned [minBound..maxBound] ++
map FloatType [minBound..maxBound] ]
data Intrinsic = IntrinsicMonoFun [PrimType] PrimType
| IntrinsicOverloadedFun [PrimType] [Maybe PrimType] (Maybe PrimType)
| IntrinsicPolyFun [TypeParamBase VName] [TypeBase () ()] (TypeBase () ())
| IntrinsicType PrimType
| IntrinsicEquality
| IntrinsicOpaque
intrinsics :: M.Map VName Intrinsic
intrinsics = M.fromList $ zipWith namify [10..] $
map primFun (M.toList Primitive.primFuns) ++
[("opaque", IntrinsicOpaque)] ++
map unOpFun Primitive.allUnOps ++
map binOpFun Primitive.allBinOps ++
map cmpOpFun Primitive.allCmpOps ++
map convOpFun Primitive.allConvOps ++
map signFun Primitive.allIntTypes ++
map unsignFun Primitive.allIntTypes ++
map intrinsicType (map Signed [minBound..maxBound] ++
map Unsigned [minBound..maxBound] ++
map FloatType [minBound..maxBound] ++
[Bool]) ++
[ ("!", IntrinsicOverloadedFun
(map Signed [minBound..maxBound] ++
map Unsigned [minBound..maxBound] ++
[Bool])
[Nothing] Nothing) ] ++
mapMaybe mkIntrinsicBinOp [minBound..maxBound] ++
[("flatten", IntrinsicPolyFun [tp_a]
[Array () Nonunique t_a (rank 2)] $
Array () Nonunique t_a (rank 1)),
("unflatten", IntrinsicPolyFun [tp_a]
[Scalar $ Prim $ Signed Int32,
Scalar $ Prim $ Signed Int32,
Array () Nonunique t_a (rank 1)] $
Array () Nonunique t_a (rank 2)),
("concat", IntrinsicPolyFun [tp_a]
[arr_a, arr_a] uarr_a),
("rotate", IntrinsicPolyFun [tp_a]
[Scalar $ Prim $ Signed Int32, arr_a] arr_a),
("transpose", IntrinsicPolyFun [tp_a] [arr_2d_a] arr_2d_a),
("cmp_threshold", IntrinsicPolyFun []
[Scalar $ Prim $ Signed Int32,
Array () Nonunique (Prim $ Signed Int32) (rank 1)] $
Scalar $ Prim Bool),
("scatter", IntrinsicPolyFun [tp_a]
[Array () Unique t_a (rank 1),
Array () Nonunique (Prim $ Signed Int32) (rank 1),
Array () Nonunique t_a (rank 1)] $
Array () Unique t_a (rank 1)),
("zip", IntrinsicPolyFun [tp_a, tp_b] [arr_a, arr_b] arr_a_b),
("unzip", IntrinsicPolyFun [tp_a, tp_b] [arr_a_b] t_arr_a_arr_b),
("gen_reduce", IntrinsicPolyFun [tp_a]
[uarr_a,
Scalar t_a `arr` (Scalar t_a `arr` Scalar t_a),
Scalar t_a,
Array () Nonunique (Prim $ Signed Int32) (rank 1),
arr_a]
uarr_a),
("map", IntrinsicPolyFun [tp_a, tp_b] [Scalar t_a `arr` Scalar t_b, arr_a] uarr_b),
("reduce", IntrinsicPolyFun [tp_a]
[Scalar t_a `arr` (Scalar t_a `arr` Scalar t_a), Scalar t_a, arr_a] $
Scalar t_a),
("reduce_comm", IntrinsicPolyFun [tp_a]
[Scalar t_a `arr` (Scalar t_a `arr` Scalar t_a), Scalar t_a, arr_a] $
Scalar t_a),
("scan", IntrinsicPolyFun [tp_a]
[Scalar t_a `arr` (Scalar t_a `arr` Scalar t_a), Scalar t_a, arr_a] uarr_a),
("partition",
IntrinsicPolyFun [tp_a]
[Scalar (Prim $ Signed Int32),
Scalar t_a `arr` Scalar (Prim $ Signed Int32), arr_a] $
tupleRecord [uarr_a, Array () Unique (Prim $ Signed Int32) (rank 1)]),
("map_stream",
IntrinsicPolyFun [tp_a, tp_b]
[Scalar (Prim $ Signed Int32) `arr` (arr_a `arr` arr_b), arr_a]
uarr_b),
("map_stream_per",
IntrinsicPolyFun [tp_a, tp_b]
[Scalar (Prim $ Signed Int32) `arr` (arr_a `arr` arr_b), arr_a]
uarr_b),
("reduce_stream",
IntrinsicPolyFun [tp_a, tp_b]
[Scalar t_b `arr` (Scalar t_b `arr` Scalar t_b),
Scalar (Prim $ Signed Int32) `arr` (arr_a `arr` Scalar t_b),
arr_a] $
Scalar t_b),
("reduce_stream_per",
IntrinsicPolyFun [tp_a, tp_b]
[Scalar t_b `arr` (Scalar t_b `arr` Scalar t_b),
Scalar (Prim $ Signed Int32) `arr` (arr_a `arr` Scalar t_b),
arr_a] $
Scalar t_b),
("trace", IntrinsicPolyFun [tp_a] [Scalar t_a] $ Scalar t_a),
("break", IntrinsicPolyFun [tp_a] [Scalar t_a] $ Scalar t_a)]
where tv_a = VName (nameFromString "a") 0
t_a = TypeVar () Nonunique (typeName tv_a) []
arr_a = Array () Nonunique t_a (rank 1)
arr_2d_a = Array () Nonunique t_a (rank 2)
uarr_a = Array () Unique t_a (rank 1)
tp_a = TypeParamType Unlifted tv_a noLoc
tv_b = VName (nameFromString "b") 1
t_b = TypeVar () Nonunique (typeName tv_b) []
arr_b = Array () Nonunique t_b (rank 1)
uarr_b = Array () Unique t_b (rank 1)
tp_b = TypeParamType Unlifted tv_b noLoc
arr_a_b = Array () Nonunique
(Record (M.fromList $ zip tupleFieldNames [Scalar t_a, Scalar t_b]))
(rank 1)
t_arr_a_arr_b = Scalar $ Record $ M.fromList $ zip tupleFieldNames [arr_a, arr_b]
arr x y = Scalar $ Arrow mempty Unnamed x y
namify i (k,v) = (VName (nameFromString k) i, v)
primFun (name, (ts,t, _)) =
(name, IntrinsicMonoFun (map unPrim ts) $ unPrim t)
unOpFun bop = (pretty bop, IntrinsicMonoFun [t] t)
where t = unPrim $ Primitive.unOpType bop
binOpFun bop = (pretty bop, IntrinsicMonoFun [t, t] t)
where t = unPrim $ Primitive.binOpType bop
cmpOpFun bop = (pretty bop, IntrinsicMonoFun [t, t] Bool)
where t = unPrim $ Primitive.cmpOpType bop
convOpFun cop = (pretty cop, IntrinsicMonoFun [unPrim ft] $ unPrim tt)
where (ft, tt) = Primitive.convOpType cop
signFun t = ("sign_" ++ pretty t, IntrinsicMonoFun [Unsigned t] $ Signed t)
unsignFun t = ("unsign_" ++ pretty t, IntrinsicMonoFun [Signed t] $ Unsigned t)
unPrim (Primitive.IntType t) = Signed t
unPrim (Primitive.FloatType t) = FloatType t
unPrim Primitive.Bool = Bool
unPrim Primitive.Cert = Bool
intrinsicType t = (pretty t, IntrinsicType t)
anyIntType = map Signed [minBound..maxBound] ++
map Unsigned [minBound..maxBound]
anyNumberType = anyIntType ++
map FloatType [minBound..maxBound]
anyPrimType = Bool : anyNumberType
mkIntrinsicBinOp :: BinOp -> Maybe (String, Intrinsic)
mkIntrinsicBinOp op = do op' <- intrinsicBinOp op
return (pretty op, op')
binOp ts = Just $ IntrinsicOverloadedFun ts [Nothing, Nothing] Nothing
ordering = Just $ IntrinsicOverloadedFun anyPrimType [Nothing, Nothing] (Just Bool)
intrinsicBinOp Plus = binOp anyNumberType
intrinsicBinOp Minus = binOp anyNumberType
intrinsicBinOp Pow = binOp anyNumberType
intrinsicBinOp Times = binOp anyNumberType
intrinsicBinOp Divide = binOp anyNumberType
intrinsicBinOp Mod = binOp anyNumberType
intrinsicBinOp Quot = binOp anyIntType
intrinsicBinOp Rem = binOp anyIntType
intrinsicBinOp ShiftR = binOp anyIntType
intrinsicBinOp ShiftL = binOp anyIntType
intrinsicBinOp Band = binOp anyIntType
intrinsicBinOp Xor = binOp anyIntType
intrinsicBinOp Bor = binOp anyIntType
intrinsicBinOp LogAnd = Just $ IntrinsicMonoFun [Bool,Bool] Bool
intrinsicBinOp LogOr = Just $ IntrinsicMonoFun [Bool,Bool] Bool
intrinsicBinOp Equal = Just IntrinsicEquality
intrinsicBinOp NotEqual = Just IntrinsicEquality
intrinsicBinOp Less = ordering
intrinsicBinOp Leq = ordering
intrinsicBinOp Greater = ordering
intrinsicBinOp Geq = ordering
intrinsicBinOp _ = Nothing
maxIntrinsicTag :: Int
maxIntrinsicTag = maximum $ map baseTag $ M.keys intrinsics
qualName :: v -> QualName v
qualName = QualName []
qualify :: v -> QualName v -> QualName v
qualify k (QualName ks v) = QualName (k:ks) v
typeName :: VName -> TypeName
typeName = typeNameFromQualName . qualName
progImports :: ProgBase f vn -> [(String,SrcLoc)]
progImports = concatMap decImports . progDecs
decImports :: DecBase f vn -> [(String,SrcLoc)]
decImports (OpenDec x _) = modExpImports x
decImports (ModDec md) = modExpImports $ modExp md
decImports SigDec{} = []
decImports TypeDec{} = []
decImports ValDec{} = []
decImports (LocalDec d _) = decImports d
decImports (ImportDec x _ loc) = [(x, loc)]
modExpImports :: ModExpBase f vn -> [(String,SrcLoc)]
modExpImports ModVar{} = []
modExpImports (ModParens p _) = modExpImports p
modExpImports (ModImport f _ loc) = [(f,loc)]
modExpImports (ModDecs ds _) = concatMap decImports ds
modExpImports (ModApply _ me _ _ _) = modExpImports me
modExpImports (ModAscript me _ _ _) = modExpImports me
modExpImports ModLambda{} = []
progModuleTypes :: Ord vn => ProgBase f vn -> S.Set vn
progModuleTypes = mconcat . map onDec . progDecs
where onDec (OpenDec x _) = onModExp x
onDec (ModDec md) =
maybe mempty (onSigExp . fst) (modSignature md) <> onModExp (modExp md)
onDec SigDec{} = mempty
onDec TypeDec{} = mempty
onDec ValDec{} = mempty
onDec LocalDec{} = mempty
onDec ImportDec{} = mempty
onModExp ModVar{} = mempty
onModExp (ModParens p _) = onModExp p
onModExp ModImport {} = mempty
onModExp (ModDecs ds _) = mconcat $ map onDec ds
onModExp (ModApply me1 me2 _ _ _) = onModExp me1 <> onModExp me2
onModExp (ModAscript me se _ _) = onModExp me <> onSigExp se
onModExp (ModLambda p r me _) =
onModParam p <> maybe mempty (onSigExp . fst) r <> onModExp me
onModParam = onSigExp . modParamType
onSigExp (SigVar v _) = S.singleton $ qualLeaf v
onSigExp (SigParens e _) = onSigExp e
onSigExp SigSpecs{} = mempty
onSigExp (SigWith e _ _) = onSigExp e
onSigExp (SigArrow _ e1 e2 _) = onSigExp e1 <> onSigExp e2
identifierReference :: String -> Maybe ((String, String, Maybe FilePath), String)
identifierReference ('`' : s)
| (identifier, '`' : '@' : s') <- break (=='`') s,
(namespace, s'') <- span isAlpha s',
not $ null namespace =
case s'' of
'@' : '"' : s'''
| (file, '"' : s'''') <- span (/= '"') s''' ->
Just ((identifier, namespace, Just file), s'''')
_ -> Just ((identifier, namespace, Nothing), s'')
identifierReference _ = Nothing
identifierReferences :: String -> [(String, String, Maybe FilePath)]
identifierReferences [] = []
identifierReferences s
| Just (ref, s') <- identifierReference s =
ref : identifierReferences s'
identifierReferences (_:s') =
identifierReferences s'
leadingOperator :: Name -> BinOp
leadingOperator s = maybe Backtick snd $ find ((`isPrefixOf` s') . fst) $
sortBy (flip $ comparing $ length . fst) $
zip (map pretty operators) operators
where s' = nameToString s
operators :: [BinOp]
operators = [minBound..maxBound::BinOp]
type UncheckedType = TypeBase (ShapeDecl Name) ()
type UncheckedTypeExp = TypeExp Name
type UncheckedTypeDecl = TypeDeclBase NoInfo Name
type UncheckedIdent = IdentBase NoInfo Name
type UncheckedDimIndex = DimIndexBase NoInfo Name
type UncheckedExp = ExpBase NoInfo Name
type UncheckedModExp = ModExpBase NoInfo Name
type UncheckedSigExp = SigExpBase NoInfo Name
type UncheckedTypeParam = TypeParamBase Name
type UncheckedPattern = PatternBase NoInfo Name
type UncheckedValBind = ValBindBase NoInfo Name
type UncheckedDec = DecBase NoInfo Name
type UncheckedProg = ProgBase NoInfo Name
type UncheckedCase = CaseBase NoInfo Name