Skip to content

Commit 4a93021

Browse files
committed
wip
1 parent a567fec commit 4a93021

File tree

7 files changed

+77
-19
lines changed

7 files changed

+77
-19
lines changed

src/Juvix/Compiler/Backend/Isabelle/Translation/FromTyped.hs

+2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ goModule onlyTypes infoTable Internal.Module {..} =
352352
Internal.ExpressionSimpleLambda {} -> unsupportedType ty
353353
Internal.ExpressionLambda {} -> unsupportedType ty
354354
Internal.ExpressionCase {} -> unsupportedType ty
355+
Internal.ExpressionNatural {} -> unsupportedType ty
355356
where
356357
unsupportedType :: Internal.Expression -> a
357358
unsupportedType e = error ("unsupported type: " <> Internal.ppTrace e)
@@ -476,6 +477,7 @@ goModule onlyTypes infoTable Internal.Module {..} =
476477
Internal.ExpressionSimpleLambda x -> goSimpleLambda x
477478
Internal.ExpressionLambda x -> goLambda x
478479
Internal.ExpressionCase x -> goCase x
480+
Internal.ExpressionNatural {} -> error "TODO"
479481
where
480482
goIden :: Internal.Iden -> Sem r Expression
481483
goIden iden = case iden of

src/Juvix/Compiler/Internal/Extra/Base.hs

