File: ffi.sgml

package info (click to toggle)
happy 1.11-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,532 kB
  • ctags: 494
  • sloc: haskell: 3,253; sh: 2,576; ansic: 613; makefile: 487
file content (1343 lines) | stat: -rw-r--r-- 34,122 bytes parent folder | download | duplicates (4)
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
<Sect1 id="sec-ffi-intro">
<Title>Introduction
</Title>

<Para>
The motivation behind this foreign function interface (FFI) specification is
to make it possible to describe in Haskell <Emphasis>source code</Emphasis>
the interface to foreign functionality in a Haskell system independent
manner. It builds on experiences made with the previous foreign function
interfaces provided by GHC and Hugs.  However, the FFI specified in this
document is not in the market of trying to completely bridge the gap between
the actual type of an external function, and what is a
<Emphasis>convenient</Emphasis> type for that function to the Haskell
programmer. That is the domain of tools like HaskellDirect or Green Card, both
of which are capable of generating Haskell code that uses this FFI.
</Para>

<Para>
In the following, we will discuss the language extensions of the FFI.
The extensions can be split up into two complementary halves; one half
that provides Haskell constructs for importing foreign functionality
into Haskell, the other which lets you expose Haskell functions to the
outside world. We start with the former, how to import external
functionality into Haskell.
</Para>

</Sect1>

<Sect1 id="sec-ffi-primitive">
<Title>Calling foreign functions
</Title>

<Para>
To bind a Haskell variable name and type to an external function, we
introduce a new construct: <Literal>foreign import</Literal>. It defines the type of a Haskell function together with the name of an external function that actually implements it. The syntax of <Literal>foreign import</Literal> construct is as follows:
</Para>

<Para>

<ProgramListing>
topdecl 
  : ...
  ..
  | 'foreign' 'import' [callconv] [ext_fun] ['unsafe'] varid '::' prim_type
</ProgramListing>

</Para>

<Para>
A <Literal>foreign import</Literal> declaration is only allowed as a toplevel
declaration. It consists of two parts, one giving the Haskell type
(<Literal>prim&lowbar;type</Literal>), Haskell name (<Literal>varid</Literal>) and a flag indicating whether the
primitive is unsafe, the other giving details of the name of the
external function (<Literal>ext&lowbar;fun</Literal>) and its calling interface
(<Literal>callconv</Literal>.)
</Para>

<Para>
Giving a Haskell name and type to an external entry point is clearly
an unsafe thing to do, as the external name will in most cases be
untyped. The onus is on the programmer using <Literal>foreign import</Literal> to
ensure that the Haskell type given correctly maps on to the
type of the external function. 
<XRef LinkEnd="sec-ffi-mapping"> specifies the mapping from 
Haskell types to external types.
</Para>

<Sect2 id="sec-ffi-prim-name">
<Title>Giving the external function a Haskell name
</Title>

