Skip to content

Support new JSON format #42

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 28 commits into from
Apr 15, 2018
Merged

Support new JSON format #42

merged 28 commits into from
Apr 15, 2018

Conversation

paulyoung
Copy link
Owner

@paulyoung paulyoung commented Feb 15, 2018

Should close #41.

This is a port of FromJSON.hs. I still need to port TestCoreFn.hs.

-- instance functorCaseAlternative :: Functor CaseAlternative where
-- map f (CaseAlternative cabs car) = CaseAlternative
-- (map (map f) cabs)
-- (either (Left <<< map (map f *** map f)) (Right <<< map f) car)
Copy link
Owner Author

Choose a reason for hiding this comment

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

I still need to figure out how to satisfy the Functor instances that this depends on.

Copy link

@gabejohnson gabejohnson Feb 15, 2018

Choose a reason for hiding this comment

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

While that either call is concise, I find it difficult to parse. A case expression might be better if you plan on having less experienced contributors in the future.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I don’t disagree but I tried to keep this port as close as possible to the Haskell version so that nothing was accidentally lost in translation.

https://github.com/purescript/purescript/blob/f999179792ae2c49b2ad3019959081540da0ad9f/src/Language/PureScript/CoreFn/Expr.hs#L89-L93

Choose a reason for hiding this comment

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

Got it. That explains the style. Thanks for the explanation and working on the port. It'll make adding backends easier in the future.

@paulyoung
Copy link
Owner Author

The tests include the JSON output from running stack test --test-arguments="-p corefn" on a modified TestCoreFn.hs, with the following diff applied:

diff --git a/tests/TestCoreFn.hs b/tests/TestCoreFn.hs
index 3f6972b1..5b0f0a79 100644
--- a/tests/TestCoreFn.hs
+++ b/tests/TestCoreFn.hs
@@ -12,6 +12,9 @@ import Data.Aeson
 import Data.Aeson.Types
 import Data.Version

+import Data.ByteString.Lazy.Char8
+import Debug.Trace
+
 import Language.PureScript.AST.Literals
 import Language.PureScript.AST.SourcePos
 import Language.PureScript.Comments
@@ -34,7 +37,8 @@ parseModule = parse moduleFromJSON
 parseMod :: Module Ann -> Result (Module Ann)
 parseMod m =
   let v = Version [0] []
-  in snd <$> parseModule (moduleToJSON v m)
+      j = moduleToJSON v m
+  in snd <$> parseModule (trace (unpack (encode j)) j)

 isSuccess :: Result a -> Bool
 isSuccess (Success _) = True

-- map f (CaseAlternative { caseAlternativeBinders, caseAlternativeResult }) = CaseAlternative
-- { caseAlternativeBinders: (map (map f) caseAlternativeBinders)
-- , caseAlternativeResult: (either (Left <<< map (map f *** map f)) (Right <<< map f) caseAlternativeResult)
-- }
Copy link
Owner Author

Choose a reason for hiding this comment

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

I think the last thing to do in this PR is to figure out why the Functor instances that are commented out don't work.

They're not actually used anywhere but it might be nice to have them.

This was taken from https://github.com/purescript/purescript/blob/c19a79285c5697b59c46803483b41ffeaa5cd7b0/src/Language/PureScript/CoreFn/Expr.hs#L89-L93

Choose a reason for hiding this comment

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

I don't see a Functor instance for Expr.

Copy link
Owner Author

Choose a reason for hiding this comment

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

The other instances are here:

-- derive instance functorExpr :: Functor Expr

-- derive instance functorBind :: Functor Bind

Copy link
Owner Author

Choose a reason for hiding this comment

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

This is the error:

[1/1 TypesDoNotUnify] src/CoreFn/Expr.purs:117:8
  117  derive instance functorBind :: Functor Bind
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  
  Could not match type
  
    b1
  
  with type
  
    a2
  
  while trying to match type Expr b1
    with type Expr t0
  while checking that expression (map (map $f146)) $v148
    has type Tuple (Tuple t0 Ident) (Expr t0)
  in value declaration functorBind
  
  where b1 is a rigid type variable
        a2 is a rigid type variable
        t0 is an unknown type

https://travis-ci.org/paulyoung/purescript-corefn/jobs/345111990#L1549

I guess the issue might just be that Functor can’t be derived, all though I’m not sure if there would be a more specific error if that was the case.

@paulyoung paulyoung changed the title [WIP] Support new JSON format Support new JSON format Feb 22, 2018

derive instance eqBind :: Eq a => Eq (Bind a)
derive instance ordBind :: Ord a => Ord (Bind a)

instance functorBindRec :: Functor Bind where
map f (NonRec a i e) = NonRec (f a) i (map f e)
map f (Rec ts) = Rec $ map (bimap (lmap f) (map f)) ts
Copy link
Owner Author

Choose a reason for hiding this comment

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

@LiamGoodacre does this look right to you?

FWIW, stack build --ghc-options "-ddump-deriv" produced .stack-work/dist/x86_64-osx/Cabal-2.0.0.2/build/src/Language/PureScript/CoreFn/Expr.dump-deriv which contained this:

instance GHC.Base.Functor
             Language.PureScript.CoreFn.Expr.Bind where
  GHC.Base.fmap
    f_amsj
    (Language.PureScript.CoreFn.Expr.NonRec a1_amsk a2_amsl a3_amsm)
    (Language.PureScript.CoreFn.Expr.NonRec a1_amsk a2_amsl a3_amsm)
    = Language.PureScript.CoreFn.Expr.NonRec
        (f_amsj a1_amsk)
        ((\ b1_amsn -> b1_amsn) a2_amsl)
        (GHC.Base.fmap f_amsj a3_amsm)
  GHC.Base.fmap f_amso (Language.PureScript.CoreFn.Expr.Rec a1_amsp)
    = Language.PureScript.CoreFn.Expr.Rec
        (GHC.Base.fmap
            (\ b3_amsq
              -> case b3_amsq of {
                    ((,) a1_amsr a2_amss)
                      -> (,)
                          ((\ b2_amst
                              -> case b2_amst of {
                                    ((,) a1_amsu a2_amsv)
                                      -> (,) (f_amso a1_amsu) ((\ b1_amsw -> b1_amsw) a2_amsv) })
                              a1_amsr)
                          (GHC.Base.fmap f_amso a2_amss) })
            a1_amsp)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Upon closer inspection I'm pretty confident that this is equivalent.

-- |
-- A generated name used only for type-checking
--
| UnusedIdent
Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm not sure whether to keep this or not. There's nothing that can be code-generated from it, but the case needs to be handled or unsafePartial used.

@paulyoung
Copy link
Owner Author

I'm seeing 2 issues with converting corefn.json files that are produced with pulp init.

I'm looking into whether this is an issue with my conversion or the original Haskell version.

@paulyoung
Copy link
Owner Author

Actually, everything is fine. Some modules in the output directory were old and using the old format.

>>= readArray
>>= traverse identFromJSON

moduleDecls <- pure []
Copy link
Owner Author

Choose a reason for hiding this comment

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

Whoops

type_ <- readProp "metaType" json >>= readString
case type_ of
"IsConstructor" -> isConstructorFromJSON json
"IsNewType" -> pure IsNewtype
Copy link
Owner Author

Choose a reason for hiding this comment

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

The test failing here might be because of the casing. Probably needs to be "IsNewtype".

@paulyoung paulyoung merged commit bec59ca into master Apr 15, 2018
@paulyoung paulyoung deleted the paulyoung/new-format branch April 15, 2018 22:15
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

Successfully merging this pull request may close these issues.

Support new corefn.json format
2 participants