将功能降低为嵌入式语言

时间:2013-08-09 19:53:49

标签: haskell types existential-type gadt

如何以尽可能类型安全的方式将Haskell函数降级为嵌入式语言。特别是,假设我有一个像

这样的值类型
data Type t where
  Num  :: Type Int
  Bool :: Type Bool

data Ty = TNum | TBool deriving Eq

data Tagged t = Tagged (Type t) t deriving Typeable
data Dynamic  = forall t . Typeable t => Dynamic (Tagged t) deriving Typeable

forget :: Typeable t => Tagged t -> Dynamic
forget = Dynamic

remember :: Typeable b => Dynamic -> Maybe b
remember (Dynamic c) = cast c

我希望将像(isSucc :: Int -> Int -> Bool)之类的函数转换为动态表单和某些类型信息的产品,例如

data SplitFun = SF { dynamic    :: [Dynamic] -> Dynamic 
                   , inputTypes :: [Ty] 
                   , outputType :: Ty
                   }

对某些apply函数

(\(a:b:_) -> isSucc a b) == apply (makeDynamicFn isSucc)

模拟一些可能的异常,如果动态类型实际上不匹配则可能抛出这些异常。或者,更明确地说,我想找到makeDynamicFn :: FunType -> SplitFun。显然,这不是一个合适的Haskell类型,并且不太可能从isSucc本身提取类型,所以它可能更像是

anInt . anInt . retBool $ isSucc :: SplitFun

其中anIntretBoolprintf - 样式类型。

这样的事情可能吗?有没有办法模拟它?

1 个答案:

答案 0 :(得分:2)

要实现类型FunType -> SplitFun的函数,我们将使用标准类型类机制来解构函数类型。

现在,实现这个功能直接变得相当困难。要从递归案例中获取inputTypesoutputType,您必须应用您的函数;但是你只能在dynamic字段中应用该函数,这样就无法填充其他字段。相反,我们将任务分为两部分:一部分函数将为我们提供Ty信息,另一部分将构建[Dynamic] -> Dynamic函数。

data Proxy a = Proxy

class Split r where
    dynFun :: r -> [Dynamic] -> Dynamic
    tyInfo :: Proxy r -> ([Ty], Ty)

    split :: r -> SplitFun
    split f = let (i, o) = tyInfo (Proxy :: Proxy r)
              in  SF (dynFun f) i o

现在,tyInfo实际上并不需要该功能,我们只使用Proxy传递类型信息,而无需在整个地方使用undefined。请注意,我们需要ScopedTypeVariables能够从实例声明中引用类型变量r。聪明地使用asTypeOf也可能有用。

我们有两个基本案例:BoolInt

instance Split Int where
    dynFun i _ = forget (Tagged Num i)
    tyInfo _ = ([], TNum)

instance Split Bool where
    dynFun b _ = forget (Tagged Bool b)
    tyInfo _ = ([], TBool)

没有输入类型,因为我们已经有了一个值,所以我们不需要询问更多Dynamic值,只需返回该特定值的Dynamic

接下来,我们有两个递归案例:Bool -> rInt -> r

instance (Split r) => Split (Int -> r) where
    dynFun f (d:ds) = case remember d :: Maybe (Tagged Int) of
        Just (Tagged _ i) -> dynFun (f i) ds
        Nothing           -> error "dynFun: wrong dynamic type"
    dynFun f []     = error "dynFun: not enough arguments"

    tyInfo _ = case tyInfo (Proxy :: Proxy r) of
         (i, o) -> (TNum:i, o)

instance (Split r) => Split (Bool -> r) where
    dynFun f (d:ds) = case remember d :: Maybe (Tagged Bool) of
        Just (Tagged _ b) -> dynFun (f b) ds
        Nothing           -> error "dynFun: wrong dynamic type"
    dynFun f []     = error "dynFun: not enough arguments"

    tyInfo _ = case tyInfo (Proxy :: Proxy r) of
         (i, o) -> (TBool:i, o)

这两个需要FlexibleInstancesdynFun检查第一个Dynamic参数,如果没问题,我们可以安全地将函数f应用于它并从那里继续。我们也可以制作dynFun :: r -> [Dynamic] -> Maybe Dynamic,但这是相当微不足道的改变。


现在,有一些重复。我们可以引入另一个类,例如:

class Concrete r where
    getTy   :: Proxy r -> Ty
    getType :: Proxy r -> Type r

然后写:

instance (Typeable r, Concrete r) => Split r where
    dynFun r _ = forget (Tagged (getType (Proxy :: Proxy r)) r)
    tyInfo _ = ([], getTy (Proxy :: Proxy r))

instance (Typeable r, Concrete r, Split s) => Split (r -> s) where
    dynFun f (d:ds) = case remember d :: Maybe (Tagged r) of
        Just (Tagged _ v) -> dynFun (f v) ds
        -- ...

    tyInfo _ = case tyInfo (Proxy :: Proxy s) of
        (i, o) -> (getTy (Proxy :: Proxy r):i, o)

但这需要OverlappingInstancesUndecidableInstances