Skip to content

Instantly share code, notes, and snippets.

@dillonkearns
Created April 3, 2026 20:35
Show Gist options
  • Select an option

  • Save dillonkearns/289891a7fa75b95b0336ffa25f0f77d6 to your computer and use it in GitHub Desktop.

Select an option

Save dillonkearns/289891a7fa75b95b0336ffa25f0f77d6 to your computer and use it in GitHub Desktop.
Demo: Elm 0.19.1 compiles (== -1) to _Utils_eq because negative numbers aren't JS AST literals
#!/usr/bin/env bash
# Demonstrates that Elm 0.19.1 compiles (== -1) to _Utils_eq, not ===
# because negative numbers are JS.Prefix expressions, not JS.Int literals.
# See: https://github.com/elm/compiler/blob/0.19.1/compiler/src/Generate/JavaScript/Expression.hs#L549-L592
set -e
DIR=$(mktemp -d)
cd "$DIR"
cat > elm.json << 'EOF'
{ "type": "application", "source-directories": ["src"], "elm-version": "0.19.1",
"dependencies": { "direct": { "elm/core": "1.0.5", "elm/json": "1.1.3" }, "indirect": {} },
"test-dependencies": { "direct": {}, "indirect": {} } }
EOF
mkdir src
cat > src/Main.elm << 'EOF'
port module Main exposing (main)
import Platform
eqNegOne : Int -> Bool
eqNegOne x = x == -1
eqPosOne : Int -> Bool
eqPosOne x = x == 1
eqString : String -> Bool
eqString s = s == "hello"
ltZero : Int -> Bool
ltZero x = x < 0
port out : Bool -> Cmd msg
main = Platform.worker
{ init = \() -> ( (), out (eqNegOne 0 && eqPosOne 0 && eqString "" && ltZero 0) )
, update = \_ m -> (m, Cmd.none)
, subscriptions = \_ -> Sub.none }
EOF
elm make --optimize src/Main.elm --output=out.js > /dev/null 2>&1
echo "Elm $(elm --version) --optimize output:"
echo ""
echo "x == -1 => $(grep -A1 'eqNegOne' out.js | grep -o '_Utils_eq.*\|x ===.*' | head -1)"
echo "x == 1 => $(grep -A1 'eqPosOne' out.js | grep -o '_Utils_eq.*\|x ===.*' | head -1)"
echo 's == "hi" => '"$(grep -A1 'eqString' out.js | grep -o '_Utils_eq.*\|s ===.*' | head -1)"
echo "x < 0 => $(grep -A1 'ltZero' out.js | grep -o 'x < 0' | head -1)"
rm -rf "$DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment