使用Typeable在运行时部分应用功能(任何时间类型匹配) [英] Using Typeable to partially apply function at run-time (any time types match)
问题描述
通用编程时间!
如果我有功能:
f :: a1 -> a2 -> a3 -> ... -> an
和一个值
v :: aX -- where 1 <= x < n
在编译时不知道f
的哪个参数是v
的正确类型(如果有),是否可以将f
部分应用于v
? (使用Typeable,Data,TH或其他任何技巧)
Without knowing at compile time which of the arguments of f
the value v
is the right type for (if any), can I partially apply f
to v
? (using Typeable, Data, TH, or any other trick)
稍微可靠一点,我可以在运行时构造函数g
(如下)吗?实际上,它不一定是多态的,我所有的类型都是单态的!
Slightly more solidly, can I construct the function g
(below) at run-time? It doesn't actually have to be polymorphic, all my types will be monomorphic!
g :: (a1 -> a2 -> a3 -> a4 -> a5) -> a3 -> (a1 -> a2 -> a4 -> a5)
g f v = \x y z -> f x y v z
我知道,使用Typeable(特别是typeRepArgs
),v
是f
的第三个参数,但这并不意味着我有办法部分应用f
.
I know that, using Typeable (typeRepArgs
specifically), v
is the 3rd argument of f
, but that doesn't mean I have a way to partially apply f
.
我的代码可能看起来像:
My code would probably look like:
import Data.Typeable
data Box = forall a. Box (TyRep, a)
mkBox :: Typeable a => a -> Box
mkBox = (typeOf a, a)
g :: Box -> Box -> [Box]
g (Box (ft,f)) (Box (vt,v)) =
let argNums = [n | n <- [1..nrArgs], isNthArg n vt ft]
in map (mkBox . magicApplyFunction f v) argNums
isNthArg :: Int -> TyRep -> TyRep -> Bool
isNthArg n arg func = Just arg == lookup n (zip [1..] (typeRepArgs func))
nrArgs :: TyRep -> Int
nrArgs = (\x -> x - 1) . length . typeRepArgs
有什么可以实现magicApplyFunction
的东西吗?
Is there anything that can implement the magicApplyFunction
?
我终于回到了这个玩法.神奇的套用功能是:
I finally got back to playing with this. The magic apply function is:
buildFunc :: f -> x -> Int -> g
buildFunc f x 0 = unsafeCoerce f x
buildFunc f x i =
let !res = \y -> (buildFunc (unsafeCoerce f y) x (i-1))
in unsafeCoerce res
推荐答案
我暂时不打算在此处编写整个解决方案,但是我敢肯定,这完全可以使用Data.Dynamic
和Typeable
完成. dynApply
和funResultTy
的来源应提供关键元素:
I'm not going to write the whole solution here for now, but I'm sure this can be done purely with Data.Dynamic
and Typeable
. The source for dynApply
and funResultTy
should provide the key elements:
dynApply :: Dynamic -> Dynamic -> Maybe Dynamic
dynApply (Dynamic t1 f) (Dynamic t2 x) =
case funResultTy t1 t2 of
Just t3 -> Just (Dynamic t3 ((unsafeCoerce f) x))
Nothing -> Nothing
funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep
funResultTy trFun trArg
= case splitTyConApp trFun of
(tc, [t1,t2]) | tc == funTc && t1 == trArg -> Just t2
_ -> Nothing
为简单起见,我将使用type Box = (Dynamic, [Either TypeRep Dynamic])
.后者以参数的typerep列表开始. magicApply
将在框中寻找第一个匹配的TypeRep,并替换值的Dynamic
.然后,您可能会得到一个extract
,其中给定了Box
的所有参数都已魔术地应用到该Box
,实际上执行了dynApply
调用以产生最终的动态结果.
To keep things simple, I'd have type Box = (Dynamic, [Either TypeRep Dynamic])
. The latter starts out as a list of typereps of arguments. magicApply
would look for the first matching TypeRep in the box and substitute the Dynamic
of the value. Then you could have an extract
that given a Box
to which all arguments have been magicapplied, actually performs the dynApply
calls to produce the resulting dynamic result.
这篇关于使用Typeable在运行时部分应用功能(任何时间类型匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!