首页 > 解决方案 > Haskell TypeSynonymInstances 实例声明

问题描述

尝试使用 ghci> 加载文件时出现以下错误:l myfile.hs

• Illegal instance declaration for ‘ExpSYM String’

    (All instance types must be of the form (T t1 ... tn)

     where T is not a synonym.

     Use TypeSynonymInstances if you want to disable this.)

• In the instance declaration for ‘ExpSYM String’

在行中:实例 ExpSYM String where


class ExpSYM repr where
    lit :: Int -> repr
    neg :: repr -> repr
    add :: repr -> repr -> repr
    
instance ExpSYM Int where
    lit n = n
    neg e = - e
    add e1 e2 = e1 + e2
    
eval :: Int -> Int
eval = id



instance ExpSYM String where
    lit n = show n
    neg e = "(−" ++ e ++ ")"
    add e1 e2 = "(" ++ e1 ++ " + " ++ e2 ++ ")"

view :: String -> String
view = id

起初我以为这是因为双实例 ExpSYM,但如果我删除第一个(实例 ExpSYM Int)并且它是 eval,我仍然会看到错误。怎么修?问题是什么?

标签: haskell

解决方案


推荐阅读