summaryrefslogtreecommitdiff
path: root/docs/users_guide/packages.rst
blob: ff6953ac8ca808a42046492d9cf4c66eba94c470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
.. _packages:

Packages
========

.. index::
   single: packages

A package is a library of Haskell modules known to the compiler. GHC
comes with several packages: see the accompanying `library
documentation <../libraries/index.html>`__. More packages to install can
be obtained from
`HackageDB <http://hackage.haskell.org/packages/hackage.html>`__.

Using a package couldn't be simpler: if you're using ``--make`` or GHCi,
then most of the installed packages will be automatically available to
your program without any further options. The exceptions to this rule
are covered below in :ref:`using-packages`.

Building your own packages is also quite straightforward: we provide the
`Cabal <http://www.haskell.org/cabal/>`__ infrastructure which automates
the process of configuring, building, installing and distributing a
package. All you need to do is write a simple configuration file, put a
few files in the right places, and you have a package. See the `Cabal
documentation <http://www.haskell.org/cabal/users-guide/>`__ for
details, and also the Cabal libraries
(:cabal-ref:`Distribution.Simple.`, for example).

.. _using-packages:

Using Packages
--------------

.. index::
   single: packages; using

GHC only knows about packages that are *installed*. Installed packages live in
package databases. For details on package databases and how to control which
package databases or specific set of packages are visible to GHC, see
:ref:`package-databases`.

To see which packages are currently available, use the ``ghc-pkg list`` command:

.. code-block:: none

    $ ghc-pkg list
    /usr/lib/ghc-6.12.1/package.conf.d:
        Cabal-1.7.4
        array-0.2.0.1
        base-3.0.3.0
        base-4.2.0.0
        bin-package-db-0.0.0.0
        binary-0.5.0.1
        bytestring-0.9.1.4
        containers-0.2.0.1
        directory-1.0.0.2
        (dph-base-0.4.0)
        (dph-par-0.4.0)
        (dph-prim-interface-0.4.0)
        (dph-prim-par-0.4.0)
        (dph-prim-seq-0.4.0)
        (dph-seq-0.4.0)
        extensible-exceptions-0.1.1.0
        ffi-1.0
        filepath-1.1.0.1
        (ghc-6.12.1)
        ghc-prim-0.1.0.0
        haskeline-0.6.2
        haskell98-1.0.1.0
        hpc-0.5.0.2
        integer-gmp-0.1.0.0
        mtl-1.1.0.2
        old-locale-1.0.0.1
        old-time-1.0.0.1
        pretty-1.0.1.0
        process-1.0.1.1
        random-1.0.0.1
        rts-1.0.1
        syb-0.1.0.0
        template-haskell-2.4.0.0
        terminfo-0.3.1
        time-1.1.4
        unix-2.3.1.0
        utf8-string-0.3.4

An installed package is either *exposed* or *hidden* by default.
Packages hidden by default are listed in parentheses (e.g.
``(lang-1.0)``), or possibly in blue if your terminal supports colour,
in the output of ``ghc-pkg list``. Command-line flags, described below,
allow you to expose a hidden package or hide an exposed one. Only
modules from exposed packages may be imported by your Haskell code; if
you try to import a module from a hidden package, GHC will emit an error
message. It should be noted that a hidden package might still get linked
with your program as a dependency of an exposed package, it is only restricted
from direct imports.

If there are multiple exposed versions of a package, GHC will
prefer the latest one. Additionally, some packages may be broken: that
is, they are missing from the package database, or one of their
dependencies are broken; in this case; these packages are excluded from
the default set of packages.

.. note::
    If you're using Cabal, then the exposed or hidden status of a
    package is irrelevant: the available packages are instead determined by
    the dependencies listed in your ``.cabal`` specification. The
    exposed/hidden status of packages is only relevant when using ``ghc`` or
    ``ghci`` directly.

Similar to a package's hidden status is a package's trusted status. A
package can be either trusted or not trusted (distrusted). By default
packages are distrusted. This property of a package only plays a role
when compiling code using GHC's Safe Haskell feature (see
:ref:`safe-haskell`) with the ``-fpackage-trust`` flag enabled.

To see which modules are provided by a package use the ``ghc-pkg``
command (see :ref:`package-management`):

.. code-block:: none

    $ ghc-pkg field network exposed-modules
    exposed-modules: Network.BSD,
                     Network.CGI,
                     Network.Socket,
                     Network.URI,
                     Network

The GHC command line options that control packages are:

.. ghc-flag:: -package ⟨pkg⟩
    :shortdesc: Expose package ⟨pkg⟩
    :type: dynamic
    :category:

    This option causes the installed package ⟨pkg⟩ to be exposed. The
    package ⟨pkg⟩ can be specified in full with its version number (e.g.
    ``network-1.0``) or the version number can be omitted in which case GHC
    will automatically expose the latest non-broken version from the installed
    versions of the package.

    By default (when :ghc-flag:`-hide-all-packages` is not specified), GHC
    exposes only one version of a package, all other versions become hidden.
    If `-package` option is specified multiple times for the same package the
    last one overrides the previous ones. On the other hand, if
    :ghc-flag:`-hide-all-packages` is used, GHC allows you to expose multiple
    versions of a package by using the `-package` option multiple times with
    different versions of the same package.

    `-package` supports thinning and renaming described in
    :ref:`package-thinning-and-renaming`.

    The ``-package ⟨pkg⟩`` option also causes package ⟨pkg⟩ to be linked into
    the resulting executable or shared object. Whether a packages'
    library is linked statically or dynamically is controlled by the
    flag pair :ghc-flag:`-static`\/ :ghc-flag:`-dynamic`.

    In :ghc-flag:`--make` mode and :ghc-flag:`--interactive` mode (see :ref:`modes`),
    the compiler normally determines which packages are required by the
    current Haskell modules, and links only those. In batch mode
    however, the dependency information isn't available, and explicit
    ``-package`` options must be given when linking. The one other time
    you might need to use ``-package`` to force linking a package is
    when the package does not contain any Haskell modules (it might
    contain a C library only, for example). In that case, GHC will never
    discover a dependency on it, so it has to be mentioned explicitly.

    For example, to link a program consisting of objects ``Foo.o`` and
    ``Main.o``, where we made use of the ``network`` package, we need to
    give GHC the ``-package`` flag thus:

    .. code-block:: sh

        $ ghc -o myprog Foo.o Main.o -package network

    The same flag is necessary even if we compiled the modules from
    source, because GHC still reckons it's in batch mode:

    .. code-block:: sh

        $ ghc -o myprog Foo.hs Main.hs -package network

