关于“FlexibleInstances”类型的类问题 [英] Type class problem concerning "FlexibleInstances"

查看:125
本文介绍了关于“FlexibleInstances”类型的类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定类型类

  class Dictionary w其中
insert :: String - >字符串 - > w - > w
remove :: String - > w - > w
lookUp :: String - > w - >字符串

我无法写入

<$ p
插入键值dic =(key,value):删除键值dic
删除键值dic = filter(\entry)$ p $ instance Dictionary [(String,String) - >(fst entry)/ = key)dic
lookUp key [] =not found
lookUp key((k,v):xs)| k == key = v
|否则=查找键xs

由于

 `Dictionary [(String,String)]'非法实例声明'
(所有实例类型都必须是形式(T a1 ... an)
其中a1 ... an是类型*变量*,
,每个类型变量在实例头中至多出现一次
如果您想禁用它,请使用-XFlexibleInstances。)
在实例`Dictionary [(String,String)]'

...的声明理解。类似这样的工作:

  newtype Dic = Dic [(String,String)] 

实例字典Dic其中
插入键值(Dic dic)= Dic $(键值):filter(\ entry - >(fst entry)/ = key)dic
删除键(Dic dic)= Dic $ filter(\entry->(fst entry)/ = key)dic
lookUp key(Dic [])=not found
lookUp key(Dic((k,v)): xs))| k == key = v
|否则= lookUp key(Dic xs)

有更好的方法吗?或者我应该使用建议的编译器指令?

解决方案

原因很简单。 Haskell 98只允许不饱和类型的实例,这是在它们的类型变量中没有固定的类型。仔细阅读它给出的错误信息,它确切地描述了编译器想要的东西。



要做到你想要的,基本上有两种方法你已经试过: / p>


  • 打开 FlexibleInstances 。这是最常用的方式,因为此扩展程序是最常用的方式之一。

  • 将其包装到 newtype 中。提供兼容性,但很难看。


选择一个;)

Given the type class

class Dictionary w where
  insert :: String -> String -> w -> w
  remove :: String -> w -> w
  lookUp :: String -> w ->  String

I can't write

instance Dictionary [(String,String)] where
  insert key value dic = (key,value) : remove key dic
  remove key dic = filter (\entry -> (fst entry) /= key) dic
  lookUp key [] = "not found"
  lookUp key ((k,v):xs) | k == key = v
                        | otherwise = lookUp key xs 

because of

Illegal instance declaration for `Dictionary[(String, String)]'
    (All instance types must be of the form (T a1 ... an)
     where a1 ... an are type *variables*,
     and each type variable appears at most once in the instance head.
     Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Dictionary[(String, String)]'

... which I don't quite understand. Something like this works:

newtype Dic = Dic [(String,String)]

instance Dictionary Dic where
  insert key value (Dic dic) = Dic $ (key,value) : filter (\entry -> (fst entry) /= key) dic
  remove key (Dic dic) = Dic $ filter (\entry -> (fst entry) /= key) dic
  lookUp key (Dic []) = "not found"
  lookUp key (Dic ((k,v):xs)) | k == key = v
                              | otherwise = lookUp key (Dic xs)  

Is there a better way? Or should I use the suggested compiler directive?

解决方案

The reason is simple. Haskell 98 only allows instances for "unsaturated" types, this is types that are not fixed in their type variables. Read the error message it gives carefully, it exactly describes what the compiler wants to have.

To do what you want, there are basically the two way you already tried:

  • Switch on FlexibleInstances. This is the most common way, as this extension is one of the most used.
  • Wrap it into a newtype. Gives compatibility but is ugly.

Choose one ;)

这篇关于关于“FlexibleInstances”类型的类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