+17
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,23 @@ freshHole l = mkHole l <$> freshNameId
712712
mkFreshHole :: (Members '[NameIdGen] r) => Interval -> Sem r Expression
713713
mkFreshHole l = ExpressionHole <$> freshHole l
714714

715+
squashBuiltinNatural :: BuiltinNatural -> Either BuiltinNatural Expression
716+
squashBuiltinNatural n
717+
| n ^. builtinNaturalSuc == 0 = case n ^. builtinNaturalArg of
718+
ExpressionNatural n' -> squashBuiltinNatural n'
719+
m -> Right m
720+
| otherwise = case n ^. builtinNaturalArg of
721+
ExpressionNatural n' -> case squashBuiltinNatural n' of
722+
Right s -> Left (set builtinNaturalArg s n)
723+
Left s ->
724+
Left
725+
BuiltinNatural
726+
{ _builtinNaturalSuc = n ^. builtinNaturalSuc + s ^. builtinNaturalSuc,
727+
_builtinNaturalArg = s ^. builtinNaturalArg,
728+
_builtinNaturalLoc = n ^. builtinNaturalLoc <> s ^. builtinNaturalLoc
729+
}
730+
m -> Right m
731+
715732
matchExpressions ::
716733
forall r.
717734
(Members '[State (HashMap Name Name), Reader (HashSet VarName), Error Text] r) =>

src/Juvix/Compiler/Internal/Extra/InstanceInfo.hs

+13-3
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,25 @@ makeRigidParam p = case p of
4747
InstanceParamHole {} -> p
4848
InstanceParamMeta v -> InstanceParamVar v
4949

50-
paramToExpression :: (Member NameIdGen r) => InstanceParam -> Sem r Expression
50+
paramToExpression :: forall r. (Member NameIdGen r) => InstanceParam -> Sem r Expression
5151
paramToExpression = \case
5252
InstanceParamVar v -> return $ ExpressionIden (IdenVar v)
53-
-- TODO use builtin nat in Internal
54-
InstanceParamNatural InstanceNat {..} -> return _instanceNatExpression
53+
InstanceParamNatural n -> goNat n
5554
InstanceParamApp InstanceApp {..} -> return _instanceAppExpression
5655
InstanceParamFun InstanceFun {..} -> return _instanceFunExpression
5756
InstanceParamHole h -> return $ ExpressionHole h
5857
InstanceParamMeta v -> ExpressionHole . mkHole (getLoc v) <$> freshNameId
58+
where
59+
goNat :: InstanceNat -> Sem r Expression
60+
goNat InstanceNat {..} = do
61+
arg <- paramToExpression _instanceNatArg
62+
return $
63+
ExpressionNatural
64+
BuiltinNatural
65+
{ _builtinNaturalSuc = _instanceNatSuc,
66+
_builtinNaturalArg = arg,
67+
_builtinNaturalLoc = _instanceNatLoc
68+
}
5969

6070
paramFromExpression :: forall r. (Members '[Reader BuiltinsTable] r) => HashSet VarName -> Expression -> Sem (Fail ': r) InstanceParam
6171
paramFromExpression metaVars e = case e of

src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/Positivity/ConstructorArg.hs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ goArg ty = case ty of
2626
ExpressionSimpleLambda {} -> invalid
2727
ExpressionLambda {} -> invalid
2828
ExpressionCase {} -> invalid
29+
ExpressionNatural {} -> invalid
2930
where
3031
invalid :: forall a. Sem r a
3132
invalid =

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

+6
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ inferLeftAppExpression mhint e = case e of
10531053
ExpressionApplication {} -> impossible
10541054
ExpressionIden i -> goIden i
10551055
ExpressionLiteral l -> goLiteral l
1056+
ExpressionNatural n -> goNatural n
10561057
ExpressionFunction f -> goFunction f
10571058
ExpressionHole h -> goHole h
10581059
ExpressionInstanceHole h -> goInstanceHole h
@@ -1062,6 +1063,9 @@ inferLeftAppExpression mhint e = case e of
10621063
ExpressionLet l -> goLet l
10631064
ExpressionCase l -> goCase l
10641065
where
1066+
goNatural :: BuiltinNatural -> Sem r TypedExpression
1067+
goNatural = error "TODO"
1068+
10651069
goLet :: Let -> Sem r TypedExpression
10661070
goLet l = do
10671071
_letClauses <- mapM goLetClause (l ^. letClauses)
@@ -1653,6 +1657,7 @@ typeArity = weakNormalize >=> go
16531657
ExpressionFunction f -> ArityFunction <$> goFun f
16541658
ExpressionHole h -> return (ArityBlocking (BlockingHole h))
16551659
ExpressionInstanceHole {} -> return ArityUnit
1660+
ExpressionNatural {} -> return ArityUnit
16561661
ExpressionLambda {} -> return ArityError
16571662
ExpressionCase {} -> return ArityNotKnown -- TODO Do better here
16581663
ExpressionUniverse {} -> return ArityUnit
@@ -1716,6 +1721,7 @@ guessArity = \case
17161721
ExpressionHole {} -> return ArityNotKnown
17171722
ExpressionInstanceHole {} -> return ArityUnit
17181723
ExpressionFunction {} -> return ArityUnit
1724+
ExpressionNatural {} -> return ArityUnit
17191725
ExpressionLiteral {} -> return arityLiteral
17201726
ExpressionApplication a -> appHelper a
17211727
ExpressionIden i -> idenHelper i

src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/TypeChecking/Data/Inference.hs

+21
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ strongNormalize' original = do
158158
ExpressionApplication app -> goApp app
159159
ExpressionLiteral {} -> return e
160160
ExpressionHole h -> goHole h
161+
ExpressionNatural n -> goNatural n
161162
ExpressionInstanceHole h -> goInstanceHole h
162163
ExpressionUniverse {} -> return e
163164
ExpressionFunction f -> ExpressionFunction <$> goFunction f
@@ -169,6 +170,11 @@ strongNormalize' original = do
169170
-- TODO it should normalize like an applied lambda
170171
ExpressionCase c -> return (ExpressionCase c)
171172

173+
goNatural :: BuiltinNatural -> Sem r Expression
174+
goNatural n = case squashBuiltinNatural n of
175+
Left m -> ExpressionNatural <$> traverseOf builtinNaturalArg go m
176+
Right e -> go e
177+
172178
goLet :: Let -> Sem r Let
173179
goLet Let {..} = do
174180
clauses' <- mapM goLetClause _letClauses
@@ -268,6 +274,7 @@ weakNormalize' = go
268274
ExpressionIden i -> goIden i
269275
ExpressionHole h -> goHole h
270276
ExpressionInstanceHole h -> goInstanceHole h
277+
ExpressionNatural h -> goNatural h
271278
ExpressionApplication a -> goApp a
272279
ExpressionLiteral {} -> return e
273280
ExpressionUniverse {} -> return e
@@ -276,6 +283,12 @@ weakNormalize' = go
276283
ExpressionLambda {} -> return e
277284
ExpressionLet {} -> return e
278285
ExpressionCase {} -> return e
286+
287+
goNatural :: BuiltinNatural -> Sem r Expression
288+
goNatural n = return $ case squashBuiltinNatural n of
289+
Left m -> ExpressionNatural m
290+
Right e -> e
291+
279292
goIden :: Iden -> Sem r Expression
280293
goIden i = case i of
281294
IdenFunction f -> do
@@ -383,13 +396,16 @@ runInferenceState inis = reinterpret (runState inis) $ \case
383396
(ExpressionUniverse u, ExpressionUniverse u') -> check (u == u')
384397
(ExpressionSimpleLambda a, ExpressionSimpleLambda b) -> goSimpleLambda a b
385398
(ExpressionLambda a, ExpressionLambda b) -> goLambda a b
399+
(ExpressionNatural a, ExpressionNatural b) -> goNatural a b
386400
(ExpressionHole h, a) -> goHole h a
387401
(a, ExpressionHole h) -> goHole h a
388402
(_, ExpressionLet r) -> go normA (r ^. letExpression)
389403
(ExpressionLiteral l, ExpressionLiteral l') -> check (l == l')
390404
(ExpressionLiteral l, ExpressionApplication a) -> goUnaryNatLiteral l a
391405
(ExpressionApplication a, ExpressionLiteral l) -> goUnaryNatLiteral l a
392406
(ExpressionLet l, _) -> go (l ^. letExpression) normB
407+
(ExpressionNatural {}, _) -> err
408+
(_, ExpressionNatural {}) -> err
393409
(ExpressionInstanceHole {}, _) -> err
394410
(_, ExpressionInstanceHole {}) -> err
395411
(ExpressionSimpleLambda {}, _) -> err
@@ -447,6 +463,11 @@ runInferenceState inis = reinterpret (runState inis) $ \case
447463
failWhen (num1 /= num2)
448464
inject ok
449465

466+
goNatural :: BuiltinNatural -> BuiltinNatural -> Sem r (Maybe MatchError)
467+
goNatural a b
468+
| a ^. builtinNaturalSuc == b ^. builtinNaturalSuc = go (a ^. builtinNaturalArg) (b ^. builtinNaturalArg)
469+
| otherwise = err
470+
450471
goHole :: Hole -> Expression -> Sem r (Maybe MatchError)
451472
goHole h t = do
452473
r <- queryMetavar' h

src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/TypeChecking/Traits/Resolver.hs

+17-16
Original file line numberDiff line numberDiff line change
@@ -278,22 +278,23 @@ lookupInstance' visited ctab tab name params
278278
modify (HashMap.insert v t)
279279
return True
280280

281-
squashInstanceNat :: InstanceNat -> Either InstanceNat InstanceParam
282-
squashInstanceNat n
283-
| n ^. instanceNatSuc == 0 = case n ^. instanceNatArg of
284-
InstanceParamNatural n' -> squashInstanceNat n'
285-
m -> Right m
286-
| otherwise = case n ^. instanceNatArg of
287-
InstanceParamNatural n' -> case squashInstanceNat n' of
288-
Right s -> Left (set instanceNatArg s n)
289-
Left s ->
290-
Left
291-
InstanceNat
292-
{ _instanceNatSuc = n ^. instanceNatSuc + s ^. instanceNatSuc,
293-
_instanceNatArg = s ^. instanceNatArg,
294-
_instanceNatLoc = n ^. instanceNatLoc <> s ^. instanceNatLoc
295-
}
296-
m -> Right m
281+
-- TODO where to use this?
282+
-- squashInstanceNat :: InstanceNat -> Either InstanceNat InstanceParam
283+
-- squashInstanceNat n
284+
-- | n ^. instanceNatSuc == 0 = case n ^. instanceNatArg of
285+
-- InstanceParamNatural n' -> squashInstanceNat n'
286+
-- m -> Right m
287+
-- | otherwise = case n ^. instanceNatArg of
288+
-- InstanceParamNatural n' -> case squashInstanceNat n' of
289+
-- Right s -> Left (set instanceNatArg s n)
290+
-- Left s ->
291+
-- Left
292+
-- InstanceNat
293+
-- { _instanceNatSuc = n ^. instanceNatSuc + s ^. instanceNatSuc,
294+
-- _instanceNatArg = s ^. instanceNatArg,
295+
-- _instanceNatLoc = n ^. instanceNatLoc <> s ^. instanceNatLoc
296+
-- }
297+
-- m -> Right m
297298

298299
lookupInstance ::
299300
forall r.

0 commit comments

Comments
 (0)