Skip to content

Commit 619e72e

Browse files
committed
wip hint
1 parent 45cc9c4 commit 619e72e

File tree

1 file changed

+44
-19
lines changed
  • src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/TypeChecking

1 file changed

+44
-19
lines changed

src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/TypeChecking/CheckerNew.hs

+44-19
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,31 @@ data AppBuilder = AppBuilder
6868
_appBuilderArgs :: [AppBuilderArg]
6969
}
7070

71+
data TypeHint = TypeHint
72+
{ _typeHint :: Maybe Expression,
73+
_typeHintTypeNatural :: Bool
74+
}
75+
76+
makeLenses ''TypeHint
7177
makeLenses ''AppBuilder
7278
makeLenses ''AppBuilderArg
7379
makeLenses ''FunctionDefault
7480
makeLenses ''FunctionDefaultInfo
7581

82+
mkTypeHint :: Maybe Expression -> TypeHint
83+
mkTypeHint ty =
84+
TypeHint
85+
{ _typeHint = ty,
86+
_typeHintTypeNatural = False
87+
}
88+
89+
emptyTypeHint :: TypeHint
90+
emptyTypeHint =
91+
TypeHint
92+
{ _typeHint = Nothing,
93+
_typeHintTypeNatural = False
94+
}
95+
7696
instance PrettyCode FunctionDefault where
7797
ppCode _ = return "ppCode(FunctionDefault)"
7898

