# Float and constructor syntax

**URL:** <https://discuss.ocaml.org/t/float-and-constructor-syntax/10763>\
**Category:** Learning\
**Tags:** ocaml\
**Created:** [November 5, 2022, 5:26pm UTC](https://discuss.ocaml.org/t/float-and-constructor-syntax/10763 "2022-11-05T17:26:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobrowser](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nobrowser/32/2099_2.png) [@nobrowser](https://discuss.ocaml.org/u/nobrowser)\
**Post date:** [November 5, 2022, 5:26pm UTC](https://discuss.ocaml.org/t/float-and-constructor-syntax/10763/1 "2022-11-05T17:26:33Z")

</div>

This looks completely trivial, and I am no rookie, but it still surprises me:

```auto
$ ocaml
OCaml version 4.14.0
Enter #help;; for help.

# (Ok 5.0) ;;
- : (float, 'a) result = Ok 5.
# (Ok -5.0) ;;
Error: The constructor Ok expects 1 argument(s),
       but is applied here to 0 argument(s)
# (Ok (-5.0)) ;;
- : (float, 'a) result = Ok (-5.)
#

```

Looks like the “-” is parsed as an operator and not as part of the float literal, but doesn’t this violate the “longest first” lexing rule?

–  
Ian

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [November 5, 2022, 6:18pm UTC](https://discuss.ocaml.org/t/float-and-constructor-syntax/10763/2 "2022-11-05T18:18:55Z")

</div>

The unary operator `-` is always tokenized as an unary operator. Thus the flow of tokens emitted by the lexer for

```auto
Ok -5.0

```

is

```auto
UIDENT("Ok") MINUS FLOAT(5.0)

```

The really strange corner case is

```auto
let (~-) x = x
let mystery =
  let x = 5.0 in
  -x + -5.0

```

(which is due to the fact that the parser recognizes the notion of signed constants)

---

<div class="post-metadata">

**Author:** ![nobrowser](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nobrowser/32/2099_2.png) [@nobrowser](https://discuss.ocaml.org/u/nobrowser)\
**Post date:** [November 5, 2022, 11:28pm UTC](https://discuss.ocaml.org/t/float-and-constructor-syntax/10763/3 "2022-11-05T23:28:47Z")

</div>

```auto
UIDENT("Ok") MINUS FLOAT(5.0)

```

Right, that is what I suspected.

> the parser recognizes the notion of signed constants

The parser, and not the lexer?

–  
Ian

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [November 6, 2022, 2:06am UTC](https://discuss.ocaml.org/t/float-and-constructor-syntax/10763/4 "2022-11-06T02:06:27Z")

</div>

I think you meant to use `+.` in there, yes?

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [November 6, 2022, 2:10am UTC](https://discuss.ocaml.org/t/float-and-constructor-syntax/10763/5 "2022-11-06T02:10:43Z")

</div>

Oho, this is interesting! So `-x` is not represented analogously to `-5.0` : the first is the application of the operator “-” (or “-.”) to the expression `x`. The second is a signed constant. Interesting. Gotta remember this!

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [November 6, 2022, 9:56am UTC](https://discuss.ocaml.org/t/float-and-constructor-syntax/10763/6 "2022-11-06T09:56:46Z")

</div>

Yes, it is the parser that recognizes that `MINUS constant` is a signed constant: [ocaml/parser.mly at trunk · ocaml/ocaml · GitHub](https://github.com/ocaml/ocaml/blob/trunk/parsing/parser.mly#L3533) .
