Commit 67465497 authored by eir@cis.upenn.edu's avatar eir@cis.upenn.edu

Add kind equalities to GHC.

This implements the ideas originally put forward in
"System FC with Explicit Kind Equality" (ICFP'13).

There are several noteworthy changes with this patch:
 * We now have casts in types. These change the kind
   of a type. See new constructor `CastTy`.

 * All types and all constructors can be promoted.
   This includes GADT constructors. GADT pattern matches
   take place in type family equations. In Core,
   types can now be applied to coercions via the
   `CoercionTy` constructor.

 * Coercions can now be heterogeneous, relating types
   of different kinds. A coercion proving `t1 :: k1 ~ t2 :: k2`
   proves both that `t1` and `t2` are the same and also that
   `k1` and `k2` are the same.

 * The `Coercion` type has been significantly enhanced.
   The documentation in `docs/core-spec/core-spec.pdf` reflects
   the new reality.

 * The type of `*` is now `*`. No more `BOX`.

 * Users can write explicit kind variables in their code,
   anywhere they can write type variables. For backward compatibility,
   automatic inference of kind-variable binding is still permitted.

 * The new extension `TypeInType` turns on the new user-facing
   features.

 * Type families and synonyms are now promoted to kinds. This causes
   trouble with parsing `*`, leading to the somewhat awkward new
   `HsAppsTy` constructor for `HsType`. This is dispatched with in
   the renamer, where the kind `*` can be told apart from a
   type-level multiplication operator. Without `-XTypeInType` the
   old behavior persists. With `-XTypeInType`, you need to import
   `Data.Kind` to get `*`, also known as `Type`.

 * The kind-checking algorithms in TcHsType have been significantly
   rewritten to allow for enhanced kinds.

 * The new features are still quite experimental and may be in flux.

 * TODO: Several open tickets: #11195, #11196, #11197, #11198, #11203.

 * TODO: Update user manual.

Tickets addressed: #9017, #9173, #7961, #10524, #8566, #11142.
Updates Haddock submodule.
parent 6e56ac58

Too many changes to show.

To preserve performance only 192 of 192+ files are displayed.
[submodule "libraries/binary"]
path = libraries/binary
url = ../packages/binary.git
url = http://git.haskell.org/packages/binary.git
ignore = untracked
[submodule "libraries/bytestring"]
path = libraries/bytestring
url = ../packages/bytestring.git
url = http://git.haskell.org/packages/bytestring.git
ignore = untracked
[submodule "libraries/Cabal"]
path = libraries/Cabal
url = ../packages/Cabal.git
url = http://git.haskell.org/packages/Cabal.git
ignore = untracked
[submodule "libraries/containers"]
path = libraries/containers
url = ../packages/containers.git
url = http://git.haskell.org/packages/containers.git
ignore = untracked
[submodule "libraries/haskeline"]
path = libraries/haskeline
url = ../packages/haskeline.git
url = http://git.haskell.org/packages/haskeline.git
ignore = untracked
[submodule "libraries/pretty"]
path = libraries/pretty
url = ../packages/pretty.git
url = http://git.haskell.org/packages/pretty.git
ignore = untracked
[submodule "libraries/terminfo"]
path = libraries/terminfo
url = ../packages/terminfo.git
url = http://git.haskell.org/packages/terminfo.git
ignore = untracked
[submodule "libraries/transformers"]
path = libraries/transformers
url = ../packages/transformers.git
url = http://git.haskell.org/packages/transformers.git
ignore = untracked
[submodule "libraries/xhtml"]
path = libraries/xhtml
url = ../packages/xhtml.git
url = http://git.haskell.org/packages/xhtml.git
ignore = untracked
[submodule "libraries/Win32"]
path = libraries/Win32
url = ../packages/Win32.git
url = http://git.haskell.org/packages/Win32.git
ignore = untracked
[submodule "libraries/primitive"]
path = libraries/primitive
url = ../packages/primitive.git
url = http://git.haskell.org/packages/primitive.git
ignore = untracked
[submodule "libraries/vector"]
path = libraries/vector
url = ../packages/vector.git
url = http://git.haskell.org/packages/vector.git
ignore = untracked
[submodule "libraries/time"]
path = libraries/time
url = ../packages/time.git
url = http://git.haskell.org/packages/time.git
ignore = untracked
[submodule "libraries/random"]
path = libraries/random
url = ../packages/random.git
url = http://git.haskell.org/packages/random.git
ignore = untracked
[submodule "libraries/array"]
path = libraries/array
url = ../packages/array.git
url = http://git.haskell.org/packages/array.git
ignore = none
[submodule "libraries/deepseq"]
path = libraries/deepseq
url = ../packages/deepseq.git
url = http://git.haskell.org/packages/deepseq.git
ignore = none
[submodule "libraries/directory"]
path = libraries/directory
url = ../packages/directory.git
url = http://git.haskell.org/packages/directory.git
ignore = none
[submodule "libraries/filepath"]
path = libraries/filepath
url = ../packages/filepath.git
url = http://git.haskell.org/packages/filepath.git
ignore = none
[submodule "libraries/hoopl"]
path = libraries/hoopl
url = ../packages/hoopl.git
url = http://git.haskell.org/packages/hoopl.git
ignore = none
[submodule "libraries/hpc"]
path = libraries/hpc
url = ../packages/hpc.git
url = http://git.haskell.org/packages/hpc.git
ignore = none
[submodule "libraries/process"]
path = libraries/process
url = ../packages/process.git
url = http://git.haskell.org/packages/process.git
ignore = none
[submodule "libraries/unix"]
path = libraries/unix
url = ../packages/unix.git
url = http://git.haskell.org/packages/unix.git
ignore = none
[submodule "libraries/parallel"]
path = libraries/parallel
url = ../packages/parallel.git
url = http://git.haskell.org/packages/parallel.git
ignore = none
[submodule "libraries/stm"]
path = libraries/stm
url = ../packages/stm.git
url = http://git.haskell.org/packages/stm.git
ignore = none
[submodule "libraries/dph"]
path = libraries/dph
url = ../packages/dph.git
url = http://git.haskell.org/packages/dph.git
ignore = none
[submodule "utils/haddock"]
path = utils/haddock
url = ../haddock.git
url = http://git.haskell.org/haddock.git
ignore = none
branch = ghc-head
[submodule "nofib"]
path = nofib
url = ../nofib.git
url = http://git.haskell.org/nofib.git
ignore = none
[submodule "utils/hsc2hs"]
path = utils/hsc2hs
url = ../hsc2hs.git
url = http://git.haskell.org/hsc2hs.git
ignore = none
[submodule "libffi-tarballs"]
path = libffi-tarballs
url = ../libffi-tarballs.git
url = http://git.haskell.org/libffi-tarballs.git
ignore = none
[submodule ".arc-linters/arcanist-external-json-linter"]
path = .arc-linters/arcanist-external-json-linter
......
The Glasgow Haskell Compiler
============================
Dependent Types Branch of GHC
=============================
[![Build Status](https://api.travis-ci.org/ghc/ghc.svg?branch=master)](http://travis-ci.org/ghc/ghc)
This is a fork of GHC, with work toward supporting dependent types.
Anyone is welcome to download and play with this implementation,
and I am happy to receive feedback and issue reports on GitHub.
This is the source tree for [GHC][1], a compiler and interactive
environment for the Haskell functional programming language.
There are two options of using this branch: manual, and Nix-based.
For more information, visit [GHC's web site][1].
Manual
------
This code should build, but I have tested it only on `DEBUG` settings;
I recommend using build style `devel2` in `build.mk`.
Here is a minimal script you can follow to build this at home;
see the [GHC Building Guide] [3] for more info.
~~~
git clone https://github.com/goldfirere/ghc.git
cd ghc
git checkout nokinds
git remote set-url origin git://git.haskell.org/ghc.git # so submodules work
git submodule update --init
cd mk
cp build.mk.sample build.mk
## edit build.mk to uncomment the line to choose the `devel2` configuration
cd ..
perl boot
./configure
make
~~~
Check out the `testsuite/tests/dependent/should_compile` directory for
a few sample programs that should compile on this fork of GHC.
For more information about GHC, visit [GHC's web site][1].
Information for developers of GHC can be found on the [GHC Trac][2].
Nix-based
---------
Thanks to @deepfire, this branch is available in Nixpkgs, which means that with
some effort it can be fairly automatically employed to build any package from
Hackage. This way, though, requires that one installs the Nix package manager in
parallel with the system package manager -- and this option is currently
unavailable on Windows.
Here are the instructions:
1. To install the Nix package manager, taking over /nix for package storage:
curl https://nixos.org/nix/install | sh
2. Make Nix use the `master` repository of Nixpkgs package definitions:
Getting the Source
==================
git clone https://github.com/NixOS/nixpkgs.git
pushd ~/.nix-defexpr
rm -rf channels
ln -s ../nixpkgs
popd
echo 'export NIX_PATH=nixpkgs=/home/---<USERNAME>---/nixpkgs' >> ~/.bashrc
export NIX_PATH=nixpkgs=/home/---<USERNAME>---/nixpkgs
There are two ways to get a source tree:
3. [OPTIONAL] To enable prebuilt binaries from Peter Simons/NixOS Hydra servers:
1. *Download source tarballs*
sudo mkdir /etc/nix
echo 'binary-caches = http://hydra.nixos.org/ http://hydra.cryp.to/' | sudo dd of=/etc/nix/nix.conf
Download the GHC source distribution:
# If you don't do that, everything will still work, just it'll have
# to build everything from source.
ghc-<version>-src.tar.bz2
4. Enter a shell with `ghc-nokinds` available:
which contains GHC itself and the "boot" libraries.
nix-shell -p haskell.compiler.ghcNokinds
2. *Check out the source code from git*
5. See it's indeed `nokinds`:
$ git clone --recursive git://git.haskell.org/ghc.git
wget https://raw.githubusercontent.com/goldfirere/ghc/nokinds/testsuite/tests/dependent/should_compile/KindEqualities2.hs
runhaskell KindEqualities2.hs
Note: cloning GHC from Github requires a special setup. See [Getting a GHC
repository from Github] [7].
To apply 'nokinds' to building packages from Hackage, the best option would be
to follow instructions from the "Nix loves Haskell" talk by Peter Simons:
**DO NOT submit pull request directly to the github repo.**
*See the GHC team's working conventions re [how to contribute a patch to GHC](http://ghc.haskell.org/trac/ghc/wiki/WorkingConventions/FixingBugs "ghc.haskell.org/trac/ghc/wiki/WorkingConventions/FixingBug").*
http://cryp.to/nixos-meetup-3-slides.pdf
..where the relevant compiler name would be "ghcNokinds".
Building & Installing
=====================
......
......@@ -413,6 +413,10 @@ isBoxed :: Boxity -> Bool
isBoxed Boxed = True
isBoxed Unboxed = False
instance Outputable Boxity where
ppr Boxed = text "Boxed"
ppr Unboxed = text "Unboxed"
{-
************************************************************************
* *
......
......@@ -32,7 +32,7 @@ import Unique
import Util
import Name
import BasicTypes
import TypeRep (Type, ThetaType)
import TyCoRep (Type, ThetaType)
import Var
import Type (mkTyConApp)
......@@ -169,7 +169,7 @@ conLikeResTy (PatSynCon ps) tys = patSynInstResTy ps tys
--
-- 7) The original result type
conLikeFullSig :: ConLike
-> ([TyVar], [TyVar], [(TyVar,Type)]
-> ([TyVar], [TyVar], [EqSpec]
, ThetaType, ThetaType, [Type], Type)
conLikeFullSig (RealDataCon con) =
let (univ_tvs, ex_tvs, eq_spec, theta, arg_tys, res_ty) = dataConFullSig con
......
......@@ -15,19 +15,23 @@ module DataCon (
StrictnessMark(..),
ConTag,
-- ** Equality specs
EqSpec, mkEqSpec, eqSpecTyVar, eqSpecType,
eqSpecPair, eqSpecPreds,
substEqSpec,
-- ** Field labels
FieldLbl(..), FieldLabel, FieldLabelString,
-- ** Type construction
mkDataCon, fIRST_TAG,
buildAlgTyCon,
mkDataCon, buildAlgTyCon, fIRST_TAG,
-- ** Type deconstruction
dataConRepType, dataConSig, dataConInstSig, dataConFullSig,
dataConName, dataConIdentity, dataConTag, dataConTyCon,
dataConOrigTyCon, dataConUserType,
dataConUnivTyVars, dataConExTyVars, dataConAllTyVars,
dataConEqSpec, eqSpecPreds, dataConTheta,
dataConEqSpec, dataConTheta,
dataConStupidTheta,
dataConInstArgTys, dataConOrigArgTys, dataConOrigResTy,
dataConInstOrigArgTys, dataConRepArgTys,
......@@ -45,37 +49,32 @@ module DataCon (
isNullarySrcDataCon, isNullaryRepDataCon, isTupleDataCon, isUnboxedTupleCon,
isVanillaDataCon, classDataCon, dataConCannotMatch,
isBanged, isMarkedStrict, eqHsBang, isSrcStrict, isSrcUnpacked,
specialPromotedDc, isLegacyPromotableDataCon, isLegacyPromotableTyCon,
-- ** Promotion related functions
promoteDataCon, promoteDataCon_maybe,
promoteType, promoteKind,
isPromotableType, computeTyConPromotability,
promoteDataCon
) where
#include "HsVersions.h"
import {-# SOURCE #-} MkId( DataConBoxer )
import Type
import ForeignCall( CType )
import TypeRep( Type(..) ) -- Used in promoteType
import PrelNames( liftedTypeKindTyConKey )
import ForeignCall ( CType )
import Coercion
import Kind
import Unify
import TyCon
import FieldLabel
import Class
import Name
import PrelNames
import NameEnv
import Var
import Outputable
import Unique
import ListSetOps
import Util
import BasicTypes
import FastString
import Module
import VarEnv
import NameSet
import Binary
import qualified Data.Data as Data
......@@ -299,11 +298,11 @@ data DataCon
-- syntax, provided its type looks like the above.
-- The declaration format is held in the TyCon (algTcGadtSyntax)
dcUnivTyVars :: [TyVar], -- Universally-quantified type vars [a,b,c]
dcUnivTyVars :: [TyVar], -- Universally-quantified type vars [a,b,c]
-- INVARIANT: length matches arity of the dcRepTyCon
--- result type of (rep) data con is exactly (T a b c)
dcExTyVars :: [TyVar], -- Existentially-quantified type vars
dcExTyVars :: [TyVar], -- Existentially-quantified type vars
-- In general, the dcUnivTyVars are NOT NECESSARILY THE SAME AS THE TYVARS
-- FOR THE PARENT TyCon. With GADTs the data con might not even have
-- the same number of type variables.
......@@ -313,8 +312,9 @@ data DataCon
-- INVARIANT: the UnivTyVars and ExTyVars all have distinct OccNames
-- Reason: less confusing, and easier to generate IfaceSyn
dcEqSpec :: [(TyVar,Type)], -- Equalities derived from the result type,
-- _as written by the programmer_
dcEqSpec :: [EqSpec], -- Equalities derived from the result type,
-- _as written by the programmer_
-- This field allows us to move conveniently between the two ways
-- of representing a GADT constructor's type:
-- MkT :: forall a b. (a ~ [b]) => b -> T a
......@@ -377,8 +377,10 @@ data DataCon
dcRep :: DataConRep,
-- Cached
dcRepArity :: Arity, -- == length dataConRepArgTys
dcSourceArity :: Arity, -- == length dcOrigArgTys
-- dcRepArity == length dataConRepArgTys
dcRepArity :: Arity,
-- dcSourceArity == length dcOrigArgTys
dcSourceArity :: Arity,
-- Result type of constructor is T t1..tn
dcRepTyCon :: TyCon, -- Result tycon, T
......@@ -402,8 +404,8 @@ data DataCon
-- Used for Template Haskell and 'deriving' only
-- The actual fixity is stored elsewhere
dcPromoted :: Promoted TyCon -- The promoted TyCon if this DataCon is promotable
-- See Note [Promoted data constructors] in TyCon
dcPromoted :: TyCon -- The promoted TyCon
-- See Note [Promoted data constructors] in TyCon
}
deriving Data.Typeable.Typeable
......@@ -496,6 +498,40 @@ data SrcUnpackedness = SrcUnpack -- ^ {-# UNPACK #-} specified
-- of the DataCon *worker* fields
data StrictnessMark = MarkedStrict | NotMarkedStrict
-- | An 'EqSpec' is a tyvar/type pair representing an equality made in
-- rejigging a GADT constructor
data EqSpec = EqSpec TyVar
Type
-- | Make an 'EqSpec'
mkEqSpec :: TyVar -> Type -> EqSpec
mkEqSpec tv ty = EqSpec tv ty
eqSpecTyVar :: EqSpec -> TyVar
eqSpecTyVar (EqSpec tv _) = tv
eqSpecType :: EqSpec -> Type
eqSpecType (EqSpec _ ty) = ty
eqSpecPair :: EqSpec -> (TyVar, Type)
eqSpecPair (EqSpec tv ty) = (tv, ty)
eqSpecPreds :: [EqSpec] -> ThetaType
eqSpecPreds spec = [ mkPrimEqPred (mkTyVarTy tv) ty
| EqSpec tv ty <- spec ]
-- | Substitute in an 'EqSpec'. Precondition: if the LHS of the EqSpec
-- is mapped in the substitution, it is mapped to a type variable, not
-- a full type.
substEqSpec :: TCvSubst -> EqSpec -> EqSpec
substEqSpec subst (EqSpec tv ty)
= EqSpec tv' (substTy subst ty)
where
tv' = getTyVar "substEqSpec" (substTyVar subst tv)
instance Outputable EqSpec where
ppr (EqSpec tv ty) = ppr (tv, ty)
{- Note [Bangs on data constructor arguments]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
......@@ -674,14 +710,13 @@ isMarkedStrict _ = True -- All others are strict
-- | Build a new data constructor
mkDataCon :: Name
-> Bool -- ^ Is the constructor declared infix?
-> Promoted TyConRepName -- ^ Whether promoted, and if so the TyConRepName
-- for the promoted TyCon
-> TyConRepName -- ^ TyConRepName for the promoted TyCon
-> [HsSrcBang] -- ^ Strictness/unpack annotations, from user
-> [FieldLabel] -- ^ Field labels for the constructor,
-- if it is a record, otherwise empty
-> [TyVar] -- ^ Universally quantified type variables
-> [TyVar] -- ^ Existentially quantified type variables
-> [(TyVar,Type)] -- ^ GADT equalities
-> [EqSpec] -- ^ GADT equalities
-> ThetaType -- ^ Theta-type occuring before the arguments proper
-> [Type] -- ^ Original argument types
-> Type -- ^ Original result type
......@@ -725,7 +760,7 @@ mkDataCon name declared_infix prom_info
dcRep = rep,
dcSourceArity = length orig_arg_tys,
dcRepArity = length rep_arg_tys,
dcPromoted = mb_promoted }
dcPromoted = promoted }
-- The 'arg_stricts' passed to mkDataCon are simply those for the
-- source-language arguments. We add extra ones for the
......@@ -733,20 +768,15 @@ mkDataCon name declared_infix prom_info
tag = assoc "mkDataCon" (tyConDataCons rep_tycon `zip` [fIRST_TAG..]) con
rep_arg_tys = dataConRepArgTys con
rep_ty = mkForAllTys univ_tvs $ mkForAllTys ex_tvs $
rep_ty = mkInvForAllTys univ_tvs $ mkInvForAllTys ex_tvs $
mkFunTys rep_arg_tys $
mkTyConApp rep_tycon (mkTyVarTys univ_tvs)
mb_promoted -- See Note [Promoted data constructors] in TyCon
= case prom_info of
NotPromoted -> NotPromoted
Promoted rep_nm -> Promoted (mkPromotedDataCon con name rep_nm prom_kind prom_roles)
prom_kind = promoteType (dataConUserType con)
prom_roles = map (const Nominal) (univ_tvs ++ ex_tvs) ++
map (const Representational) orig_arg_tys
promoted -- See Note [Promoted data constructors] in TyCon
= mkPromotedDataCon con name prom_info (dataConUserType con) roles
eqSpecPreds :: [(TyVar,Type)] -> ThetaType
eqSpecPreds spec = [ mkEqPred (mkTyVarTy tv) ty | (tv,ty) <- spec ]
roles = map (const Nominal) (univ_tvs ++ ex_tvs) ++
map (const Representational) orig_arg_tys
-- | The 'Name' of the 'DataCon', giving it a unique, rooted identification
dataConName :: DataCon -> Name
......@@ -791,11 +821,30 @@ dataConAllTyVars (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs })
= univ_tvs ++ ex_tvs
-- | Equalities derived from the result type of the data constructor, as written
-- by the programmer in any GADT declaration
dataConEqSpec :: DataCon -> [(TyVar,Type)]
dataConEqSpec = dcEqSpec
-- | The *full* constraints on the constructor type
-- by the programmer in any GADT declaration. This includes *all* GADT-like
-- equalities, including those written in by hand by the programmer.
dataConEqSpec :: DataCon -> [EqSpec]
dataConEqSpec (MkData { dcEqSpec = eq_spec, dcOtherTheta = theta })
= eq_spec ++
[ spec -- heterogeneous equality
| Just (tc, [_k1, _k2, ty1, ty2]) <- map splitTyConApp_maybe theta
, tc `hasKey` heqTyConKey
, spec <- case (getTyVar_maybe ty1, getTyVar_maybe ty2) of
(Just tv1, _) -> [mkEqSpec tv1 ty2]
(_, Just tv2) -> [mkEqSpec tv2 ty1]
_ -> []
] ++
[ spec -- homogeneous equality
| Just (tc, [_k, ty1, ty2]) <- map splitTyConApp_maybe theta
, tc `hasKey` eqTyConKey
, spec <- case (getTyVar_maybe ty1, getTyVar_maybe ty2) of
(Just tv1, _) -> [mkEqSpec tv1 ty2]
(_, Just tv2) -> [mkEqSpec tv2 ty1]
_ -> []
]
-- | The *full* constraints on the constructor type.
dataConTheta :: DataCon -> ThetaType
dataConTheta (MkData { dcEqSpec = eq_spec, dcOtherTheta = theta })
= eqSpecPreds eq_spec ++ theta
......@@ -906,10 +955,9 @@ dataConBoxer _ = Nothing
--
-- 4) The /original/ result type of the 'DataCon'
dataConSig :: DataCon -> ([TyVar], ThetaType, [Type], Type)
dataConSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs,
dcEqSpec = eq_spec, dcOtherTheta = theta,
dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})
= (univ_tvs ++ ex_tvs, eqSpecPreds eq_spec ++ theta, arg_tys, res_ty)
dataConSig con@(MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs,
dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})
= (univ_tvs ++ ex_tvs, dataConTheta con, arg_tys, res_ty)
dataConInstSig
:: DataCon
......@@ -926,7 +974,7 @@ dataConInstSig (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs
, substTheta subst (eqSpecPreds eq_spec ++ theta)
, substTys subst arg_tys)
where
univ_subst = zipTopTvSubst univ_tvs univ_tys
univ_subst = zipOpenTCvSubst univ_tvs univ_tys
(subst, ex_tvs') = mapAccumL Type.substTyVarBndr univ_subst ex_tvs