According the F# specification for operator overloading
<# #> op_Quotation
<## ##> op_QuotationUntyped
is given as with many other operators. Unless I'm missing something I don't believe that I can use this for custom types, so why is it listed?
I think you are right that there is no way of actually using those as custom operators. I suspect those are treated as operators in case this was useful, at some point in the future of the language, for some clever new feature.
The documentation really merely explains how the names of the operators get encoded. For non-special operator names, F# encodes those in a systematic way. For the ones listed in the page, it has a special nicer name. Consider this type:
type X() =
static member (<^><>) (a:int,b:int) = a + b
static member (<# #>) (a:int,b:int) = a + b
If you look at the names of those members:
[ for m in typeof<X>.GetMembers() -> m.Name ]
You see that the first operator got compiled as op_LessHatGreaterLessGreater, while the second one as op_Quotation. So this is where the name memntioned in the table comes in - it is probably good this is documented somewhere, but I think you're right, that this is not particularly useful!
Related
I have found this superb JSON library for F#, it's inspired by Elm's Json.Decode and it defines a basic Decoder type like this:
type Decoder<'T> = string -> obj -> Result<'T, DecoderError> (here)
There are functions like Decode.map and I wish I could make it F#+ compatible, so I could use it like:
let customerId = Decode.string |>> CustomerId
(see |>> infix version of generic map)
As far as I can see, to make a 3rd party library use F#+ concepts like Functors, one needs to extend the 3rd party type with static member Map, but the Decoder<'T> is just an abbreviation.
Is there any solution? Did anyone try?
P.S.
My solution so far is a custom binding:
let (<!>) = Decode.map
let customerId: Decoder<CustomerId> = CustomerId <!> Decode.string
let submitId: Decoder<SubmitId> = SubmitId <!> Decode.string
let currency: Decoder<Currency> = Currency <!> Decode.string
// etc…
The problem is that you don't control the code. If you had control over it, a solution would be:
Implement Decoder as a concrete type as opposed to a type alias. A type alias can't have additional members, because it's not really another type. Library authors should use single case discriminated unions, now that you can make them structs with nearly zero overhead.
Add the member Map with the required signature.
Alternatively, if extensions members becomes visible for trait constraints in a future F# version, you would be able just extend the type with the Map function, maybe with some undesired effects as it's a type alias for a function with 2 arguments.
So, I think the best you can do is what you already showed.
As a side note, you might be interested in having a look at Fleece which also provides decoders, but it also goes one step further and provides you codecs which goes in both directions (and you can map and <*> over it).
Looking for information about statically resolved type parameters for inline functions I stumbled upon the definitions of various primitive operators in FSharp.Core:
let inline (+) (x: ^T) (y: ^U) : ^V =
CheckedAdditionDynamic<(^T),(^U),(^V)> x y
when ^T : int32 and ^U : int32 = (# "add.ovf" x y : int32 #)
when ^T : float and ^U : float = (# "add" x y : float #)
// <snip>
when ^T : ^T = ((^T or ^U): (static member (+) : ^T * ^U -> ^V) (x,y))
As can be seen in the snippet above the when keyword is used in the format of: when expr1 = expr2 for various built-in types. I'm guessing that this is some sort of compiler equivalent of "if T=int use opcode add.ovf, else if ..., else do that".
However, I could not find a single reference/explanation to this kind of syntax in the F# documentation. Could someone with some inside knowledge of F# explain what is going on in that snippet above?
User Carsten has provided the following comment to this answer as he considers it to be wrong.
the thing is: when used as is here has nothing to do with the
documented usages - it seems to be called static conditional
optimization and should not be used outside the core libraries -
indeed go on and try to use it - you will see that you cannot unless
you use the tricks mentioned in Johns answer (other question)
User Carsten added an additional comment to this answer:
I added a comment - I don't think my educated guess is worth an answer
- I hoped that one of the insiders hanging around would finally put an official answer to it
The answer referred to in Carsten's first comment is by user John Palmer in April 2013 which links to this answer he provided on the (# ..... #) syntax, What is the (# ... #) syntax seen in F3 standard library implementation?
You can actually use this but you have to specify the
--compiling-fslib (undocumented) and --standalone flags in your code.
User MisterMetaphor provided an answer quoting a posting in a forum that said the following:
Embedded IL in F# codes. Is this feature officially supported?
Not really. The 99.9% purpose of this feature is for operations
defined in FSharp.Core.dll (called fslib.dll in 1.9.2.9 and before).
For other uses of the when keyword see the following.
This Microsoft document describes using the when keyword for additional conditions on matching, Match Expressions (F#).
This Microsoft document describes using the when keyword to express constraints for generic type parameters, Constraints (F#).
Also see this Microsoft document describing pattern matching with the when keyword in various settings, Pattern Matching (F#).
The Pattern Matching document says the following along with several examples.
Patterns are rules for transforming input data. They are used
throughout the F# language to compare data with a logical structure or
structures, decompose data into constituent parts, or extract
information from data in various ways.
The Match Expression document says the following along with an example.
You can use a when clause to specify an additional condition that the
variable must satisfy to match a pattern. Such a clause is referred to
as a guard. The expression following the when keyword is not evaluated
unless a match is made to the pattern associated with that guard.
On line 5633 in prim-types.fs (v1.9.7.8) there is the following type abbreviation:
type 'T ``lazy`` = Lazy<'T>
I have a few questions about it.
What do the double backticks mean?
Is this definition equivalent to type lazy<'T> = Lazy<'T>? (If not, how is it different?)
The double back ticks are a way of allowing an F# keyword to be used as an identifier. Another example would be
let ``let`` = 42
To answer the second half of your question, generic types in F# can be specified using either the O'Caml-style syntax where the generic parameter precedes the type (e.g 'a list, int array, etc.), or the .NET-style with angle brackets (e.g. list<'a>, array<int>, etc.), so the two definitions are indeed basically equivalent (except that your version as written is syntactically invalid because lazy is a keyword). For multi-parameter generic types, the O'Caml style is deprecated and will generate a warning (e.g. let (m:(int,string) Map) = Map.empty should be rewritten as let (m:Map<int,string>) = Map.empty).
I'm getting stymied by the way "dot notation" works with objects and records when trying to program in a point-free functional style (which I think is a great, concise way to use a functional language that curries by default).
Is there an operator or function I'm missing that lets me do something like:
(.) object method instead of object.method?
(From what I was reading about the new ? operator, I think it works like this. Except it requires definition and gets into the whole dynamic binding thing, which I don't think I need.)
In other words, can I apply a method to its object as an argument like I would apply a normal function to its argument?
Short answer: no.
Longer answer: you can of course create let-bound functions in a module that call a method on a given type... For example in the code
let l = [1;2;3]
let h1 = l.Head
let h2 = List.hd l
there is a sense in which "List.hd" is the version of what you want for ".Head on a list". Or locally, you can always do e.g.
let AnotherWay = (fun (l:list<_>) -> l.Head)
let h3 = AnotherWay l
But there is nothing general, since there is no good way to 'name' an arbitrary instance method on a given type; 'AnotherWay' shows a way to "make a function out of the 'Head' property on a 'list<_>' object", but you need such boilerplate for every instance method you want to treat as a first-class function value.
I have suggested creating a language construct to generalize this:
With regards to language design
suggestions, what if
SomeType..Foo optArgs // note *two* dots
meant
fun (x : SomeType) -> x.Foo optArgs
?
In which case you could write
list<_>..Head
as a way to 'functionize' this instance property, but if we ever do anything in that arena in F#, it would be post-VS2010.
If I understand your question correctly, the answer is: no you can't. Dot (.) is not an operator in F#, it is built into the language, so can't be used as function.
I'm trying to understand what identifiers represent and what they don't represent.
As I understand it, an identifier is a name for a method, a constant, a variable, a class, a package/module. It covers a lot. But what can you not use it for?
Every language differs in terms of what entities/abstractions can or cannot be named and reused in that language.
In most languages, you can't use an identifier for infix arithmetic operations.
For example, plus is an identifier and you can make a function named plus. But write you can write a = b + c;, there's no way to define an operator named plus to make a = b plus c; work because the language grammar simply does not allow an identifier there.
An identifier allows you to assign a name to some data, so that you can reference it later. That is the limit of what identifiers do; you cannot "use" it for anything other than a reference to some data.
That said, there are a lot of implications that come from this, some subtle. For example, in most languages functions are, to some degree or another, considered to be data, and so a function name is an identifier. In languages where functions are values, but not "first-class" values, you can't use an identifier for a function in an place you could use an identifier for something else. In some languages, there will even be separate namespaces for functions and other data, and so what is textually the same identifier might refer to two different things, and they would be distinguished by the context in which they are used.
An example of what you usually (i.e., in most languages) cannot use an identifier for is as a reference to a language keyword. For example, this sort of thing generally can't be done:
let during = while;
during (true) { print("Hello, world."); }
You could say it's used for everything that you'll want to refer to multiple times, or maybe even once (but use it to clarify the referent's purpose).
What can/can't be named differs per language, it's often quite intuitive, IMHO.
An "Anonymous" entity is something which is not named, although referred to somehow.
#!/usr/bin/perl
$subroutine = sub { return "Anonymous subroutine returning this text"; }
In Perl-speak, this is anonymous - the subroutine is not named, but it is referred to by the reference variable $subroutine.
PS: In Perl, the subroutine would be named like this:
sub NAME_HERE {
# some code...
}
Say, in Java your cannot write something like:
Object myIf = if;
myIf (a == b) {
System.out.println("True!");
}
So, you cannot name some code statement, giving it an alias. While in REBOL it is perfectly possible:
myIf: if
myIf a = b [print "True!"]
What can and what can't be named depends on language, as you see.
as its name implifies, an identifier is used to identify something. so for everything that can be identified uniquely, you can use an identifier. But for example a literal (e.g. string literal) is not unique so you can't use an identifier for it. However you can create a variable and assign a string literal to it.
Making soup out them is rather foul.
In languages such as Lisp, an identifier exists in its own right as an symbol, whereas in languages which are not introspective identifiers don't exist in the runtime.
You write a literal identifier/symbol by putting a single quote in front of it:
[1]> 'a
A
You can create a variable and assign a symbol literal to it:
[2]> (setf a 'Hello)
HELLO
[3]> a
HELLO
[4]> (print a)
HELLO
HELLO
You can set two variables to the same symbol
[10]> (setf b a)
HELLO
[11]> b
HELLO
[12]> a
HELLO
[13]> (eq b a)
T
[14]> (eq b 'Hello)
T
Note that the values bound to b and a are the same, and the value is the literal symbol 'Hello
You can bind a function to the symbol
[15]> (defun hello () (print 'hello))
HELLO
and call it:
[16]> (hello)
HELLO
HELLO
In common lisp, the variable binding and the function binding are distinct
[19]> (setf hello 'goodbye)
GOODBYE
[20]> hello
GOODBYE
[21]> (hello)
HELLO
HELLO
but in Scheme or JavaScript the bindings are in the same namespace.
There are many other things you can do with identifiers, if they are reified as symbols. I suspect that someone more knowledgable than me in Lisp will be able to demonstrate any of the things that you 'can't do with identifiers' exist.
But even Lisp can not make identifier soup.
Sort of a left-field thought, but JSON has all those quotations in it to eliminate the danger of a JavaScript keyword messing up the parsing.