Skip to content

Commit 9abd345

Browse files
authored
Refactor internals of promoteMethod (#407)
`promoteMethod` is unnecessarily complicated. It does a lot of work to construct a type family defintion and defunctionalization symbols for the method's default implementation. However, these definitions were actually computed earlier in the call to `promoteLetDecRHS`, but `promoteMethod` throws them away and recomputes them anyway! Instead, we can simply reuse the results of `promoteLetDecRHS`, which this patch accomplishes. This also allows the return type of `promoteLetDecRHS` to be simplified slightly, as we no longer need to return a nested tuple of assorted things. Instead, we now have a flat tuple of assorted things :) This commit is purely an internal refactor—no user-facing changes here.
1 parent ee19ecc commit 9abd345

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

Diff for: src/Data/Singletons/Promote.hs

+11-22
Original file line numberDiff line numberDiff line change
@@ -447,18 +447,10 @@ promoteMethod inst_sigs_map m_subst orig_sigs_map (meth_name, meth_rhs) = do
447447
-- strictly necessary, as kind inference can figure them out just as well.
448448
family_args = map DVarT meth_arg_tvs
449449
helperName <- newUniqueName helperNameBase
450-
let proHelperName = promoteValNameLhs helperName
451-
((_, _, _, eqns), _defuns, ann_rhs)
450+
(pro_dec, defun_decs, ann_rhs)
452451
<- promoteLetDecRHS (Just (meth_arg_kis, meth_res_ki)) OMap.empty OMap.empty
453452
noPrefix helperName meth_rhs
454-
let tvbs = zipWith DKindedTV meth_arg_tvs meth_arg_kis
455-
emitDecs [DClosedTypeFamilyD (DTypeFamilyHead
456-
proHelperName
457-
tvbs
458-
(DKindSig meth_res_ki)
459-
Nothing)
460-
eqns]
461-
emitDecsM (defunctionalize proHelperName Nothing tvbs (Just meth_res_ki))
453+
emitDecs (pro_dec:defun_decs)
462454
return ( DTySynInstD
463455
(DTySynEqn Nothing
464456
(foldType (DConT proName) family_args)
@@ -544,12 +536,12 @@ promoteLetDecEnv prefixes (LetDecEnv { lde_defns = value_env
544536

545537
-- promote all the declarations, producing annotated declarations
546538
let (names, rhss) = unzip $ OMap.assocs value_env
547-
(payloads, defun_decss, ann_rhss)
539+
(pro_decs, defun_decss, ann_rhss)
548540
<- fmap unzip3 $ zipWithM (promoteLetDecRHS Nothing type_env fix_env prefixes) names rhss
549541

550542
emitDecs $ concat defun_decss
551543
bound_kvs <- allBoundKindVars
552-
let decs = map payload_to_dec payloads ++ infix_decls
544+
let decs = pro_decs ++ infix_decls
553545

554546
-- build the ALetDecEnv
555547
let let_dec_env' = LetDecEnv { lde_defns = OMap.fromList $ zip names ann_rhss
@@ -559,12 +551,6 @@ promoteLetDecEnv prefixes (LetDecEnv { lde_defns = value_env
559551
, lde_bound_kvs = OMap.fromList $ map (, bound_kvs) names }
560552

561553
return (decs, let_dec_env')
562-
where
563-
payload_to_dec (name, tvbs, m_ki, eqns) = DClosedTypeFamilyD
564-
(DTypeFamilyHead name tvbs sig Nothing)
565-
eqns
566-
where
567-
sig = maybe DNoSig DKindSig m_ki
568554

569555
promoteInfixDecl :: Name -> Fixity -> Maybe DDec
570556
promoteInfixDecl name fixity
@@ -590,7 +576,7 @@ promoteLetDecRHS :: Maybe ([DKind], DKind) -- the promoted type of the RHS (if
590576
-> (String, String) -- let-binding prefixes
591577
-> Name -- name of the thing being promoted
592578
-> ULetDecRHS -- body of the thing
593-
-> PrM ( (Name, [DTyVarBndr], Maybe DKind, [DTySynEqn]) -- "type family"
579+
-> PrM ( DDec -- "type family"
594580
, [DDec] -- defunctionalization
595581
, ALetDecRHS ) -- annotated RHS
596582
promoteLetDecRHS m_rhs_ki type_env fix_env prefixes name (UValue exp) = do
@@ -612,8 +598,9 @@ promoteLetDecRHS m_rhs_ki type_env fix_env prefixes name (UValue exp) = do
612598
m_fixity = OMap.lookup name fix_env
613599
tvbs = map DPlainTV all_locals
614600
defuns <- defunctionalize proName m_fixity tvbs res_kind
615-
return ( ( proName, tvbs, res_kind
616-
, [DTySynEqn Nothing (foldType (DConT proName) $ map DVarT all_locals) exp'] )
601+
return ( DClosedTypeFamilyD
602+
(DTypeFamilyHead proName tvbs (maybeKindToResultSig res_kind) Nothing)
603+
[DTySynEqn Nothing (foldType (DConT proName) $ map DVarT all_locals) exp']
617604
, defuns
618605
, AValue (foldType (DConT proName) (map DVarT all_locals))
619606
num_arrows ann_exp )
@@ -652,7 +639,9 @@ promoteLetDecRHS m_rhs_ki type_env fix_env prefixes name (UFunction clauses) = d
652639
(eqns, ann_clauses) <- forallBind lde_kvs_to_bind $
653640
mapAndUnzipM (promoteClause proName) expClauses
654641
prom_fun <- lookupVarE name
655-
return ( (proName, all_args, m_resK, eqns)
642+
return ( DClosedTypeFamilyD
643+
(DTypeFamilyHead proName all_args (maybeKindToResultSig m_resK) Nothing)
644+
eqns
656645
, defun_decs
657646
, AFunction prom_fun ty_num_args ann_clauses )
658647

Diff for: src/Data/Singletons/Util.hs

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ resultSigToMaybeKind (DKindSig k) = Just k
249249
resultSigToMaybeKind (DTyVarSig (DPlainTV _)) = Nothing
250250
resultSigToMaybeKind (DTyVarSig (DKindedTV _ k)) = Just k
251251

252+
maybeKindToResultSig :: Maybe DKind -> DFamilyResultSig
253+
maybeKindToResultSig = maybe DNoSig DKindSig
254+
252255
-- Reconstruct arrow kind from the list of kinds
253256
ravel :: [DType] -> DType -> DType
254257
ravel [] res = res

0 commit comments

Comments
 (0)