繁体   English   中英

Haskell中的隐式模式匹配

[英]Implicit pattern matching in Haskell

是否应该预期这两个empty之间的性能差异,还是只是风格偏好的问题?

foo list = case list of
   []      -> True
   (_ : _) -> False

bar list = case list of
   (_ : _) -> False
   _       -> True

一般来说,你不应该期望性能可以通过简单的摆弄与你所询问的模式之间的变化来预测,并且通常可以期望生成的代码是相同的。

但实际检查的方法是使用标准来查看核心和/或基准。 在这种情况下,生成的代码是相同的,实际上GHC似乎实际上将它们组合在一起:

我用上面编译了上面的代码片段

ghc -Wall -O2 -ddump-to-file -ddump-simpl -dsuppress-module-prefixes -dsuppress-uniques -fforce-recomp  YourCode.hs

我们看到了这个核心:

foo :: forall t. [t] -> Bool
[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=False)
         Tmpl= \ (@ t) (list [Occ=Once!] :: [t]) ->
                 case list of _ [Occ=Dead] {
                   [] -> True;
                   : _ [Occ=Dead] _ [Occ=Dead] -> False
                 }}]
foo =
  \ (@ t) (list :: [t]) ->
    case list of _ [Occ=Dead] {
      [] -> True;
      : ds ds1 -> False
    }

-- RHS size: {terms: 1, types: 0, coercions: 0}
bar :: forall t. [t] -> Bool
[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=False)
         Tmpl= \ (@ t) (list [Occ=Once!] :: [t]) ->
                 case list of _ [Occ=Dead] {
                   [] -> True;
                   : _ [Occ=Dead] _ [Occ=Dead] -> False
                 }}]
bar = foo

我认为Tmpl东西是在其他模块中内联的原始实现,但我不确定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM