In Haskell, when using the XStrict language extension, is if short-circuiting?Is there a Haskell compiler or preprocessor that uses strict evaluation?Getting started with HaskellWhat is Haskell used for in the real world?Large-scale design in Haskell?Haskell: How does non-strict and lazy differ?Non-lazy branch of GHCWhat would pattern matching look like in a strict Haskell?Can every functional language be lazy?Call-by-name in Scala vs lazy evaluation in Haskell?How Haskell handles parallel computing on a multicore machine/cluster
Is there a general term for the items in a directory?
How do you say 3 ↑↑↑ 3?
How to trick a fairly simplistic kill-counter?
When did J.K. Rowling decide to make Ron and Hermione a couple?
Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?
Being told my "network" isn't PCI Complaint. I don't even have a server! Do I have to comply?
In Haskell, when using the XStrict language extension, is if short-circuiting?
Plotting Chebyshev polynomials using PolarPlot and FilledCurve
Is verification of a blockchain computationally cheaper than recreating it?
A conjectural trigonometric identity
Applied Meditation
Is Norway in the Single Market?
How long should I wait to plug in my refrigerator after unplugging it?
Adding a (stair/baby) gate without facing walls
Does the problem of P vs NP come under the category of Operational Research?
How to draw twisted cuves?
Were there any unmanned expeditions to the moon that returned to Earth prior to Apollo?
Can an alphabet for a Turing machine contain subsets of other alphabets?
Does the use of a new concept require a prior definition?
What is the most 'environmentally friendly' way to learn to fly?
Could flaps be raised upward to serve as spoilers / lift dumpers?
How is Sword Coast North governed?
Ernie and the Superconducting Boxes
Feedback diagram
In Haskell, when using the XStrict language extension, is if short-circuiting?
Is there a Haskell compiler or preprocessor that uses strict evaluation?Getting started with HaskellWhat is Haskell used for in the real world?Large-scale design in Haskell?Haskell: How does non-strict and lazy differ?Non-lazy branch of GHCWhat would pattern matching look like in a strict Haskell?Can every functional language be lazy?Call-by-name in Scala vs lazy evaluation in Haskell?How Haskell handles parallel computing on a multicore machine/cluster
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Standard Haskell is lazily evaluated, so if myCondition then someValue else doSomeLargeComputation x y z
will avoid evaluating doSomeLargeComputation x y z
if myCondition
is true. My question is, if I enable the language extension XStrict
then will doSomeLargeComputation x y z
now be evaluated even when myCondition
is true?
If so, is there a control flow construct other than explicitly marking doSomeLargeComputation x y z
as lazy that can be used to avoid calculating it (like a short-circuiting if statement in a strict language)?
haskell
add a comment |
Standard Haskell is lazily evaluated, so if myCondition then someValue else doSomeLargeComputation x y z
will avoid evaluating doSomeLargeComputation x y z
if myCondition
is true. My question is, if I enable the language extension XStrict
then will doSomeLargeComputation x y z
now be evaluated even when myCondition
is true?
If so, is there a control flow construct other than explicitly marking doSomeLargeComputation x y z
as lazy that can be used to avoid calculating it (like a short-circuiting if statement in a strict language)?
haskell
if
is non-strict in any language I know -- this is even more important in impure languages, where you do not want to observe the side effects of both branches. In an (insane, IMO) higher-order language with a strictif
we could still write(if cond then (_-> doX) else (_-> doY)) ()
to force it to be lazy, but that hack would need to be used to often that it should be the default.
– chi
6 hours ago
add a comment |
Standard Haskell is lazily evaluated, so if myCondition then someValue else doSomeLargeComputation x y z
will avoid evaluating doSomeLargeComputation x y z
if myCondition
is true. My question is, if I enable the language extension XStrict
then will doSomeLargeComputation x y z
now be evaluated even when myCondition
is true?
If so, is there a control flow construct other than explicitly marking doSomeLargeComputation x y z
as lazy that can be used to avoid calculating it (like a short-circuiting if statement in a strict language)?
haskell
Standard Haskell is lazily evaluated, so if myCondition then someValue else doSomeLargeComputation x y z
will avoid evaluating doSomeLargeComputation x y z
if myCondition
is true. My question is, if I enable the language extension XStrict
then will doSomeLargeComputation x y z
now be evaluated even when myCondition
is true?
If so, is there a control flow construct other than explicitly marking doSomeLargeComputation x y z
as lazy that can be used to avoid calculating it (like a short-circuiting if statement in a strict language)?
haskell
haskell
asked 9 hours ago
LogicChainsLogicChains
1,7242 gold badges10 silver badges16 bronze badges
1,7242 gold badges10 silver badges16 bronze badges
if
is non-strict in any language I know -- this is even more important in impure languages, where you do not want to observe the side effects of both branches. In an (insane, IMO) higher-order language with a strictif
we could still write(if cond then (_-> doX) else (_-> doY)) ()
to force it to be lazy, but that hack would need to be used to often that it should be the default.
– chi
6 hours ago
add a comment |
if
is non-strict in any language I know -- this is even more important in impure languages, where you do not want to observe the side effects of both branches. In an (insane, IMO) higher-order language with a strictif
we could still write(if cond then (_-> doX) else (_-> doY)) ()
to force it to be lazy, but that hack would need to be used to often that it should be the default.
– chi
6 hours ago
if
is non-strict in any language I know -- this is even more important in impure languages, where you do not want to observe the side effects of both branches. In an (insane, IMO) higher-order language with a strict if
we could still write (if cond then (_-> doX) else (_-> doY)) ()
to force it to be lazy, but that hack would need to be used to often that it should be the default.– chi
6 hours ago
if
is non-strict in any language I know -- this is even more important in impure languages, where you do not want to observe the side effects of both branches. In an (insane, IMO) higher-order language with a strict if
we could still write (if cond then (_-> doX) else (_-> doY)) ()
to force it to be lazy, but that hack would need to be used to often that it should be the default.– chi
6 hours ago
add a comment |
1 Answer
1
active
oldest
votes
No, the branch not taken is not evaluated.
For example:
-# LANGUAGE Strict #-
import Debug.Trace
main :: IO ()
main = print (f (fib 3))
f i =
if i < 5
then trace "then" 0
else trace "else" 1
-- Using fib to avoid inlining or optimization from messing up or test.
fib :: Int -> Int
fib 1 = 1
fib n = n + fib (n-1)
Prints:
*Main> main
else
1
However, if we lift the branches to let bindings then yes, those are strictly evaluated:
f :: Int -> Int
f i =
let f1 = trace "then" 1 in
let f2 = trace "else" 2 in
if i < 5
then f1
else f2
Yields:
% ./LogicChains
then
else
2
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57339988%2fin-haskell-when-using-the-xstrict-language-extension-is-if-short-circuiting%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, the branch not taken is not evaluated.
For example:
-# LANGUAGE Strict #-
import Debug.Trace
main :: IO ()
main = print (f (fib 3))
f i =
if i < 5
then trace "then" 0
else trace "else" 1
-- Using fib to avoid inlining or optimization from messing up or test.
fib :: Int -> Int
fib 1 = 1
fib n = n + fib (n-1)
Prints:
*Main> main
else
1
However, if we lift the branches to let bindings then yes, those are strictly evaluated:
f :: Int -> Int
f i =
let f1 = trace "then" 1 in
let f2 = trace "else" 2 in
if i < 5
then f1
else f2
Yields:
% ./LogicChains
then
else
2
add a comment |
No, the branch not taken is not evaluated.
For example:
-# LANGUAGE Strict #-
import Debug.Trace
main :: IO ()
main = print (f (fib 3))
f i =
if i < 5
then trace "then" 0
else trace "else" 1
-- Using fib to avoid inlining or optimization from messing up or test.
fib :: Int -> Int
fib 1 = 1
fib n = n + fib (n-1)
Prints:
*Main> main
else
1
However, if we lift the branches to let bindings then yes, those are strictly evaluated:
f :: Int -> Int
f i =
let f1 = trace "then" 1 in
let f2 = trace "else" 2 in
if i < 5
then f1
else f2
Yields:
% ./LogicChains
then
else
2
add a comment |
No, the branch not taken is not evaluated.
For example:
-# LANGUAGE Strict #-
import Debug.Trace
main :: IO ()
main = print (f (fib 3))
f i =
if i < 5
then trace "then" 0
else trace "else" 1
-- Using fib to avoid inlining or optimization from messing up or test.
fib :: Int -> Int
fib 1 = 1
fib n = n + fib (n-1)
Prints:
*Main> main
else
1
However, if we lift the branches to let bindings then yes, those are strictly evaluated:
f :: Int -> Int
f i =
let f1 = trace "then" 1 in
let f2 = trace "else" 2 in
if i < 5
then f1
else f2
Yields:
% ./LogicChains
then
else
2
No, the branch not taken is not evaluated.
For example:
-# LANGUAGE Strict #-
import Debug.Trace
main :: IO ()
main = print (f (fib 3))
f i =
if i < 5
then trace "then" 0
else trace "else" 1
-- Using fib to avoid inlining or optimization from messing up or test.
fib :: Int -> Int
fib 1 = 1
fib n = n + fib (n-1)
Prints:
*Main> main
else
1
However, if we lift the branches to let bindings then yes, those are strictly evaluated:
f :: Int -> Int
f i =
let f1 = trace "then" 1 in
let f2 = trace "else" 2 in
if i < 5
then f1
else f2
Yields:
% ./LogicChains
then
else
2
answered 9 hours ago
Thomas M. DuBuissonThomas M. DuBuisson
57.7k7 gold badges91 silver badges155 bronze badges
57.7k7 gold badges91 silver badges155 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57339988%2fin-haskell-when-using-the-xstrict-language-extension-is-if-short-circuiting%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
if
is non-strict in any language I know -- this is even more important in impure languages, where you do not want to observe the side effects of both branches. In an (insane, IMO) higher-order language with a strictif
we could still write(if cond then (_-> doX) else (_-> doY)) ()
to force it to be lazy, but that hack would need to be used to often that it should be the default.– chi
6 hours ago