- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
GHC 可以将 id = (\(a, b) -> (a, b)).(\(a, b) -> (a, b))
简化为 id = \(a, b) -> (a, b)
吗?
更复杂的情况呢:
id (Just x) = Just x
id Nothing = Nothing
map f (Just x) = Just (f x)
map _ Nothing = Nothing
id . map
简化为
map
吗?
最佳答案
您可以通过运行 -ddump-simpl
来询问 ghc 的这些问题。这将导致 ghc 转储它编译程序的“核心”代码。 Core 是一种介于解释 Haskell 代码的编译器部分和将代码转换为机器代码的编译器部分之间的中间语言。
当我用 -O2 -ddump-simpl
编译以下内容时,结果让我感到惊讶。
tupid1 :: (a, b) -> (a, b)
tupid1 = (\(a, b) -> (a, b))
tupid2 :: (a, b) -> (a, b)
tupid2 = (\(a, b) -> (a, b)) . (\(a, b) -> (a, b))
tupid1
核心生成了一个新的专用标识函数。
-- RHS size: {terms: 4, types: 7, coercions: 0}
tupid1 :: forall a_aqo b_aqp. (a_aqo, b_aqp) -> (a_aqo, b_aqp)
[GblId,
Arity=1,
Caf=NoCafRefs,
Str=DmdType <S,1*U(U,U)>m,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=True)
Tmpl= \ (@ a_ayd)
(@ b_aye)
(ds_dIl [Occ=Once] :: (a_ayd, b_aye)) ->
ds_dIl}]
tupid1 = \ (@ a_ayd) (@ b_aye) (ds_dIl :: (a_ayd, b_aye)) -> ds_dIl
tupid1
使用其中两个类型参数,名为
a_ayd
和
b_aye
,用于其签名中的两个类型变量
a
和
b
。它还需要一个术语
ds_dIl
,它具有这两种类型 (
ds_dIl :: (a_ayd, b_aye)
) 的元组类型,并未经修改地返回它。
tupid2
...
-- RHS size: {terms: 1, types: 0, coercions: 0}
tupid2 :: forall a_aqm b_aqn. (a_aqm, b_aqn) -> (a_aqm, b_aqn)
[GblId,
Arity=1,
Caf=NoCafRefs,
Str=DmdType <S,1*U(U,U)>m,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=True)
Tmpl= \ (@ a_axZ) (@ b_ay0) (x_aIw [Occ=Once] :: (a_axZ, b_ay0)) ->
x_aIw}]
tupid2 = tupid1
tupid1
!它如何推断出超出了我直接知识或发现能力的范围。
Maybe
的身份示例
maybeid :: Maybe a -> Maybe a
maybeid (Just x) = Just x
maybeid Nothing = Nothing
-- RHS size: {terms: 3, types: 4, coercions: 0}
maybeid :: forall a_aqn. Maybe a_aqn -> Maybe a_aqn
[GblId,
Arity=1,
Caf=NoCafRefs,
Str=DmdType <S,1*U>,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=True)
Tmpl= \ (@ a_aqI) (ds_dIq [Occ=Once] :: Maybe a_aqI) -> ds_dIq}]
maybeid = \ (@ a_aqI) (ds_dIq :: Maybe a_aqI) -> ds_dIq
map
的
Maybe
核心对于这个问题不感兴趣
maybemap :: (a -> b) -> Maybe a -> Maybe b
maybemap f (Just x) = Just (f x)
maybemap _ Nothing = Nothing
maybeid
组成的
maybeidmap :: (a -> b) -> Maybe a -> Maybe b
maybeidmap f = maybeid . maybemap f
maybemap
-- RHS size: {terms: 1, types: 0, coercions: 0}
maybeidmap
:: forall a_aqp b_aqq.
(a_aqp -> b_aqq) -> Maybe a_aqp -> Maybe b_aqq
[GblId,
Arity=2,
Caf=NoCafRefs,
Str=DmdType <L,1*C1(U)><S,1*U>,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=0,unsat_ok=True,boring_ok=True)
Tmpl= maybemap}]
maybeidmap = maybemap
id
由
f
组成,它会做同样的事情。
maybemapid :: (a -> b) -> Maybe a -> Maybe b
maybemapid f = maybemap (id . f)
maybemap
-- RHS size: {terms: 1, types: 0, coercions: 0}
maybemapid
:: forall a_aqq b_aqr.
(a_aqq -> b_aqr) -> Maybe a_aqq -> Maybe b_aqr
[GblId,
Arity=2,
Caf=NoCafRefs,
Str=DmdType <L,1*C1(U)><S,1*U>,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=2,unsat_ok=True,boring_ok=False)
Tmpl= \ (@ a_ar2)
(@ b_ar3)
(f_aqL [Occ=Once!] :: a_ar2 -> b_ar3)
(eta_B1 [Occ=Once!] :: Maybe a_ar2) ->
case eta_B1 of _ [Occ=Dead] {
Nothing -> GHC.Base.Nothing @ b_ar3;
Just x_aqJ [Occ=Once] -> GHC.Base.Just @ b_ar3 (f_aqL x_aqJ)
}}]
maybemapid = maybemap
关于Haskell : Will GHC optimize this?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43972040/
runghc 和 ghc 有什么区别? 我运行了一个简短的程序,似乎可以用两者编译,除了我用 runghc 得到了以下内容,但不是普通的 ghc: error: Variable not in sco
如果 friend 想要运行我的 Haskell 二进制文件,他是否必须先安装 Haskell,还是可以立即自行运行二进制文件? Mac、Windows 和 Linux 上的答案相同吗? 最佳答案 G
ffunction glMultiDrawElements 需要一个指向指针的指针作为其参数之一。如何从 StorableArray Int a 获取 Ptr(Ptr a) ? 最佳答案 您需要先将索
module Has (r,p,s) where import Prelude ((==),Bool(..),otherwise,(||),Eq) import qualified Data.List
我已经构建了 ghc-HEAD,我想尝试构建所有的 stackage lts 或 nightly 看看它能做多少。 无论我说什么都无法说服 stack 使用我的新 ghc 构建任何东西。我尝试设置如下
我正在使用 Emacs (24.3.1) 在 haskell-mode 中与 ghc-mod 一起使用 Haskell。 现在除了一件烦人的事情外,一切都很好: GHC 信息缓冲区中的每个输出仅包含第
为什么升级到 OSX Mavericks 后我的 GHC 7.6.3 不能工作? 最佳答案 花了很长时间才弄清楚如何同时使用 OSX 10.9 和 GHC 7.6.3,这里有一些技巧可以帮助您重新构建
我有一个带有 cabal 文件的主要 Haskell 可执行程序。在那里,我指定 ghc-options . 这个可执行文件链接到野外的其他库。请问ghc-options忽略这些库的 cabal 文件
我有一个使用 -Wall 编译的库,并且我有一个使用 -Wall -Wno-incomplete-patterns 的测试套件 当我使用 stack ghci --test 启动 ghci 时,ghc
我正在尝试使用以下脚本在 Windows 上安装 GHC(并将 ghc 放在路径中),但是当我尝试运行 ghci --version 时它失败了。我认为脚本有问题。 install: - ps:
我在 $HOME 中安装了 ghc-7.2.2 unknown linux tar.bz2在archlinux上。 在使用 cabal-dev 成功安装多个软件包后,尝试安装例如。我得到的解析数字、文
想法 我正在写 DSL ,编译为 Haskell。 该语言的用户可以定义自己的不可变数据结构和相关函数。关联函数是指属于数据结构的函数。 例如,用户可以编写(在“pythonic”伪代码中): dat
(我的问题是在没有 haskell-platform、ghc、cabal 等的情况下分发二进制文件) 我需要部署一个结构良好的 haskell 应用程序(Yesod 脚手架),但我有磁盘空间限制。 G
我试图将 Cygwin 安装程序指向 http://haskell.org/ghc/cygwin ,但安装程序无法找到 setup.ini.sig。如果可能的话,我该如何编辑我的 .bashrc 以引
我有一个现有的 Haskell 函数,它使用 GHC API 从模块中动态加载已编译的代码。它基于博客文章中的代码 Dynamic Compilation and Loading of Modules
我使用:Ubuntu 上的 GHC 7.6.3(通过 apt-get install haskell-platform) 从当前存储库安装它。 正在尝试安装ghc-mod ,因为我的 ide 插件需要
AFAIK,有两种方法可以在 Haskell 中获取用于调试的调用堆栈: 添加 HasCallStack代码中的约束 使用 ghc -prof -fprof-auto-top 编译代码 我的测试代码:
我想用 64 位 GHC 构建 32 位 DLL。这是最小的例子。 测试.hs {-# LANGUAGE ForeignFunctionInterface #-} module Test where
ghc-gc-tune 0.2.1 可以与 ghc 7.4.1 一起使用吗?看来 ghc-gc-tune 已经有一段时间没有更新了,可能只适用于 ghc 6.x?我找不到任何可靠的信息。 我收到以下错
语言扩展 ExplicitForall 使得使用 forall 绑定(bind)类型变量成为可能但不是必需的。 例如,下面的程序可以编译 {-# LANGUAGE ExplicitForAll #-}
我是一名优秀的程序员,十分优秀!