English 中文(简体)
采用参考性-透明度,在高原中采用先质的数值
原标题:Using referential-transparency to pre-compute values in haskell

让我们说,我们有一个这样的方案:

list = [1..10000000]

main = print $ sum list

我希望能够汇编成册,这样,可以起诉的仅印刷500 000 000美元,而没有花费太多的时间和精力。

基本上来说,任何肯定的表述(也许严格分析有助于这里)可在汇编期间 预先计算(因此,我们在计算价值时使用参考性-透明度来说确实如此)。

简言之:has to be computed” + Referenceential-transparency = 可以事先获得投入

这好像是管理该方案,直到我们打上了取决于投入的东西(即方案的核心,在所有投入中都是共同的,将预先投入)

目前是否有实现这一目的的现有机制(以Haskell或任何其他语言)。 [请不要象C++中的模板那样指出,因为首先,C++中的模板具有参考性。]

如果不是的话,这个问题是如何棘手的? [随之而来的技术(和理论)问题是什么?]

问题回答

这在一般情况下是不安全的。 理由是Haskell的言论可以是pure,但也可以是fail to end。 汇编者应当永远终止,因此,你所能做的最好办法是“评估这一结果的1 000个步骤”。 但是,如果你增加这种限制,那么,如果你重新汇编一个与区域援助团的恐怖相联的超级计算机组别的方案,以及汇编者不记名,那么什么呢?

你可以增加许多限制,但归根结底,你会把选择权降低到一种缓慢的连续组合形式上(特别是对于大多数计算依靠实时用户投入的方案)。 由于<代码>[1.10000000]在座次半部分,因此不大可能属于任何合理的限制。

当然,像这种情况这样的具体案件往往可以选择不适用,而世界卫生联合会常常选择取消这种复杂表述。 但是,汇编者只能安全地评估任何表述;它必须受到非常严格的限制,而且可以争论的是,在这种限制下,它会有什么帮助。 它是一个汇编者,而不是口译人员:

<>1> 这会大大降低编制does的任何方案的工作速度。 有许多fin,因为Haskell是没有限制的,比你认为更有可能。 更常见的是,任何包含大量长期截肢的方案。

The general-purpose answer to doing compile-time computation is to use Template Haskell. But for this specific use case, you can use the vector package and the LLVM backend, and GHC will optimize away this sum.

sorghum:~% cat >test.hs
import Data.Vector.Unboxed as V
main = print (V.sum $ V.enumFromN (1 :: Int) 10000000)
sorghum:~% ghc --make -O2 -fllvm test.hs
[1 of 1] Compiling Main             ( test.hs, test.o )
Linking test ...
sorghum:~% time ./test
50000005000000
./test  0.00s user 0.00s system 0% cpu 0.002 total
sorghum:~% objdump -d test | grep 2d7988896b40
  40653d:   48 bb 40 6b 89 88 79    movabs $0x2d7988896b40,%rbx
  406547:   49 be 40 6b 89 88 79    movabs $0x2d7988896b40,%r14

(如不明显,<代码>0x2d79888896b40>为5000000<0000)

象“超级工程”这样的音响! 问题如对该问题的描述和对不终止问题的讨论,反映了超级开发商面临的问题。 我在GHC的座右上看到,有人在生产超级工厂工作,但并没有这样做。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签