- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样:
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Map (Map)
import qualified Data.Map as Map
class Format f where type Target f
class Format f => Formatter x f where
target :: forall y. Formatable y => Target f -> x -> y
class Formatable y where name :: y -> String
instance Formatable Integer where name = show
instance Formatable Int where name = show
split :: forall x f. (Format f, Formatter x f) => x -> f -> String -> [Either String (Target f)]
split = undefined
display :: forall x f. (Format f, Formatter x f) => f -> String -> x -> String
display f str x = let
chunks = split x f str
built = foldr apply "" chunks
apply (Left s) accum = accum ++ s
apply (Right t) accum = accum ++ name (target t x)
in foldr apply "" chunks
本质上,我们有多态的Format
,它定义了许多Target
。还有许多 Formattable
对象,它们知道如何响应一堆不同的格式选项(这里简化为 name
)。
这些格式表
以多种方式组成,并且可以响应许多不同的目标。 Formatter
本质上是 Format
和 Formattable
之间的路由器——给定目标(来自特定格式),它们会用合适的 进行响应可格式化
对象。
这一切都非常抽象。这是一个例子:
DateFormat
指定诸如 Year
、Month
和 Day
之类的目标。MonthType
是 Int
的 Formattable
新类型,其名称例如“February”实例Formattable Int where name = show
DateTime
可能是 (Int, MonthType, Int)
的类型同义词。(显然,我在这里删除了很多机制,例如通过管道传递正确的值,但您明白了。)
display
函数相当简单。它需要一个格式化程序、一个指定格式的字符串、一个要显示的对象,并将其全部呈现为一个字符串。
首先,它将字符串分解为目标和字符串。例如,日期格式化程序可能会将字符串 "%Y-%m-%d"
分解为 [Right Year, Left "-", Right Month, Left "-", Right Day ]
。 split
函数执行此操作,并已在此处进行编辑。
display
函数只是跟踪每个目标的Formattable
并累积字符串。
或者,至少,应该是这样。
但是类型检查失败并出现以下错误:
Reduced.hs:20:16:
Could not deduce (Target f ~ Target f0)
from the context (Format f, Formatter x f)
bound by the type signature for
display :: (Format f, Formatter x f) => f -> String -> x -> String
at Reduced.hs:(19,5)-(24,30)
NB: `Target' is a type function, and may not be injective
Expected type: [Either [Char] (Target f0)]
Actual type: [Either String (Target f)]
In the return type of a call of `split'
In the expression: split x f str
In an equation for `chunks': chunks = split x f str
Failed, modules loaded: none.
我一生都无法弄清楚为什么。我做错了什么?
最佳答案
问题在于目标f
无法确定f
,这意味着该函数
target :: (Formatter f x, Formatable y) => Target f -> x -> y
永远无法被调用。无论您为 target
提供什么类型注释,您都无法确定 f
是什么,因此编译器永远无法确定哪个 Formatter
> 要使用的实例。我不是 100% 确定,但解决方案可能不是使用多参数类型类,而是让 x 或 f 之一成为另一个的函数。另外,您可能应该完全删除 Format
类(您知道不需要类来使用类型系列吗?)。也许是这样的:
class Formatter x where
type Format x
target :: Formatable y => Format x -> x -> y
关于haskell - 类型 家庭 类型 黑客,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10059557/
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
有时,当我看到一个我喜欢的网站或来自受人尊敬的人的网站时,我会查看源代码并尝试理解它们(就像我们所有人一样)。 关于 Jeremy Keiths他使用以下代码的网站: [role="navigatio
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
This question already has answers here: Prevent users from modifying part of the text in SLComposeVi
我正在使用 rockettheme 模板并使用自定义 css 文件编辑了一些 css 代码。 我已经设法让它在 Firefox 和 Chrome 上看起来像我想要的那样,但是 IE 看起来很奇怪。
我在 Dreamweaver 中右击我的行号,试图为一行添加书签。 我注意到这个菜单项: 什么是 Caio Hack? alert('hi'); 突出显示,结果如下: 最佳答案 这是一个古老的 CSS
我在看this SSL讲座。 Zeldovich 教授在这里说,如果连接是 http,黑客可能会让您相信您正在与 Amazon.com 对话,而实际上您正在与他的(黑客)服务器对话。 这是怎么发生的?
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我有一个相当复杂的 JTable 子类(WidgetTable 及其 WidgetTableModel),当我将它添加到虚拟 JPanel 以进行测试时它工作正常。 因为我绝对讨厌使用 LayoutM
我遇到了这个很酷的 Swift 技术的例子,但没有解释它是如何工作的或者它做了什么,尽管我已经写了相当多的 Swift 代码,但它充满了错综复杂的 Swiftisms,它目前正在扭曲我的转过头去。 如
服务器在互联网运转中为使用者提供计算或应用服务,企业的业务流程基本上都是在服务器上进行,大批量的数据信息都会存储在此,若是服务器在黑客的入侵下中毒,后果将不堪设想。想要避免黑客的入侵,我们可以从下面
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
我有一个 DropDownList,我正在尝试阻止它被用作攻击媒介。我是否可以假设用户无法实际更改 DDL 的值并回发到服务器?目前,如果我尝试在提交后更改数据包,则会抛出此 ASP.NET 错误消息
我有一堆 C++ 结构。我想将它保存到文件并再次加载它们。问题是我的一些结构是指向基类(/结构)的指针。所以我需要一种方法来找出类型并创建它。它们实际上只是 POD,它们都有公共(public)成员,
我正在使用 Sprite 作为输入按钮。它在 Firefox 和 > IE8 中运行良好。但是,在 IE7 和 8 中,ID 值显示在 png 图形上。它看起来像这样: (我会嵌入,但不允许):Pre
以下安全吗? struct K { ... } struct A { A(int psize) : size(psize), foo(nullptr), bar(nullptr) {
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我正在破解一个画廊插件,我想在其中禁用缩略图的点击事件并将其替换为悬停事件。 这就是我所做的:http://jsbin.com/enezol/3 $(function() { var galler
昨天这里有一篇帖子因问题过于本地化而被关闭。好吧,我现在遇到了同样的问题,而且这似乎是一个相当新的问题。 基本上,从昨天开始,我的防病毒软件就阻止了我的网站。该网站的 https://版本加载绝对正常
我有一个 Unity WebGL 应用程序在 Web 浏览器中运行,它使用普通的旧 Unity WWW 和 http://.... 与远程服务器通信,它调用服务器上的几个不同的 php 脚本并使用 P
我是一名优秀的程序员,十分优秀!