# Does the underscore short-circuit

**URL:** <https://discourse.elm-lang.org/t/does-the-underscore-short-circuit/7879>\
**Category:** Learn\
**Created:** [November 4, 2021, 8:15am UTC](https://discourse.elm-lang.org/t/does-the-underscore-short-circuit/7879 "2021-11-04T08:15:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![j340m3](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/j340m3/32/2960_2.png) [@j340m3](https://discourse.elm-lang.org/u/j340m3)\
**Post date:** [November 4, 2021, 8:15am UTC](https://discourse.elm-lang.org/t/does-the-underscore-short-circuit/7879/1 "2021-11-04T08:15:39Z")

</div>

I am trying to replicate the short-circuiting behaviour of (&&). Would Elm translate this code to only evaluate the second argument if the first were True?

```auto
(&&) : Bool -> Bool -> Bool
(&&) a =
    case a of
        False -> \_ -> False
        True -> id

```

The documentation says AFAIK “discards the input”. Is the elm/compiler optimizing it then as shortcircuit?  
If it doesn’t, how could I implement short circuiting as frictionless as possibe? The best I could come up with is:

```auto
type Bool = Bool BVal
type BVal 
    = True
    | False

true : Bool
true = Bool True

false : Bool
false = Bool False
    
(&&) : Bool -> Bool -> Bool
(&&) (Bool a) =
    case a of
        False -> \_ -> Bool False
        True -> id

```

but that’s rather ugly IMO and causes horrible code.

I have been digging in elm/compiler for two hours right now, and am unable to test/verify my assumption.

Context: [elm-in-elm Issue](https://github.com/elm-in-elm/compiler/issues/50#issuecomment-960495445)

---

<div class="post-metadata">

**Author:** ![gampleman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gampleman/32/43_2.png) [@gampleman](https://discourse.elm-lang.org/u/gampleman)\
**Post date:** [November 4, 2021, 9:28am UTC](https://discourse.elm-lang.org/t/does-the-underscore-short-circuit/7879/2 "2021-11-04T09:28:07Z")

</div>

No, it doesn’t short-circuit. You can try it out like this:

[https://ellie-app.com/fLgLnwsQjyJa1](https://ellie-app.com/fLgLnwsQjyJa1)

`Debug.log` is quite a useful tool to figure out if something gets executed or not.

I’m afraid the only option is to use explicit thunks.

i.e.

`(&&) : Bool -> (() -> Bool) -> Bool`

---

<div class="post-metadata">

**Author:** ![j340m3](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/j340m3/32/2960_2.png) [@j340m3](https://discourse.elm-lang.org/u/j340m3)\
**Post date:** [November 4, 2021, 9:52am UTC](https://discourse.elm-lang.org/t/does-the-underscore-short-circuit/7879/3 "2021-11-04T09:52:58Z")

</div>

Thanks for the quick reply!  
Nice trick with Debug.log, why didn’t I come up with it? 🤦‍♂️

```auto
and : Bool -> (() -> Bool) -> Bool
and a b =
    case a of
        False ->
            False

        True ->
            b ()

```

would be your implementation? Isn’t that basically how lazy works 😜?

So the second `Bool` needs always an lambda prepended? as in

```auto
and (isShining sun) 
    (\() -> isGreen grass)

```

or can I somehow wrap the lambda expression away?

```auto
lazy : a -> () -> a
lazy x () = x

and (isShining sun) 
    (lazy <| isGreen grass)

```

causes x to be evaluated directly again, doesn’t it?

---

<div class="post-metadata">

**Author:** ![gampleman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gampleman/32/43_2.png) [@gampleman](https://discourse.elm-lang.org/u/gampleman)\
**Post date:** [November 4, 2021, 10:05am UTC](https://discourse.elm-lang.org/t/does-the-underscore-short-circuit/7879/4 "2021-11-04T10:05:02Z")

</div>

Yeah I’m afraid there isn’t much of a way to get from explicit thunks if you want to control when stuff is evaluated.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 14, 2021, 10:05am UTC](https://discourse.elm-lang.org/t/does-the-underscore-short-circuit/7879/5 "2021-11-14T10:05:23Z")

</div>

Closed on November 14, 2021, 10:05am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
