-
-
Notifications
You must be signed in to change notification settings - Fork 173
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
Double arithmetic? #166
Comments
How about adding a cookieMaxAge = Natural/toDouble (14 * 24 * 60 * 60) ... or |
I think just Integer/toDouble (or Double/fromInteger). Natural -> Double
can be derived from primitives and be done in Dhall.
…On Fri, Jun 1, 2018 at 3:29 PM Gabriel Gonzalez ***@***.***> wrote:
How about adding a Natural/toDouble built-in? Then you could do:
cookieMaxAge = Natural/toDouble (14 * 24 * 60 * 60)
... or Integer/toDouble (since Natural/toInteger exists) or both
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#166 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABRjtSLxlJR_Zdrh-gYCPKVbGPg4fCGks5t4U_AgaJpZM4UWlbx>
.
|
Yeah, I'm also leaning towards only providing |
|
The proposed change to the standard is here: #168 |
@Gabriel439 one more thought: this won’t allow Double calculations like
|
@michalrus: That's correct. The proposed change still doesn't permit double arithmetic |
Yes, yes, I totally understand that from the diff and this discussion above. But, now, after going through our Dhall files, I’m thinking that m-m-maaaybe being able to say |
I should have read all of the files when posting this issue, sorry. 😿 |
@michalrus: No worries :) I think I'll postpone adding |
I've ended up with another use-case for Double arithmetic- I wanted to have some user-supplied templates that are essentially "fragments" of SVG, and know how to draw themselves at supplied coordinates + dimensions. Basically, the user-supplied Dhall expression is something like this:
Would there be an issue with adding division only for Doubles, and just allowing "Infinity" values when division by zero occurs? I see that division for other types might be a problem (because of exceptions) though. |
@statusfailed What if you made a new data type that represented ratios like |
@statusfailed I don’t know that it’ll provide what you want, but as @FintanH alluded to, I do have a nascent “dhall-numerics” library I’ve been working on. I can publish it later today, and you can take a look to see what’s there. |
@FintanH I think I'd still need a function to get the decimal representation for my final output, right? Can this be done within Dhall? To solve the immediate problem I might just add Double/div in this way |
@sellout sure, I'll have a look, thanks! |
So the idea would be that you delay the computation until you interpret into your final language, e.g. Haskell, and then it will translate your Ratio into Double. The plus side of this is that you won't have the potential of precision errors until the very last moment :) |
@statusfailed: Yeah, I think you will probably want to add built-in @FintanH: My understanding is that implementing the |
Unless you can delay the |
@FintanH @Gabriel439 I threw together a little example of how this can be done here based on the Wiki guide. Hopefully this helps anyone else who needs to do the same. Thanks everyone for your help! |
@statusfailed: Awesome! Thank you 🙂 |
Probably worth adding to the Wiki guide as a working example? |
Hmm... seems like I've hit another problem: I'm not able to use (in Haskell-land) functions defined in terms of my newly-added Double arithmetic functions. For example, when I define a dhall file like this:
I get this error in my Haskell program when trying to use it with
Whereas if I only use "existing" Double functions (like the below Dhall function), everything works:
Is there a way I can solve this? |
Can you provide a full example? |
From dhall-lang issue #166 here: dhall-lang/dhall-lang#166 Using "broken.dhall" will result in this exception: *** Exception: Interpret: You cannot decode a function if it does not have the correct type Using "working.dhall" will display "1.0".
I've created a full example on the branch There are two Dhall files added.
And FWIW, I am using Nix, so running this with version 1.19.1 of Dhall - not sure if this would make a difference. Let me know if there's anything else I can provide - cheers! |
@statusfailed: I'll check this soon. Version 1.19.1 should be new enough since we haven't changed anything related to this since then |
@statusfailed: I found the problem. The issue is that the instance (Inject a, Interpret b) => Interpret (a -> b) where
autoWith opts = Type extractOut expectedOut
where
-- The `Dhall.Core.normalize` is ignoring your customizations
extractOut e = Just (\i -> case extractIn (Dhall.Core.normalize (App e (embed i))) of
Just o -> o
Nothing -> error "Interpret: You cannot decode a function if it does not have the correct type" )
expectedOut = Pi "_" declared expectedIn
InputType {..} = inject
Type extractIn expectedIn = autoWith opts This is why it works on built-in functions but not with your custom additions I think fixing this properly requires adding a field to |
This fixes the problem found in dhall-lang/dhall-lang#166 (comment) This ensures that the functions marshalled into Haskell can correctly take advantage of user-supplied language extensions
Alright, I have a fix up here: dhall-lang/dhall-haskell#777 I was able to verify it works using the following change to your diff --git a/Main.hs b/Main.hs
index abe5afc..16b6c06 100644
--- a/Main.hs
+++ b/Main.hs
@@ -60,12 +60,15 @@ doubleNormalizer =
main :: IO ()
main = do
- text <- Data.Text.IO.getContents
let context = doubleEnrichedContext
normalizer = ReifiedNormalizer $ pure . doubleNormalizer
addSettings = Lens.set Dhall.normalizer normalizer
. Lens.set Dhall.startingContext context
inputSettings = addSettings Dhall.defaultInputSettings
- x <- Dhall.inputWithSettings inputSettings Dhall.auto text
- Data.Text.IO.putStrLn x
+ interpretOptions =
+ Dhall.defaultInterpretOptions
+ { Dhall.inputNormalizer = normalizer }
+
+ f <- Dhall.inputWithSettings inputSettings (Dhall.autoWith interpretOptions) "Double/add" :: IO (Double -> Double -> Double)
+ print (f 1 2) |
Updated Main.hs based on Gabriel439's code from this thread dhall-lang/dhall-lang#166. Also changed nix build to add these files: * dhall-haskell.nix * double-dhall.nix * release.nix * shell.nix `dhall-haskell` points to the bleeding-edge commit (see this PR: dhall-lang/dhall-haskell#777) which fixes the "You cannot decode a function..." problem.
Beautiful, works perfectly. I updated my branch on double-dhall to include your fix. Thanks very much! |
@statusfailed: You're welcome! Thank you for reporting the issue 🙂 |
This fixes the problem found in dhall-lang/dhall-lang#166 (comment) This ensures that the functions marshalled into Haskell can correctly take advantage of user-supplied language extensions
I know it’s deliberately left out, but we have quite a few of these in config files:
As sometimes sub-second
NominalDiffTime
s are needed:What would be the best way to go here?
The text was updated successfully, but these errors were encountered: