diff options
| author | Edward Z. Yang <ezyang@cs.stanford.edu> | 2014-10-23 18:19:46 -0700 |
|---|---|---|
| committer | Edward Z. Yang <ezyang@cs.stanford.edu> | 2014-10-23 18:19:52 -0700 |
| commit | 427925d0806fca7facf76ba42429a3b0ebc4eb8a (patch) | |
| tree | c85acaecbde54e58f6e8d8a29a774383bd1cd94c | |
| parent | a11f71eff15ba2706cbb2ee29aaf7350909e0d2f (diff) | |
| download | haskell-427925d0806fca7facf76ba42429a3b0ebc4eb8a.tar.gz | |
More updates to Backpack manual [skip ci]
Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>
| -rw-r--r-- | docs/backpack/backpack-manual.tex | 259 |
1 files changed, 112 insertions, 147 deletions
diff --git a/docs/backpack/backpack-manual.tex b/docs/backpack/backpack-manual.tex index 31a0ce5d84..51e9055a40 100644 --- a/docs/backpack/backpack-manual.tex +++ b/docs/backpack/backpack-manual.tex @@ -45,12 +45,33 @@ signature (without a client implementation.)} The syntax of an example of a module signature representing an abstract map type: \begin{verbatim} +module Map where type role Map nominal representational data Map k v instance Functor (Map k) empty :: Map k a \end{verbatim} +For entities that can be explicitly exported and imported, the +export list of a module signature behaves in the same way as +the export list for a normal module (e.g., if no list is provided, +only entities defined in the signature are made available.) + +\begin{color}{red} +However, type class instances and type family instances operate +differently: an instance is \emph{only} exported if it is directly +defined in the signature. This is in contrast to the module behavior, +where an instance is \emph{implicitly} brought into scope if it is +imported in any way (even with an empty import list.) + +Even if an instance is ``hidden'' (i.e., not exported by a signature +but in the implementation), we still take it into account when calculating +conflicting instances (e.g., the soundness checks for type families). Thus, +some compilation errors may only occur when linking an implementation +and user, even if they compiled individually fine against the signature +in question. +\end{color} + An \texttt{hsig} file can either be type-checked or compiled against some \emph{backing implementation}, an \texttt{hs} module which provides all of the declarations that a signature advertises. @@ -90,7 +111,7 @@ identifier for a symbol whose provenance is not known). Each instance of \texttt{origname} in the signature is instead assigned the original name \texttt{origname}, instead of the default original name. -(TODO: This interface will work pretty poorly with \texttt{--make}) +(ToDo: This interface will work pretty poorly with \texttt{--make}) \end{color} \paragraph{Compiling} We can specify a backing implementation for @@ -120,30 +141,35 @@ interface file is similar to a module which reexports identifiers from another module, except that we also record the backing implementation for the purpose of handling imports, described in the next section.} -\subsection{Extended format in the installed package database} +\begin{color}{red} +ToDo: In what cases is a type class instance/type family instance reexported? +Currently, type classes from the backing implementation leak through. +We also need to fix \#9422. +\end{color} + +\subsection{Extended format in the installed package database}\label{sec:pkgdb} After a set of Haskell modules has been compiled, they can be registered -in the installed package database so that their exposed modules and -signatures can be made available for import using the \texttt{-package} -flag. An entry in the installed package database specifies what modules -and signatures from the package itself are exposed. It can also -re-export modules and signatures from other packages.\footnote{Signature -reexports are essential for creating signature packages in a modular -way; module reexports are very useful for backwards-compatibility -packages and also taking an package that has been instantiated multiple -ways and giving its modules unique names.} +as a package in the \emph{installed package database} using +\texttt{ghc-pkg}. An entry in the installed package database specifies +what modules and signatures from the package itself are available for +import. It can also re-export modules and signatures from other +packages.\footnote{Signature reexports are essential for creating +signature packages in a modular way; module reexports are very useful +for backwards-compatibility packages and also taking an package that has +been instantiated multiple ways and giving its modules unique names.} There are three fields of an entry in the installed package database of note. \begin{color}{red} -First, the \texttt{exposed-modules} field is a comma-separated list of -module names, possibly with two extra, optional pieces of information +\paragraph{exposed-modules} A comma-separated list of +module names which this package makes available for import, possibly with two extra, optional pieces of information about the module in question: what the \emph{original module/signature} -is (if it is a reexport)\footnote{Knowing the original module/signature +is (\texttt{from MODULE})\footnote{Knowing the original module/signature makes it possible for GHC to directly load the interface file, without having to follow multiple hops in the package database.}, and what the -\emph{backing implementation} is (if it is a signature)\footnote{Knowing +\emph{backing implementation} is (\texttt{is MODULE})\footnote{Knowing the backing implementation makes it possible to tell if an import is unambiguous without having to load the interface file first.}. @@ -159,13 +185,7 @@ exposed-modules: If no reexports or signatures are used, the commas can be omitted (making this syntax backwards compatible with the original syntax.) -Entries to the installed package database are registered using \texttt{ghc-pkg}. -\texttt{ghc-pkg} validates that all entries in \texttt{exposed-modules} are -unambiguous; the criterion for which we will establish in the next section.\footnote{Note -that this does mean duplicates can be allowed in some circumstances. This is most useful -when combining multiple signatures together.} - -TODO: What is currently implemented is +ToDo: What is currently implemented is that \texttt{reexported-modules} has a seperate field, where the original module is always set and backing implementation is always empty. I came to this generalization when I found I needed to add support for @@ -179,18 +199,20 @@ suggests we add the comma variant because it is unambiguous with the old syntax. \end{color} -Second, the \texttt{instantiated-with} field records the \emph{original -module} which instantiated any holes associated with a package (i.e., -what \texttt{-sig-of} parameter was used during compilation.) +\paragraph{instantiated-with} A map from hole name to the \emph{original +module} which instantiated the hole (i.e., what \texttt{-sig-of} +parameters were used during compilation.) -Finally, the \texttt{key} field records the \emph{package key} of a -package. A package key is an opaque identifier identifying a package +\paragraph{key} The \emph{package key} of a +package, an opaque identifier identifying a package which serves as the basis for type identity and linker symbols.\footnote{Informally, you might think of a package as a package name and its version, e.g., \texttt{containers-0.9}; however, sometimes, it is necessary to distinguish between installed instances of a package with the same name and version which were compiled with different -dependencies.} \footnote{The package key is different from an +dependencies.} When files are compiled as part of a package, the +package key must be specified using the \texttt{-this-package-key} +flag.\footnote{The package key is different from an \emph{installed package ID}, which is a more fine-grained identifier for a package. Identical installed package IDs imply identical package keys, but not vice versa. However, within a single run of GHC, we @@ -201,22 +223,25 @@ The package key is programatically generated by Cabal\footnote{In practice, a package key looks something like \texttt{conta\_GtvvBIboSRuDmyUQfSZoAx}. In this document, we'll use \texttt{containers\_KEY} as a convenient shorthand to refer to some -package key for the \texttt{containers} package.}; while GHC has no say -in the generation process, Cabal's must choose distinct package keys if +package key for the \texttt{containers} package.}. While GHC doesn't +specify what the format of the package key is, Cabal's must choose distinct package keys if any of the following fields in the installed package database are distinct: \begin{itemize} \item \texttt{name} (e.g., \texttt{containers}) \item \texttt{version} (e.g., \texttt{0.8}) -\item \texttt{depends} modulo package keys -\item \texttt{instantiated-with} modulo package keys +\item \texttt{depends} (with respect to package keys) +\item \texttt{instantiated-with} (with respect to package keys and module names) \end{itemize} \subsection{Module thinning and renaming} +The command line flag \texttt{-package pkgname} causes all +exposed modules of \texttt{pkgname} (from the installed package database) to become visible under their +original names for imports. The \texttt{-package} flag and its variants (\texttt{-package-id} and -\texttt{-package-key}) now support ``thinning and renaming'' +\texttt{-package-key}) support ``thinning and renaming'' annotations, which allows a user to selectively expose only certain modules from a package, possibly under different names.\footnote{This feature has utility both with and without Backpack. The ability to @@ -225,17 +250,14 @@ export conflicting module names; under Backpack, this situation becomes especially common when an indefinite package is instantiated multiple time with different dependencies.} -The command line flag \texttt{-package pkgname} causes all -exposed modules of \texttt{pkgname} to become visible under their -original names for imports. Thinning and renaming can be applied -using the extended syntax \texttt{-package "pkgname -(rns)"}, where \texttt{rns} is a comma separated list of +Thinning and renaming can be applied +using the extended syntax \verb|-package "pkgname (rns)"|, where \texttt{rns} is a comma separated list of module renamings \texttt{OldName as NewName}. Bare module names are also accepted, where \texttt{Name} is shorthand for \texttt{Name as Name}. A package exposed this way only causes modules (specified before the \texttt{as}) explicitly listed in the renamings to become visible under their new names (specified after the \texttt{as}). For example, -\texttt{-package "containers (Data.Set, Data.Map as Map)"} makes +\verb|-package "containers (Data.Set, Data.Map as Map)"| makes \texttt{Data.Set} and \texttt{Map} (pointing to \texttt{Data.Map}) available for import.\footnote{See also Cabal files for a twist on this syntax.} @@ -243,8 +265,7 @@ for a twist on this syntax.} When the \texttt{-hide-all-packages} flag is applied, uses of the \texttt{-package} flag are \emph{cumulative}; each argument is processed and its bindings added to the global module map. For example, -\texttt{-hide-all-packages -package containers -package "containers -(Data.Map as Map)"} brings both the default exposed modules of +\verb|-hide-all-packages -package containers -package "containers (Data.Map as Map)"| brings both the default exposed modules of containers and a binding for \texttt{Map} into scope.\footnote{The previous behavior, and the current behavior when \texttt{-hide-all-packages} is not set, is for a second package flag for @@ -279,38 +300,6 @@ This mapping says that this package reexports \texttt{pkg:AImpl} as \texttt{pkg:AImpl}, and reexports a signature from \texttt{other-pkg} which itself was compiled against \texttt{pkg:AImpl}. -Here are some more examples: - -\begin{verbatim} -$ cat > AImpl.hsig -module AImpl where - foo = 2 -$ cat > ASig.hsig -module ASig where - foo :: Int -$ cat > BSig.hsig -module BSig where - foo :: Int -$ cat > Main.hs -module Main where - import ASig - import BSig - main = print foo -$ ghc -c AImpl.hs -$ ghc -c ASig.hsig -sig-of "main:AImpl" -$ ghc -c BSig.hsig -sig-of "main:AImpl" -$ ghc -c Main.hs -SUCCEEDS - -$ cat > BImpl.hs -module BImpl where - foo = 4 -$ ghc -c BImpl.hs -$ ghc -c BSig.hsig -sig-of "main:BImpl" # recompile -$ ghc -c Main.hs -FAILS due to ambiguous original name -\end{verbatim} - \paragraph{Typechecking} \Red{When typechecking only, there is not necessarily a backing implementation associated with a signature. In this case, even if the original names match up, we must perform an @@ -319,54 +308,45 @@ This check is not necessary during compilation, because \texttt{-sig-of} will ensure that the signatures are compatible with a common, unique backing implementation. -\begin{color}{red} -\begin{verbatim} -$ ghc -c ASig.hsig -fno-code -fwrite-interface -$ ghc -c BSig.hsig -fno-code -fwrite-interface -$ ghc -c Main.hs -fno-code -fwrite-interface -FAILS due to ambiguous original name (default used) - -$ ghc -c ASig.hsig -shape-of "foo is main:AImpl" -fno-code -fwrite-interface -$ ghc -c BSig.hsig -shape-of "foo is main:AImpl" -fno-code -fwrite-interface -$ ghc -c Main.hs -fno-code -fwrite-interface -SUCCEEDS - -$ ghc -c ASig.hsig -shape-of "foo is main:AImpl" -fno-code -fwrite-interface -$ ghc -c BSig.hsig -shape-of "foo is main:BImpl" -fno-code -fwrite-interface -$ ghc -c Main.hs -fno-code -fwrite-interface -FAILS due to ambiguous original name - -$ cat > BSig.hsig -module BSig where - foo :: Bool - -$ ghc -c ASig.hsig -shape-of "foo is main:AImpl" -fno-code -fwrite-interface -$ ghc -c BSig.hsig -shape-of "foo is main:AImpl" -fno-code -fwrite-interface -$ ghc -c Main.hs -fno-code -fwrite-interface -FAILS due to mismatchng types -\end{verbatim} -\end{color} - -\Red{EDITORS CUT HERE} - \subsection{Indefinite external packages} \Red{Not implemented yet.} \section{Cabal} -\subsection{Reexports and signatures} +\subsection{Fields in the Cabal file} The Cabal file is a user-facing description of a package, which is -processed into an \texttt{InstalledPackageInfo} by Cabal. Currently, -package authors use \texttt{exposed-modules} in the \texttt{library} -section to specify what \texttt{hs} modules are available for import -when the package is exposed. Backpack extends the Cabal files with four -new fields: +converted into an \texttt{InstalledPackageInfo} during a Cabal build. +Backpack extends the Cabal files with four new fields, all of which +are only valid in the \texttt{library} section of a package: + +\paragraph{required-signatures} A space-separated list of module names +specifying internal signatures (in \texttt{hsig} files) of the package. +\Red{Signatures specified in this field are not put in the \texttt{exposed-modules} field in the installed package database and +are not available for external import}; however, in order for a package to be +compiled, implementations for all of its signatures must be provided (so +they are not completely \emph{hidden} in the same way \texttt{other-modules} are). + +\paragraph{exposed-signatures} A space-separated list of module names +specifying externally visible signatures (in \texttt{hsig} files) of the package. It is +represented in the installed package database as an \texttt{exposed-module} with a +non-empty backing implementation (\texttt{Sig is Impl}). Signatures exposed in this way are +available for external import. In order for a package to be compiled, +implementations for all exposed signatures must be provided. + +\paragraph{indefinite} A package is \emph{indefinite} if it has any +uninstantiated +\texttt{required-signatures} or \texttt{exposed-signatures}, or it +depends on an indefinite package without instantiating all of the holes +of that package. In principle, this parameter can be calculated +by Cabal, but it serves a documentory purpose for packages which do not +have any signatures themselves, but depend on packages which are indefinite. +\Red{Actually, this field is in the top-level at the moment.} \paragraph{reexported-modules} A comma-separated list of module or signature reexports. It is represented in the installed package -database as an module with a non-empty original module/signature: the +database as a module with a non-empty original module/signature: the original module is resolved by Cabal. There are three valid syntactic forms: @@ -391,35 +371,6 @@ In this way, packages which reexport multiple signatures to the same name can be valid; a package may also reexport a signature onto a home \texttt{hsig} signature. -\paragraph{required-signatures} A space-separated list of module names -specifying internal signatures (in \texttt{hsig} files) of the package. -This field is analogous to \texttt{other-modules}, but with signatures. -Signatures specified in this field can be imported by other home -modules, but are not registered in the installed package database and -available for external import. In order for this package to be -compiled, implementations for all these signatures must be provided (see -the section on \texttt{Setup}). - -\paragraph{exposed-signatures} A space-separated list of module names -specifying externally visible signatures (in \texttt{hsig} files) of the package. It is -represented in the installed package database as a module with a -non-empty backing implementation. Signatures exposed in this way are -available external import. In order for this package to be compiled, -implementations for all these signatures must be provided (see the -section on \texttt{Setup}). - -\paragraph{indefinite} A package is \emph{indefinite} if it has any -\texttt{required-signatures} or \texttt{exposed-signatures}, or it -transitively depends on an indefinite package whose signatures have -not been instantiated. In principle, this parameter can be calculated -by Cabal, but serves a documentory purpose for packages which do not -have any signatures themselves, but depend on packages which do (without -providing implementations for those signatures.) - -\subsection{Package key generation} - -The algorithm Cabal uses in order to calculate what - \subsection{build-depends} This field has been extended with new syntax @@ -429,30 +380,44 @@ and to have the ability to include an indefinite package \emph{multiple times} \emph{primary} mechanism by which holes are instantiated in a mix-in module system, however, this instantiation only occurs when running \texttt{cabal-install}. -Here is an example entry in \texttt{build-depends}: \texttt{foo >= 0.8 -(ASig as A1, B as B1; ASig as A2, ...)}. This statement includes the +Here is an example entry in \texttt{build-depends}: +\verb|foo >= 0.8 (ASig as A1, B as B1; ASig as A2, ...)|. This statement includes the package \texttt{foo} twice, once with \texttt{ASig} instantiated with \texttt{A1} and \texttt{B} renamed as \texttt{B1}, and once with \texttt{ASig} instantiated with \texttt{A2}, and all other modules imported with their original names. Assuming that the key of the first instance of \texttt{foo} is \texttt{foo\_KEY1} and the key of the second instance is \texttt{foo\_KEY2}, and that \texttt{ASig} is an \texttt{exposed-signature}, then this \texttt{build-depends} would turn into -these flags for GHC: \texttt{-package-key "foo\_KEY1 (ASig as A1, B as B1)" -package-key "foo\_KEY2" -package-key "foo\_KEY2 (ASig as A2)"} +these flags for GHC\@: \verb|-package-key "foo\_KEY1 (ASig as A1, B as B1)" -package-key "foo\_KEY2" -package-key "foo\_KEY2 (ASig as A2)"| Syntactically, the thinnings and renamings are placed inside a parenthetical after the package name and version constraints. Semicolons distinguish separate inclusions of the package, and the inner comma-separated lists indicate the thinning/renamings of the module. -You can also write \texttt{...}, which simply +You can also write \verb|...|, which simply includes all of the default bindings from the package. \Red{This is not implemented. Should this only refer to modules which were not referred to already? Should it refer only to holes?} -There are two marks that should be made about separate instantiations of +There are two remarks that should be made about separate instantiations of the package. First, Cabal will automatically ``de-duplicate'' instances of -the package which are equivalent: thus, \texttt{foo (A; B)} is equivalent to -\texttt{foo (A, B)} when \texttt{foo} is a definite package, or +the package which are equivalent: thus, \verb|foo (A; B)| is equivalent to +\texttt{foo (A, B)} when \texttt{foo} is a definite package, or when the +holes instantiation for each instance is equivalent. Second, when merging +two \texttt{build-depends} statements together (for example, due to +a conditional section in a Cabal file), they are considered \emph{separate +inclusions of a package.} + +\subsection{Setup flags} + +There is one new flag for the \texttt{Setup} script, which can be +used to manually provide instantiations for holes in a package: +\verb|--instantiate-with NAME=PKG:MOD|, which binds a module \verb|NAME| +to the implementation \verb|MOD| provided by installed package ID \verb|PKG|. +The flag can be specified multiply times to provide bindings for all +signatures. The module in question must be the \emph{original} module, +not a re-export. + -\subsection{Setup new flags} \subsection{Metadata in the installed package database} |
