Skip to content
Snippets Groups Projects
Commit d1cc991f authored by Adam Gundry's avatar Adam Gundry
Browse files

WIP: beginning of a module with a concrete representation of Type and conversions

parent 01d92e0c
Branches wip/T2-views
No related tags found
1 merge request!2Draft: Initial WIP towards views (#2)
-- | This module defines a concrete representation of the syntax of types, and
-- provides conversion functions between the concrete representation and the
-- compiler-internal representation (which is intended to remain abstract).
--
module TemplateHaskell.Syntax.Datatypes.Type
( Type(..)
, Specificity(..)
, TyVarBndr(..)
, Kind
, SumArity
, Cxt
, Pred
, reifyType
, reflectType
)
where
import Data.Data (Data)
import GHC.Generics (Generic)
import GHC.Internal.TH.Syntax (Name, Q)
import qualified GHC.Internal.TH.Syntax as Internal
import qualified GHC.Internal.TH.Ppr as Ppr
-- | A Haskell type.
data Type = ForallT [TyVarBndr Specificity] Cxt Type -- ^ @forall \<vars\>. \<ctxt\> => \<type\>@
| ForallVisT [TyVarBndr ()] Type -- ^ @forall \<vars\> -> \<type\>@
| AppT Type Type -- ^ @T a b@
| AppKindT Type Kind -- ^ @T \@k t@
| SigT Type Kind -- ^ @t :: k@
| VarT Name -- ^ @a@
| ConT Name -- ^ @T@
| PromotedT Name -- ^ @'T@
| InfixT Type Name Type -- ^ @T + T@
| UInfixT Type Name Type -- ^ @T + T@
--
-- See "Language.Haskell.TH.Syntax#infix"
| PromotedInfixT Type Name Type -- ^ @T :+: T@
| PromotedUInfixT Type Name Type -- ^ @T :+: T@
--
-- See "Language.Haskell.TH.Syntax#infix"
| ParensT Type -- ^ @(T)@
-- See Note [Representing concrete syntax in types]
| TupleT Int -- ^ @(,)@, @(,,)@, etc.
| UnboxedTupleT Int -- ^ @(\#,\#)@, @(\#,,\#)@, etc.
| UnboxedSumT SumArity -- ^ @(\#|\#)@, @(\#||\#)@, etc.
| ArrowT -- ^ @->@
| MulArrowT -- ^ @%n ->@
--
-- Generalised arrow type with multiplicity argument
| EqualityT -- ^ @~@
| ListT -- ^ @[]@
| PromotedTupleT Int -- ^ @'()@, @'(,)@, @'(,,)@, etc.
| PromotedNilT -- ^ @'[]@
| PromotedConsT -- ^ @'(:)@
| StarT -- ^ @*@
| ConstraintT -- ^ @Constraint@
| LitT TyLit -- ^ @0@, @1@, @2@, etc.
| WildCardT -- ^ @_@
| ImplicitParamT String Type -- ^ @?x :: t@
deriving( Show, Eq, Ord, Data, Generic )
-- | The specificity of a type variable in a @forall ...@.
data Specificity = SpecifiedSpec -- ^ @a@
| InferredSpec -- ^ @{a}@
deriving( Show, Eq, Ord, Data, Generic )
-- | The @flag@ type parameter is instantiated to one of the following types:
--
-- * 'Specificity' (examples: 'ForallC', 'ForallT')
-- * 'BndrVis' (examples: 'DataD', 'ClassD', etc.)
-- * '()', a catch-all type for other forms of binders, including 'ForallVisT', 'DataInstD', 'RuleP', and 'TyVarSig'
--
data TyVarBndr flag = PlainTV Name flag -- ^ @a@
| KindedTV Name flag Kind -- ^ @(a :: k)@
deriving( Show, Eq, Ord, Data, Generic, Functor, Foldable, Traversable )
-- | To avoid duplication between kinds and types, they
-- are defined to be the same. Naturally, you would never
-- have a type be 'StarT' and you would never have a kind
-- be 'SigT', but many of the other constructors are shared.
-- Note that the kind @Bool@ is denoted with 'ConT', not
-- 'PromotedT'. Similarly, tuple kinds are made with 'TupleT',
-- not 'PromotedTupleT'.
type Kind = Type
-- | In 'UnboxedSumE', 'UnboxedSumT', and 'UnboxedSumP', the total number of
-- 'SumAlt's. For example, @(\#|\#)@ has a 'SumArity' of 2.
type SumArity = Int
-- | A context, as found on the left side of a @=>@ in a type.
type Cxt = [Pred] -- ^ @(Eq a, Ord b)@
-- | Since the advent of @ConstraintKinds@, constraints are really just types.
-- Equality constraints use the 'EqualityT' constructor. Constraints may also
-- be tuples of other constraints.
type Pred = Type
-- | Type-level literals.
data TyLit = NumTyLit Integer -- ^ @2@
| StrTyLit String -- ^ @\"Hello\"@
| CharTyLit Char -- ^ @\'C\'@, @since 4.16.0.0
deriving ( Show, Eq, Ord, Data, Generic )
-- | Convert the internal abstract representation of types used by the compiler
-- into a concrete 'Type'.
--
-- This may fail if the type being reified uses a newly-introduced feature that
-- is not representable in the concrete representation supported by this version
-- of the library.
--
reifyType :: MonadFail m => Internal.Type -> m Type
reifyType (Internal.VarT n) = pure $ VarT n
reifyType t = fail $ "reifyType does not support: " ++ Ppr.pprint t
-- | Convert a concrete 'Type' to the internal abstract representation of types
-- used by the compiler.
--
-- This may fail if the type being reflected uses a deprecated feature that is
-- no longer representable in the abstract representation.
--
reflectType :: MonadFail m => Type -> m Internal.Type
reflectType (VarT n) = pure $ Internal.VarT n
......@@ -42,6 +42,7 @@ Library
TemplateHaskell.Splice
TemplateHaskell.Syntax
TemplateHaskell.Syntax.Datatypes
TemplateHaskell.Syntax.Datatypes.Type
TemplateHaskell.Syntax.Monadic
TemplateHaskell.Typed
TemplateHaskell.Typed.CodeDo
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment