Skip to content

[#1392] Injecting Data.Map #1412

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

Merged
merged 20 commits into from
Oct 15, 2019
Merged

[#1392] Injecting Data.Map #1412

merged 20 commits into from
Oct 15, 2019

Conversation

jiegillet
Copy link
Collaborator

@jiegillet jiegillet commented Oct 10, 2019

Fixes #1392

To do:

  • instance Inject (Data.Map.Map k v)
  • Clean up comments and extra empty lines
  • Add quicktest tests (Haskell => Dhall => Haskell == id)
  • Data.Map Text v embedded as record via newtype RecordMap

Question: I made two implementations (in my first two commits), one using Generic, and an explicit one. Is there a difference in efficiency? The first looks cooler to me.

@sjakobi
Copy link
Collaborator

sjakobi commented Oct 10, 2019

Question: I made two implementations (in my first two commits), one using Generic, and an explicit one. Is there a difference in efficiency? The first looks cooler to me.

You can benchmark this if you want. I'm not sure. I believe the current version is easier to understand and should compile faster.

Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with the update to the submodule?

declaredOut = App List $ Record $ Dhall.Map.fromList
[("mapKey", declaredK), ("mapValue", declaredV)]

mapEntries = fmap recordPair . Data.Sequence.fromList . Data.Map.toList
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a hunch that this would be more efficient, but no proof… :/

Suggested change
mapEntries = fmap recordPair . Data.Sequence.fromList . Data.Map.toList
mapEntries = Data.Sequence.fromList . map recordPair . Data.Map.toList

| Data.Map.null m = Just declaredOut
| otherwise = Nothing

declaredOut = App List $ Record $ Dhall.Map.fromList
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far the code base mostly uses parentheses instead of $. @Gabriel439 can probably explain why.

I think we should stick with the convention unless there's a good reason not to.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It comes from this: http://www.haskellforall.com/2015/09/how-to-make-your-haskell-code-more.html

... although I usually only apply that to my own coding style. I usually do not review other people's code for style that much

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting read, thank you!

@jiegillet
Copy link
Collaborator Author

I don't know what's up with the submodule either, I've never used one before, it keeps sneaking in and it doesn't really show in git diff... I'll get rid of it somehow.

I posted in the issue, but do you know what I can do about the RecordMap instance?

@sjakobi
Copy link
Collaborator

sjakobi commented Oct 11, 2019

I don't know what's up with the submodule either, I've never used one before, it keeps sneaking in and it doesn't really show in git diff... I'll get rid of it somehow.

This should work:

$ cd dhall/dhall-lang
$ git checkout cb569fc
$ cd ../..
$ git add dhall/dhall-lang
$ git commit

I posted in the issue, but do you know what I can do about the RecordMap instance?

I've responded in the issue.

@jiegillet
Copy link
Collaborator Author

jiegillet commented Oct 12, 2019

I've added a property test. There are so many tests in the project, I'm not sure where I should have placed mine, let me know if I should move it.
The property is general enough to test any (Inject a, Interpret a).

By the way, while I was playing with it, I saw the property fail for Scientific (due to some rounding errors, I suppose that's to be expected) and Text (I think due to non-printable characters, probably because I use show in the test, let me know if there is a better way).

@@ -432,6 +439,12 @@ normalizingAnExpressionDoesntChangeItsInferredType expression =
filterOutEmbeds :: Typer a
filterOutEmbeds _ = Const Sort -- This could be any ill-typed expression.

injectThenInterpretIsIdentity :: (Inject a, Interpret a, Eq a) => a -> Property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add a Typeable constraint to this then you can have this generate not only the Property, but the description, too:

injectThenInterpretIsIdentity a =
    ( "Injecting then Interpreting is identity for " <> show (typeOf a)
    , property
    , QuickCheckTests 100
    )
  where
    property = monadicIO $ do 

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, tasty :) (no pun intended)

@@ -1860,6 +1857,34 @@ instance Inject a => Inject (Data.HashSet.HashSet a) where

instance (Inject a, Inject b) => Inject (a, b)

{-| Inject a `Data.Map` to a @Prelude.Map.Type@

>>> prettyExpr $ embed inject (Data.Map.fromList [(1 :: Integer, True )])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually for examples I try to use Natural instead of Integer (to encourage more people to use Natural)

@jiegillet
Copy link
Collaborator Author

jiegillet commented Oct 13, 2019

Error in the automated tests:

C:\projects\dhall-haskell\dhall\tests\Dhall\Test\QuickCheck.hs:8:14:
    Unsupported extension: TypeApplications

Suggestions? :(

More:

Cabal-simple_Z6RU0evB_1.22.5.0_ghc-7.10.3.exe: The package dhall-1.26.1
    requires the following language extensions which are not supported by
    ghc-7.10.3: TypeApplications

Should I just not use TypeApplications?

Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Very nice tests!

@@ -56,6 +66,7 @@ import qualified Dhall.Map
import qualified Dhall.Set
import qualified Dhall.TypeCheck
import qualified Generic.Random
import qualified GHC.Natural as GHCNat
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs for that module say:

Note: This is an internal GHC module with an API subject to change. It's recommended use the Numeric.Natural module to import the Natural type.

Suggested change
import qualified GHC.Natural as GHCNat
import qualified Numeric.Natural

)
where
prop a = monadicIO $ do
a' <- run . input auto . Text.pack . show . prettyExpr . embed inject $ a
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you pretty-print and re-parse the embedded expressions?

I you'd simply extract the embedded Expr, we wouldn't need IO

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man... I feel I'm doing so many obvious mistakes. Thank you!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! :)

jiegillet and others added 2 commits October 15, 2019 07:31
Co-Authored-By: Simon Jakobi <[email protected]>
Co-Authored-By: Simon Jakobi <[email protected]>
This was referenced Oct 15, 2019
@mergify mergify bot merged commit 1cd856a into master Oct 15, 2019
@mergify mergify bot deleted the jie-map branch October 15, 2019 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Injecting Data.Map
3 participants