# Weird edge case in the language syntax '1 - 2' /= '1 -2'

**URL:** <https://discourse.elm-lang.org/t/weird-edge-case-in-the-language-syntax-1-2-1-2/2371>\
**Category:** Learn\
**Created:** [October 30, 2018, 7:05pm UTC](https://discourse.elm-lang.org/t/weird-edge-case-in-the-language-syntax-1-2-1-2/2371 "2018-10-30T19:05:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cout970](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@cout970](https://discourse.elm-lang.org/u/cout970)\
**Post date:** [October 30, 2018, 7:05pm UTC](https://discourse.elm-lang.org/t/weird-edge-case-in-the-language-syntax-1-2-1-2/2371/1 "2018-10-30T19:05:07Z")

</div>

I’ve been trying to implement an Elm compile ([here](https://github.com/cout970/Elm-interpreter)) in my free time and I found a weird edge case in the language syntax with the prefix minus. Using the `elm repl` I get the following output:

```auto
> 1 - 2
-1 : number
> 1 -2
-- TOO MANY ARGS ----------------------------------------------------------- elm

This value is not a function, but it was given 1 argument.

4| 1 -2
    ^
Are there any missing commas? Or missing parentheses?
> 1- 2
-1 : number
> 1-2
-1 : number

```

In the second example there is an error because the prefix minus has higher priority that the function application, but in fourth example there is no error.

I want to know how the compiler determines the priority of the prefix minus, unfortunately I don’t know Haskell well enough to just read the compiler source code.

---

<div class="post-metadata">

**Author:** ![hkgumbs](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hkgumbs/32/1251_2.png) [@hkgumbs](https://discourse.elm-lang.org/u/hkgumbs)\
**Post date:** [October 30, 2018, 9:15pm UTC](https://discourse.elm-lang.org/t/weird-edge-case-in-the-language-syntax-1-2-1-2/2371/2 "2018-10-30T21:15:43Z")

</div>

That sounds like a really fun project! I hope we hear more about it someday 🙂. To answer your question, when the elm parser sees `-` it first parses it as a binary operator. Then it does an explicit check for this exact scenario: _if I just parsed a minus sign, and there’s no trailing space, then convert it to a negation_. At least, that’s my best attempt at translating the Haskell. Here are the exact lines if you want to dig more.

> <https://github.com/elm/compiler/blob/master/compiler/src/Parse/Expression.hs#L292-L309>

---

<div class="post-metadata">

**Author:** ![cout970](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@cout970](https://discourse.elm-lang.org/u/cout970)\
**Post date:** [October 30, 2018, 10:42pm UTC](https://discourse.elm-lang.org/t/weird-edge-case-in-the-language-syntax-1-2-1-2/2371/4 "2018-10-30T22:42:44Z")

</div>

Thanks for your attempt to translate the Haskell code, even if it’s similar to Elm, I get lost at those `$` and `<-`

Seems like I just need to handle the special case, the problem is that I don’t have the position of the tokens when parsing, I plan to add that in the near future for better error reporting, but for now I will have to use different tokens for the `-` and `-` with spaces.

Also since the project is in Rust I can compile it to WebAssembly, so you will probably hear about the project soon.

---

<div class="post-metadata">

**Author:** ![itsgreggreg](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/itsgreggreg/32/462_2.png) [@itsgreggreg](https://discourse.elm-lang.org/u/itsgreggreg)\
**Post date:** [October 30, 2018, 11:54pm UTC](https://discourse.elm-lang.org/t/weird-edge-case-in-the-language-syntax-1-2-1-2/2371/5 "2018-10-30T23:54:09Z")

</div>

Totally an opinion and clearly not on parity with how elm currently works but I say make infix operators not surrounded by whitespace a syntax error. elm-format isn’t going to let you get away with writing `1-2`, `1- 2` or `23|>String.fromInt`, I can’t recall a time I’ve seen anyone use those forms, and elm is a whitespace significant language anyway.

On a related note, `1<-2` produces:

```auto
I do not recognize the (<-) operator.

34| let _ = 1<-2
                 ^^
Is there an `import` and `exposing` entry for it? Maybe you want (-) or (<)
instead?

```

---

<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 9, 2018, 11:54pm UTC](https://discourse.elm-lang.org/t/weird-edge-case-in-the-language-syntax-1-2-1-2/2371/6 "2018-11-09T23:54:14Z")

</div>

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