.. ghc-flag:: -package-id ⟨unit-id⟩
    :shortdesc: Expose package by id ⟨unit-id⟩
    :type: dynamic
    :category:

    Exposes a package like :ghc-flag:`-package ⟨pkg⟩`, but the package is named
    by its unit ID (i.e. the value of ``id`` in its entry in the installed
    package database, also previously known as an installed package ID) rather
    than by name. This is a more robust way to name packages, and can be used
    to select packages that would otherwise be shadowed. Cabal passes
    ``-package-id`` flags to GHC.  ``-package-id`` supports thinning and
    renaming described in :ref:`package-thinning-and-renaming`.

.. ghc-flag:: -hide-all-packages
    :shortdesc: Hide all packages by default
    :type: dynamic
    :category:

    Ignore the exposed flag on installed packages, and hide them all by
    default. If you use this flag, then any packages you require
    (including ``base``) need to be explicitly exposed using
    :ghc-flag:`-package ⟨pkg⟩` options.

    This is a good way to insulate your program from differences in the
    globally exposed packages, and being explicit about package
    dependencies is a Good Thing. Cabal always passes the
    ``-hide-all-packages`` flag to GHC, for exactly this reason.

.. ghc-flag:: -hide-package ⟨pkg⟩
    :shortdesc: Hide package ⟨pkg⟩
    :type: dynamic
    :category:

    This option does the opposite of :ghc-flag:`-package ⟨pkg⟩`: it causes the
    specified package to be hidden, which means that none of its modules
    will be available for import by Haskell ``import`` directives.

    Note that the package might still end up being linked into the final
    program, if it is a dependency (direct or indirect) of another
    exposed package.

.. ghc-flag:: -ignore-package ⟨pkg⟩
    :shortdesc: Ignore package ⟨pkg⟩
    :type: dynamic
    :category:

    Causes the compiler to behave as if package ⟨pkg⟩, and any packages
    that depend on ⟨pkg⟩, are not installed at all.

    Saying ``-ignore-package ⟨pkg⟩`` is the same as giving
    :ghc-flag:`-hide-package ⟨pkg⟩` flags for ⟨pkg⟩ and all the packages that
    depend on ⟨pkg⟩. Sometimes we don't know ahead of time which packages will
    be installed that depend on ⟨pkg⟩, which is when the
    :ghc-flag:`-ignore-package ⟨pkg⟩` flag can be useful.

.. ghc-flag:: -no-auto-link-packages
    :shortdesc: Don't automatically link in the base and rts packages.
    :type: dynamic
    :category:

    By default, GHC will automatically link in the ``base`` and ``rts``
    packages. This flag disables that behaviour.

.. ghc-flag:: -this-unit-id ⟨unit-id⟩
    :shortdesc: Compile to be part of unit (i.e. package)
        ⟨unit-id⟩
    :type: dynamic
    :category:

    Tells GHC that the module being compiled forms part of unit ID
    ⟨unit-id⟩; internally, these keys are used to determine type equality
    and linker symbols.  As of GHC 8.0, unit IDs must consist solely
    of alphanumeric characters, dashes, underscores and periods.  GHC
    reserves the right to interpret other characters in a special
    way in later releases.

.. ghc-flag:: -trust ⟨pkg⟩
    :shortdesc: Expose package ⟨pkg⟩ and set it to be trusted
    :type: dynamic
    :category:
    :noindex:

    This option causes the install package ⟨pkg⟩ to be both exposed and
    trusted by GHC. This command functions in a very similar way
    to the :ghc-flag:`-package ⟨pkg⟩` command but in addition sets the selected
    packages to be trusted by GHC, regardless of the contents of the
    package database. (see :ref:`safe-haskell`).

.. ghc-flag:: -distrust ⟨pkg⟩
    :shortdesc: Expose package ⟨pkg⟩ and set it to be distrusted
    :type: dynamic
    :category:
    :noindex:

    This option causes the install package ⟨pkg⟩ to be both exposed and
    distrusted by GHC. This command functions in a very similar way to the
    :ghc-flag:`-package ⟨pkg⟩` command but in addition sets the selected
    packages to be distrusted by GHC, regardless of the contents of the package
    database. (see :ref:`safe-haskell`).

.. ghc-flag:: -distrust-all-packages
    :shortdesc: Distrust all packages by default
    :type: dynamic
    :category:
    :noindex:

    Ignore the trusted flag on installed packages, and distrust them by
    default. If you use this flag and Safe Haskell then any packages you
    require to be trusted (including ``base``) need to be explicitly trusted
    using :ghc-flag:`-trust ⟨pkg⟩` options. This option does not change the
    exposed/hidden status of a package, so it isn't equivalent to applying
    :ghc-flag:`-distrust ⟨pkg⟩` to all packages on the system. (see
    :ref:`safe-haskell`).

.. _package-main:

The ``main`` package
--------------------

Every complete Haskell program must define ``main`` in module ``Main`` in
package ``main``. Omitting the :ghc-flag:`-this-unit-id ⟨unit-id⟩` flag
compiles code for package ``main``. Failure to do so leads to a somewhat
obscure link-time error of the form:

.. code-block:: none

    /usr/bin/ld: Undefined symbols:
    _ZCMain_main_closure

.. _package-overlaps:

Consequences of packages for the Haskell language
-------------------------------------------------

It is possible that by using packages you might end up with a program
that contains two modules with the same name: perhaps you used a package
``P`` that has a *hidden* module ``M``, and there is also a module ``M`` in your
program. Or perhaps the dependencies of packages that you used contain
some overlapping modules. Perhaps the program even contains multiple
versions of a certain package, due to dependencies from other packages.

None of these scenarios gives rise to an error on its own [1]_, but they
may have some interesting consequences. For instance, if you have a type
``M.T`` from version 1 of package ``P``, then this is *not* the same as
the type ``M.T`` from version 2 of package ``P``, and GHC will report an
error if you try to use one where the other is expected.

Formally speaking, in Haskell 98, an entity (function, type or class) in
a program is uniquely identified by the pair of the module name in which
it is defined and its name. In GHC, an entity is uniquely defined by a
triple: package, module, and name.

.. _package-thinning-and-renaming:

Thinning and renaming modules
-----------------------------

When incorporating packages from multiple sources, you may end up in a
situation where multiple packages publish modules with the same name.
Previously, the only way to distinguish between these modules was to use
:ref:`package-qualified-imports`. However, since GHC 7.10, the
:ghc-flag:`-package ⟨pkg⟩` flags (and their variants) have been extended to
allow a user to explicitly control what modules a package brings into scope, by
analogy to the import lists that users can attach to module imports.

The basic syntax is that instead of specifying a package name P to the
package flag ``-package``, instead we specify both a package name and a
parenthesized, comma-separated list of module names to import. For
example, ``-package "base (Data.List, Data.Bool)"`` makes only
``Data.List`` and ``Data.Bool`` visible from package ``base``. We also
support renaming of modules, in case you need to refer to both modules
simultaneously; this is supporting by writing
``OldModName as NewModName``, e.g.
``-package "base (Data.Bool as Bool)``. You can also write
``-package "base with (Data.Bool as Bool)`` to include all of the
original bindings (e.g. the renaming is strictly additive). It's
important to specify quotes so that your shell passes the package name
and thinning/renaming list as a single argument to GHC.

Package imports with thinning/renaming do not hide other versions of the
package: e.g. if containers-0.9 is already exposed,
``-package "containers-0.8 (Data.List as ListV8)"`` will only add an
additional binding to the environment. Similarly,
``-package "base (Data.Bool as Bool)" -package "base (Data.List as List)"``
is equivalent to
``-package "base (Data.Bool as Bool, Data.List as List)"``. Literal
names must refer to modules defined by the original package, so for
example ``-package "base (Data.Bool as Bool, Bool as Baz)"`` is invalid
unless there was a ``Bool`` module defined in the original package.
Hiding a package also clears all of its renamings.

You can use renaming to provide an alternate prelude, e.g.
``-hide-all-packages -package "basic-prelude (BasicPrelude as Prelude)"``,
in lieu of the :ref:`rebindable-syntax` extension.

.. _package-databases:

Package Databases
-----------------

A package database is where the details about installed packages are
stored. It is a directory, usually called ``package.conf.d``, that
contains a file for each package, together with a binary cache of the
package data in the file :file:`package.cache`. Normally you won't need to
look at or modify the contents of a package database directly; all
management of package databases can be done through the :command:`ghc-pkg` tool
(see :ref:`package-management`).

GHC knows about two package databases in particular:

-  The *global package database*, which comes with your GHC installation,
   e.g. ``/usr/lib/ghc-6.12.1/package.conf.d``.

-  The *user package database* private to each user. On Unix systems this will
   be ``$XDG_DATA_HOME/ghc/arch-os-version/package.conf.d``, and on Windows it will
   be something like
   ``C:\Documents And Settings\user\ghc\package.conf.d``. The
   ``ghc-pkg`` tool knows where this file should be located, and will
   create it if it doesn't exist (see :ref:`package-management`).

*Package database stack:* Package databases are arranged in a stack structure.
When GHC starts up it adds the global and the user package databases to the
stack, in that order, unless :envvar:`GHC_PACKAGE_PATH` is specified. When
`GHC_PACKAGE_PATH` is specified then it will determine the initial database
stack. Several command line options described below can further manipulate this
initial stack. You can see GHC's effective package database stack by running
GHC with the :ghc-flag:`-v` flag.

This stack structure means that the order of :ghc-flag:`-package-db ⟨file⟩`
flags or :envvar:`GHC_PACKAGE_PATH` is important.  Each substack of the stack
must be well formed (packages in databases on top of the stack can refer to
packages below, but not vice versa).

*Package shadowing:* When multiple package databases are in use it
is possible, though rarely, that the same installed package id is present in
more than one database. In that case, packages closer to the top of the stack
will override (*shadow*) those below them. If the conflicting packages are
found to be equivalent (by ABI hash comparison) then one of them replaces all
references to the other, otherwise the overridden package and all those
depending on it will be removed.

*Package version selection:* When selecting a package, GHC will search for
packages in all available databases. If multiple versions of the same package
are available the latest non-broken version will be chosen.

*Version conflict resolution:* If multiple instances of a package version
chosen by GHC are available then GHC will choose an unspecified instance.

You can control GHC's package database stack using the following
options:

.. ghc-flag:: -package-db ⟨file⟩
    :shortdesc: Add ⟨file⟩ to the package db stack.
    :type: dynamic
    :category:

    Add the package database ⟨file⟩ on top of the current stack.

.. ghc-flag:: -no-global-package-db
    :shortdesc: Remove the global package db from the stack.
    :type: dynamic
    :category:

    Remove the global package database from the package database stack.

.. ghc-flag:: -no-user-package-db
    :shortdesc: Remove the user's package db from the stack.
    :type: dynamic
    :category:

    Prevent loading of the user's local package database in the initial
    stack.

.. ghc-flag:: -clear-package-db
    :shortdesc: Clear the package db stack.
    :type: dynamic
    :category:

    Reset the current package database stack. This option removes every
    previously specified package database (including those read from the
    :envvar:`GHC_PACKAGE_PATH` environment variable) from the package database
    stack.

.. ghc-flag:: -global-package-db
    :shortdesc: Add the global package db to the stack.
    :type: dynamic
    :category:

    Add the global package database on top of the current stack. This
    option can be used after :ghc-flag:`-no-global-package-db` to specify the
    position in the stack where the global package database should be
    loaded.

.. ghc-flag:: -user-package-db
    :shortdesc: Add the user's package db to the stack.
    :type: dynamic
    :category:

    Add the user's package database on top of the current stack. This
    option can be used after :ghc-flag:`-no-user-package-db` to specify the
    position in the stack where the user's package database should be
    loaded.

.. _ghc-package-path:

The ``GHC_PACKAGE_PATH`` environment variable
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. envvar:: GHC_PACKAGE_PATH

    The `GHC_PACKAGE_PATH` environment variable may be set to a
    ``:``\-separated (``;``\-separated on Windows) list of files containing
    package databases. This list of package databases, used by GHC and ghc-pkg,
    specifies a stack of package databases from top to bottom. This order was
    chosen to match the behaviour of the :envvar:`PATH` environment variable
    where entries earlier in the PATH override ones that come later. See
    :ref:`package-databases` for details on how the package database stack is
    used.

    Normally `GHC_PACKAGE_PATH` replaces the default package stack. For
    example, all of the following commands are equivalent, creating a stack with
    db1 at the top followed by db2 (use ``;`` instead of ``:`` on Windows):

    .. code-block:: none

        $ ghc -clear-package-db -package-db db2.conf -package-db db1.conf
        $ env GHC_PACKAGE_PATH=db1.conf:db2.conf ghc
        $ env GHC_PACKAGE_PATH=db2.conf ghc -package-db db1.conf

    However, if `GHC_PACKAGE_PATH` ends in a separator, the default databases
    (i.e. the user and global package databases, in that order) are appended
    to the path. For example, to augment the usual set of packages with a
    database of your own, you could say (on Unix):

    .. code-block:: none

        $ export GHC_PACKAGE_PATH=$XDG_DATA_HOME/.my-ghc-packages.conf:

    To check whether your `GHC_PACKAGE_PATH` setting is doing the right
    thing, ``ghc-pkg list`` will list all the databases in use, in the
    reverse order they are searched.

.. _package-environments:

Package environments
~~~~~~~~~~~~~~~~~~~~

.. index::
   single: package environments
   single: environment file

A *package environment file* is a file that tells ``ghc`` precisely which
packages should be visible. It can be used to create environments for ``ghc``
or ``ghci`` that are local to a shell session or to some file system location.
They are intended to be managed by build/package tools, to enable ``ghc`` and
``ghci`` to automatically use an environment created by the tool.

In the case of ``ghci``, the environment file will be read once, during
initialisation. If the file changes then you have to restart GHCi to reflect
the updated file.

The file contains package IDs and optionally package databases, one directive
per line:

.. code-block:: none

    clear-package-db
    global-package-db
    user-package-db
    package-db db.d/
    package-id id_1
    package-id id_2
    ...
    package-id id_n

If such a package environment is found, it is equivalent to passing these
command line arguments to ``ghc``:

.. code-block:: none

    -hide-all-packages
    -clear-package-db
    -global-package-db
    -user-package-db
    -package-db db.d/
    -package-id id_1
    -package-id id_2
    ...
    -package-id id_n

Note the implicit :ghc-flag:`-hide-all-packages` and the fact that it is
:ghc-flag:`-package-id ⟨unit-id⟩`, not :ghc-flag:`-package ⟨pkg⟩`. This is
because the environment specifies precisely which packages should be visible.

Note that for the ``package-db`` directive, if a relative path is given it
must be relative to the location of the package environment file.

.. ghc-flag:: -package-env ⟨file⟩|⟨name⟩
    :shortdesc: Use the specified package environment.
    :type: dynamic
    :category:

    Use the package environment in ⟨file⟩, or in
    ``$XDG_DATA_HOME/ghc/arch-os-version/environments/⟨name⟩``
    If set to ``-`` no package environment is read.

.. envvar:: GHC_ENVIRONMENT

    Specifies the path to the package environment file to be used by GHC.
    Overridden by the :ghc-flag:`-package-env ⟨file⟩|⟨name⟩` flag if set.

In order, ``ghc`` will look for the package environment in the following
locations:

-  File ⟨file⟩ if you pass the option :ghc-flag:`-package-env ⟨file⟩|⟨name⟩`.

-  File ``$XDG_DATA_HOME/ghc/arch-os-version/environments/name`` if you pass the
   option ``-package-env ⟨name⟩``.

-  File ⟨file⟩ if the environment variable :envvar:`GHC_ENVIRONMENT` is set to
   ⟨file⟩.

-  File ``$XDG_DATA_HOME/ghc/arch-os-version/environments/name`` if the
   environment variable :envvar:`GHC_ENVIRONMENT` is set to ⟨name⟩.

Additionally, unless ``-hide-all-packages`` is specified ``ghc`` will also
look for the package environment in the following locations:

-  File ``.ghc.environment.arch-os-version`` if it exists in the current
   directory or any parent directory (but not the user's home directory).

-  File ``$XDG_DATA_HOME/ghc/arch-os-version/environments/default`` if it
   exists.

Package environments can be modified by further command line arguments;
for example, if you specify ``-package foo`` on the command line, then
package ⟨foo⟩ will be visible even if it's not listed in the currently
active package environment.

.. _package-ids:

Installed package IDs, dependencies, and broken packages
--------------------------------------------------------

Each installed package has a unique identifier (the "installed package
ID"), which distinguishes it from all other installed packages on the
system. To see the installed package IDs associated with each installed
package, use ``ghc-pkg list -v``:

.. code-block:: none

    $ ghc-pkg list -v
    using cache: /usr/lib/ghc-6.12.1/package.conf.d/package.cache
    /usr/lib/ghc-6.12.1/package.conf.d
       Cabal-1.7.4 (Cabal-1.7.4-48f5247e06853af93593883240e11238)
       array-0.2.0.1 (array-0.2.0.1-9cbf76a576b6ee9c1f880cf171a0928d)
       base-3.0.3.0 (base-3.0.3.0-6cbb157b9ae852096266e113b8fac4a2)
       base-4.2.0.0 (base-4.2.0.0-247bb20cde37c3ef4093ee124e04bc1c)
       ...

The string in parentheses after the package name is the installed
package ID: it normally begins with the package name and version, and
ends in a hash string derived from the compiled package. Dependencies
between packages are expressed in terms of installed package IDs, rather
than just packages and versions. For example, take a look at the
dependencies of the ``haskell98`` package:

.. code-block:: none

    $ ghc-pkg field haskell98 depends
    depends: array-0.2.0.1-9cbf76a576b6ee9c1f880cf171a0928d
             base-4.2.0.0-247bb20cde37c3ef4093ee124e04bc1c
             directory-1.0.0.2-f51711bc872c35ce4a453aa19c799008
             old-locale-1.0.0.1-d17c9777c8ee53a0d459734e27f2b8e9
             old-time-1.0.0.1-1c0d8ea38056e5087ef1e75cb0d139d1
             process-1.0.1.1-d8fc6d3baf44678a29b9d59ca0ad5780
             random-1.0.0.1-423d08c90f004795fd10e60384ce6561

The purpose of the installed package ID is to detect problems caused by
re-installing a package without also recompiling the packages that
depend on it. Recompiling dependencies is necessary, because the newly
compiled package may have a different ABI (Application Binary Interface)
than the previous version, even if both packages were built from the
same source code using the same compiler. With installed package IDs, a
recompiled package will have a different installed package ID from the
previous version, so packages that depended on the previous version are
now orphaned - one of their dependencies is not satisfied. Packages that
are broken in this way are shown in the ``ghc-pkg list`` output either
in red (if possible) or otherwise surrounded by braces. In the following
example, we have recompiled and reinstalled the ``filepath`` package,
and this has caused various dependencies including ``Cabal`` to break:

.. code-block:: none

    $ ghc-pkg list
    WARNING: there are broken packages.  Run 'ghc-pkg check' for more details.
    /usr/lib/ghc-6.12.1/package.conf.d:
        {Cabal-1.7.4}
        array-0.2.0.1
        base-3.0.3.0
        ... etc ...

Additionally, ``ghc-pkg list`` reminds you that there are broken
packages and suggests ``ghc-pkg check``, which displays more information
about the nature of the failure:

.. code-block:: none

    $ ghc-pkg check
    There are problems in package ghc-6.12.1:
      dependency "filepath-1.1.0.1-87511764eb0af2bce4db05e702750e63" doesn't exist
    There are problems in package haskeline-0.6.2:
      dependency "filepath-1.1.0.1-87511764eb0af2bce4db05e702750e63" doesn't exist
    There are problems in package Cabal-1.7.4:
      dependency "filepath-1.1.0.1-87511764eb0af2bce4db05e702750e63" doesn't exist
    There are problems in package process-1.0.1.1:
      dependency "filepath-1.1.0.1-87511764eb0af2bce4db05e702750e63" doesn't exist
    There are problems in package directory-1.0.0.2:
      dependency "filepath-1.1.0.1-87511764eb0af2bce4db05e702750e63" doesn't exist

    The following packages are broken, either because they have a problem
    listed above, or because they depend on a broken package.
    ghc-6.12.1
    haskeline-0.6.2
    Cabal-1.7.4
    process-1.0.1.1
    directory-1.0.0.2
    bin-package-db-0.0.0.0
    hpc-0.5.0.2
    haskell98-1.0.1.0

To fix the problem, you need to recompile the broken packages against
the new dependencies. The easiest way to do this is to use
``cabal-install``, or download the packages from
`HackageDB <http://hackage.haskell.org/packages/hackage.html>`__ and
build and install them as normal.

Be careful not to recompile any packages that GHC itself depends on, as
this may render the ``ghc`` package itself broken, and ``ghc`` cannot be
simply recompiled. The only way to recover from this would be to
re-install GHC.

.. _package-management:

Package management (the ``ghc-pkg`` command)
--------------------------------------------

.. index::
   single: packages; management

The :command:`ghc-pkg` tool is for querying and modifying package databases. To
see what package databases are in use, use ``ghc-pkg list``. The stack of
databases that :command:`ghc-pkg` knows about can be modified using the
:envvar:`GHC_PACKAGE_PATH` environment variable (see :ref:`ghc-package-path`,
and using :ghc-flag:`-package-db ⟨file⟩` options on the :command:`ghc-pkg`
command line.

When asked to modify a database, ``ghc-pkg`` modifies the global
database by default. Specifying ``--user`` causes it to act on the user
database, or ``--package-db`` can be used to act on another database
entirely. When multiple of these options are given, the rightmost one is
used as the database to act upon.

Commands that query the package database (list, latest, describe, field,
dot) operate on the list of databases specified by the flags ``--user``,
``--global``, and ``--package-db``. If none of these flags are given,
the default is ``--global --user``.

If the environment variable :envvar:`GHC_PACKAGE_PATH` is set, and its value
does not end in a separator (``:`` on Unix, ``;`` on Windows), then the
last database is considered to be the global database, and will be
modified by default by ``ghc-pkg``. The intention here is that
``GHC_PACKAGE_PATH`` can be used to create a virtual package environment
into which Cabal packages can be installed without setting anything
other than ``GHC_PACKAGE_PATH``.

The ``ghc-pkg`` program may be run in the ways listed below. Where a
package name is required, the package can be named in full including the
version number (e.g. ``network-1.0``), or without the version number.
Naming a package without the version number matches all versions of the
package; the specified action will be applied to all the matching
packages. A package specifier that matches all version of the package
can also be written ``⟨pkg⟩ -*``, to make it clearer that multiple
packages are being matched. To match against the installed package ID
instead of just package name and version, pass the ``--ipid`` flag.

``ghc-pkg init path``
    Creates a new, empty, package database at ⟨path⟩, which must not
    already exist.

``ghc-pkg register ⟨file⟩``
    Reads a package specification from ⟨file⟩ (which may be "``-``"
    to indicate standard input), and adds it to the database of
    installed packages. The syntax of ⟨file⟩ is given in
    :ref:`installed-pkg-info`.

    The package specification must be a package that isn't already
    installed.

``ghc-pkg update ⟨file⟩``
    The same as ``register``, except that if a package of the same name
    is already installed, it is replaced by the new one.

``ghc-pkg unregister ⟨P⟩``
    Remove the specified package from the database.

``ghc-pkg check``
    Check consistency of dependencies in the package database, and
    report packages that have missing dependencies.

``ghc-pkg expose ⟨P⟩``
    Sets the ``exposed`` flag for package ⟨P⟩ to ``True``.

``ghc-pkg hide ⟨P⟩``
    Sets the ``exposed`` flag for package ⟨P⟩ to ``False``.

``ghc-pkg trust ⟨P⟩``
    Sets the ``trusted`` flag for package ⟨P⟩ to ``True``.

``ghc-pkg distrust ⟨P⟩``
    Sets the ``trusted`` flag for package ⟨P⟩ to ``False``.

``ghc-pkg list [⟨P⟩] [--simple-output]``
    This option displays the currently installed packages, for each of
    the databases known to ``ghc-pkg``. That includes the global
    database, the user's local database, and any further files specified
    using the ``-f`` option on the command line.

    Hidden packages (those for which the ``exposed`` flag is ``False``)
    are shown in parentheses in the list of packages.

    If an optional package identifier ⟨P⟩ is given, then only packages
    matching that identifier are shown.

    If the option ``--simple-output`` is given, then the packages are
    listed on a single line separated by spaces, and the database names
    are not included. This is intended to make it easier to parse the
    output of ``ghc-pkg list`` using a script.

``ghc-pkg find-module ⟨M⟩ [--simple-output]``
    This option lists registered packages exposing module ⟨M⟩. Examples:

    ::

        $ ghc-pkg find-module Var
        c:/fptools/validate/ghc/driver/package.conf.inplace:
            (ghc-6.9.20080428)

        $ ghc-pkg find-module Data.Sequence
        c:/fptools/validate/ghc/driver/package.conf.inplace:
            containers-0.1

    Otherwise, it behaves like ``ghc-pkg list``, including options.

``ghc-pkg latest ⟨P⟩``
    Prints the latest available version of package ⟨P⟩.

``ghc-pkg describe ⟨P⟩``
    Emit the full description of the specified package. The description
    is in the form of an ``InstalledPackageInfo``, the same as the input
    file format for ``ghc-pkg register``. See :ref:`installed-pkg-info`
    for details.

    If the pattern matches multiple packages, the description for each
    package is emitted, separated by the string ``---`` on a line by
    itself.

``ghc-pkg field ⟨P⟩ ⟨field⟩[,⟨field⟩]*``
    Show just a single field of the installed package description for
    ``P``. Multiple fields can be selected by separating them with
    commas

``ghc-pkg dot``
    Generate a graph of the package dependencies in a form suitable for
    input for the `graphviz <http://www.graphviz.org/>`__ tools. For
    example, to generate a PDF of the dependency graph:

    ::

        ghc-pkg dot | tred | dot -Tpdf >pkgs.pdf

``ghc-pkg dump``
    Emit the full description of every package, in the form of an
    ``InstalledPackageInfo``. Multiple package descriptions are
    separated by the string ``---`` on a line by itself.

    This is almost the same as ``ghc-pkg describe '*'``, except that
    ``ghc-pkg dump`` is intended for use by tools that parse the
    results, so for example where ``ghc-pkg describe '*'`` will emit an
    error if it can't find any packages that match the pattern,
    ``ghc-pkg dump`` will simply emit nothing.

``ghc-pkg recache``
    Re-creates the binary cache file ``package.cache`` for the selected
    database. This may be necessary if the cache has somehow become
    out-of-sync with the contents of the database (``ghc-pkg`` will warn
    you if this might be the case).

    The other time when ``ghc-pkg recache`` is useful is for registering
    packages manually: it is possible to register a package by simply
    putting the appropriate file in the package database directory and
    invoking ``ghc-pkg recache`` to update the cache. This method of
    registering packages may be more convenient for automated packaging
    systems.

Substring matching is supported for ⟨M⟩ in ``find-module`` and for ⟨P⟩
in ``list``, ``describe``, and ``field``, where a ``'*'`` indicates open
substring ends (``prefix*``, ``*suffix``, ``*infix*``). Examples (output
omitted):

.. code-block:: none

    -- list all regex-related packages
    ghc-pkg list '*regex*' --ignore-case
    -- list all string-related packages
    ghc-pkg list '*string*' --ignore-case
    -- list OpenGL-related packages
    ghc-pkg list '*gl*' --ignore-case
    -- list packages exporting modules in the Data hierarchy
    ghc-pkg find-module 'Data.*'
    -- list packages exporting Monad modules
    ghc-pkg find-module '*Monad*'
    -- list names and maintainers for all packages
    ghc-pkg field '*' name,maintainer
    -- list location of haddock htmls for all packages
    ghc-pkg field '*' haddock-html
    -- dump the whole database
    ghc-pkg describe '*'

Additionally, the following flags are accepted by ``ghc-pkg``:

``-f ⟨file⟩``, ``-package-db ⟨file⟩``
    .. index::
       single: -f; ghc-pkg option
       single: -package-db; ghc-pkg option

    Adds ⟨file⟩ to the stack of package databases. Additionally, ⟨file⟩
    will also be the database modified by a ``register``,
    ``unregister``, ``expose`` or ``hide`` command, unless it is
    overridden by a later ``--package-db``, ``--user`` or ``--global``
    option.

``--force``
    .. index::
       single: --force; ghc-pkg option

    Causes ``ghc-pkg`` to ignore missing dependencies, directories and
    libraries when registering a package, and just go ahead and add it
    anyway. This might be useful if your package installation system
    needs to add the package to GHC before building and installing the
    files.

``--global``
    .. index::
       single: --global; ghc-pkg option

    Operate on the global package database (this is the default). This
    flag affects the ``register``, ``update``, ``unregister``,
    ``expose``, and ``hide`` commands.

``--help``, ``-?``
    .. index::
       single: --help; ghc-pkg option
       single: -?; ghc-pkg option

    Outputs the command-line syntax.

``--user``
    .. index::
       single: --user; ghc-pkg option

    Operate on the current user's local package database. This flag
    affects the ``register``, ``update``, ``unregister``, ``expose``,
    and ``hide`` commands.

``-v [⟨n⟩]``, ``--verbose [=⟨n⟩]``
    .. index::
       single: -v; ghc-pkg option
       single: --verbose; ghc-pkg option

    Control verbosity. Verbosity levels range from 0-2, where the
    default is 1, and ``-v`` alone selects level 2.

``-V``; \ ``--version``
    .. index::
       single: -V; ghc-pkg option
       single: --version; ghc-pkg option

    Output the ``ghc-pkg`` version number.

``--ipid``, ``--unit-id``
    .. index::
       single: --ipid; ghc-pkg option
       single: --unit-id; ghc-pkg option

    Causes ``ghc-pkg`` to interpret arguments as installed unit IDs
    (e.g., an identifier like
    ``unix-2.3.1.0-de7803f1a8cd88d2161b29b083c94240``). This is useful
    if providing just the package name and version are ambiguous (in old
    versions of GHC, this was guaranteed to be unique, but this
    invariant no longer necessarily holds).

.. _building-packages:

Building a package from Haskell source
--------------------------------------

.. index::
   single: packages; building

We don't recommend building packages the hard way. Instead, use the
`Cabal <http://www.haskell.org/cabal/users-guide/>`__ infrastructure if
possible. If your package is particularly complicated or requires a lot
of configuration, then you might have to fall back to the low-level
mechanisms, so a few hints for those brave souls follow.

You need to build an "installed package info" file for passing to
``ghc-pkg`` when installing your package. The contents of this file are
described in :ref:`installed-pkg-info`.

The Haskell code in a package may be built into one or more archive
libraries (e.g. ``libHSfoo.a``), or a single shared object (e.g.
``libHSfoo.dll/.so/.dylib``). The restriction to a single shared object
is because the package system is used to tell the compiler when it
should make an inter-shared-object call rather than an
intra-shared-object-call call (inter-shared-object calls require an
extra indirection).

-  Building a static library is done by using the :command:`ar` tool, like so:

   .. code-block:: sh

       ar cqs libHSfoo-1.0.a A.o B.o C.o ...

   where ``A.o``, ``B.o`` and so on are the compiled Haskell modules,
   and ``libHSfoo.a`` is the library you wish to create. The syntax may
   differ slightly on your system, so check the documentation if you run
   into difficulties.

-  To load a package ``foo``, GHCi can load its ``libHSfoo.a`` library
   directly, but it can also load a package in the form of a single
   ``HSfoo.o`` file that has been pre-linked. Loading the ``.o`` file is
   slightly quicker, but at the expense of having another copy of the
   compiled package. The rule of thumb is that if the modules of the
   package were compiled with :ghc-flag:`-split-sections` then building the
   ``HSfoo.o`` is worthwhile because it saves time when loading the
   package into GHCi. Without :ghc-flag:`-split-sections`, there is not much
   difference in load time between the ``.o`` and ``.a`` libraries, so
   it is better to save the disk space and only keep the ``.a`` around.
   In a GHC distribution we provide ``.o`` files for most packages
   except the GHC package itself.

   The ``HSfoo.o`` file is built by Cabal automatically; use
   ``--disable-library-for-ghci`` to disable it. To build one manually,
   the following GNU ``ld`` command can be used:

   .. code-block:: sh

       ld -r --whole-archive -o HSfoo.o libHSfoo.a

   (replace ``--whole-archive`` with ``-all_load`` on MacOS X)

-  When building the package as shared library, GHC can be used to
   perform the link step. This hides some of the details out the
   underlying linker and provides a common interface to all shared
   object variants that are supported by GHC (DLLs, ELF DSOs, and Mac OS
   dylibs). The shared object must be named in specific way for two
   reasons: (1) the name must contain the GHC compiler version, so that
   two library variants don't collide that are compiled by different
   versions of GHC and that therefore are most likely incompatible with
   respect to calling conventions, (2) it must be different from the
   static name otherwise we would not be able to control the linker as
   precisely as necessary to make the :ghc-flag:`-static`\/:ghc-flag:`-dynamic`
   flags work, see :ref:`options-linker`.

   .. code-block:: sh

       ghc -shared -dynamic -o libHSfoo-1.0-ghcGHCVersion.so A.o B.o C.o

   Using GHC's version number in the shared object name allows different
   library versions compiled by different GHC versions to be installed
   in standard system locations, e.g. under \*nix ``/usr/lib``. To obtain
   the version number of GHC invoke ``ghc --numeric-version`` and use
   its output in place of ⟨GHCVersion⟩. See also :ref:`options-codegen`
   on how object files must be prepared for shared object linking.

-  When building a shared library, care must be taken to ensure that the
   resulting object is named appropriately. In particular, GHC expects the
   name of a shared object to have the form ``libHS<unit id>-ghc<ghc
   version>.<ext>`` where *unit id* is the unit ID given during compilation via
   the :ghc-flag:`-this-unit-id ⟨unit-id⟩` flag, *ghc version* is the version of
   GHC that produced/consumes the object and *ext* is the host system's usual
   file extension for shared objects.

To compile a module which is to be part of a new package, use the
``-package-name`` (to identify the name of the package) and
``-library-name`` (to identify the version and the version hashes of its
identities.) options (:ref:`using-packages`). Failure to use these
options when compiling a package will probably result in disaster, but
you will only discover later when you attempt to import modules from the
package. At this point GHC will complain that the package name it was
expecting the module to come from is not the same as the package name
stored in the ``.hi`` file.

It is worth noting with shared objects, when each package is built as a
single shared object file, since a reference to a shared object costs an
extra indirection, intra-package references are cheaper than
inter-package references. Of course, this applies to the ``main``
package as well.

.. _installed-pkg-info:

``InstalledPackageInfo``: a package specification
-------------------------------------------------

A package specification is a Haskell record; in particular, it is the
record :cabal-ref:`Distribution.InstalledPackageInfo.InstalledPackageInfo`
in the module Distribution.InstalledPackageInfo, which is part of the
Cabal package distributed with GHC.

An ``InstalledPackageInfo`` has a human readable/writable syntax. The
functions ``parseInstalledPackageInfo`` and ``showInstalledPackageInfo``
read and write this syntax respectively. Here's an example of the
``InstalledPackageInfo`` for the ``unix`` package:

.. code-block:: none

    $ ghc-pkg describe unix
    name: unix
    version: 2.3.1.0
    id: unix-2.3.1.0-de7803f1a8cd88d2161b29b083c94240
    license: BSD3
    copyright:
    maintainer: libraries@haskell.org
    stability:
    homepage:
    package-url:
    description: This package gives you access to the set of operating system
                 services standardised by POSIX 1003.1b (or the IEEE Portable
                 Operating System Interface for Computing Environments -
                 IEEE Std. 1003.1).
                 .
                 The package is not supported under Windows (except under Cygwin).
    category: System
    author:
    exposed: True
    exposed-modules: System.Posix System.Posix.DynamicLinker.Module
                     System.Posix.DynamicLinker.Prim System.Posix.Directory
                     System.Posix.DynamicLinker System.Posix.Env System.Posix.Error
                     System.Posix.Files System.Posix.IO System.Posix.Process
                     System.Posix.Process.Internals System.Posix.Resource
                     System.Posix.Temp System.Posix.Terminal System.Posix.Time
                     System.Posix.Unistd System.Posix.User System.Posix.Signals
                     System.Posix.Signals.Exts System.Posix.Semaphore
                     System.Posix.SharedMem
    hidden-modules:
    trusted: False
    import-dirs: /usr/lib/ghc-6.12.1/unix-2.3.1.0
    library-dirs: /usr/lib/ghc-6.12.1/unix-2.3.1.0
    hs-libraries: HSunix-2.3.1.0
    extra-libraries: rt util dl
    extra-ghci-libraries:
    include-dirs: /usr/lib/ghc-6.12.1/unix-2.3.1.0/include
    includes: HsUnix.h execvpe.h
    depends: base-4.2.0.0-247bb20cde37c3ef4093ee124e04bc1c
    hugs-options:
    cc-options:
    ld-options:
    framework-dirs:
    frameworks:
    haddock-interfaces: /usr/share/doc/ghc/html/libraries/unix/unix.haddock
    haddock-html: /usr/share/doc/ghc/html/libraries/unix

Here is a brief description of the syntax of this file:

A package description consists of a number of field/value pairs. A field
starts with the field name in the left-hand column followed by a
"``:``", and the value continues until the next line that begins in
the left-hand column, or the end of file.

The syntax of the value depends on the field. The various field types
are:

freeform
    Any arbitrary string, no interpretation or parsing is done.

string
    A sequence of non-space characters, or a sequence of arbitrary
    characters surrounded by quotes ``"...."``.

string list
    A sequence of strings, separated by commas. The sequence may be
    empty.

In addition, there are some fields with special syntax (e.g. package
names, version, dependencies).

The allowed fields, with their types, are:

``name``
    .. index::
       single: name; package specification

    (string) The package's name (without the version).

``id``
    .. index::
       single: id; package specification

    (string) The installed package ID. It is up to you to choose a suitable one.

``version``
    .. index::
       single: version; package specification

    (string) The package's version, usually in the form ``A.B`` (any number of
    components are allowed).

``license``
    .. index::
       single: auto; package specification

    (string) The type of license under which this package is
    distributed. This field is a value of the
    :cabal-ref:`Distribution.License.License` type.

``license-file``
    .. index::
       single: license-file; package specification

    (optional string) The name of a file giving detailed license
    information for this package.

``copyright``
    .. index::
       single: copyright; package specification

    (optional freeform) The copyright string.

``maintainer``
    .. index::
       single: maintainer; package specification

    (optional freeform) The email address of the package's maintainer.

``stability``
    .. index::
       single: stability; package specification

    (optional freeform) A string describing the stability of the package
    (e.g. stable, provisional or experimental).

``homepage``
    .. index::
       single: homepage; package specification

    (optional freeform) URL of the package's home page.

``package-url``
    .. index::
       single: package-url; package specification

    (optional freeform) URL of a downloadable distribution for this
    package. The distribution should be a Cabal package.

``description``
    .. index::
       single: description; package specification

    (optional freeform) Description of the package.

``category``
    .. index::
       single: category; package specification

    (optional freeform) Which category the package belongs to. This
    field is for use in conjunction with a future centralised package
    distribution framework, tentatively titled Hackage.

``author``
    .. index::
       single: author; package specification

    (optional freeform) Author of the package.

``exposed``
    .. index::
       single: exposed; package specification

    (bool) Whether the package is exposed or not.

``exposed-modules``
    .. index::
       single: exposed-modules; package specification

    (string list) modules exposed by this package.

``hidden-modules``
    .. index::
       single: hidden-modules; package specification

    (string list) modules provided by this package, but not exposed to
    the programmer. These modules cannot be imported, but they are still
    subject to the overlapping constraint: no other package in the same
    program may provide a module of the same name.

``reexported-modules``
    .. index::
       single: reexported-modules; reexport specification

    Modules reexported by this package. This list takes the form of
    ``pkg:OldName as NewName (A@orig-pkg-0.1-HASH)``: the first portion
    of the string is the user-written reexport specification (possibly
    omitting the package qualifier and the renaming), while the
    parenthetical is the original package which exposed the module under
    are particular name. Reexported modules have a relaxed overlap
    constraint: it's permissible for two packages to reexport the same
    module as the same name if the reexported moduleis identical.

``trusted``
    .. index::
       single: trusted; package specification

    (bool) Whether the package is trusted or not.

``import-dirs``
    .. index::
       single: import-dirs; package specification

    (string list) A list of directories containing interface files
    (``.hi`` files) for this package.

    If the package contains profiling libraries, then the interface
    files for those library modules should have the suffix ``.p_hi``. So
    the package can contain both normal and profiling versions of the
    same library without conflict (see also ``library_dirs`` below).

``library-dirs``
    .. index::
       single: library-dirs; package specification

    (string list) A list of directories containing libraries for this
    package.

``hs-libraries``
    .. index::
       single: hs-libraries; package specification

    (string list) A list of libraries containing Haskell code for this
    package, with the ``.a`` or ``.dll`` suffix omitted. When packages
    are built as libraries, the ``lib`` prefix is also omitted.

    For use with GHCi, each library should have an object file too. The
    name of the object file does *not* have a ``lib`` prefix, and has
    the normal object suffix for your platform.

    For example, if we specify a Haskell library as ``HSfoo`` in the
    package spec, then the various flavours of library that GHC actually
    uses will be called:

    ``libHSfoo.a``
        The name of the library on Unix and Windows (mingw) systems.

    ``HSfoo.dll``
        The name of the dynamic library on Windows systems (optional).

    ``HSfoo.o``; \ ``HSfoo.obj``
        The object version of the library used by GHCi.

``extra-libraries``
    .. index::
       single: extra-libraries; package specification

    (string list) A list of extra libraries for this package. The
    difference between ``hs-libraries`` and ``extra-libraries`` is that
    ``hs-libraries`` normally have several versions, to support
    profiling, parallel and other build options. The various versions
    are given different suffixes to distinguish them, for example the
    profiling version of the standard prelude library is named
    ``libHSbase_p.a``, with the ``_p`` indicating that this is a
    profiling version. The suffix is added automatically by GHC for
    ``hs-libraries`` only, no suffix is added for libraries in
    ``extra-libraries``.

    The libraries listed in ``extra-libraries`` may be any libraries
    supported by your system's linker, including dynamic libraries
    (``.so`` on Unix, ``.DLL`` on Windows).

    Also, ``extra-libraries`` are placed on the linker command line
    after the ``hs-libraries`` for the same package. If your package has
    dependencies in the other direction (i.e. ``extra-libraries``
    depends on ``hs-libraries``), and the libraries are static, you
    might need to make two separate packages.

``include-dirs``
    .. index::
       single: include-dirs; package specification

    (string list) A list of directories containing C includes for this
    package.

``includes``
    .. index::
       single: includes; package specification

    (string list) A list of files to include for via-C compilations
    using this package. Typically the include file(s) will contain
    function prototypes for any C functions used in the package, in case
    they end up being called as a result of Haskell functions from the
    package being inlined.

``depends``
    .. index::
       single: depends; package specification

    (package id list) Packages on which this package depends.

``hugs-options``
    .. index::
       single: hugs-options; package specification

    (string list) Options to pass to Hugs for this package.

``cc-options``
    .. index::
       single: cc-options; package specification

    (string list) Extra arguments to be added to the gcc command line
    when this package is being used (only for via-C compilations).

``ld-options``
    .. index::
       single: ld-options; package specification

    (string list) Extra arguments to be added to the ``gcc`` command
    line (for linking) when this package is being used.

``framework-dirs``
    .. index::
       single: framework-dirs; package specification

    (string list) On Darwin/MacOS X, a list of directories containing
    frameworks for this package. This corresponds to the
    ``-framework-path`` option. It is ignored on all other platforms.

``frameworks``
    .. index::
       single: frameworks; package specification

    (string list) On Darwin/MacOS X, a list of frameworks to link to.
    This corresponds to the ``-framework`` option. Take a look at
    Apple's developer documentation to find out what frameworks actually
    are. This entry is ignored on all other platforms.

``haddock-interfaces``
    .. index::
       single: haddock-interfaces; package specification

    (string list) A list of filenames containing
    `Haddock <http://www.haskell.org/haddock/>`__ interface files
    (``.haddock`` files) for this package.

``haddock-html``
    .. index::
       single: haddock-html; package specification

    (optional string) The directory containing the Haddock-generated
    HTML for this package.

.. [1] it used to in GHC 6.4, but not since 6.6