@@ -600,10 +600,11 @@ checkExpression ::
600
600
Expression ->
601
601
Sem r Expression
602
602
checkExpression expectedTy e = do
603
- let hint = TypeHint {
604
- _typeHint = Just expectedTy,
605
- _typeHintTypeNatural = False
606
- }
603
+ let hint =
604
+ TypeHint
605
+ { _typeHint = Just expectedTy,
606
+ _typeHintTypeNatural = False
607
+ }
607
608
e' <- inferExpression' hint e
608
609
let inferredType = e' ^. typedType
609
610
whenJustM (matchTypes expectedTy inferredType) (const (err e'))
@@ -1161,7 +1162,7 @@ inferLeftAppExpression mhint e = case e of
1161
1162
1162
1163
goLiteral :: LiteralLoc -> Sem r TypedExpression
1163
1164
goLiteral lit@ (WithLoc i l) = case l of
1164
- LitNumeric v -> outHole v >> typedLitNumeric v
1165
+ LitNumeric v -> typedLitNumeric v
1165
1166
LitInteger {} -> do
1166
1167
ty <- getIntTy
1167
1168
return $
@@ -1184,46 +1185,70 @@ inferLeftAppExpression mhint e = case e of
1184
1185
_typedType = ExpressionIden (IdenAxiom str)
1185
1186
}
1186
1187
where
1188
+ unaryNatural :: Natural -> Sem r TypedExpression
1189
+ unaryNatural n = do
1190
+ natTy <- getNatTy
1191
+ zero' <- mkBuiltinConstructor BuiltinNatZero
1192
+ suc' <- mkBuiltinConstructor BuiltinNatSuc
1193
+ let mkSuc :: Expression -> Expression
1194
+ mkSuc num = suc' @@ num
1195
+ return
1196
+ TypedExpression
1197
+ { _typedExpression = iterateNat n mkSuc zero',
1198
+ _typedType = natTy
1199
+ }
1200
+
1187
1201
typedLitNumeric :: Integer -> Sem r TypedExpression
1188
1202
typedLitNumeric v
1189
- | v < 0 = getIntTy >>= typedLit LitInteger BuiltinFromInt
1190
- | otherwise = getNatTy >>= typedLit LitNatural BuiltinFromNat
1203
+ | mhint ^. typeHintTypeNatural, v >= 0 = unaryNatural (fromInteger v)
1204
+ | otherwise = do
1205
+ castHole v
1206
+ if
1207
+ | v < 0 -> getIntTy >>= typedLit LitInteger BuiltinFromInt
1208
+ | otherwise -> getNatTy >>= typedLit LitNatural BuiltinFromNat
1191
1209
where
1192
1210
typedLit :: (Integer -> Literal ) -> BuiltinFunction -> Expression -> Sem r TypedExpression
1193
1211
typedLit litt blt ty = do
1194
1212
from <- getBuiltinNameTypeChecker i blt
1195
1213
ihole <- freshHoleImpl i ImplicitInstance
1196
1214
let ty' = maybe ty (adjustLocation i) (mhint ^. typeHint)
1197
- -- inferExpression' (Just ty') $
1198
- inferExpression' todo $
1215
+ inferExpression' (mkTypeHint (Just ty')) $
1199
1216
foldApplication
1200
1217
(ExpressionIden (IdenFunction from))
1201
1218
[ ApplicationArg Implicit ty',
1202
1219
ApplicationArg ImplicitInstance ihole,
1203
1220
ApplicationArg Explicit (ExpressionLiteral (WithLoc i (litt v)))
1204
1221
]
1205
1222
1223
+ mkBuiltinIden :: (IsBuiltin a ) => (Name -> Iden ) -> a -> Sem r Expression
1224
+ mkBuiltinIden mkIden = fmap (ExpressionIden . mkIden) . getBuiltinNameTypeChecker i
1225
+
1226
+ mkBuiltinConstructor :: BuiltinConstructor -> Sem r Expression
1227
+ mkBuiltinConstructor = mkBuiltinIden IdenConstructor
1228
+
1206
1229
mkBuiltinInductive :: BuiltinInductive -> Sem r Expression
1207
- mkBuiltinInductive = fmap ( ExpressionIden . IdenInductive ) . getBuiltinNameTypeChecker i
1230
+ mkBuiltinInductive = mkBuiltinIden IdenInductive
1208
1231
1209
1232
getIntTy :: Sem r Expression
1210
1233
getIntTy = mkBuiltinInductive BuiltinInt
1211
1234
1212
1235
getNatTy :: Sem r Expression
1213
1236
getNatTy = mkBuiltinInductive BuiltinNat
1214
1237
1215
- outHole :: Integer -> Sem r ()
1216
- outHole v
1217
- | v < 0 = case mhint of
1218
- Just (ExpressionHole h) ->
1219
- output CastHole {_castHoleHole = h, _castHoleType = CastInt }
1220
- _ ->
1221
- return ()
1222
- | otherwise = case mhint of
1223
- Just (ExpressionHole h) ->
1224
- output CastHole {_castHoleHole = h, _castHoleType = CastNat }
1225
- _ ->
1226
- return ()
1238
+ castHole :: Integer -> Sem r ()
1239
+ castHole v =
1240
+ case mhint ^. typeHint of
1241
+ Just (ExpressionHole h) ->
1242
+ let outCastHole ty =
1243
+ output
1244
+ CastHole
1245
+ { _castHoleHole = h,
1246
+ _castHoleType = ty
1247
+ }
1248
+ in if
1249
+ | v < 0 -> outCastHole CastInt
1250
+ | otherwise -> outCastHole CastNat
1251
+ _ -> return ()
1227
1252
1228
1253
goIden :: Iden -> Sem r TypedExpression
1229
1254
goIden i = case i of
@@ -1247,11 +1272,15 @@ inferLeftAppExpression mhint e = case e of
1247
1272
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
1248
1273
holesHelper mhint expr = do
1249
1274
let (f, args) = unfoldExpressionApp expr
1250
- hint
1251
- | null args = mhint
1252
- | otherwise = set typeHint Nothing mhint
1275
+ hint <- execState mhint $ do
1276
+ unless (null args) (modify (set typeHint Nothing ))
1277
+ f' <- weakNormalize f
1278
+ case f' of
1279
+ ExpressionIden IdenInductive {} -> modify (set typeHintTypeNatural True )
1280
+ _ -> return ()
1253
1281
arityCheckBuiltins f args
1254
1282
fTy <- inferLeftAppExpression hint f
1283
+
1255
1284
iniBuilderType <- mkInitBuilderType fTy
1256
1285
let iniArg :: ApplicationArg -> AppBuilderArg
1257
1286
iniArg a =
0 commit comments