Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literal casting #2457

Merged
merged 14 commits into from
Nov 3, 2023
Merged
42 changes: 0 additions & 42 deletions src/Juvix/Compiler/Builtins/Base.hs

This file was deleted.

9 changes: 8 additions & 1 deletion src/Juvix/Compiler/Builtins/Int.hs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ registerIntLt :: forall r. (Members '[Builtins, NameIdGen] r) => FunctionDef ->
registerIntLt f = do
int <- builtinName BuiltinInt
bool_ <- builtinName BuiltinBool
ofNat <- toExpression <$> builtinName BuiltinIntOfNat
suc <- toExpression <$> getBuiltinName (getLoc f) BuiltinNatSuc
zero <- toExpression <$> getBuiltinName (getLoc f) BuiltinNatZero
intLe <- toExpression <$> builtinName BuiltinIntLe
intPlus <- toExpression <$> builtinName BuiltinIntPlus
let l = getLoc f
Expand All @@ -380,7 +383,7 @@ registerIntLt f = do
(.+.) :: (IsExpression a, IsExpression b) => a -> b -> Expression
x .+. y = intPlus @@ x @@ y
(.<=.) :: (IsExpression a, IsExpression b) => a -> b -> Expression
lit1 = ExpressionLiteral (WithLoc (getLoc f) (LitInteger 1))
lit1 = ofNat @@ (suc @@ zero)
x .<=. y = intLe @@ x @@ y
m = toExpression varm
n = toExpression varn
Expand All @@ -399,3 +402,7 @@ registerIntLt f = do
where
builtinName :: (IsBuiltin a) => a -> Sem r Name
builtinName = getBuiltinName (getLoc f)

registerFromInt :: (Members '[Builtins, NameIdGen] r) => FunctionDef -> Sem r ()
registerFromInt f = do
registerBuiltin BuiltinFromInt (f ^. funDefName)
4 changes: 4 additions & 0 deletions src/Juvix/Compiler/Builtins/Nat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,7 @@ registerNatEq f = do
_funInfoFreeVars = [varn, varm],
_funInfoFreeTypeVars = []
}

registerFromNat :: (Members '[Builtins, NameIdGen] r) => FunctionDef -> Sem r ()
registerFromNat f = do
registerBuiltin BuiltinFromNat (f ^. funDefName)
12 changes: 11 additions & 1 deletion src/Juvix/Compiler/Concrete/Data/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ data BuiltinFunction
| BuiltinIntNonNeg
| BuiltinIntLe
| BuiltinIntLt
| BuiltinFromNat
| BuiltinFromInt
| BuiltinSeq
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Data)

Expand Down Expand Up @@ -138,6 +140,8 @@ instance Pretty BuiltinFunction where
BuiltinIntNonNeg -> Str.intNonNeg
BuiltinIntLe -> Str.intLe
BuiltinIntLt -> Str.intLt
BuiltinFromNat -> Str.fromNat
BuiltinFromInt -> Str.fromInt
BuiltinSeq -> Str.builtinSeq

data BuiltinAxiom
Expand Down Expand Up @@ -224,5 +228,11 @@ isIntBuiltin = \case
BuiltinIntLt -> True
_ -> False

isCastBuiltin :: BuiltinFunction -> Bool
isCastBuiltin = \case
BuiltinFromNat -> True
BuiltinFromInt -> True
_ -> False

isIgnoredBuiltin :: BuiltinFunction -> Bool
isIgnoredBuiltin f = not (isNatBuiltin f) && not (isIntBuiltin f)
isIgnoredBuiltin f = not (isNatBuiltin f) && not (isIntBuiltin f) && not (isCastBuiltin f)
6 changes: 4 additions & 2 deletions src/Juvix/Compiler/Concrete/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ deriving stock instance Ord (Statement 'Scoped)
data ProjectionDef s = ProjectionDef
{ _projectionConstructor :: S.Symbol,
_projectionField :: SymbolType s,
_projectionFieldIx :: Int
_projectionFieldIx :: Int,
_projectionFieldBuiltin :: Maybe (WithLoc BuiltinFunction)
}

deriving stock instance Show (ProjectionDef 'Parsed)
Expand Down Expand Up @@ -595,7 +596,8 @@ deriving stock instance Ord (RecordDefineField 'Scoped)
data RecordField (s :: Stage) = RecordField
{ _fieldName :: SymbolType s,
_fieldColon :: Irrelevant (KeywordRef),
_fieldType :: ExpressionType s
_fieldType :: ExpressionType s,
_fieldBuiltin :: Maybe (WithLoc BuiltinFunction)
}

deriving stock instance Show (RecordField 'Parsed)
Expand Down
8 changes: 6 additions & 2 deletions src/Juvix/Compiler/Concrete/Print/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,12 @@ instance (SingI s) => PrettyPrint (RhsGadt s) where
ppCode _rhsGadtColon <+> ppExpressionType _rhsGadtType

instance (SingI s) => PrettyPrint (RecordField s) where
ppCode RecordField {..} =
ppSymbolType _fieldName <+> ppCode _fieldColon <+> ppExpressionType _fieldType
ppCode RecordField {..} = do
let builtin' = (<> line) . ppCode <$> _fieldBuiltin
builtin'
?<> ppSymbolType _fieldName
<+> ppCode _fieldColon
<+> ppExpressionType _fieldType

instance (SingI s) => PrettyPrint (RhsRecord s) where
ppCode RhsRecord {..} = do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ checkProjectionDef p = do
ProjectionDef
{ _projectionFieldIx = p ^. projectionFieldIx,
_projectionConstructor = p ^. projectionConstructor,
_projectionFieldBuiltin = p ^. projectionFieldBuiltin,
_projectionField
}

Expand Down Expand Up @@ -1339,7 +1340,8 @@ checkSections sec = do
ProjectionDef
{ _projectionConstructor = headConstr,
_projectionField = field ^. fieldName,
_projectionFieldIx = idx
_projectionFieldIx = idx,
_projectionFieldBuiltin = field ^. fieldBuiltin
}

getFields :: Sem (Fail ': s') [RecordStatement 'Parsed]
Expand Down
6 changes: 6 additions & 0 deletions src/Juvix/Compiler/Concrete/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@ builtinStatement = do
<|> (builtinFunction >>= fmap StatementFunctionDef . builtinFunctionDef)
<|> (builtinAxiom >>= fmap StatementAxiom . builtinAxiomDef)

builtinRecordField :: (Members '[InfoTableBuilder, PragmasStash, JudocStash, NameIdGen] r) => ParsecS r (WithLoc BuiltinFunction)
builtinRecordField = do
void (kw kwBuiltin)
builtinFunction

--------------------------------------------------------------------------------
-- Syntax declaration
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -1265,6 +1270,7 @@ rhsGadt = P.label "<constructor gadt>" $ do

recordField :: (Members '[InfoTableBuilder, PragmasStash, JudocStash, NameIdGen] r) => ParsecS r (RecordField 'Parsed)
recordField = do
_fieldBuiltin <- optional builtinRecordField
_fieldName <- symbol
_fieldColon <- Irrelevant <$> kw kwColon
_fieldType <- parseExpressionAtoms
Expand Down
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Core/Translation/FromInternal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ goApplication a = do
goLiteral :: Symbol -> Symbol -> Internal.LiteralLoc -> Node
goLiteral intToNat intToInt l = case l ^. withLocParam of
Internal.LitString s -> mkLitConst (ConstString s)
Internal.LitNumeric i -> mkLitConst (ConstInteger i)
Internal.LitInteger i -> mkApp' (mkIdent' intToInt) (mkLitConst (ConstInteger i))
Internal.LitNatural i -> mkApp' (mkIdent' intToNat) (mkLitConst (ConstInteger i))
where
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Core/Translation/Stripped/FromCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fromCore tab =
_infoInductives = HashMap.filter (maybe True shouldKeepType . (^. inductiveBuiltin)) (tab ^. infoInductives),
_infoConstructors = HashMap.filter (maybe True shouldKeepConstructor . (^. constructorBuiltin)) (tab ^. infoConstructors)
}

shouldKeepFunction :: BuiltinFunction -> Bool
shouldKeepFunction = \case
BuiltinNatPlus -> False
Expand Down Expand Up @@ -51,6 +52,8 @@ fromCore tab =
BuiltinIntLe -> False
BuiltinIntLt -> False
BuiltinSeq -> False
BuiltinFromNat -> True
BuiltinFromInt -> True

shouldKeepConstructor :: BuiltinConstructor -> Bool
shouldKeepConstructor = \case
Expand Down
18 changes: 18 additions & 0 deletions src/Juvix/Compiler/Internal/Data/Cast.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Juvix.Compiler.Internal.Data.Cast where

import Juvix.Compiler.Internal.Language
import Juvix.Prelude

data CastType = CastInt | CastNat

data CastHole = CastHole
{ _castHoleHole :: Hole,
_castHoleType :: CastType
}

makeLenses ''CastHole

isCastInt :: CastType -> Bool
isCastInt = \case
CastInt -> True
CastNat -> False
7 changes: 4 additions & 3 deletions src/Juvix/Compiler/Internal/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ genFieldProjection ::
forall r.
(Members '[NameIdGen] r) =>
FunctionName ->
Maybe BuiltinFunction ->
ConstructorInfo ->
Int ->
Sem r FunctionDef
genFieldProjection _funDefName info fieldIx = do
genFieldProjection _funDefName _funDefBuiltin info fieldIx = do
body' <- genBody
let (inductiveParams, constrArgs) = constructorArgTypes info
implicity = constructorImplicity info
Expand All @@ -97,12 +98,12 @@ genFieldProjection _funDefName info fieldIx = do
_funDefTerminating = False,
_funDefInstance = False,
_funDefCoercion = False,
_funDefBuiltin = Nothing,
_funDefArgsInfo = mempty,
_funDefPragmas = mempty {_pragmasInline = Just InlineAlways},
_funDefBody = body',
_funDefType = foldFunType (inductiveArgs ++ [saturatedTy]) retTy,
..
_funDefName,
_funDefBuiltin
}
where
genBody :: Sem r Expression
Expand Down
3 changes: 0 additions & 3 deletions src/Juvix/Compiler/Internal/Extra/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,6 @@ genWildcard loc impl = do
var <- varFromWildcard (Wildcard loc)
return (PatternArg impl Nothing (PatternVariable var))

freshWildcard :: (Members '[NameIdGen] r) => Interval -> Sem r Hole
freshWildcard l = mkHole l <$> freshNameId

freshHole :: (Members '[NameIdGen] r) => Interval -> Sem r Hole
freshHole l = mkHole l <$> freshNameId

Expand Down
Loading