Skip to content

Construct record datacons in tactics #1356

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 4 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/CodeGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ buildDataCon
-> DataCon -- ^ The data con to build
-> [Type] -- ^ Type arguments for the data con
-> RuleM (Trace, LHsExpr GhcPs)
buildDataCon jdg dc apps = do
let args = dataConInstOrigArgTys' dc apps
buildDataCon jdg dc tyapps = do
let args = dataConInstOrigArgTys' dc tyapps
(tr, sgs)
<- fmap unzipTrace
$ traverse ( \(arg, n) ->
Expand Down
19 changes: 12 additions & 7 deletions plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/CodeGen/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

module Ide.Plugin.Tactic.CodeGen.Utils where

import Data.List
import DataCon
import Development.IDE.GHC.Compat
import GHC.Exts
import GHC.SourceGen (RdrNameStr)
import GHC.SourceGen.Overloaded
import Name
import Data.List
import DataCon
import Development.IDE.GHC.Compat
import GHC.Exts
import GHC.SourceGen (recordConE, RdrNameStr)
import GHC.SourceGen.Overloaded
import Ide.Plugin.Tactic.GHC (getRecordFields)
import Name


------------------------------------------------------------------------------
Expand All @@ -20,6 +21,10 @@ mkCon dcon (fmap unLoc -> args)
| dataConIsInfix dcon
, (lhs : rhs : args') <- args =
noLoc $ foldl' (@@) (op lhs (coerceName dcon_name) rhs) args'
| Just fields <- getRecordFields dcon =
noLoc $ recordConE (coerceName dcon_name) $ do
(arg, (field, _)) <- zip args fields
pure (coerceName field, arg)
| otherwise =
noLoc $ foldl' (@@) (bvar' $ occName dcon_name) args
where
Expand Down
14 changes: 13 additions & 1 deletion plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Control.Monad.State
import qualified Data.Map as M
import Data.Maybe (isJust)
import Data.Traversable
import qualified DataCon as DataCon
import DataCon
import Development.IDE.GHC.Compat
import Generics.SYB (mkT, everywhere)
import Ide.Plugin.Tactic.Types
Expand Down Expand Up @@ -88,6 +88,18 @@ freshTyvars t = do
) t


------------------------------------------------------------------------------
-- | Given a datacon, extract its record fields' names and types. Returns
-- nothing if the datacon is not a record.
getRecordFields :: DataCon -> Maybe [(OccName, CType)]
getRecordFields dc =
case dataConFieldLabels dc of
[] -> Nothing
lbls -> for lbls $ \lbl -> do
(_, ty) <- dataConFieldType_maybe dc $ flLabel lbl
pure (mkVarOccFS $ flLabel lbl, CType ty)


------------------------------------------------------------------------------
-- | Is this an algebraic type?
algebraicTyCon :: Type -> Maybe TyCon
Expand Down
1 change: 1 addition & 0 deletions test/functional/Tactic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ tests = testGroup
, expectFail "GoldenFish.hs" 5 18 Auto ""
, goldenTest "GoldenArbitrary.hs" 25 13 Auto ""
, goldenTest "FmapBoth.hs" 2 12 Auto ""
, goldenTest "RecordCon.hs" 7 8 Auto ""
, goldenTest "FmapJoin.hs" 2 14 Auto ""
, goldenTest "Fgmap.hs" 2 9 Auto ""
, goldenTest "FmapJoinInLet.hs" 4 19 Auto ""
Expand Down
9 changes: 9 additions & 0 deletions test/testdata/tactic/RecordCon.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data MyRecord a = Record
{ field1 :: a
, field2 :: Int
}

blah :: (a -> Int) -> a -> MyRecord a
blah = _


9 changes: 9 additions & 0 deletions test/testdata/tactic/RecordCon.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data MyRecord a = Record
{ field1 :: a
, field2 :: Int
}

blah :: (a -> Int) -> a -> MyRecord a
blah = (\ fai a -> Record {field1 = a, field2 = fai a})