Why is this (~=) considered a prefix operator? - f#

let tolerance = 0.00000001
let (~=) x1 x2 = abs(x1 - x2) < tolerance
This throws the error:
"invalid operator definition. Prefix operator definitions must use a valid prefix operator name"
This isn't even a prefix operator, I don't get why it thinks so.
However the following is fine:
let (=~) x1 x2 = abs(x1 - x2) < tolerance
I only switched the order, so "=" comes before "~".
Is there any document online stating some rules regarding this?
I'm using Visual Studio 2013 with "F# 2013". The interactive console says "F# Interactive version 12.0.21005.1"

You can't define an infix operator starting with ~ in F#.
The F# 3.0 specification, section Categorization of Symbolic Operators explains the reason quite clearly:
The operators +, -, +., -., %, %%, &, && can be used as both prefix and infix operators. When these operators are used as prefix operators, the tilde character is prepended internally to generate the operator name so that the parser can distinguish such usage from an infix use of the operator. For example, -x is parsed as an application of the operator ~- to the identifier x. This generated name is also used in definitions for these prefix operators. Consequently, the definitions of the following prefix operators include the ~ character.

In F#, the ~ character in first position denotes a prefix operator. For example, (~-) is the prefix "opposite" operator: (~-) 3 is equivalent to - 3.

Related

Parsing an expression with binary prefix, infix and postfix operators

