我正在设计一种打字的形式语言,即一种形式语言,其中每个字母都有特定类型的表示。到目前为止,我有以下内容:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Kind
data Typey (s :: c) = Marker
class Character c where
showVal :: Typey (s :: c a) -> a -> ShowS
class Character (Letter l) => Alphabet l where
data family Letter l :: * -> *
data Symbol l (a :: Letter l s) where
Terminal :: (Alphabet l) => s -> Symbol l (a :: Letter l s)
instance (Alphabet l) => Show (Symbol l a) where
showsPrec d (Terminal val) = showVal (Marker :: Typey a) val
-- Example language
data ExampleLanguage = ExampleLanguage
instance Alphabet ExampleLanguage where
data Letter ExampleLanguage a where
Variable :: Letter ExampleLanguage String
Comment :: Letter ExampleLanguage String
EqualSign :: Letter ExampleLanguage ()
Deref :: Letter ExampleLanguage ()
instance Character (Letter ExampleLanguage) where
-- showVal (_ :: Typey Variable) = showString
-- showVal (_ :: Typey Comment) = showString
-- showVal (_ :: Typey EqualSign) = const $ showString "="
-- showVal (_ :: Typey Deref) = const $ showString "*"
showVal _ = const $ showString "error"
test :: Symbol ExampleLanguage Comment
test = Terminal "some comment"
你可能已经看到我开始实现showVal
但是在这里,我在完成实现时遇到了问题。当我尝试取消注释instance Letter (Character ExampleLanguage)
中的行时,编译器很快就会抱怨:
main.hs:40:14: error:
• Couldn't match type ‘a1’ with ‘()’
‘a1’ is a rigid type variable bound by
the type signature for:
showVal :: forall k (a :: k) a1 (s :: Character
ExampleLanguage a1).
Typey s -> a1 -> ShowS
at main.hs:38:5
Expected type: Typey 'EqualSign
Actual type: Typey s
• When checking that the pattern signature: Typey 'EqualSign
fits the type of its context: Typey s
In the pattern: _ :: Typey EqualSign
In an equation for ‘showVal’:
showVal (_ :: Typey EqualSign) = const $ showString "="
• Relevant bindings include
showVal :: Typey s -> a1 -> ShowS (bound at main.hs:38:5)
我不确定我理解编译器在抱怨什么。从GADT Character ExampleLanguage
的定义可以清楚地看出,我试图模式匹配不同的构造函数,根据字母输出不同的字符串。
我该如何使这项工作?数据格式不固定,只要Symbol
包含有关字母,字母和字母代表类型的类型信息,就可以随意修改它。
答案 0 :(得分:2)
GADT的构造函数上的模式匹配,而不是它们的类型
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
class Character c where
showVal :: c a -> a -> ShowS
class Character (Letter l) => Alphabet l where
data family Letter l :: * -> *
-- Example language
data ExampleLanguage = ExampleLanguage
instance Alphabet ExampleLanguage where
data Letter ExampleLanguage a where
Variable :: Letter ExampleLanguage String
Comment :: Letter ExampleLanguage String
EqualSign :: Letter ExampleLanguage ()
Deref :: Letter ExampleLanguage ()
instance Character (Letter ExampleLanguage) where
showVal Variable = showString
showVal Comment = showString
showVal EqualSign = const $ showString "="
showVal Deref = const $ showString "*"
在Symbol
中添加此标记,以便稍后对其进行模式匹配。
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeInType #-}
data Symbol l (a :: Letter l s) where
Terminal :: Letter l s -> s -> Symbol l (a :: Letter l s)
instance (Alphabet l) => Show (Symbol l a) where
showsPrec d (Terminal tag val) = showVal tag val
test :: Symbol ExampleLanguage Comment
test = Terminal Comment "some comment"