Skip to content

Commit 1759cb3

Browse files
author
Your Name
committed
Add StaticPtr approach
1 parent fa1be74 commit 1759cb3

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

src/Hell.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
module Main (main, specMain) where
4444

4545
#if __GLASGOW_HASKELL__ >= 906
46+
import StaticNamed
4647
import GHC.StaticPtr
4748
import Language.Haskell.TH (TExp, examineCode)
4849
import Control.Monad
@@ -546,6 +547,41 @@ lookg :: Gar env t -> env -> TH.Code Q t
546547
lookg (ZGar slot) (_, x) = [|| $$slot $$x ||]
547548
lookg (SGar v) (env, _) = lookg v env
548549

550+
--------------------------------------------------------------------------------
551+
-- StaticPtr-based TH support
552+
553+
-- $((>>= TH.stringE . show . TH.ppr) $ TH.unTypeCode $ compl () $ AppS (LamS (SarS (ZSar [|| id ||]))) $ LitS staticTup)
554+
-- "(\\x_0 -> GHC.Internal.Base.id x_0) (GHC.Internal.StaticPtr.deRefStaticPtr (StaticNamed.grab GHC.Internal.Base.$ Data.Text.unpackCStringLen# \"<binary data>\" (GHC.Types.I# 42#)))"
555+
--
556+
-- ghci> $$(compl () $ AppS (LamS (SarS (ZSar [|| id ||]))) $ LitS staticTup)
557+
-- ()
558+
559+
data Serm g t where
560+
SarS :: Sar g t -> Serm g t
561+
LamS :: Serm (g, TH.Code Q a) b -> Serm g (a -> b)
562+
AppS :: Serm g (s -> t) -> Serm g s -> Serm g t
563+
LitS :: StaticPtr a -> Serm g a
564+
565+
data Sar g t where
566+
ZSar :: TH.Code Q (t -> a) -> Sar (h, TH.Code Q t) a
567+
SSar :: Sar h t -> Sar (h, s) t
568+
569+
-- This is the complr. Type-safe and total.
570+
compl :: env -> Serm env t -> TH.Code Q t
571+
compl env (SarS v) = looks v env
572+
compl env (LamS e) = [|| \x -> $$(compl (env, [||x||]) e) ||]
573+
compl env (AppS e1 e2) = [|| $$(compl env e1) $$(compl env e2) ||]
574+
compl _env (LitS a) = [|| deRefStaticPtr a ||]
575+
576+
-- Type-safe, total lookup. The final @slot@ determines which slot of
577+
-- a given tuple to pick out.
578+
looks :: Sar env t -> env -> TH.Code Q t
579+
looks (ZSar slot) (_, x) = [|| $$slot $$x ||]
580+
looks (SSar v) (env, _) = looks v env
581+
582+
staticTup :: StaticPtr ()
583+
staticTup = static ()
584+
549585
--------------------------------------------------------------------------------
550586
-- Typed AST support for eval
551587
--

0 commit comments

Comments
 (0)