You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a Helper.hs module that I feel is obfuscating what could be straightforward/simple bindings:
--| Given a name @fname@, a name of a C function @cname@ and the desired-- Haskell type @ftype@, this function generates:---- * A foreign import of @cname@, named as @fname'@.-- * An always-inline MonadIO version of @fname'@, named @fname@.liftF::String->String->QType->Q [Dec]
liftF fname cname ftype =dolet f' = mkName $ fname ++"'"-- Direct binding.let f = mkName fname -- Lifted.
t' <- ftype -- Type of direct binding.-- The generated function accepts n arguments.
args <- replicateM (countArgs t') $ newName "x"-- If the function has no arguments, then we just liftIO it directly.-- However, this fails to typecheck without an explicit type signature.-- Therefore, we include one. TODO: Can we get rid of this?
sigd <-case args of[]-> ((:[]) .SigD f) `fmap` liftType t'
_ ->return[]return$concat
[
[ ForeignD$ImportFCCallSafe cname f' t'
, PragmaD$InlineP f InlineFunLikeAllPhases
]
, sigd
, [ FunD f
[ Clause
(mapVarP args)
(NormalB$ 'liftIO `applyTo` [f' `applyTo`mapVarE args])
[]
]
]
]
--| How many arguments does a function of a given type take?countArgs::Type->Int
countArgs = count 0where
count !n =\case
(AppT (AppTArrowT _) t) -> count (n+1) t
(ForallT _ _ t) -> count n t
(SigT t _) -> count n t
_ -> n
--| An expression where f is applied to n arguments.applyTo::Name-> [Exp] ->Exp
applyTo f []=VarE f
applyTo f es = loop (tail es) .AppE (VarE f) $head es
where
loop as e =foldlAppE e as
--| Fuzzily speaking, converts a given IO type into a MonadIO m one.liftType::Type->QType
liftType =\caseAppT _ t ->do
m <- newName "m"return$ForallT
[PlainTV m]
[AppT (ConT ''MonadIO) $VarT m]
(AppT (VarT m) t)
t ->return t
I suggest each function just be written by hand, and all this TH deleted. I'm happy to provide a patch if you agree!
The text was updated successfully, but these errors were encountered:
There is a
Helper.hs
module that I feel is obfuscating what could be straightforward/simple bindings:I suggest each function just be written by hand, and all this TH deleted. I'm happy to provide a patch if you agree!
The text was updated successfully, but these errors were encountered: