我正在使用嵌入在Haskell中的语言。我的语言可以作为源代码打印出来,因此我创建了一个Compile
类,并为每个可以打印出来的程序元素创建了一个类实例。这样我就可以合成地转储我的代码。在考虑模式的概念之前,这种方法很好。
每种语言都可以使用两种模式(作为类Mode
的实例实现)。在简单模式下一切正常。在命名模式下,许多程序元素可以用字符串替换。 (它的工作方式与宏定义类似。)
我想保持所有表示类型安全。因此,不同语言或不同模式的程序元素不能混合使用。
所以问题是:如何在不管模式的情况下转储语言?
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances #-}
class Compile a where
comp :: a -> String
-- common elements in all languages
data ElemA l m = ElemA (ElemB l m)
data ElemB l m = ElemB
class Lang l where
-- language-specific elements
data Instructions l :: * -> *
-- common modes for all languages
class Mode l m where
type MElemA l m :: *
type MElemB l m :: *
-- mode with normal program elements
data SimpleMode
instance Mode l SimpleMode where
type MElemA l SimpleMode = ElemA l SimpleMode
type MElemB l SimpleMode = ElemB l SimpleMode
-- a mode where each program element can be replaced with a string
data NamedMode
instance Mode l NamedMode where
type MElemA l NamedMode = Either String (ElemA l NamedMode)
type MElemB l NamedMode = Either String (ElemB l NamedMode)
-- definition of Lang1 language
data Lang1
instance Lang Lang1 where
data Instructions Lang1 m
= Add (MElemA Lang1 m) (MElemA Lang1 m) (MElemA Lang1 m)
| Mul (MElemA Lang1 m) (MElemA Lang1 m) (MElemA Lang1 m)
-- | ...
-- dumping the source code of Lang1 langauge
-- ILLEGAL TYPE SYNONYM FAMILY APPLICATION HERE
instance Compile (MElemA Lang1 m) where
comp _ = "A"
-- AND HERE
instance Compile (MElemB Lang1 m) where
comp _ = "B"
我知道类型同义词系列不适用于类,所以我正在寻找另一种解决方案。
我知道的可能解决方案(但不想使用):
comp
函数。NamedMode
进行展示答案 0 :(得分:3)
我的一个朋友ZoltánKelemen给我一个解决方案。他使用包装类来封装指定语言的程序元素。通过这种方式,他从实例头中删除了类型族应用程序,而没有超出必要的开销。
我也在寻找其他解决方案。
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}
class Compile a where
comp :: a -> String
-- common elements in all languages
data ElemA l m = ElemA (ElemB l m)
data ElemB l m = ElemB
class Lang l where
-- language-specific elements
data Instructions l :: * -> *
-- wrapper classes for program elements of Lang1
data Lang1A m = WrapperA (ElemA Lang1 m)
data Lang1B m = WrapperB (ElemB Lang1 m)
-- common modes for all languages
class Mode l m where
type MElemA l m :: *
type MElemB l m :: *
-- mode with normal program elements
data SimpleMode
instance Mode l SimpleMode where
type MElemA l SimpleMode = ElemA l SimpleMode
type MElemB l SimpleMode = ElemB l SimpleMode
-- a mode where each program element can be replaced with a string
data NamedMode
instance Mode l NamedMode where
type MElemA l NamedMode = Either String (ElemA l NamedMode)
type MElemB l NamedMode = Either String (ElemB l NamedMode)
-- definition of Lang1 language
data Lang1
instance Lang Lang1 where
data Instructions Lang1 m
= Add (MElemA Lang1 m) (MElemA Lang1 m) (MElemA Lang1 m)
| Mul (MElemA Lang1 m) (MElemA Lang1 m) (MElemA Lang1 m)
-- | ...
-- dumping the source code of Lang1 langauge
-- ILLEGAL TYPE SYNONYM FAMILY APPLICATION HERE
instance Compile (Lang1A m) where
comp (WrapperA e) = "A"
-- AND HERE
instance Compile (Lang1B m) where
comp (WrapperB e) = "B"