lazy-evaluation

Disadvantage of unlifted type products?

社会主义新天地 提交于 2019-12-03 11:14:45
In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and (a, (b, c)). If all pattern matches of all products was always irrefutable, then there would be no difference, and (a, b, c) could be syntactic sugar for (a, (b, c)). Why did Haskell choose to lift type products? One reason is that implementing seq for an unlifted product requires parallel/interleaved computation, since seq (a, b) True would be supposed to be True if and only if at least one of a and b is non-bottom. You might not find this reason terribly convincing, depending on how you feel about

When does a Stream need to be lazy?

送分小仙女□ 提交于 2019-12-03 11:06:20
The following are both meant to create a Stream of integers: val s: Stream[Int] = 1 #:: s.map(_ + 1) def makeStream = { val s: Stream[Int] = 1 #:: s.map(_ + 1) s } The first is fine; however the makeStream method won't compile: error: forward reference extends over definition of value s val s: Stream[Int] = 1 #:: s.map(_ + 1) ^ It only compiles if we make s a lazy val . Why does it need to be a lazy val in a method, but not outside? Inside a class, a val definition decompiles into an "getter" method that references a hidden class field. These "getter" methods can be self-referential (or rather

Force pre-computation of a constant

喜你入骨 提交于 2019-12-03 10:03:51
问题 I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I'm seeing some code that looks roughly like, myList = [(a, b), (c, d)] ... map (f . fst) myList take time in the fst call when I profile it (it does have 168M calls). The binary representation of myList is quite small, and could be, for example, copied into global memory [if this were a C program]. I'm compiling with -O3 -optc-O3 of course. Thanks very much! Generating Lift instances for a custom type

What is the opposite of lazy loading?

谁说我不能喝 提交于 2019-12-03 09:37:18
Is there a common term / catch-phrase for the opposite of lazy loading? The oposite term for lazy loading is eager loading. Eager loading is essentially computing the tasks when you ask for it. Lazy Loading is when you only do the computation when it is required. I've seen the terms "Eager Loading" and "Aggressive Initialization" both used. I'd say that the opposite of lazy is proactive loading , i.e. loading something in advance, before it's really needed. However it's hard to pick the opposites when you have 3 entities {lazy, eager, proactive} 来源: https://stackoverflow.com/questions/5427949

Is everything in Haskell stored in thunks, even simple values?

馋奶兔 提交于 2019-12-03 08:08:51
问题 What do the thunks for the following value/expression/function look like in the Haskell heap? val = 5 -- is `val` a pointer to a box containing 5? add x y = x + y result = add 2 val main = print $ result Would be nice to have a picture of how these are represented in Haskell, given its lazy evaluation mode. 回答1: Official answer It's none of your business. Strictly implementation detail of your compiler. Short answer Yes. Longer answer To the Haskell program itself, the answer is always yes,

Force lazy entity to load real instance

非 Y 不嫁゛ 提交于 2019-12-03 07:14:34
I have a proxy for a lazy entity which has been created in the session by loading a child entity. A subsequent fetch on the parent entity only returns the NH proxy. I need the actual instance to check the type (the entity has joined subclasses). I must be missing something, but I can't find a way to do this. Session.Refresh(proxy) does not appear to help, nor does any flavour of HQL that I've tried. Can anyone help? Erik Öjebo To force a proxy to be fetched from the database, you can use the NHibernateUtil.Initialize(proxy) method, or access a method/property of the proxy. var foo = session

Reading large file in haskell?

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:01:18
问题 i've been trying to read a large file in haskell. I need to compress it using a custom algorithm for a university project. Everything works fine untill i start to compress big files. I extracted what was going wrong out of my program, and i expose it here in the form of a "Hello big file": import System import qualified Data.ByteString.Lazy as BL import Data.Word fold_tailrec :: (a -> b -> a) -> a -> [b] -> a fold_tailrec _ acc [] = acc fold_tailrec foldFun acc (x : xs) = fold_tailrec foldFun

How do I handle an infinite list of IO objects in Haskell?

旧城冷巷雨未停 提交于 2019-12-03 06:56:07
I'm writing a program that reads from a list of files. The each file either contains a link to the next file or marks that it's the end of the chain. Being new to Haskell, it seemed like the idiomatic way to handle this is is a lazy list of possible files to this end, I have getFirstFile :: String -> DataFile getNextFile :: Maybe DataFile -> Maybe DataFile loadFiles :: String -> [Maybe DataFile] loadFiles = iterate getNextFile . Just . getFirstFile getFiles :: String -> [DataFile] getFiles = map fromJust . takeWhile isJust . loadFiles So far, so good. The only problem is that, since

When are scala's for-comprehensions lazy?

☆樱花仙子☆ 提交于 2019-12-03 06:15:45
In Python, I can do something like this: lazy = ((i,j) for i in range(0,10000) for j in range(0,10000)) sum((1 for i in lazy)) It will take a while, but the memory use is constant. The same construct in scala: (for(i<-0 to 10000; j<-i+1 to 10000) yield (i,j)).count((a:(Int,Int)) => true) After a while, I get a java.lang.OutOfMemoryError , even though it should be evaluated lazily. Nothing's inherently lazy about Scala's for-comprehension; it's syntactic sugar* which won't change the fact that the combination of your two ranges will be eager. If you work with lazy view s of your ranges, the

In Haskell, I want to read a file and then write to it. Do I need strictness annotation?

▼魔方 西西 提交于 2019-12-03 06:08:06
Still quite new to Haskell.. I want to read the contents of a file, do something with it possibly involving IO (using putStrLn for now) and then write new contents to the same file. I came up with: doit :: String -> IO () doit file = do contents <- withFile tagfile ReadMode $ \h -> hGetContents h putStrLn contents withFile tagfile WriteMode $ \h -> hPutStrLn h "new content" However this doesn't work due to laziness. The file contents are not printed. I found this post which explains it well. The solution proposed there is to include putStrLn within the withFile : doit :: String -> IO () doit