Boxed foreign prim

Joachim Breitner mail at
Fri Mar 9 08:04:57 CET 2012


Am Donnerstag, den 08.03.2012, 23:12 -0500 schrieb Edward Kmett:
> But I can only pass unboxed types to foreign prim. 
> Is this an intrinsic limitation or just an artifact of the use cases
> that have presented themselves to date? 

funny, I just stumbled over this two days ago as well. In my case, I’m
trying to investigate the actual data on the heap, so I am also trying
to pass arbitrary values to a foreign prim function, which then receive
the pointer to the heap object as the argument. Since real primops (e.g.
unpackClosure#) can take arbitrary values, I figured that this is an
artifact, as you say.

I made this change to enable this feature:

diff --git a/compiler/typecheck/TcType.lhs b/compiler/typecheck/TcType.lhs
index 669545a..83a59bd 100644
--- a/compiler/typecheck/TcType.lhs
+++ b/compiler/typecheck/TcType.lhs
@@ -1166,7 +1166,7 @@ isOverloadedTy _               = False
 isFloatTy, isDoubleTy, isIntegerTy, isIntTy, isWordTy, isBoolTy,
-    isUnitTy, isCharTy :: Type -> Bool
+    isUnitTy, isCharTy, isAnyTy :: Type -> Bool
 isFloatTy      = is_tc floatTyConKey
 isDoubleTy     = is_tc doubleTyConKey
 isIntegerTy    = is_tc integerTyConKey
@@ -1175,6 +1175,7 @@ isWordTy       = is_tc wordTyConKey
 isBoolTy       = is_tc boolTyConKey
 isUnitTy       = is_tc unitTyConKey
 isCharTy       = is_tc charTyConKey
+isAnyTy        = is_tc anyTyConKey
 isStringTy :: Type -> Bool
 isStringTy ty
@@ -1344,7 +1345,7 @@ isFFIPrimArgumentTy :: DynFlags -> Type -> Bool
 -- Checks for valid argument type for a 'foreign import prim'
 -- Currently they must all be simple unlifted types.
 isFFIPrimArgumentTy dflags ty
-   = checkRepTyCon (legalFIPrimArgTyCon dflags) ty
+   = isAnyTy ty || checkRepTyCon (legalFIPrimArgTyCon dflags) ty
 isFFIPrimResultTy :: DynFlags -> Type -> Bool
 -- Checks for valid result type for a 'foreign import prim'

Initially, I had isFFIPrimArgumentTy modfified to really accept "a ->",
but I found it cleaner to just accept the Any type, to make it more
obvious that, morally speaking, not the value is passed to the function,
but a pointer to Any thing. In the Haskell code, I’m unsafeCoerce#’ing
the a to Any. Seems to work smoothly.


Joachim "nomeata" Breitner
  mail at  |  nomeata at  |  GPG: 0x4743206C
  xmpp: nomeata at |

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <>

More information about the Glasgow-haskell-users mailing list