Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign upInterpret instance for unnamed data fields #103
Conversation
Gabriel439
reviewed
Aug 22, 2017
|
I think this is a great idea! If we do this then we can eliminate the need for |
| genericAutoWith options@(InterpretOptions {..}) = Type {..} | ||
| genericAutoWith options@(InterpretOptions {..}) = do | ||
| Type extractL (Union expectedL) <- genericAutoWith options | ||
| Type extractR expectedR <- genericAutoWith options |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Aug 22, 2017
Collaborator
Shouldn't this line be:
let Type extractR expectedR = evalState (genericAutoWith options) 1| instance (Constructor c, GenericInterpret f, GenericInterpret (g :+: h)) => GenericInterpret (M1 C c f :+: (g :+: h)) where | ||
| genericAutoWith options@(InterpretOptions {..}) = Type {..} | ||
| genericAutoWith options@(InterpretOptions {..}) = do | ||
| Type extractL expectedL <- genericAutoWith options |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Aug 22, 2017
Collaborator
Same comment here. Shouldn't this be:
let Type extractL expectedL = evalState (genericAutoWith options) 1| @@ -505,20 +506,34 @@ defaultInterpretOptions = InterpretOptions | |||
| for automatically deriving a generic implementation | |||
| -} | |||
| class GenericInterpret f where | |||
| genericAutoWith :: InterpretOptions -> Type (f a) | |||
| genericAutoWith :: InterpretOptions -> State Int (Type (f a)) | |||
This comment has been minimized.
This comment has been minimized.
Gabriel439
Aug 22, 2017
Collaborator
Since we're turning genericAutoWith into a monadic function, we can also automatically thread the InterpretOptions through the code by changing the type to:
genericAutoWith :: ReaderT InterpretOptions (State Int (Type (f a)))... although it might be slightly easier to work with this type:
genericAutoWith :: StateT Int (Reader InterpretOptions (Type (fa)))
This comment has been minimized.
This comment has been minimized.
bosu
Aug 22, 2017
Author
Collaborator
The amended commit much simplified the diff. IMHO Reader may be unnecessary anymore...
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE TypeApplications #-} |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Aug 22, 2017
Collaborator
Note that TypeApplications only works for GHC 8.0 and later, but the Travis test suite still supports GHC 7.8 (in spirit; the test is currently failing due to cabal solver issues)
However, I'd be willing to drop GHC 7.8 support because even Debian stable now supports GHC 8.0
This comment has been minimized.
This comment has been minimized.
| put (i + 1) | ||
| pure ("_" ++ show i) | ||
| nn -> pure nn | ||
| pure (Type { expected = expected_name name, extract = extract_name name }) |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Aug 22, 2017
Collaborator
Minor suggestion: you can simplify this a little bit by relocating expected and extract inside the do block, like this:
name <- case ...
let extract (RecordLit m) = ...
let expected = ...
pure (Type {..})| unnamedFields :: TestTree | ||
| unnamedFields = Test.Tasty.HUnit.testCase "Unnamed Fields" (do | ||
| let ty = Dhall.auto @Foo | ||
| Test.HUnit.assertEqual "Good type" (Dhall.expected ty) (Dhall.Core.Union ( |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Aug 22, 2017
Collaborator
I believe Test.Tasty.Hunit also provides assertEqual, so you don't need to pull in the additional HUnit dependency
(To be honest, I'm not even sure what's the difference between the two libraries. I didn't originally author the test suite)
| @@ -144,6 +144,8 @@ Test-Suite test | |||
| Util | |||
| Build-Depends: | |||
| base >= 4 && < 5 , | |||
| containers >= 0.5.7.1 && < 0.6 , | |||
This comment has been minimized.
This comment has been minimized.
Gabriel439
Aug 22, 2017
Collaborator
You can lower the lower bound on containers to >= 0.5.0.0 (consistent with the containers dependency for the main library) since that version still has Data.Map.fromList
bosu
force-pushed the
bosu:unnamed-fields
branch
from
bf0d29c
to
018ac7c
Aug 22, 2017
This comment has been minimized.
This comment has been minimized.
|
Sorry for losing the remarks by using amended push request. All of them were addressed. Now I see the advantages of additional commits :) Regarding R2 removal: it is still needed by Inject instance... Probably the same kind of refactoring can happen there. |
Gabriel439
merged commit b581007
into
dhall-lang:master
Aug 22, 2017
1 check failed
This comment has been minimized.
This comment has been minimized.
|
Thanks! :) |
bosu commentedAug 21, 2017
Maps unnamed data fields to the positional record keys. For example:
data Foo = Foo Bool Integer Natural deriving (Generic, Interpret)is mapped to:
Record { _1 : Bool, _2 : Integer, _3 : Natural }Dhall data type.