<Para>
The external function has to be given a Haskell name. The name
must be a Haskell <Literal>varid</Literal>, so the language rules regarding
variable names must be followed, i.e., it must start with a
lower case letter followed by a sequence of alphanumeric
(`in the Unicode sense') characters or '.

<Footnote>
<Para>
Notice that with Haskell 98, underscore ('&lowbar;') is included in
the character class <Literal>small</Literal>.
</Para>
</Footnote>

</Para>

<Para>
<ProgramListing>
varid : small ( small | large | udigit | ' )*
</ProgramListing>
</Para>

</Sect2>

<Sect2 id="sec-ffi-prim-ext-name">
<Title>Naming the external function
</Title>

<Para>
The name of the external function is a string:
</Para>

<ProgramListing>
ext_fun  : string</ProgramListing>

<Para>
For example,
</Para>

<ProgramListing>
foreign import stdcall "RegCloseKey" regCloseKey :: Ptr a -> IO ()
</ProgramListing>

<Para>
states that the external function named <Function>RegCloseKey</Function> should be bound to the Haskell name <Function>regCloseKey</Function>.</Para>

<Para>
The details of where exactly the external name can be found, such as
whether or not it is dynamically linked, and which library it might
come from, are implementation dependent.  This information is expected
to be provided using a compiler-specific method (eg. GHC uses either
packages or command-line options to specify libraries and extra
include files).</para>

<Para>
If the Haskell name of the imported function is identical to the
external name, the <Literal>ext&lowbar;fun</Literal> can be
omitted. e.g.:
</Para>

<Para>

<ProgramListing>
foreign import sin :: Double -> IO Double
</ProgramListing>

</Para>

<Para>
is identical to 
</Para>

<Para>

<ProgramListing>
foreign import "sin" sin :: Double -> IO Double
</ProgramListing>

</Para>

</Sect2>

<Sect2 id="sec-ffi-cconv">
<Title>Calling conventions
</Title>

<Para>
The number of calling conventions supported is fixed:
</Para>

<Para>

<ProgramListing>
callconv : ccall | stdcall
</ProgramListing>

</Para>

<Para>
<VariableList>

<VarListEntry>
<Term><Literal>ccall</Literal></Term>
<ListItem>
<Para>
The 'default' calling convention on a platform, i.e., the one
used to do (C) function calls.
</Para>

<Para>
In the case of x86 platforms, the caller pushes function arguments
from right to left on the C stack before calling. The caller is
responsible for popping the arguments off of the C stack on return.
</Para>
</ListItem>
</VarListEntry>
<VarListEntry>
<Term><Literal>stdcall</Literal></Term>
<ListItem>
<Para>
A Win32 specific calling convention. The same as <Literal>ccall</Literal>, except
that the callee cleans up the C stack before returning.

<Footnote>
<Para>
The <Literal>stdcall</Literal> is a Microsoft Win32 specific wrinkle; it's used
throughout the Win32 API, for instance. On platforms where
<Literal>stdcall</Literal> isn't meaningful, it should be treated as being equal
to <Literal>ccall</Literal>.
</Para>
</Footnote>

</Para>
</ListItem>
</VarListEntry>
</VariableList>
</Para>

<Para>
<Emphasis remap="bf">Some remarks:</Emphasis>

<ItemizedList>
<ListItem>

<Para>
Interoperating well with external code is the name of the game here,
so the guiding principle when deciding on what calling conventions
to include in <Literal>callconv</Literal> is that there's a demonstrated need for
a particular calling convention. Should it emerge that the inclusion
of other calling conventions will generally improve the quality of
this Haskell FFI, they will be considered for future inclusion in
<Literal>callconv</Literal>.
</Para>
</ListItem>
<ListItem>

<Para>
Supporting <Literal>stdcall</Literal> (and perhaps other platform-specific calling
conventions) raises the issue of whether a Haskell FFI should allow
the user to write platform-specific Haskell code. The calling
convention is clearly an integral part of an external function's
interface, so if the one used differs from the standard one specified
by the platform's ABI <Emphasis>and</Emphasis> that convention is used by a
non-trivial amount of external functions, the view of the FFI authors
is that a Haskell FFI should support it.
</Para>
</ListItem>
<ListItem>

<Para>
For <Literal>foreign import</Literal> (and other <Literal>foreign</Literal> declarations),
supplying the calling convention is optional. If it isn't supplied,
it is treated as if <Literal>ccall</Literal> was specified. Users are encouraged
to leave out the specification of the calling convention, if possible.
</Para>
</ListItem>

</ItemizedList>

</Para>

</Sect2>

<Sect2 id="sec-ffi-prim-types">
<Title>External function types
</Title>

<Para>
The range of types that can be passed as arguments to an external
function is restricted (as are the range of results coming back):
</Para>

<Para>

<ProgramListing>
prim_type : IO prim_result
          | prim_result
          | prim_arg '->' prim_type
</ProgramListing>

</Para>

<Para>

<ItemizedList>
<ListItem>

<Para>
If you associate a non-IO type with an external function, you
have the same 'proof obligations' as when you make use of
<Function>IOExts.unsafePerformIO</Function> in your Haskell programs.
</Para>
</ListItem>
<ListItem>

<Para>
The external function is strict in all its arguments.
</Para>
</ListItem>

</ItemizedList>

</Para>

<Para>
<XRef LinkEnd="sec-ffi-results"> defines
<Literal>prim&lowbar;result</Literal>; <XRef LinkEnd="sec-ffi-arguments">
defines <Literal>prim&lowbar;arg</Literal>.
</Para>

<Sect3 id="sec-ffi-arguments">
<Title>Argument types
</Title>

<Para>
The external function expects zero or more arguments. The set of legal
argument types is restricted to the following set:
</Para>

<Para>

<ProgramListing>
prim_arg : ext_ty | new_ty | ForeignPtr a

new_ty : a Haskell newtype of a prim_arg.

ext_ty : int_ty   | word_ty | float_ty
       | Ptr a    | Char    | StablePtr a
       | Bool

int_ty       : Int   | Int8   | Int16   | Int32 | Int64
word_ty      : Word8 | Word16 | Word32  | Word64
float_ty     : Float | Double
</ProgramListing>

</Para>

<Para>

<ItemizedList>
<ListItem>

<Para>
<Literal>ext&lowbar;ty</Literal> represent the set of basic types supported by
C-like languages, although the numeric types are explicitly sized.

The <Emphasis>stable pointer</Emphasis> <Literal>StablePtr</Literal> type looks out of place in
this list of C-like types, but it has a well-defined and simple
C mapping, see <XRef LinkEnd="sec-ffi-mapping">
for details.

</Para>
</ListItem>
<ListItem>

<Para>
<Literal>prim&lowbar;arg</Literal> represent the set of permissible
argument types. In addition to <Literal>ext&lowbar;ty</Literal>,
<Literal>ForeignPtr</Literal> is also included.

The <Literal>ForeignPtr</Literal> type represent values that are
pointers to some external entity/object. It differs from the
<Literal>Ptr</Literal> type in that <Literal>ForeignPtr</Literal>s are
<Emphasis>finalized</Emphasis>, i.e., once the garbage collector
determines that a <Literal>ForeignPtr</Literal> is unreachable, it
will invoke a finalising procedure attached to the
<Literal>ForeignPtr</Literal> to notify the outside world that we're
through with using it.

</Para>
</ListItem>
<ListItem>

<Para>
Haskell <Literal>newtype</Literal>s that wrap up a
<Literal>prim&lowbar;arg</Literal> type can also be passed to external
functions.
</Para>
</ListItem>
<ListItem>

<Para>
Haskell type synonyms for any of the above can also be used
in <Literal>foreign import</Literal> declarations. Qualified names likewise,
i.e. <Literal>Word.Word32</Literal> is legal.

</Para>
</ListItem>
<ListItem>

<Para>
<Literal>foreign import</Literal> does not support the binding to external
constants/variables. A <Literal>foreign import</Literal> declaration that takes no
arguments represent a binding to a function with no arguments.
</Para>
</ListItem>

<ListItem>
<Para>
A GHC extension is the support for unboxed types:


<ProgramListing>
prim_arg : ...  | unboxed_h_ty
ext_ty   : .... | unboxed_ext_ty

unboxed_ext_ty : Int#   | Word#    | Char#
               | Float# | Double#  | Addr# 
	       | StablePtr# a
unboxed_h_ty : MutableByteArray# | ForeignObj#
             | ByteArray#
</ProgramListing>


Clearly, if you want to be portable across Haskell systems, using 
system-specific extensions such as this is not advisable; avoid
using them if you can. (Support for using unboxed types might
be withdrawn sometime in the future.)
</Para>
</ListItem>

</ItemizedList>

</Para>

</Sect3>

<Sect3 id="sec-ffi-results">
<Title>Result type
</Title>

<Para>
An external function is permitted to return the following
range of types:
</Para>

<Para>

<ProgramListing>
prim_result : ext_ty | new_ext_ty | ()

new_ext_ty : a Haskell newtype of an ext_ty.
</ProgramListing>

</Para>

<Para>
where <Literal>()</Literal> represents <Literal>void</Literal> / no result. 
</Para>

<Para>

<ItemizedList>
<ListItem>

<Para>
External functions cannot raise exceptions (IO exceptions or non-IO ones.)
It is the responsibility of the <Literal>foreign import</Literal> user to layer
any error handling on top of an external function.
</Para>
</ListItem>
<ListItem>

<Para>
Only external types (<Literal>ext&lowbar;ty</Literal>) can be passed
back, i.e., returning <Literal>ForeignPtr</Literal>s is not
supported/allowed.
</Para>
</ListItem>
<ListItem>

<Para>
Haskell newtypes that wrap up <Literal>ext&lowbar;ty</Literal> are also permitted.
</Para>
</ListItem>

</ItemizedList>

</Para>

</Sect3>

</Sect2>

<Sect2 id="sec-ffi-mapping">
<Title>Type mapping
</Title>

<Para>
For the FFI to be of any practical use, the properties and sizes of
the various types that can be communicated between the Haskell world
and the outside, needs to be precisely defined. We do this by
presenting a mapping to C, as it is commonly used and most other
languages define a mapping to it. Table
<XRef LinkEnd="sec-ffi-mapping-table">
defines the mapping between Haskell and C types.
</Para>

<Para>

<Table id="sec-ffi-mapping-table">
<Title>Mapping of Haskell types to C types</Title>

<TGroup Cols="4">
<ColSpec Align="Left" Colsep="0">
<ColSpec Align="Left" Colsep="0">
<ColSpec Align="Left" Colsep="0">
<ColSpec Align="Left" Colsep="0">
<TBody>
<Row RowSep="1">
<Entry>Haskell type </Entry>
<Entry> C type </Entry>
<Entry> requirement </Entry>
<Entry> range (9) </Entry>
<Entry> </Entry>
<Entry> </Entry>
</Row>
<Row>
<Entry>
<Literal>Char</Literal> </Entry>
<Entry> <Literal>HsChar</Literal> </Entry>
<Entry> unspec. integral type </Entry>
<Entry> <Literal>HS&lowbar;CHAR&lowbar;MIN</Literal> .. <Literal>HS&lowbar;CHAR&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Int</Literal> </Entry>
<Entry> <Literal>HsInt</Literal> </Entry>
<Entry> signed integral of unspec. size(4) </Entry>
<Entry> <Literal>HS&lowbar;INT&lowbar;MIN</Literal> ..
<Literal>HS&lowbar;INT&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Int8</Literal> (2) </Entry>
<Entry> <Literal>HsInt8</Literal> </Entry>
<Entry> 8 bit signed integral </Entry>
<Entry> <Literal>HS&lowbar;INT8&lowbar;MIN</Literal> 
..
<Literal>HS&lowbar;INT8&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Int16</Literal> (2) </Entry>
<Entry> <Literal>HsInt16</Literal> </Entry>
<Entry> 16 bit signed integral </Entry>
<Entry> <Literal>HS&lowbar;INT16&lowbar;MIN</Literal>
.. <Literal>HS&lowbar;INT16&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Int32</Literal> (2) </Entry>
<Entry> <Literal>HsInt32</Literal> </Entry>
<Entry> 32 bit signed integral </Entry>
<Entry> <Literal>HS&lowbar;INT32&lowbar;MIN</Literal> ..
<Literal>HS&lowbar;INT32&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Int64</Literal> (2,3) </Entry>
<Entry> <Literal>HsInt64</Literal> </Entry>
<Entry> 64 bit signed integral (3) </Entry>
<Entry> <Literal>HS&lowbar;INT64&lowbar;MIN</Literal> ..
<Literal>HS&lowbar;INT64&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Word8</Literal> (2) </Entry>
<Entry> <Literal>HsWord8</Literal> </Entry>
<Entry> 8 bit unsigned integral </Entry>
<Entry> <Literal>0</Literal> ..
<Literal>HS&lowbar;WORD8&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Word16</Literal> (2) </Entry>
<Entry> <Literal>HsWord16</Literal> </Entry>
<Entry> 16 bit unsigned integral </Entry>
<Entry> <Literal>0</Literal> ..
<Literal>HS&lowbar;WORD16&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Word32</Literal> (2) </Entry>
<Entry> <Literal>HsWord32</Literal> </Entry>
<Entry> 32 bit unsigned integral </Entry>
<Entry> <Literal>0</Literal> ..
<Literal>HS&lowbar;WORD32&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Word64</Literal> (2,3) </Entry>
<Entry> <Literal>HsWord64</Literal> </Entry>
<Entry> 64 bit unsigned integral (3) </Entry>
<Entry> <Literal>0</Literal> ..
<Literal>HS&lowbar;WORD64&lowbar;MAX</Literal></Entry>
</Row>
<Row>
<Entry>
<Literal>Float</Literal> </Entry>
<Entry> <Literal>HsFloat</Literal> </Entry>
<Entry> floating point of unspec. size (5) </Entry>
<Entry> (10) </Entry>
</Row>
<Row>
<Entry>
<Literal>Double</Literal> </Entry>
<Entry> <Literal>HsDouble</Literal> </Entry>
<Entry> floating point of unspec. size (5) </Entry>
<Entry> (10) </Entry>
</Row>
<Row>
<Entry>
<Literal>Bool</Literal> </Entry>
<Entry> <Literal>HsBool</Literal> </Entry>
<Entry> unspec. integral type </Entry>
<Entry> (11) </Entry>
</Row>
<Row>
<Entry>
<Literal>Ptr a</Literal> </Entry>
<Entry> <Literal>HsPtr</Literal> </Entry>
<Entry> void* (6) </Entry>
<Entry> </Entry>
</Row>
<Row>
<Entry>
<Literal>ForeignPtr a</Literal> </Entry>
<Entry> <Literal>HsForeignPtr</Literal> </Entry>
<Entry> void* (7) </Entry>
<Entry> </Entry>
</Row>
<Row>
<Entry>
<Literal>StablePtr a</Literal> </Entry>
<Entry> <Literal>HsStablePtr</Literal> </Entry>
<Entry> void* (8) </Entry>
<Entry> </Entry>
</Row>
</TBody>

</TGroup>

</Table>

</Para>

<Para>
<Emphasis remap="bf">Some remarks:</Emphasis>

<OrderedList>
<ListItem>

<Para>
A Haskell system that implements the FFI will supply a header file
<Filename>HsFFI.h</Filename> that includes target platform specific definitions
for the above types and values.
</Para>
</ListItem>
<ListItem>

<Para>
The sized numeric types <Literal>Hs&lcub;Int,Word&rcub;&lcub;8,16,32,64&rcub;</Literal> have
a 1-1 mapping to ISO C 99's <Literal>&lcub;,u&rcub;int&lcub;8,16,32,64&rcub;&lowbar;t</Literal>. For systems
that doesn't support this revision of ISO C, a best-fit mapping
onto the supported C types is provided.
</Para>
</ListItem>
<ListItem>

<Para>
An implementation which does not support 64 bit integral types
on the C side should implement <Literal>Hs&lcub;Int,Word&rcub;64</Literal> as a struct. In
this case the bounds <Constant>HS&lowbar;INT64&lowbar;&lcub;MIN,MAX&rcub;</Constant> and <Constant>HS&lowbar;WORD64&lowbar;MAX</Constant>
are undefined.
</Para>
</ListItem>
<ListItem>

<Para>
A valid Haskell representation of <Literal>Int</Literal> has to be equal to or
wider than 30 bits. The <Literal>HsInt</Literal> synonym is guaranteed to map
onto a C type that satisifies Haskell's requirement for <Literal>Int</Literal>.
</Para>
</ListItem>
<ListItem>

<Para>
It is guaranteed that <Literal>Hs&lcub;Float,Double&rcub;</Literal> are one of C's
floating-point types <Literal>float</Literal>/<Literal>double</Literal>/<Literal>long double</Literal>.
</Para>
</ListItem>
<ListItem>

<Para>
It is guaranteed that <Literal>HsAddr</Literal> is of the same size as <Literal>void*</Literal>, so
any other pointer type can be converted to and from HsAddr without any
loss of information (K&amp;R, Appendix A6.8).
</Para>
</ListItem>
<ListItem>

<Para>
Foreign objects are handled like <Literal>Ptr</Literal> by the FFI, so there
is again the guarantee that <Literal>HsForeignPtr</Literal> is the same as
<Literal>void*</Literal>. The separate name is meant as a reminder that there is
a finalizer attached to the object pointed to.
</Para>
</ListItem>
<ListItem>

<Para>
Stable pointers are passed as addresses by the FFI, but this is
only because a <Literal>void*</Literal> is used as a generic container in most
APIs, not because they are real addresses. To make this special
case clear, a separate C type is used here. 
</Para>
</ListItem>
<ListItem>

<Para>
The bounds are preprocessor macros, so they can be used in
<Literal>&num;if</Literal> and for array bounds.
</Para>
</ListItem>
<ListItem>

<Para>
Floating-point limits are a little bit more complicated, so
preprocessor macros mirroring ISO C's <Filename>float.h</Filename> are provided:

<ProgramListing>
HS_{FLOAT,DOUBLE}_RADIX
HS_{FLOAT,DOUBLE}_ROUNDS
HS_{FLOAT,DOUBLE}_EPSILON
HS_{FLOAT,DOUBLE}_DIG
HS_{FLOAT,DOUBLE}_MANT_DIG
HS_{FLOAT,DOUBLE}_MIN
HS_{FLOAT,DOUBLE}_MIN_EXP
HS_{FLOAT,DOUBLE}_MIN_10_EXP
HS_{FLOAT,DOUBLE}_MAX
HS_{FLOAT,DOUBLE}_MAX_EXP
HS_{FLOAT,DOUBLE}_MAX_10_EXP
</ProgramListing>

</Para>
</ListItem>
<ListItem>

<Para>
It is guaranteed that Haskell's <Literal>False</Literal>/<Literal>True</Literal> map to
C's <Literal>0</Literal>/<Literal>1</Literal>, respectively, and vice versa. The mapping of
any other integral value to <Literal>Bool</Literal> is left unspecified.
</Para>
</ListItem>
<ListItem>

<Para>
To avoid name clashes, identifiers starting with <Literal>Hs</Literal> and
macros starting with <Literal>HS&lowbar;</Literal> are reserved for the FFI.
</Para>
</ListItem>
<ListItem>

<Para>
<Emphasis>GHC only:</Emphasis> The GHC specific types <Literal>ByteArray</Literal> and
<Literal>MutableByteArray</Literal> both map to <Literal>char*</Literal>.
</Para>
</ListItem>

</OrderedList>

</Para>

</Sect2>

<Sect2 id="sec-ffi-prim-remarks">
<Title>Some <Literal>foreign import</Literal> wrinkles
</Title>

<Para>

<ItemizedList>
<ListItem>

<Para>
By default, a <Literal>foreign import</Literal> function is <Emphasis>safe</Emphasis>. A safe
external function may cause a Haskell garbage collection as a result
of being called. This will typically happen when the imported
function end up calling Haskell functions that reside in the same
'Haskell world' (i.e., shares the same storage manager heap) -- see
<XRef LinkEnd="sec-ffi-entry"> for
details of how the FFI let's you call Haskell functions from the outside.

If the programmer can guarantee that the imported function won't
call back into Haskell, the <Literal>foreign import</Literal> can be marked as
'unsafe' (see <XRef LinkEnd="sec-ffi-primitive"> for details of
how to do this.)

Unsafe calls are cheaper than safe ones, so distinguishing the two
classes of external calls may be worth your while if you're extra
conscious about performance.

</Para>
</ListItem>
<ListItem>

<Para>
A <Literal>foreign import</Literal>ed function should clearly not need to know that
it is being called from Haskell. One consequence of this is that the
lifetimes of the arguments that are passed from Haskell <Emphasis>must</Emphasis>
equal that of a normal C call. For instance, for the following decl,


<ProgramListing>
foreign import "mumble" mumble :: ForeignPtr a -> IO ()

f :: Ptr a -> IO ()
f ptr = do
  fo &#60;- newForeignObj ptr myFinalizer
  mumble fo
</ProgramListing>


The <Literal>ForeignPtr</Literal> must live across the call to
<Function>mumble</Function> even if it is not subsequently
used/reachable. Why the insistence on this?  Consider what happens if
<Function>mumble</Function> calls a function which calls back into the
Haskell world to execute a function, behind our back as it were. This
evaluation may possibly cause a garbage collection, with the result
that <Literal>fo</Literal> may end up being finalised.

By guaranteeing that <Literal>fo</Literal> will be considered live
across the call to <Function>mumble</Function>, the unfortunate
situation where <Literal>fo</Literal> is finalised (and hence the
reference passed to <Function>mumble</Function> is suddenly no longer
valid) is avoided.


</Para>
</ListItem>

</ItemizedList>

</Para>

</Sect2>

</Sect1>

<Sect1 id="sec-ffi-prim-dynamic">
<Title>Invoking external functions via a pointer
</Title>

<Para>
A <Literal>foreign import</Literal> declaration imports an external 
function into Haskell. (The name of the external function
is statically known, but the loading/linking of it may very well
be delayed until run-time.) A <Literal>foreign import</Literal> declaration is then
(approximately) just a type cast of an external function with a
<Emphasis>statically known name</Emphasis>. 
</Para>

<Para>
An extension of <Literal>foreign import</Literal> is the support for <Emphasis>dynamic</Emphasis> type
casts of external names/addresses:
</Para>

<Para>

<ProgramListing>
topdecl 
   : ...
   ..
   | 'foreign' 'import' [callconv] 'dynamic' ['unsafe']
            varid :: Addr -> (prim_args -> IO prim_result)
</ProgramListing>

</Para>

<Para>
i.e., identical to a <Literal>foreign import</Literal> declaration, but for the
specification of <Literal>dynamic</Literal> instead of the name of an external
function. The presence of <Literal>dynamic</Literal> indicates that when an
application of <Literal>varid</Literal> is evaluated, the function pointed to by its
first argument will be invoked, passing it the rest of <Literal>varid</Literal>'s
arguments.
</Para>

<Para>
What are the uses of this? Native invocation of COM methods,
<Footnote>
<Para>
Or the interfacing to any other software component technologies.
</Para>
</Footnote>
Haskell libraries that want to be dressed up as C libs (and hence may have
to support C callbacks), Haskell code that need to dynamically load
and execute code.
</Para>

</Sect1>

<Sect1 id="sec-ffi-entry">
<Title>Exposing Haskell functions
</Title>

<Para>
So far we've provided the Haskell programmer with ways of importing
external functions into the Haskell world. The other half of the FFI
coin is how to expose Haskell functionality to the outside world. So,
dual to the <Literal>foreign import</Literal> declaration is <Literal>foreign export</Literal>:
</Para>

<Para>

<ProgramListing>
topdecl 
  : ...
  ..
  | 'foreign' 'export' callconv [ext_name] varid :: prim_type
</ProgramListing>

</Para>

<Para>
A <Literal>foreign export</Literal> declaration tells the compiler to expose a
locally defined Haskell function to the outside world, i.e., wrap
it up behind a calling interface that's useable from C. It is only
permitted at the toplevel, where you have to specify the type at
which you want to export the function, along with the calling
convention to use. For instance, the following export declaration:
</Para>

<Para>

<ProgramListing>
foreign export ccall "foo" bar :: Int -> Addr -> IO Double
</ProgramListing>

</Para>

<Para>
will cause a Haskell system to generate the following C callable
function:
</Para>

<Para>

<ProgramListing>
HsDouble foo(HsInt arg1, HsAddr arg2);
</ProgramListing>

</Para>

<Para>
When invoked, it will call the Haskell function <Function>bar</Function>, passing
it the two arguments that was passed to <Function>foo()</Function>. 
</Para>

<Para>

<ItemizedList>
<ListItem>

<Para>
The range of types that can be passed as arguments and results
is restricted, since <Literal>varid</Literal> has got a <Literal>prim&lowbar;type</Literal>.
</Para>
</ListItem>
<ListItem>

<Para>
It is not possible to directly export operator symbols.
</Para>
</ListItem>
<ListItem>

<Para>
The type checker will verify that the type given for the
<Literal>foreign export</Literal> declaration is compatible with the type given to
function definition itself.  The type in the <Literal>foreign export</Literal> may
be less general than that of the function itself.  For example,
this is legal:


<ProgramListing>
   f :: Num a => a -> a
   foreign export ccall "fInt"   f :: Int -> Int
   foreign export ccall "fFloat" f :: Float -> Float
</ProgramListing>


These declarations export two C-callable procedures <Literal>fInt</Literal> and
<Literal>fFloat</Literal>, both of which are implemented by the (overloaded)
Haskell function <Function>f</Function>.

</Para>
</ListItem>
<ListItem>

<Para>
The <Literal>foreign export</Literal>ed IO action must catch all exceptions, as
the FFI does not address how to signal Haskell exceptions to the
outside world.
</Para>
</ListItem>

</ItemizedList>

</Para>

<Sect2 id="sec-ffi-callback">
<Title>Exposing Haskell function values
</Title>

<Para>
The <Literal>foreign export</Literal> declaration gives the C programmer access to
statically defined Haskell functions. It does not allow you to
conveniently expose dynamically-created Haskell function values as C
function pointers though. To permit this, the FFI supports
<Emphasis>dynamic</Emphasis> <Literal>foreign export</Literal>s:
</Para>

<Para>

<ProgramListing>
topdecl 
  : ...
  ..
  | 'foreign' 'export' [callconv] 'dynamic' varid :: prim_type -> IO Addr
</ProgramListing>

</Para>

<Para>
A <Literal>foreign export dynamic</Literal> declaration declares a C function
pointer <Emphasis>generator</Emphasis>. Given a Haskell function value of some restricted
type, the generator wraps it up behind an externally callable interface,
returning an <Literal>Addr</Literal> to an externally callable (C) function pointer.
</Para>

<Para>
When that function pointer is eventually called, the corresponding
Haskell function value is applied to the function pointer's arguments
and evaluated, returning the result (if any) back to the caller.
</Para>

<Para>
The mapping between the argument to a <Literal>foreign export dynamic</Literal>
declaration and its corresponding C function pointer type, is as
follows:
</Para>

<Para>

<ProgramListing>
typedef cType[[Res]] (*Varid_FunPtr)
        (cType[[Ty_1]] ,.., cType[[Ty_n]]);
</ProgramListing>

</Para>

<Para>
where <Literal>cType[[]]</Literal> is the Haskell to C type mapping presented
in <XRef LinkEnd="sec-ffi-mapping">.
</Para>

<Para>
To make it all a bit more concrete, here's an example:
</Para>

<Para>

<ProgramListing>
foreign export dynamic mkCallback :: (Int -> IO Int) -> IO Addr

foreign import registerCallback :: Addr -> IO ()

exportCallback :: (Int -> IO Int) -> IO ()
exportCallback f = do
  fx &#60;- mkCallback f
  registerCallback fx
</ProgramListing>

</Para>

<Para>
The <Literal>exportCallback</Literal> lets you register a Haskell function value as
a callback function to some external library. The C type of the
callback that the external library expects in <Literal>registerCallback()</Literal>,
is:
<Footnote>
<Para>
An FFI implementation is encouraged to generate the C typedef corresponding
to a <Literal>foreign export dynamic</Literal> declaration, but isn't required
to do so.
</Para>
</Footnote>

</Para>

<Para>

<ProgramListing>
typedef HsInt (*mkCallback_FunPtr) (HsInt arg1);
</ProgramListing>

</Para>

<Para>
Creating the view of a Haskell closure as a C function pointer entails
registering the Haskell closure as a 'root' with the underlying
Haskell storage system, so that it won't be garbage collected. The FFI
implementation takes care of this, but when the outside world is
through with using a C function pointer generated by a <Literal>foreign
export dynamic</Literal> declaration, it needs to be explicitly freed. This is
done by calling:
</Para>

<Para>

<ProgramListing>
void freeHaskellFunctionPtr(void *ptr);
</ProgramListing>

</Para>

<Para>
In the event you need to free these function pointers from within
Haskell, a standard 'foreign import'ed binding of the above C entry
point is also provided,
</Para>

<Para>

<ProgramListing>
Foreign.freeHaskellFunctionPtr :: Addr -> IO ()
</ProgramListing>

</Para>

</Sect2>

<Sect2 id="sec-ffi-foreign-label">
<Title>Code addresses
</Title>

<Para>
The <Literal>foreign import</Literal> declaration allows us to invoke an external
function by name from within the comforts of the Haskell world, while
<Literal>foreign import dynamic</Literal> lets us invoke an external function by
address. However, there's no way of getting at the code address of
some particular external label though, which is at times useful,
e.g. for the construction of method tables for, say, Haskell COM
components. To support this, the FFI has got <Literal>foreign label</Literal>s:
</Para>

<Para>

<ProgramListing>
foreign label "freeAtLast" addrOf_freeAtLast :: Addr
</ProgramListing>

</Para>

<Para>
The meaning of this declaration is that <Literal>addrOf&lowbar;freeAtLast</Literal> will now
contain the address of the label <Literal>freeAtLast</Literal>.
</Para>

</Sect2>

</Sect1>
<!-- This doesn't need to be seen in the docs
<Sect1 id="sec-ffi-changelog">
<Title>Change history
</Title>

<Para>

<ItemizedList>
<ListItem>

<Para>
0.95 &gt; 0.96:

<ItemizedList>
<ListItem>

<Para>
changed the C representation of
<Literal>Haskell&lowbar;ForeignPtr</Literal> from
<Literal>(long*)</Literal> to <Literal>(void*)</Literal> ANSI C
guarantees that <Literal>(void*)</Literal> is the widest possible data
pointer.
</Para>
</ListItem>
<ListItem>

<Para>
Updated defnition of <Literal>varid</Literal> in
<XRef LinkEnd="sec-ffi-prim-name"> to reflect Haskell98's.
</Para>
</ListItem>
<ListItem>

<Para>
Replaced confusing uses of <Literal>stdcall</Literal> with <Literal>ccall</Literal>.
</Para>
</ListItem>

</ItemizedList>

</Para>
</ListItem>
<ListItem>

<Para>
0.96 &gt; 0.97:

<ItemizedList>
<ListItem>

<Para>
Simplified the calling convention section, support for Pascal (and
fastcall) calling conventions dropped. 
</Para>
</ListItem>
<ListItem>

<Para>
Clarified that the arguments to a safe <Literal>foreign import</Literal> must have
lifetimes that equal that of a C function application.
</Para>
</ListItem>
<ListItem>

<Para>
Outlawed the use of the (GHC specific) types <Literal>ByteArray</Literal>
and <Literal>MutableByteArray</Literal> in safe <Literal>foreign import</Literal>s.
</Para>
</ListItem>
<ListItem>

<Para>
Added a note that support for the use of unboxed types in
<Literal>foreign import</Literal> may be withdrawn/deprecated sometime in the future.
</Para>
</ListItem>
<ListItem>

<Para>
Simplified section which sketches a possible implementation.
</Para>
</ListItem>
<ListItem>

<Para>
Use <Literal>Hs</Literal> as prefix for the typedefs for the primitive Haskell
FFI types rather than the longer <Literal>Haskell&lowbar;</Literal>.
</Para>
</ListItem>

</ItemizedList>

</Para>
</ListItem>
<ListItem>

<Para>
0.97 &gt; 0.98:

<ItemizedList>
<ListItem>

<Para>
Leave out implementation section; of limited interest.
</Para>
</ListItem>
<ListItem>

<Para>
Outlined the criteria used to decide on what calling
conventions to support.
</Para>
</ListItem>
<ListItem>

<Para>
Include <Literal>newtype</Literal>s that wrap primitive types in the list
of types that can be both passed to and returned from external
functions.
</Para>
</ListItem>

</ItemizedList>

</Para>
</ListItem>
<ListItem>

<Para>
0.98 &gt; 0.99:

<ItemizedList>
<ListItem>

<Para>
Updated the section on type mapping to integrate some comments
from people on &lt;ffi@haskell.org&gt; (a fair chunk of the text
in that section was contributed by Sven Panne.)
</Para>
</ListItem>
<ListItem>

<Para>
<Function>freeHaskellFunctionPtr</Function> should belong to module <Literal>Foreign</Literal>, not <Literal>IOExts</Literal>.
</Para>
</ListItem>

</ItemizedList>


</Para>
</ListItem>
<ListItem>

<Para>
0.99 &gt; 0.99.1:

<ItemizedList>
<ListItem>

<Para>
<Literal>Bool</Literal> is now an FFI-supported type (i.e., added it to
<Literal>ext&lowbar;ty</Literal>.)
</Para>
</ListItem>

</ItemizedList>


</Para>
</ListItem>

</ItemizedList>

</Para>

</Sect1>
-->