考虑以下GADT定义的表达式函子:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}

import Control.Monad.Free

data ExprF :: * -> * where
  Term :: Foo a -> (a -> r) -> ExprF r

instance Functor ExprF where
  fmap f (Term d k) = Term d (f . k)

type Expr = Free ExprF
Foo定义为
data Foo :: * -> * where
  Bar :: Int    -> Foo Int
  Baz :: Double -> Foo Double

instance Show a => Show (Foo a) where
  show (Bar j) = show j
  show (Baz j) = show j
(a -> r)中的ExprF字段与(否则希望)限制性的GADT构造函数的结合,似乎使编写漂亮的打印解释器变得不可能:
pretty (Pure r)          = show r
pretty (Free (Term f k)) = "Term " ++ show f ++ pretty (k _)

类型孔是人们期望的:
Found hole ‘_’ with type: a1
Where: ‘a1’ is a rigid type variable bound by
            a pattern with constructor
              Term :: forall r a. Foo a -> (a -> r) -> ExprF r,
            in an equation for ‘pretty’
            at Test.hs:23:15
Relevant bindings include
  k :: a1 -> Free ExprF a (bound at Test.hs:23:22)
  f :: Foo a1 (bound at Test.hs:23:20)
  pretty :: Free ExprF a -> String (bound at Test.hs:22:1)
In the first argument of ‘k’, namely ‘_’
In the first argument of ‘pretty’, namely ‘(k _)’
In the second argument of ‘(++)’, namely ‘pretty (k _)’

似乎没有办法为延续性提供所需的类型的值。该类型使用f编码,我正在使用的其他解释器都以某种方式处理f以提取适当类型的值。但是String表示形式的路径似乎被阻塞。

我在这里缺少一些常见的成语吗?如果确实有可能,如何打印漂亮的Expr值?如果不可能的话,ExprF的哪种替代构造可以捕获相同的结构,但还支持漂亮的打印机?

最佳答案

只需对f进行模式匹配。如果这样做,k的类型将被精炼以匹配Foo内包含的类型:

pretty (Pure r)          = show r
pretty (Free (Term f k)) = "Term " ++ show f ++ pretty r where
  r = case f of
    Bar a -> k a
    Baz a -> k a

您可能需要排除这种模式:
applyToFoo :: (a -> r) -> Foo a -> r
applyToFoo f (Bar a) = f a
applyToFoo f (Baz a) = f a

pretty (Pure r)          = show r
pretty (Free (Term f k)) = "Term " ++ show f ++ pretty (applyToFoo k f)

关于haskell - 使用免费的monad和GADT进行 pretty-print ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29455850/

10-12 16:32