Is it possible to parse an expression (without ambiguity) that can contains binary prefix, binary infix and binary postfix operators (let's assume that all the symbols are different) with precedence between them? For example:
a = 2 3 post+
b = pre+ 2 3*4
Then a would equal to 5 because = has lower precedence than the postfix post+ operator and b would be 14. I know that you can parse infix notated expressions with operator precedence parse or shunting yard but this problem seems far more complex for me.
Edit:
Parenthesis are allowed and pre/post variations of an operator have the same precedence as the infix one.
I would like to roll a hand-written algorithm.
Edit2:
By precedence I mean how much to consume. For example this:
a = 2 3 post+
Could result in these AST-s:
'=' has higher precedence than 'post+':
post+
/ \
= 3
/ \
a 2
'post+' has higher precedence than '=':
=
/ \
a post+
/ \
2 3
(The second one is what I need in this situation). I can't really use existing parser generators or fixed grammar for operands because the operators are loaded dynamically.

Error: Unexpected infix operator in expression, about a successfully compiled prefix operator

Playing around a little bit with infix operators, I was surprised about the following:
let (>~~~) = function null -> String.Empty | s -> s // compiles fine, see screenshot
match >~~~ input with .... // error: Unexpected infix operator in expression
and:
Changing the first characters of the prefix operator (to !~~~ for instance) fixes it. That I get an error that the infix operator is unexpected is rather weird. Hovering shows the definition to be string -> string.
I'm not too surprised about the error, F# requires (iirc) that the first character of a prefix operator must itself be one of the predefined prefix operators. But why does it compile just fine, and when I use it, the compiler complains?
Update: the F# compiler seems to know in other cases just fine when I use an invalid character in my operator definition, it says "Invalid operator definition. Prefix operator definitions must use a valid prefix operator name."
The rules for custom operators in F# are quite tight - so even though you can define custom operators, there is a lot of rules about how they will behave and you cannot change those. In particular:
Only some operators (mainly those with ! and ~) can be used as prefix operators. With ~ you can also overload unary operators +, -, ~ and ~~, so if you define an operator named ~+., you can then use it as e.g. +. 42.
Other operators (including those starting with >) can only be used as infix. You can turn any operator into ordinary function using parentheses, which is why e.g. (+) 1 2 is valid.
The ? symbols is special (it is used for dynamic invocation) and cannot appear as the first symbol of a custom operator.
I think the most intuitive way of thinking about this is that custom operators will behave like standard F# operators, but you can add additional symbols after the standard operator name.

swift2 Xcode7.0 Expected ',' separator [duplicate]

Why i get error constantly on that?
var rotation:Float= Double(arc4random_uniform(50))/ Double(100-0.2)
Actually i try this one too:
var rotation:Double= Double(arc4random_uniform(50))/ Double(100-0.2)
thank you
Swift has strict rules about the whitespace around operators. Divide '/' is a binary operator.
The important rules are:
If an operator has whitespace around both sides or around neither
side, it is treated as a binary operator. As an example, the +
operator in a+b and a + b is treated as a binary operator.
If an operator has whitespace on the left side only, it is treated as a
prefix unary operator. As an example, the ++ operator in a ++b is
treated as a prefix unary operator.
If an operator has whitespace on
the right side only, it is treated as a postfix unary operator. As an
example, the ++ operator in a++ b is treated as a postfix unary
operator.
That means that you need to add a space before the / or remove the space after it to indicate that it is a binary operator:
var rotation = Double(arc4random_uniform(50)) / (100.0 - 0.2)
If you want rotation to be a Float, you should use that instead of Double:
var rotation = Float(arc4random_uniform(50)) / (100.0 - 0.2)
There is no need to specify the type explicitly since it will be inferred from the value you are assigning to. Also, you do not need to explicitly construct your literals as a specific type as those will conform to the type you are using them with.

Divided operation in Swift

Why i get error constantly on that?
var rotation:Float= Double(arc4random_uniform(50))/ Double(100-0.2)
Actually i try this one too:
var rotation:Double= Double(arc4random_uniform(50))/ Double(100-0.2)
thank you
Swift has strict rules about the whitespace around operators. Divide '/' is a binary operator.
The important rules are:
If an operator has whitespace around both sides or around neither
side, it is treated as a binary operator. As an example, the +
operator in a+b and a + b is treated as a binary operator.
If an operator has whitespace on the left side only, it is treated as a
prefix unary operator. As an example, the ++ operator in a ++b is
treated as a prefix unary operator.
If an operator has whitespace on
the right side only, it is treated as a postfix unary operator. As an
example, the ++ operator in a++ b is treated as a postfix unary
operator.
That means that you need to add a space before the / or remove the space after it to indicate that it is a binary operator:
var rotation = Double(arc4random_uniform(50)) / (100.0 - 0.2)
If you want rotation to be a Float, you should use that instead of Double:
var rotation = Float(arc4random_uniform(50)) / (100.0 - 0.2)
There is no need to specify the type explicitly since it will be inferred from the value you are assigning to. Also, you do not need to explicitly construct your literals as a specific type as those will conform to the type you are using them with.

How to write an infix function

Is there a way to write an infix function not using symbols? Something like this:
let mod x y = x % y
x mod y
Maybe a keyword before "mod" or something.
The existing answer is correct - you cannot define an infix function in F# (just a custom infix operator). Aside from the trick with pipe operators, you can also use extension members:
// Define an extension member 'modulo' that
// can be called on any Int32 value
type System.Int32 with
member x.modulo n = x % n
// To use it, you can write something like this:
10 .modulo 3
Note that the space before . is needed, because otherwise the compiler tries to interpret 10.m as a numeric literal (like 10.0f).
I find this a bit more elegant than using pipeline trick, because F# supports both functional style and object-oriented style and extension methods are - in some sense - close equivalent to implicit operators from functional style. The pipeline trick looks like a slight misuse of the operators (and it may look confusing at first - perhaps more confusing than a method invocation).
That said, I have seen people using other operators instead of pipeline - perhaps the most interesting version is this one (which also uses the fact that you can omit spaces around operators):
// Define custom operators to make the syntax prettier
let (</) a b = a |> b
let (/>) a b = a <| b
let modulo a b = a % b
// Then you can turn any function into infix using:
10 </modulo/> 3
But even this is not really an established idiom in the F# world, so I would probably still prefer extension members.
Not that I know of, but you can use the left and right pipe operators. For example
let modulo x y = x % y
let FourMod3 = 4 |> modulo <| 3
To add a little bit to the answers above, you can create a custom infix operators but the vocabulary is limited to:
!, $, %, &, *, +, -, ., /, <, =, >, ?, #, ^, |, and ~
Meaning you can build your infix operator using combining these symbols.
Please check the full documentation on MS Docs.
https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/operator-overloading

Resources