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

Code Quotation for constraints #1

Open
voronoipotato opened this issue Dec 12, 2017 · 10 comments
Open

Code Quotation for constraints #1

voronoipotato opened this issue Dec 12, 2017 · 10 comments

Comments

@voronoipotato
Copy link

voronoipotato commented Dec 12, 2017

Is it possible instead of using a string to use a Code Quotation?

present

let [<Literal>] multiplesOf3Rule = "let f inputValue = inputValue % 3 = 0 \nf" 

proposed

let [<Literal>] multiplesOf3Rule = <@ let f inputValue = inputValue % 3 = 0 @>

This would allow for a better experience with your editor as you could use code highlighting and other important features.

@voronoipotato
Copy link
Author

This is really cool! Sorry I started with a feature request, obviously this is a nice to have not a need to have. I can't wait to try this out when I get home if I can get the internet set up. I just moved lol.

@ReedCopsey
Copy link

@voronoipotato This wouldn't work since it's a type provider - the input needs to be a literal, and F# doesn't allow a quotation as a literal value.

@Lleutch
Copy link
Owner

Lleutch commented Dec 13, 2017

@voronoipotato Sorry for the late reply from my part !!
@ReedCopsey is correct there, sadly we cannot do that. I have thought for a different way of dealing with that User-friendliness issue, for instance writing the function in another file script.fsx, but then you have the constraint rule defined in another file, in addition you need to find a way to have a separation between the different rules within that file. So all that is tricky.

Also, the last commit I have done is bad/not compiling (I don't remember why I have done it, there must have been a reason I completely forgot), take the one previous to that one if you want to give that a try.

I will close that issue then, if you want to discuss more on it don't hesitate to re-open it!! 😃

@Lleutch Lleutch closed this as completed Dec 13, 2017
@voronoipotato
Copy link
Author

Couldn't we as a Fake step regex replace <@ with " and @> with \nf" . This way from an editor perspective you still get all your highlighting etc, you don't have to use an entire other file, which I think would be cumbersome to switch between. We'd just have to be very explicit in the readme that your quoted expressions are being replaced when the line begins with or is preceded by [<Literal>]. That way people who prefer the string method could still use that without issue.

@voronoipotato
Copy link
Author

I guess we can't because they are writing the literal in their code. Can we keep this open and have it be the broader issue of "improving the user-friendliness of type-constraints". Just mark it as low priority, but it's something I could maybe help with, whereas I think everything else is above my paygrade.

@Lleutch
Copy link
Owner

Lleutch commented Dec 13, 2017

Yeah, we definitely can let this ticket open 😃
I will try to fine a nice way to ensure that ( maybe via attributes definition!!) .
Bear in mind that even though it is a string, you have compile-time guarantees, as the TP verifies at compile-time the correctness of the string as if it was purely F# code.

Also you can transform "let f x = x < 5 /nf" to "fun x -> x < 5" If I remember correctly, this should 😃

@Lleutch Lleutch reopened this Dec 13, 2017
@voronoipotato
Copy link
Author

yeah that makes sense, but especially as a beginner I still rely on the code highlighting, code completion etcetera quite a bit. However I do think the lambda version you showed there is a lot cleaner. Perhaps making a Fake extension associated with this that does the regex replace I was talking about would make it simpler to get started. I looked into custom attributes but I didn't see a way to resolve them at compile time.

@voronoipotato
Copy link
Author

fsharp/fslang-suggestions#450

Sounds like this issue is related to what you're working on.

@Lleutch
Copy link
Owner

Lleutch commented Dec 14, 2017

Yes exactly.
However, as you have seen it hasn't been implemented yet, and there are reasons for not having that to be implemented yet.
It is hard. I think for the purpose of TPs what needs to be done first is having type as TPs static parameters. Once we have this then we can somehow extend F# compiler to accept Expr<'a> types.
I think it would be a great idea to have, however the work needed behind is quite huge and not that trivial I think. It would of course go within the ML spirit, as to provide a better MetaProg story to F#.

Fake extension, won't work, because you need to have intellisense to show you the correct generated Types/Methods/Properties. And this is dependent of your static parameters. This means that you need at some point to have something that is given as a static parameter (that is a literal, not a quotation), that will be used to infer what will be generated. If you use quotation at design-time, then you won't have the TP generating the types at design-time, and thus you will lose all the purpose of using TPs sadly.
So it is a bit of a difficult situation we have here 😄.

P.S: By the way, thank you for your interest in this project 😄.

@voronoipotato
Copy link
Author

If the constraint is a pure lambda though we won't need to refer to anything outside the quoted expression being passed into the type provider. I guess your dependent types would need to be in a separate project? No problem!

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

No branches or pull requests

3 participants