Skip to content

Instantly share code, notes, and snippets.

@alt-romes
Created March 9, 2026 15:57
Show Gist options
  • Select an option

  • Save alt-romes/12cfb751f586b0f78dbf6420ec598fef to your computer and use it in GitHub Desktop.

Select an option

Save alt-romes/12cfb751f586b0f78dbf6420ec598fef to your computer and use it in GitHub Desktop.
Allocating Maybe in Cmm foreign import Haskell
{-# LANGUAGE GHCForeignImportPrim, MagicHash, UnliftedFFITypes, UnboxedTuples #-}
import GHC.Exts
foreign import prim "test" tt :: Word# -> Addr#
main = do
case addrToAny# (tt 0##) of
(# mb #) -> print (mb :: Maybe (Maybe ()))
ghc Main.hs X.cmm
./Main # expected result: Just Nothing
#include "Cmm.h"
test (W_ r1)
{
I64[Hp - 16] = ghczminternal_GHCziInternalziMaybe_Just_con_info;
I64[Hp - 8] = ghczminternal_GHCziInternalziMaybe_Nothing_closure;
// Tag 0: Unevaluated
// Tag 1: Nothing
// Tag 2: Just <-- use this one
return (Hp - 14);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment