Skip to content

Commit 65bc6a7

Browse files
authored
Add HSE.SrcSpanInfo to UTerm (#42)
1 parent c171ee7 commit 65bc6a7

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/Hell.hs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,21 @@ lookp (SVar v) (env, _) = lookp v env
281281
-- type-checked. The HSE AST is desugared into this one.
282282

283283
data UTerm t
284-
= UVar t String
285-
| ULam t Binding (Maybe SomeStarType) (UTerm t)
286-
| UApp t (UTerm t) (UTerm t)
284+
= UVar HSE.SrcSpanInfo t String
285+
| ULam HSE.SrcSpanInfo t Binding (Maybe SomeStarType) (UTerm t)
286+
| UApp HSE.SrcSpanInfo t (UTerm t) (UTerm t)
287287

288288
-- IRep below: The variables are poly types, they aren't metavars,
289289
-- and need to be instantiated.
290-
| UForall t [SomeTypeRep] Forall [TH.Uniq] (IRep TH.Uniq) [t]
290+
| UForall HSE.SrcSpanInfo t [SomeTypeRep] Forall [TH.Uniq] (IRep TH.Uniq) [t]
291291
deriving (Traversable, Functor, Foldable)
292292

293293
typeOf :: UTerm t -> t
294294
typeOf = \case
295-
UVar t _ -> t
296-
ULam t _ _ _ -> t
297-
UApp t _ _ -> t
298-
UForall t _ _ _ _ _ -> t
295+
UVar _ t _ -> t
296+
ULam _ t _ _ _ -> t
297+
UApp _ t _ _ -> t
298+
UForall _ t _ _ _ _ _ -> t
299299

300300
data Binding = Singleton String | Tuple [String]
301301

@@ -326,7 +326,7 @@ data Forall where
326326
Final :: (forall g. Typed (Term g)) -> Forall
327327

328328
lit :: Type.Typeable a => a -> UTerm ()
329-
lit l = UForall () [] (Final (Typed (Type.typeOf l) (Lit l))) [] (fromSomeStarType (SomeStarType (Type.typeOf l))) []
329+
lit l = UForall HSE.noSrcSpan () [] (Final (Typed (Type.typeOf l) (Lit l))) [] (fromSomeStarType (SomeStarType (Type.typeOf l))) []
330330

331331
data SomeStarType = forall (a :: Type). SomeStarType (TypeRep a)
332332
deriving instance Show SomeStarType
@@ -371,10 +371,10 @@ check = tc
371371

372372
-- Type check a term given an environment of names.
373373
tc :: (UTerm SomeTypeRep) -> TyEnv g -> Either TypeCheckError (Typed (Term g))
374-
tc (UVar _ v) env = do
374+
tc (UVar _ _ v) env = do
375375
Typed ty v' <- lookupVar v env
376376
pure $ Typed ty (Var v')
377-
tc (ULam (StarTypeRep lam_ty) s _ body) env =
377+
tc (ULam _ (StarTypeRep lam_ty) s _ body) env =
378378
case lam_ty of
379379
Type.Fun bndr_ty' _ |
380380
Just Type.HRefl <- Type.eqTypeRep (typeRepKind bndr_ty') (typeRep @Type) ->
@@ -387,9 +387,9 @@ tc (ULam (StarTypeRep lam_ty) s _ body) env =
387387
Just Type.HRefl -> Right $ Typed lam_ty (Lam body')
388388
Nothing -> Left InferredCheckedDisagreeBug
389389
_ -> Left LambdaIsNotAFunBug
390-
tc (ULam (SomeTypeRep{}) _ _ _) _ =
390+
tc (ULam _ (SomeTypeRep{}) _ _ _) _ =
391391
Left LambdaMustBeStarBug
392-
tc (UApp _ e1 e2) env =
392+
tc (UApp _ _ e1 e2) env =
393393
case tc e1 env of
394394
Left e -> Left e
395395
Right (Typed (Type.Fun bndr_ty body_ty) e1') ->
@@ -408,7 +408,7 @@ tc (UApp _ e1 e2) env =
408408
_ -> Left TypeCheckMismatch
409409
Right{} -> Left TypeOfApplicandIsNotFunction
410410
-- Polytyped terms, must be, syntactically, fully-saturated
411-
tc (UForall _ _ fall _ _ reps0) _env = go reps0 fall where
411+
tc (UForall _ _ _ fall _ _ reps0) _env = go reps0 fall where
412412
go :: [SomeTypeRep] -> Forall -> Either TypeCheckError (Typed (Term g))
413413
go [] (Final typed') = pure typed'
414414
go (StarTypeRep rep:reps) (NoClass f) = go reps (f rep)
@@ -523,15 +523,15 @@ desugarExp :: Map String (UTerm ()) -> HSE.Exp HSE.SrcSpanInfo ->
523523
desugarExp globals = go mempty where
524524
go scope = \case
525525
HSE.Paren _ x -> go scope x
526-
HSE.If _ i t e ->
527-
(\e' t' i' -> UApp () (UApp () (UApp () bool' e') t') i')
526+
HSE.If l i t e ->
527+
(\e' t' i' -> UApp l () (UApp l () (UApp l () (bool' l) e') t') i')
528528
<$> go scope e <*> go scope t <*> go scope i
529-
HSE.Tuple _ HSE.Boxed xs -> do
529+
HSE.Tuple l HSE.Boxed xs -> do
530530
xs' <- traverse (go scope) xs
531-
pure $ foldl (UApp ()) (tuple' (length xs)) xs'
532-
HSE.List _ xs -> do
531+
pure $ foldl (UApp l ()) (tuple' (length xs) l) xs'
532+
HSE.List l xs -> do
533533
xs' <- traverse (go scope) xs
534-
pure $ foldr (\x y -> UApp () (UApp () cons' x) y) nil' xs'
534+
pure $ foldr (\x y -> UApp l () (UApp l () (cons' l) x) y) (nil' l) xs'
535535
HSE.Lit _ lit' -> case lit' of
536536
HSE.Char _ char _ -> pure $ lit char
537537
HSE.String _ string _ -> pure $ lit $ Text.pack string
@@ -544,13 +544,13 @@ desugarExp globals = go mempty where
544544
desugarQName scope globals qname reps
545545
HSE.Var _ qname ->
546546
desugarQName scope globals qname []
547-
HSE.App _ f x -> UApp () <$> go scope f <*> go scope x
548-
HSE.InfixApp _ x (HSE.QVarOp l f) y -> UApp () <$> (UApp () <$> go scope (HSE.Var l f) <*> go scope x) <*> go scope y
549-
HSE.Lambda _ pats e -> do
547+
HSE.App l f x -> UApp l () <$> go scope f <*> go scope x
548+
HSE.InfixApp l x (HSE.QVarOp l'op f) y -> UApp l () <$> (UApp l'op () <$> go scope (HSE.Var l'op f) <*> go scope x) <*> go scope y
549+
HSE.Lambda l pats e -> do
550550
args <- traverse desugarArg pats
551551
let stringArgs = concatMap (bindingStrings . fst) args
552552
e' <- go (foldr Set.insert scope stringArgs) e
553-
pure $ foldr (\(name,ty) inner -> ULam () name ty inner) e' args
553+
pure $ foldr (\(name,ty) inner -> ULam l () name ty inner) e' args
554554
HSE.Con _ qname ->
555555
desugarQName scope globals qname []
556556
HSE.Do _ stmts -> do
@@ -587,7 +587,7 @@ bindingStrings (Tuple tups) = tups
587587
desugarQName :: Set String -> Map String (UTerm ()) -> HSE.QName HSE.SrcSpanInfo -> [SomeTypeRep] -> Either DesugarError (UTerm ())
588588
desugarQName scope globals qname [] =
589589
case qname of
590-
HSE.UnQual _ (HSE.Ident _ string) | Set.member string scope -> pure $ UVar () string
590+
HSE.UnQual _ (HSE.Ident l string) | Set.member string scope -> pure $ UVar l () string
591591
HSE.Qual _ (HSE.ModuleName _ "Main") (HSE.Ident _ string)
592592
| Just uterm <- Map.lookup string globals ->
593593
pure uterm
@@ -600,15 +600,15 @@ desugarQName scope globals qname [] =
600600
_ -> desugarPolyQName qname []
601601
desugarQName _ _ qname treps = desugarPolyQName qname treps
602602

603-
desugarPolyQName :: Show l => HSE.QName l -> [SomeTypeRep] -> Either DesugarError (UTerm ())
603+
desugarPolyQName :: HSE.QName HSE.SrcSpanInfo -> [SomeTypeRep] -> Either DesugarError (UTerm ())
604604
desugarPolyQName qname treps =
605605
case qname of
606-
HSE.Qual _ (HSE.ModuleName _ prefix) (HSE.Ident _ string)
606+
HSE.Qual l (HSE.ModuleName _ prefix) (HSE.Ident _ string)
607607
| Just (forall', vars, irep, _) <- Map.lookup (prefix ++ "." ++ string) polyLits -> do
608-
pure (UForall () treps forall' vars irep [])
609-
HSE.UnQual _ (HSE.Symbol _ string)
608+
pure (UForall l () treps forall' vars irep [])
609+
HSE.UnQual l (HSE.Symbol _ string)
610610
| Just (forall', vars, irep, _) <- Map.lookup string polyLits -> do
611-
pure (UForall () treps forall' vars irep [])
611+
pure (UForall l () treps forall' vars irep [])
612612
_ -> Left $ InvalidVariable $ HSE.prettyPrint qname
613613

614614
desugarArg :: HSE.Pat HSE.SrcSpanInfo -> Either DesugarError (Binding, Maybe SomeStarType)
@@ -1176,26 +1176,26 @@ polyLits = Map.fromList
11761176
--------------------------------------------------------------------------------
11771177
-- Internal-use only, used by the desugarer
11781178

1179-
cons' :: UTerm ()
1179+
cons' :: HSE.SrcSpanInfo -> UTerm ()
11801180
cons' = unsafeGetForall "List.cons"
11811181

1182-
nil' :: UTerm ()
1182+
nil' :: HSE.SrcSpanInfo -> UTerm ()
11831183
nil' = unsafeGetForall "List.nil"
11841184

1185-
bool' :: UTerm ()
1185+
bool' :: HSE.SrcSpanInfo -> UTerm ()
11861186
bool' = unsafeGetForall "Bool.bool"
11871187

1188-
tuple' :: Int -> UTerm ()
1188+
tuple' :: Int -> HSE.SrcSpanInfo -> UTerm ()
11891189
tuple' 0 = unsafeGetForall "Tuple.()"
11901190
tuple' 2 = unsafeGetForall "Tuple.(,)"
11911191
tuple' 3 = unsafeGetForall "Tuple.(,,)"
11921192
tuple' 4 = unsafeGetForall "Tuple.(,,,)"
11931193
tuple' _ = error "Bad compile-time lookup for tuple'."
11941194

1195-
unsafeGetForall :: String -> UTerm ()
1196-
unsafeGetForall key = Maybe.fromMaybe (error $ "Bad compile-time lookup for " ++ key) $ do
1195+
unsafeGetForall :: String -> HSE.SrcSpanInfo -> UTerm ()
1196+
unsafeGetForall key l = Maybe.fromMaybe (error $ "Bad compile-time lookup for " ++ key) $ do
11971197
(forall', vars, irep, _) <- Map.lookup key polyLits
1198-
pure (UForall () [] forall' vars irep [])
1198+
pure (UForall l () [] forall' vars irep [])
11991199

12001200
--------------------------------------------------------------------------------
12011201
-- UTF-8 specific operations without all the environment gubbins
@@ -1375,27 +1375,27 @@ elaborate = fmap getEqualities . flip runStateT empty . flip runReaderT mempty .
13751375
getEqualities (term, Elaborate{equalities}) = (term, equalities)
13761376
go :: UTerm () -> ReaderT (Map String (IRep IMetaVar)) (StateT Elaborate (Either ElaborateError)) (UTerm (IRep IMetaVar))
13771377
go = \case
1378-
UVar () string -> do
1378+
UVar l () string -> do
13791379
env <- ask
13801380
ty <- case Map.lookup string env of
13811381
Just typ -> pure typ
13821382
Nothing -> lift $ lift $ Left $ VariableNotInScope string
1383-
pure $ UVar ty string
1384-
UApp () f x -> do
1383+
pure $ UVar l ty string
1384+
UApp l () f x -> do
13851385
f' <- go f
13861386
x' <- go x
13871387
b <- fmap IVar freshIMetaVar
13881388
equal (typeOf f') (IFun (typeOf x') b)
1389-
pure $ UApp b f' x'
1390-
ULam () binding mstarType body -> do
1389+
pure $ UApp l b f' x'
1390+
ULam l () binding mstarType body -> do
13911391
a <- case mstarType of
13921392
Just ty -> pure $ fromSomeStarType ty
13931393
Nothing -> fmap IVar freshIMetaVar
13941394
vars <- lift $ bindingVars a binding
13951395
body' <- local (Map.union vars) $ go body
13961396
let ty = IFun a (typeOf body')
1397-
pure $ ULam ty binding mstarType body'
1398-
UForall () types forall' uniqs polyRep _ -> do
1397+
pure $ ULam l ty binding mstarType body'
1398+
UForall l () types forall' uniqs polyRep _ -> do
13991399
-- Generate variables for each unique.
14001400
vars <- for uniqs \uniq -> do
14011401
v <- freshIMetaVar
@@ -1409,7 +1409,7 @@ elaborate = fmap getEqualities . flip runStateT empty . flip runReaderT mempty .
14091409
for_ (zip vars types) \((_uniq, var), someTypeRep) ->
14101410
equal (fromSomeType someTypeRep) (IVar var)
14111411
-- Done!
1412-
pure $ UForall monoType types forall' uniqs polyRep (map (IVar . snd) vars)
1412+
pure $ UForall l monoType types forall' uniqs polyRep (map (IVar . snd) vars)
14131413

14141414
bindingVars :: IRep IMetaVar -> Binding -> StateT Elaborate (Either ElaborateError) (Map String (IRep IMetaVar))
14151415
bindingVars irep (Singleton name) = pure $ Map.singleton name irep

0 commit comments

Comments
 (0)