Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCaml not well defined? #7171

Closed
vicuna opened this issue Mar 9, 2016 · 4 comments
Closed

OCaml not well defined? #7171

vicuna opened this issue Mar 9, 2016 · 4 comments
Assignees

Comments

@vicuna
Copy link

vicuna commented Mar 9, 2016

Original bug ID: 7171
Reporter: bay2@illinois.edu
Assigned to: @lpw25
Status: closed (set by @xavierleroy on 2017-09-24T15:32:03Z)
Resolution: not a bug
Priority: normal
Severity: minor
Version: 4.02.3
Category: back end (clambda to assembly)

Bug description

Dear Sir or Madam,

Running the following code on OCaml version 4.02.3 yields the following output:

let f ?x y = match x with None -> y | Some z -> z +y ;;

val f : ?x:int -> int -> int =

f 2 ?x:(Some 3);;

  • : int = 5

(f 2) ?x:(Some 3);;

Error: This expression has type int
This is not a function; it cannot be applied.

This seems to indicate that in an application, OCaml evaluates the argument list to a function before applying one argument after the other to the function. Is OCaml not well defined? Do you have a semantics for OCaml to share?

Best,
Michael

Steps to reproduce

let f ?x y = match x with None -> y | Some z -> z +y ;;

val f : ?x:int -> int -> int =

f 2 ?x:(Some 3);;

  • : int = 5

(f 2) ?x:(Some 3);;

Error: This expression has type int
This is not a function; it cannot be applied.

@vicuna
Copy link
Author

vicuna commented Mar 9, 2016

Comment author: @lpw25

See section 4.1.1 of the manual.

@vicuna
Copy link
Author

vicuna commented Mar 9, 2016

Comment author: @gasche

More precisely, the "Function Applciation" documentation
http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#sec120
explicitly mentions that the application form is "f arg1 arg2 arg3 ...", so the semantics of "(f x y) z w" and "f x y z w" need not be the same (the former has two forms, the latter only one, it's a different AST). The textual explanation details the optional parameter semantics in a way that explains the difference you observe.

See Jacques Garrigue' research work with Jun Furuse on optional parameters for a formalization of the labels as found in OCaml:

http://www.math.nagoya-u.ac.jp/~garrigue/research.html#sel

A label-selective lambda-calculus with optional arguments and its compilation method
Jun Furuse and Jacques Garrigue, 1995
http://www.math.nagoya-u.ac.jp/~garrigue/papers/rims-1041.html

@vicuna
Copy link
Author

vicuna commented Mar 13, 2016

Comment author: bay2@illinois.edu

Gasche,

Thank you for your answer. I understand the application form as f arglist.

However, the example seems to give lie to the statement from the manual:

The expressions ( expr ) and begin expr end have the same value as expr.

@vicuna
Copy link
Author

vicuna commented Mar 13, 2016

Comment author: @gasche

I see what you mean: he fact that "(expr)" and "expr" evaluate in the same way is not compositional: it does not mean that "(expr) foo" and "expr foo" evaluate in the same way. But this is true in most languages, for example "(1+2)3" and "1+23" are distinct, yet explaining that parentheses do not affect the value seems reasonable even of arithmetical expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants