- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
免责声明:我才刚刚开始学习 Haskell,我不确定“严格”在这里是不是正确的词。
我试图缩小我的问题范围,但我真的找不到问题,所以这是我的代码 无法编译 :
module Json where
import Data.List (intersperse)
data JNode =
JObject [(String, JNode)]
| JArray [JNode]
| JString String
| JNumber Double
| JBool Bool
| JNull
instance Show JNode where
show = show_node 0 where
glue = foldl (++) ""
show_tabs n = glue $ take n $ repeat " "
show_list n = glue . intersperse ",\n" . map (show_pair (n + 1))
show_sect n l r xs = glue ["\n", tabs, l, "\n", show_list n xs, "\n", tabs, r] where tabs = show_tabs n
-- show_pair :: (Show a) => Int -> (a, JNode) -> String -- works when uncommented
show_pair n (name, val) = glue [show_tabs n, show name, " : ", show_node n val]
show_node n (JObject xs) = show_sect n "{" "}" xs
show_node n (JArray xs) = show_sect n "[" "]" $ zip [0..] xs
show_node n (JString x ) = show x
show_node n (JNumber x ) = show x
show_node n (JBool x ) = show x
show_node n (JNull ) = "null"
Prelude> :l scripts\json.hs
[1 of 1] Compiling Json ( scripts\json.hs, interpreted )
scripts\json.hs:21:59:
No instance for (Enum String)
arising from the arithmetic sequence `0 .. '
In the first argument of `zip', namely `([0 .. ])'
In the second argument of `($)', namely `zip ([0 .. ]) xs'
In the expression: show_sect n "[" "]" $ zip ([0 .. ]) xs
scripts\json.hs:21:60:
No instance for (Num String) arising from the literal `0'
In the expression: 0
In the first argument of `zip', namely `[0 .. ]'
In the second argument of `($)', namely `zip [0 .. ] xs'
Failed, modules loaded: none.
String
而不仅仅是
Show a
.有趣的是,它仍然需要
name
成为
String
当我什至不使用它时,例如更换时
show_pair
用这个实现:
show_pair n (name, val) = show_node n val
data TFoo =
FooStr (String, TFoo)
| FooNum (Int, TFoo)
-- show_pair :: (a, TFoo) -> String
show_pair (_, val) = show_node val
show_node (FooStr x) = show_pair x
show_node (FooNum x) = show_pair x
最佳答案
tl;博士 :当您希望某物具有多态性时,请始终使用显式签名。
在 System F (这是 Haskell 的类型系统所基于的)每个用于多态的类型变量都需要通过 type-level-lambda/for-all (∀) 显式量化到范围内。所以事实上你需要有
show_pair :: ∀ a. (Show a) => Int -> (a, JNode) -> String
show_pair :: (Show a) => Int -> (a, JNode) -> String
show_pair
使用时,它具有您需要拥有的至少一种类型实例化的完整上下文。它假设您只需要一个实例化。有了它,它会尝试为单态
show_pair
推断某种类型。 ,并且失败。只有通过添加显式签名才能强制编译器考虑多态签名。正如评论中所说,这实际上不是真的,因为从 GHC-7.0 开始,就有一个叫做
let generalisation 的东西。即使没有签名,它也会使本地绑定(bind)多态。我不知道这是默认开启的。 — 即使可以省略签名,多态函数也应该更好地使用 IMO。
-XScopedTypeVariables
扩大。
关于Haskell 推断出过于严格的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32662237/
基础问题 我要解决的基本问题是: 我有一个模板参数包 ArgTypes,我需要用包装在 std::optional 中的每个类型创建一个元组。例如:make_optional_tuple应该返回 st
我使用 createEntityAdapter 设置了一个简单的 redux store。初始状态包含实体、ID、状态、错误设置等 const carouselEventAdapter = creat
我有一些(遗留)代码,如下所示: void castFoo(string type, void* foo) { FooA* foo_a = NULL; FooB* foo_b = NULL;
我的代码是 const int *const ptrA = nullptr; auto *ptrB = &ptrA; 我对 const int *const ptrA 的看法是: (*
我目前正在尝试用 C++ 实现 XOR 链表。我尝试使用模板使其通用。编译时会弹出此错误,我无法解决这个问题。 我尝试使用模板在谷歌上搜索 XOR 链表,但到目前为止似乎还没有实现它。 异或链表.h:
我正在尝试找到一种方法来调用多个类成员函数,每个函数都有不同的参数,并且在调用前后会发生某些已知功能。 这个包装函数是我试过的,但是例如对它的最终调用不会编译错误: 'bool Wrapper(Wor
此代码在 上编译成功g++ ( Coliru ) ,但不是 Visual C++ ( rextester ) - 在线和我的桌面。 它是一个更大的 Visual Studio 2015 项目的简化版本
我正在尝试编写一个通用类,它传递一个键 key 对应于一组已知接口(interface)中的一个的键,稍后可以传递一个对象 thing 并类型安全地访问 thing[key]。这是我得到的: inte
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我是一名优秀的程序员,十分优秀!