Skip to content

Commit 9ed664c

Browse files
committed
Define an unchecked variant of caseAltsBinderTys to use in Builder.
1 parent 6ea43db commit 9ed664c

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/lib/Builder.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import qualified Unsafe.Coerce as TrulyUnsafe
6666
import qualified RawName as R
6767
import Name
6868
import Syntax
69-
import CheckType (caseAltsBinderTys)
7069
import QueryType
7170
import PPrint (prettyBlock)
7271
import CheapReduction

src/lib/CheckType.hs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module CheckType (
88
CheckableE (..), CheckableB (..),
99
checkTypes, checkTypesM,
1010
checkExtends, checkedApplyDataDefParams, checkedApplyClassParams,
11-
caseAltsBinderTys, tryGetType,
11+
tryGetType,
1212
checkUnOp, checkBinOp,
1313
isData, asFirstOrderFunction, asFFIFunType,
1414
isSingletonType, singletonTypeVal, asNaryPiType,
@@ -836,14 +836,14 @@ checkCase scrut alts resultTy effs = do
836836
declareEffs =<< substM effs
837837
resultTy' <- substM resultTy
838838
scrutTy <- getTypeE scrut
839-
altsBinderTys <- caseAltsBinderTys scrutTy
839+
altsBinderTys <- checkCaseAltsBinderTys scrutTy
840840
forMZipped_ alts altsBinderTys \alt bs ->
841841
checkAlt resultTy' bs alt
842842
return resultTy'
843843

844-
caseAltsBinderTys :: (Fallible1 m, EnvReader m)
844+
checkCaseAltsBinderTys :: (Fallible1 m, EnvReader m)
845845
=> Type n -> m n [EmptyAbs (Nest Binder) n]
846-
caseAltsBinderTys ty = case ty of
846+
checkCaseAltsBinderTys ty = case ty of
847847
TypeCon _ defName params -> do
848848
def <- lookupDataDef defName
849849
cons <- checkedApplyDataDefParams def params
@@ -870,11 +870,6 @@ checkDataConRefEnv (EmptyAbs (Nest b restBs)) (EmptyAbs (Nest refBinding restRef
870870
checkDataConRefEnv restBs' (EmptyAbs restRefs)
871871
checkDataConRefEnv _ _ = throw CompilerErr $ "Mismatched args and binders"
872872

873-
typeAsBinderNest :: ScopeReader m => Type n -> m n (Abs (Nest Binder) UnitE n)
874-
typeAsBinderNest ty = do
875-
Abs ignored body <- toConstAbs UnitE
876-
return $ Abs (Nest (ignored:>ty) Empty) body
877-
878873
checkAlt :: HasType body => Typer m
879874
=> Type o -> EmptyAbs (Nest Binder) o -> AltP body i -> m i o ()
880875
checkAlt resultTyReq reqBs (Abs bs body) = do

src/lib/QueryType.hs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ module QueryType (
22
getType, getTypeSubst, HasType,
33
getEffects, getEffectsSubst,
44
computeAbsEffects, declNestEffects,
5-
depPairLeftTy, extendEffect,
5+
caseAltsBinderTys, depPairLeftTy, extendEffect,
66
getAppType, getTabAppType, getMethodType, getBaseMonoidType, getReferentTy,
77
getMethodIndex,
88
instantiateDataDef, instantiateDepPairTy, instantiatePi, instantiateTabPi,
99
litType, lamExprTy,
1010
numNaryPiArgs, naryLamExprType,
11-
oneEffect, projectLength, sourceNameType,
11+
oneEffect, projectLength, sourceNameType, typeAsBinderNest
1212
) where
1313

1414
import Control.Monad
@@ -55,6 +55,19 @@ getEffectsSubst e = do
5555

5656
-- === Exposed helpers for querying types and effects ===
5757

58+
caseAltsBinderTys :: (Fallible1 m, EnvReader m)
59+
=> Type n -> m n [EmptyAbs (Nest Binder) n]
60+
caseAltsBinderTys ty = case ty of
61+
TypeCon _ defName params -> do
62+
def <- lookupDataDef defName
63+
cons <- applyDataDefParams def params
64+
return [bs | DataConDef _ bs <- cons]
65+
VariantTy (NoExt types) -> do
66+
mapM typeAsBinderNest $ toList types
67+
VariantTy _ -> fail "Can't pattern-match partially-known variants"
68+
SumTy cases -> mapM typeAsBinderNest cases
69+
_ -> fail $ "Case analysis only supported on ADTs and variants, not on " ++ pprint ty
70+
5871
depPairLeftTy :: DepPairType n -> Type n
5972
depPairLeftTy (DepPairType (_:>ty) _) = ty
6073
{-# INLINE depPairLeftTy #-}
@@ -159,6 +172,9 @@ naryLamExprType (NaryLamExpr (NonEmptyNest b bs) eff body) = liftTypeQueryM idSu
159172
binderToPiBinder :: Binder n l -> PiBinder n l
160173
binderToPiBinder (nameBinder:>ty) = PiBinder nameBinder ty PlainArrow
161174

175+
oneEffect :: Effect n -> EffectRow n
176+
oneEffect eff = EffectRow (S.singleton eff) Nothing
177+
162178
projectLength :: (Fallible1 m, EnvReader m) => Type n -> m n Int
163179
projectLength ty = case ty of
164180
TypeCon _ defName params -> do
@@ -181,8 +197,11 @@ sourceNameType v = do
181197
UClassVar v' -> lookupEnv v' >>= \case ClassBinding def -> return $ getClassTy def
182198
UMethodVar v' -> lookupEnv v' >>= \case MethodBinding _ _ e -> getType e
183199

184-
oneEffect :: Effect n -> EffectRow n
185-
oneEffect eff = EffectRow (S.singleton eff) Nothing
200+
typeAsBinderNest :: ScopeReader m => Type n -> m n (Abs (Nest Binder) UnitE n)
201+
typeAsBinderNest ty = do
202+
Abs ignored body <- toConstAbs UnitE
203+
return $ Abs (Nest (ignored:>ty) Empty) body
204+
{-# INLINE typeAsBinderNest #-}
186205

187206
-- === computing effects ===
188207

0 commit comments

Comments
 (0)