gpt4 book ai didi

haskell : Illegal type synonym family application in instance

转载 作者:行者123 更新时间:2023-12-02 16:44:45 25 4
gpt4 key购买 nike

我正在使用 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 进行表示

最佳答案

我的一位 friend Zoltán Kelemen 向我发送了一个解决方案。他使用包装类来封装指定语言的程序元素。通过这种方式,他从实例头中消除了类型系列应用程序,而没有超出必要的开销。

我也在寻找其他解决方案。

{-# 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"

关于 haskell : Illegal type synonym family application in instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18126475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com