类型项限制的类型列表 [英] List of items of types restricted by typeclass

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

问题描述

  {p> 

我试图编码一个类型限制为某些类型实例的项目列表。 - #LANGUAGE RankNTypes,TypeSynonymInstances,LiberalTypeSynonyms# - }
module Test其中

类Someable a where
some :: a - >字符串

数据Some =某些字符串

类型SomeGroup = forall a。 Someable a => [a]

实例Someable某些地方
some(Some v)= v

实例Someable SomeGroup其中
some(x:xs)=(一些x)++,++(某些xs)

main =做
putStrLn $ show.some [某些A,[某些B,一些C ]]

但编译失败并显示错误:

  Test.hs:14:10:
非法多态或限定类型:SomeGroup
在`Someable SomeGroup'的实例声明中

看来我甚至没有定义类型同义实例...

我知道异构集合维基文章,但想知道为什么我的方法不适合 t工作 - 对我来说,通过限制集合来定义类型似乎很自然,只包含具有某种类型实例的类型的项目。

解决方案

如果我注意要正确地把事情做好,需要有一个包装存在的数据类型,以便有一个地方来存储

添加一些包装可以使其工作:

  { - #LANGUAGE ExistentialQuantification,TypeSynonymInstances# - } 

module测试其中

class Someable a where
some :: a - >字符串

数据Some =某些字符串

数据SomeWrapper = forall a。 Someable a => SomeWrapper a
$ b $类型SomeGroup = [SomeWrapper]

实例Someable某些地方
some(Some v)= v

实例Someable SomeWrapper其中
some(SomeWrapper v)= some v

实例Someable SomeGroup其中
some(x:xs)=(some x)++,++(some xs )

main = do
putStrLn $ some [SomeWrapper(SomeA),SomeWrapper [SomeWrapper(SomeB),SomeWrapper(SomeC)]]

当然,这有点难看。不幸的是,我还没有意识到更好的方法。

I'm trying to encode a list of items which have types restricted to be instances of some type class:

{-# LANGUAGE RankNTypes, TypeSynonymInstances, LiberalTypeSynonyms #-}
module Test where

class Someable a where
  some :: a -> String

data Some = Some String

type SomeGroup = forall a. Someable a => [a]

instance Someable Some where
  some (Some v) = v

instance Someable SomeGroup where
  some (x:xs) = (some x) ++ ", " ++ (some xs)

main = do
  putStrLn $ show.some [Some "A", [Some "B", Some "C"]]

But compilation fails with error:

Test.hs:14:10:
    Illegal polymorphic or qualified type: SomeGroup
    In the instance declaration for `Someable SomeGroup'

It seems I even failed to define instance for type synonymous...

I'm aware of heterogenous collections wiki article, but want to know why exactly my approach doesn't work — it seems natural to me to define type by restricting collection only to contain items with types which is instance of some type class.

解决方案

If I understand things correctly, there needs to be a data type wrapping the existential in order for there to be a place to store the type class dictionary along with each element.

Adding some wrappers makes it work:

{-# LANGUAGE ExistentialQuantification, TypeSynonymInstances #-}

module Test where

class Someable a where
  some :: a -> String

data Some = Some String

data SomeWrapper = forall a. Someable a => SomeWrapper a

type SomeGroup = [SomeWrapper]

instance Someable Some where
  some (Some v) = v

instance Someable SomeWrapper where
  some (SomeWrapper v) = some v

instance Someable SomeGroup where
  some (x:xs) = (some x) ++ ", " ++ (some xs)

main = do
  putStrLn $ some [SomeWrapper (Some "A"), SomeWrapper [SomeWrapper (Some "B"), SomeWrapper (Some "C")]]

Of course, this is a bit ugly. I'm not aware of any better way, unfortunately.

这篇关于类型项限制的类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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