gpt4 book ai didi

haskell - 对 GHC 分析器输出中的类型类函数进行拆解

转载 作者:行者123 更新时间:2023-12-04 07:45:52 26 4
gpt4 key购买 nike

在分析用 GHC 编写的 Haskell 程序时,类型类函数的名称在 .prof 文件中被修改,以区分一个实例的实现与另一个实例的实现。我怎样才能解开这些名称以找出它是哪种类型的实例?

例如,假设我有以下程序,其中类型 FastSlow都实现Show :

import Data.List (foldl')

sum' = foldl' (+) 0

data Fast = Fast
instance Show Fast where
show _ = show $ sum' [1 .. 10]

data Slow = Slow
instance Show Slow where
show _ = show $ sum' [1 .. 100000000]

main = putStrLn (show Fast ++ show Slow)

我用 -prof -auto-all -caf-all 编译并使用 +RTS -p 运行.在生成的 .prof 文件中,我看到最高成本中心是:
COST CENTRE                    MODULE               %time %alloc

show_an9 Main 71.0 83.3
sum' Main 29.0 16.7

在树中,我同样看到(省略不相关的行):
                                                individual    inherited
COST CENTRE MODULE no. entries %time %alloc %time %alloc

main Main 232 1 0.0 0.0 100.0 100.0
show_an9 Main 235 1 71.0 83.3 100.0 100.0
sum' Main 236 0 29.0 16.7 29.0 16.7
show_anx Main 233 1 0.0 0.0 0.0 0.0

我怎么知道 show_an9Slowshow 的实现而不是 Fast的?

最佳答案

不,你不能。 _an9_anx部分是随机生成的。 (当我再次编译时,我得到了 _ane_anC 。)

您可以使用 SCC (set-cost-center) pragma 手动插入成本中心:

data Fast = Fast
instance Show Fast where
show _ = {-# SCC "show(Fast)" #-} show $ sum' [1 .. 10]

data Slow = Slow
instance Show Slow where
show _ = {-# SCC "show(Slow)" #-} show $ sum' [1 .. 100000000]

配置文件应显示:
  main
show_an9
show(Slow)
sum'
show_anx
show(Fast)

关于haskell - 对 GHC 分析器输出中的类型类函数进行拆解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2807428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com