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

Provide a pure version of `input` #87

Closed
lthms opened this Issue Jul 20, 2017 · 12 comments

Comments

Projects
None yet
2 participants
@lthms

lthms commented Jul 20, 2017

Hi! I am currently working on a project whichin I would love to use dhall to improve its customization power. However, in order to do so without breaking too many things, I was wondering if it would be possible to use dhall script in pure functions.

That is, I would love to be able to load and parse the script once, then run it in a pure way several time with different arguments.

Do you think it is possible?

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 20, 2017

Collaborator

The next release of dhall will expose the internals of the Type constructor: 0221dd4

... so you will have access to a new extract function of type:

extract :: Type a -> Expr X X -> Maybe a

... and if you apply that to auto, it would have type:

extract auto :: Interpret a => Expr X X -> Maybe a

... which gives you a pure extraction function. Would that satisfy your use case?

Collaborator

Gabriel439 commented Jul 20, 2017

The next release of dhall will expose the internals of the Type constructor: 0221dd4

... so you will have access to a new extract function of type:

extract :: Type a -> Expr X X -> Maybe a

... and if you apply that to auto, it would have type:

extract auto :: Interpret a => Expr X X -> Maybe a

... which gives you a pure extraction function. Would that satisfy your use case?

@lthms

This comment has been minimized.

Show comment
Hide comment
@lthms

lthms Jul 21, 2017

That sounds great indeed! What if my dhall script is a function, lets say \(str: Text) -> ... which returns a Text. My understanding is that currently dhall does not let you just use input on the script without an argument, as there is no Interpret instance for Text -> Text. Will there be such an instance in the next release? Or, do you think such an instance could be written? I would be happy to help.

lthms commented Jul 21, 2017

That sounds great indeed! What if my dhall script is a function, lets say \(str: Text) -> ... which returns a Text. My understanding is that currently dhall does not let you just use input on the script without an argument, as there is no Interpret instance for Text -> Text. Will there be such an instance in the next release? Or, do you think such an instance could be written? I would be happy to help.

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 21, 2017

Collaborator

Assuming that you have a Dhall function of type Text → Text encoded in Haskell as a value of the following type:

myDhallFunction :: Expr X X

You can turn that into a Haskell function of type Text -> Text like this:

myHaskellFunction :: Text -> Text
myHaskellFunction input = case extract auto (App myDhallFunction (TextLit input)) of
    Just input -> input
    Nothing    -> error "This shouldn't happen if the function type-checks"

I can add an interpret instance that does this for a limited subset of functions but it will take some time because it requires adding a new class, so I would recommend use the above approach for now

Collaborator

Gabriel439 commented Jul 21, 2017

Assuming that you have a Dhall function of type Text → Text encoded in Haskell as a value of the following type:

myDhallFunction :: Expr X X

You can turn that into a Haskell function of type Text -> Text like this:

myHaskellFunction :: Text -> Text
myHaskellFunction input = case extract auto (App myDhallFunction (TextLit input)) of
    Just input -> input
    Nothing    -> error "This shouldn't happen if the function type-checks"

I can add an interpret instance that does this for a limited subset of functions but it will take some time because it requires adding a new class, so I would recommend use the above approach for now

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 21, 2017

Collaborator

Sorry, one correction. It should be:

myHaskellFunction :: Text -> Text
myHaskellFunction input = case extract auto (normalize (App myDhallFunction (TextLit input))) of
    Just input -> input
    Nothing    -> error "This shouldn't happen if the function type-checks"
Collaborator

Gabriel439 commented Jul 21, 2017

Sorry, one correction. It should be:

myHaskellFunction :: Text -> Text
myHaskellFunction input = case extract auto (normalize (App myDhallFunction (TextLit input))) of
    Just input -> input
    Nothing    -> error "This shouldn't happen if the function type-checks"
@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 21, 2017

Collaborator

Here's a first draft of what I have in mind: #88

It still needs more work though (to add tests, documentation, and generic programming support for user-defined types)

Collaborator

Gabriel439 commented Jul 21, 2017

Here's a first draft of what I have in mind: #88

It still needs more work though (to add tests, documentation, and generic programming support for user-defined types)

@lthms

This comment has been minimized.

Show comment
Hide comment
@lthms

lthms Jul 21, 2017

Thanks a lot @Gabriel439!

I need some time to process that, as I am not very familiar with the dhall package interface. Is the Dhall.Tutorial up-to-date with the latest changes you’ve made?

lthms commented Jul 21, 2017

Thanks a lot @Gabriel439!

I need some time to process that, as I am not very familiar with the dhall package interface. Is the Dhall.Tutorial up-to-date with the latest changes you’ve made?

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 21, 2017

Collaborator

The tutorial doesn't explain how to use this yet, but here's an example:

>>> :set -XOverloadedStrings
>>> f <- input auto "λ(x : Text) → λ(y : Text) → x ++ y" :: IO (Text -> Text -> Text)
>>> f "ABC" "DEF"
"ABCDEF"
Collaborator

Gabriel439 commented Jul 21, 2017

The tutorial doesn't explain how to use this yet, but here's an example:

>>> :set -XOverloadedStrings
>>> f <- input auto "λ(x : Text) → λ(y : Text) → x ++ y" :: IO (Text -> Text -> Text)
>>> f "ABC" "DEF"
"ABCDEF"
@lthms

This comment has been minimized.

Show comment
Hide comment
@lthms

lthms Jul 21, 2017

This is awesome! Thank you very much, that is exactly what I need.

lthms commented Jul 21, 2017

This is awesome! Thank you very much, that is exactly what I need.

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 22, 2017

Collaborator

Fixed by #88

Collaborator

Gabriel439 commented Jul 22, 2017

Fixed by #88

@Gabriel439 Gabriel439 closed this Jul 22, 2017

@lthms

This comment has been minimized.

Show comment
Hide comment
@lthms

lthms Jul 23, 2017

Thanks for the impressive reactivity. Do you plan to release a new version any time soon?

lthms commented Jul 23, 2017

Thanks for the impressive reactivity. Do you plan to release a new version any time soon?

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 23, 2017

Collaborator

Yes. I will probably publish a new version within about a week

Collaborator

Gabriel439 commented Jul 23, 2017

Yes. I will probably publish a new version within about a week

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Jul 28, 2017

Collaborator

This is now up on Hackage as dhall-1.5.0

Collaborator

Gabriel439 commented Jul 28, 2017

This is now up on Hackage as dhall-1.5.0

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