strictness

What does “⊥” mean in “The Strictness Monad” from P. Wadler's paper?

一世执手 提交于 2020-01-14 07:21:08
问题 Can someone help me understand the following definition from Wadler's paper titled "Comprehending Monads"? (Excerpt is from section 3.2/page 9, i.e., the "Strictness Monad" subsection.) Sometimes it is necessary to control order of evaluation in a lazy functional program. This is usually achieved with the computable function strict , defined by strict f x = if x ≠ ⊥ then f x else ⊥. Operationally, strict f x is reduced by first reducing x to weak head normal form (WHNF) and then reducing the

Logical AND strictness with IO monad

一世执手 提交于 2020-01-02 04:51:10
问题 I am trying to write a simple program in Haskell. It should basically run two shell commands in parallel. Here is the code: import System.Cmd import System.Exit import Control.Monad exitCodeToBool ExitSuccess = True exitCodeToBool (ExitFailure _) = False run :: String -> IO Bool run = (fmap exitCodeToBool) . system main = liftM2 (&&) (run "foo") (run "bar") But command "foo" returns ExitFailure and I expect "bar" never to run. This is not the case! They both run and both show errors on the

How does the bind operator for Eval in Control.Parallel.Strategies evaluate its argument strictly?

♀尐吖头ヾ 提交于 2019-12-11 03:49:37
问题 The source code for Control.Parallel.Strategies ( http://hackage.haskell.org/packages/archive/parallel/3.1.0.1/doc/html/src/Control-Parallel-Strategies.html#Eval ) contains a type Eval defined as: data Eval a = Done a which has the following Monad instance: instance Monad Eval where return x = Done x Done x >>= k = k x -- Note: pattern 'Done x' makes '>>=' strict Note the comment in the definition of bind. Why is this comment true? My understanding of strictness is that a function is only

Weak head normal form and order of evaluation

旧巷老猫 提交于 2019-12-10 19:52:15
问题 I've read lots on weak head normal form and seq. But I'm still have trouble imagining the logic behind Haskell's order of evaluation A common example demonstrating when and how to use but I still don't understand how the common example foldl (+) 0 [1..5000000] can result in a stack overflow. While another fold definition using seq doesn't foldl' _ a [] = a foldl' f a (x:xs) = let a' = f a x in a' `seq` foldl' f a' xs foldl' (+) 0 [0..5000000] From explanations of seq that I've read, authors

Forced strictness for lists in haskell

喜你入骨 提交于 2019-12-10 01:32:12
问题 I made really time consuming algorithm which produces a short string as the result. When I try to print it (via putStrLn) it appears on the screen character by character. I did understand why that happened, and I tried to force evaluation of the string before actual printing. myPrint !str = putStrLn str But this help very little. When I ran the program in debug I noticed that the !str forced evaluation only for the first character. Does anyone know why is that, and how to deal with this? 回答1:

Is foldl ever preferable to its strict cousin, foldl'?

蓝咒 提交于 2019-12-09 14:26:46
问题 Haskell has two left fold functions for lists: foldl , and a "strict" version, foldl' . The problem with the non-strict foldl is that it builds a tower of thunks: foldl (+) 0 [1..5] --> ((((0 + 1) + 2) + 3) + 4) + 5 --> 15 This wastes memory, and may cause a stack overflow if the list has too many items. foldl' , on the other hand, forces the accumulator on every item. However, as far as I can tell, foldl' is semantically equivalent to foldl . Evaluating foldl (+) 0 [1..5] to head normal form

Evaluation and space leaks in Haskell

狂风中的少年 提交于 2019-12-09 09:04:00
问题 I'm learning Haskell and currently trying to wrap my head around monads. While playing with some random number generation I got tripped on lazy evaluation once again. In an effort to simplify something close to the: roll :: State StdGen Int roll = do gen <- get let (n, newGen) = randomR (0,1) gen put newGen return n main = do gen <- getStdGen let x = sum $ evalState (replicateM iterations roll) gen print x into something like this: roll' :: IO Int roll' = getStdRandom $ randomR (0,1) main =

Infinite recursion when enumerating all values of a Generic instance

[亡魂溺海] 提交于 2019-12-08 16:10:31
问题 For another answer of mine, I wrote the following code, providing diagonally traversed Universe instances for enumerable Generic s (it's slightly updated from the version there, but uses the same logic): {-# LANGUAGE DeriveGeneric, TypeOperators, ScopedTypeVariables #-} {-# LANGUAGE FlexibleInstances, FlexibleContexts, DefaultSignatures #-} {-# LANGUAGE UndecidableInstances, OverlappingInstances #-} import Data.Universe import Control.Monad.Omega import GHC.Generics import Control.Monad

Is there any guarantee about the evaluation order within a pattern match?

ぃ、小莉子 提交于 2019-12-05 13:11:56
问题 The following (&&) :: Bool -> Bool -> Bool False && _ = False True && False = False True && True = True has the desired short-circuit property False && undefined ≡ False . The first clause, which is non-strict in the right argument, is guaranteed to be checked before anything else is tried. Apparently, it still works if I change the order and even uncurry the function both :: (Bool,Bool) -> Bool both (True,False) = False both (True, True) = True both (False, _) = False Prelude> both (False,

Logical AND strictness with IO monad

不羁的心 提交于 2019-12-05 08:27:22
I am trying to write a simple program in Haskell. It should basically run two shell commands in parallel. Here is the code: import System.Cmd import System.Exit import Control.Monad exitCodeToBool ExitSuccess = True exitCodeToBool (ExitFailure _) = False run :: String -> IO Bool run = (fmap exitCodeToBool) . system main = liftM2 (&&) (run "foo") (run "bar") But command "foo" returns ExitFailure and I expect "bar" never to run. This is not the case! They both run and both show errors on the console. At the same time False && (all (/= 0) [1..]) evaluates perfectly well; this means the second