@@ -332,9 +352,9 @@ checkMutualStatement ::
332352
Sem r MutualStatement
333353
checkMutualStatement = \case
334354
StatementFunction f -> do
335-
traceM ("f : " <> ppTrace f)
355+
-- traceM ("f : " <> ppTrace f)
336356
f' <- resolveInstanceHoles (resolveCastHoles (checkFunctionDef f))
337-
traceM ("f' : " <> ppTrace f')
357+
-- traceM ("f' : " <> ppTrace f')
338358
return (StatementFunction f')
339359
StatementInductive f -> StatementInductive <$> resolveInstanceHoles (resolveCastHoles (checkInductiveDef f))
340360
StatementAxiom ax -> do
@@ -363,7 +383,7 @@ checkFunctionDef ::
363383
checkFunctionDef FunctionDef {..} = do
364384
funDef <- do
365385
_funDefType' <- checkDefType _funDefType
366-
traceM ("----------------fundeftype': " <> ppTrace _funDefType')
386+
-- traceM ("----------------fundeftype': " <> ppTrace _funDefType')
367387
registerIdenType _funDefName _funDefType'
368388
_funDefBody' <- checkFunctionBody _funDefType' _funDefBody
369389
params <- fst <$> unfoldFunType' _funDefType'
@@ -580,7 +600,11 @@ checkExpression ::
580600
Expression ->
581601
Sem r Expression
582602
checkExpression expectedTy e = do
583-
e' <- inferExpression' (Just expectedTy) e
603+
let hint = TypeHint {
604+
_typeHint = Just expectedTy,
605+
_typeHintTypeNatural = False
606+
}
607+
e' <- inferExpression' hint e
584608
let inferredType = e' ^. typedType
585609
whenJustM (matchTypes expectedTy inferredType) (const (err e'))
586610
return (e' ^. typedExpression)
@@ -649,7 +673,7 @@ inferExpressionRepl ::
649673
Maybe Expression ->
650674
Expression ->
651675
Sem r TypedExpression
652-
inferExpressionRepl hint = resolveInstanceHoles . resolveCastHoles . inferExpression' hint
676+
inferExpressionRepl hint = resolveInstanceHoles . resolveCastHoles . inferExpression' (mkTypeHint hint)
653677

654678
lookupVar ::
655679
(HasCallStack) =>
@@ -979,7 +1003,7 @@ checkPattern = go
9791003
inferExpression' ::
9801004
forall r.
9811005
(Members '[Reader InfoTable, Reader BuiltinsTable, ResultBuilder, Reader LocalVars, Error TypeCheckerError, NameIdGen, Inference, Output TypedInstanceHole, Termination, Output CastHole, Reader InsertedArgsStack, Reader InsertedArgsStack] r) =>
982-
Maybe Expression ->
1006+
TypeHint ->
9831007
Expression ->
9841008
Sem r TypedExpression
9851009
inferExpression' = holesHelper
@@ -988,7 +1012,7 @@ inferExpression' = holesHelper
9881012
inferLeftAppExpression ::
9891013
forall r.
9901014
(Members '[Reader InfoTable, Reader BuiltinsTable, ResultBuilder, Reader LocalVars, Error TypeCheckerError, NameIdGen, Inference, Output TypedInstanceHole, Termination, Output CastHole, Reader InsertedArgsStack] r) =>
991-
Maybe Expression ->
1015+
TypeHint ->
9921016
Expression ->
9931017
Sem r TypedExpression
9941018
inferLeftAppExpression mhint e = case e of
@@ -1030,7 +1054,7 @@ inferLeftAppExpression mhint e = case e of
10301054
goHole :: Hole -> Sem r TypedExpression
10311055
goHole h = do
10321056
void (queryMetavar h)
1033-
ty <- maybe (freshHoleImpl (getLoc h) Implicit) return mhint
1057+
ty <- maybe (freshHoleImpl (getLoc h) Implicit) return (mhint ^. typeHint)
10341058
return
10351059
TypedExpression
10361060
{ _typedExpression = ExpressionHole h,
@@ -1039,7 +1063,7 @@ inferLeftAppExpression mhint e = case e of
10391063

10401064
goInstanceHole :: InstanceHole -> Sem r TypedExpression
10411065
goInstanceHole h = do
1042-
let ty = fromMaybe impossible mhint
1066+
let ty = fromMaybe impossible (mhint ^. typeHint)
10431067
locals <- ask
10441068
output (TypedInstanceHole h ty locals)
10451069
return
@@ -1050,7 +1074,7 @@ inferLeftAppExpression mhint e = case e of
10501074

10511075
goSimpleLambda :: SimpleLambda -> Sem r TypedExpression
10521076
goSimpleLambda (SimpleLambda (SimpleBinder v ty) b) = do
1053-
b' <- withLocalType v ty (inferExpression' Nothing b)
1077+
b' <- withLocalType v ty (inferExpression' emptyTypeHint b)
10541078
let smallUni = smallUniverseE (getLoc ty)
10551079
ty' <- checkExpression smallUni ty
10561080
let fun = Function (unnamedParameter smallUni) (b' ^. typedType)
@@ -1062,10 +1086,10 @@ inferLeftAppExpression mhint e = case e of
10621086

10631087
goCase :: Case -> Sem r TypedExpression
10641088
goCase c = do
1065-
ty <- case mhint of
1089+
ty <- case mhint ^. typeHint of
10661090
Nothing -> freshHoleImpl (getLoc c) Implicit
10671091
Just hi -> return hi
1068-
typedCaseExpression <- inferExpression' Nothing (c ^. caseExpression)
1092+
typedCaseExpression <- inferExpression' emptyTypeHint (c ^. caseExpression)
10691093
let _caseExpression = typedCaseExpression ^. typedExpression
10701094
_caseExpressionType = Just (typedCaseExpression ^. typedType)
10711095
_caseExpressionWholeType = Just ty
@@ -1093,7 +1117,7 @@ inferLeftAppExpression mhint e = case e of
10931117

10941118
goLambda :: Lambda -> Sem r TypedExpression
10951119
goLambda l = do
1096-
ty <- case mhint of
1120+
ty <- case mhint ^. typeHint of
10971121
Just hi -> return hi
10981122
Nothing -> freshHoleImpl (getLoc l) Implicit
10991123
_lambdaClauses <- mapM (goClause ty) (l ^. lambdaClauses)
@@ -1169,8 +1193,9 @@ inferLeftAppExpression mhint e = case e of
11691193
typedLit litt blt ty = do
11701194
from <- getBuiltinNameTypeChecker i blt
11711195
ihole <- freshHoleImpl i ImplicitInstance
1172-
let ty' = maybe ty (adjustLocation i) mhint
1173-
inferExpression' (Just ty') $
1196+
let ty' = maybe ty (adjustLocation i) (mhint ^. typeHint)
1197+
-- inferExpression' (Just ty') $
1198+
inferExpression' todo $
11741199
foldApplication
11751200
(ExpressionIden (IdenFunction from))
11761201
[ ApplicationArg Implicit ty',
@@ -1218,13 +1243,13 @@ inferLeftAppExpression mhint e = case e of
12181243
kind <- lookupInductiveType v
12191244
return (TypedExpression kind (ExpressionIden i))
12201245

1221-
-- | The hint is used for trailing holes only
1222-
holesHelper :: forall r. (Members '[Reader InfoTable, Reader BuiltinsTable, ResultBuilder, Reader LocalVars, Error TypeCheckerError, NameIdGen, Inference, Output TypedInstanceHole, Termination, Output CastHole, Reader InsertedArgsStack] r) => Maybe Expression -> Expression -> Sem r TypedExpression
1246+
-- | The _typeHint is used for trailing holes only
1247+
holesHelper :: forall r. (Members '[Reader InfoTable, Reader BuiltinsTable, ResultBuilder, Reader LocalVars, Error TypeCheckerError, NameIdGen, Inference, Output TypedInstanceHole, Termination, Output CastHole, Reader InsertedArgsStack] r) => TypeHint -> Expression -> Sem r TypedExpression
12231248
holesHelper mhint expr = do
12241249
let (f, args) = unfoldExpressionApp expr
12251250
hint
12261251
| null args = mhint
1227-
| otherwise = Nothing
1252+
| otherwise = set typeHint Nothing mhint
12281253
arityCheckBuiltins f args
12291254
fTy <- inferLeftAppExpression hint f
12301255
iniBuilderType <- mkInitBuilderType fTy
@@ -1363,7 +1388,7 @@ holesHelper mhint expr = do
13631388
gets (^. appBuilderType) >>= applyCtx >>= modify' . set appBuilderType
13641389

13651390
goArgs :: forall r'. (r' ~ State AppBuilder ': Output InsertedArg ': r) => Sem r' ()
1366-
goArgs = peekArg >>= maybe (insertTrailingHolesMay mhint) goNextArg
1391+
goArgs = peekArg >>= maybe (insertTrailingHolesMay (mhint ^. typeHint)) goNextArg
13671392
where
13681393
insertTrailingHolesMay :: Maybe Expression -> Sem r' ()
13691394
insertTrailingHolesMay = flip whenJust insertTrailingHoles

0 commit comments

Comments
 (0)