-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor of registration of literalIntTo{Nat, Int} functions
- Loading branch information
1 parent
7d5cc5a
commit b7be2b3
Showing
4 changed files
with
140 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 32 additions & 49 deletions
81
src/Juvix/Compiler/Core/Translation/FromInternal/Builtins/Int.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,39 @@ | ||
module Juvix.Compiler.Core.Translation.FromInternal.Builtins.Int where | ||
|
||
import Data.HashMap.Strict qualified as HashMap | ||
import Juvix.Compiler.Core.Data | ||
import Juvix.Compiler.Core.Extra | ||
import Juvix.Compiler.Core.Info.NameInfo | ||
import Juvix.Compiler.Core.Language | ||
|
||
setupLiteralIntToInt :: Symbol -> InfoTable -> InfoTable | ||
setupLiteralIntToInt sym tab = | ||
tab | ||
{ _infoIdentifiers = HashMap.insert sym ii (tab ^. infoIdentifiers), | ||
_identContext = HashMap.insert sym node (tab ^. identContext), | ||
_infoLiteralIntToInt = Just sym | ||
} | ||
where | ||
ii = | ||
IdentifierInfo | ||
{ _identifierSymbol = sym, | ||
_identifierName = freshIdentName tab "intToNat", | ||
_identifierLocation = Nothing, | ||
_identifierArgsNum = 1, | ||
_identifierType = mkPi mempty (Binder "x" Nothing mkTypeInteger') targetType, | ||
_identifierIsExported = False, | ||
_identifierBuiltin = Nothing | ||
} | ||
node = | ||
case (tagOfNatM, tagNegSucM, boolSymM, intToNatSymM) of | ||
(Just tagOfNat, Just tagNegSuc, Just boolSym, Just intToNatSym) -> | ||
mkLambda' mkTypeInteger' $ | ||
mkIf' | ||
boolSym | ||
(mkBuiltinApp' OpIntLt [mkVar' 0, mkConstant' (ConstInteger 0)]) | ||
( mkConstr | ||
(setInfoName "negSuc" mempty) | ||
tagNegSuc | ||
[ mkBuiltinApp' | ||
OpIntSub | ||
[ mkConstant' (ConstInteger 0), | ||
mkBuiltinApp' OpIntAdd [mkVar' 0, mkConstant' (ConstInteger 1)] | ||
] | ||
] | ||
) | ||
( mkConstr | ||
(setInfoName "ofNat" mempty) | ||
tagOfNat | ||
[mkApp' (mkIdent' intToNatSym) (mkVar' 0)] | ||
) | ||
_ -> mkLambda' mkTypeInteger' $ mkVar' 0 | ||
|
||
targetType = maybe mkTypeInteger' (\s -> mkTypeConstr (setInfoName "Int" mempty) s []) natIntM | ||
tagOfNatM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinIntOfNat | ||
tagNegSucM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinIntNegSuc | ||
boolSymM = (^. inductiveSymbol) <$> lookupBuiltinInductive tab BuiltinBool | ||
natIntM = (^. inductiveSymbol) <$> lookupBuiltinInductive tab BuiltinInt | ||
intToNatSymM = tab ^. infoLiteralIntToNat | ||
-- | Returns the node representing a function Int -> Int that transforms literal | ||
-- integers to builtin Int. | ||
literalIntToIntNode :: Member InfoTableBuilder r => Sem r Node | ||
literalIntToIntNode = do | ||
tab <- getInfoTable | ||
let intToNatSymM = tab ^. infoLiteralIntToNat | ||
tagOfNatM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinIntOfNat | ||
tagNegSucM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinIntNegSuc | ||
boolSymM = (^. inductiveSymbol) <$> lookupBuiltinInductive tab BuiltinBool | ||
return $ | ||
case (tagOfNatM, tagNegSucM, boolSymM, intToNatSymM) of | ||
(Just tagOfNat, Just tagNegSuc, Just boolSym, Just intToNatSym) -> | ||
mkLambda' mkTypeInteger' $ | ||
mkIf' | ||
boolSym | ||
(mkBuiltinApp' OpIntLt [mkVar' 0, mkConstant' (ConstInteger 0)]) | ||
( mkConstr | ||
(setInfoName "negSuc" mempty) | ||
tagNegSuc | ||
[ mkBuiltinApp' | ||
OpIntSub | ||
[ mkConstant' (ConstInteger 0), | ||
mkBuiltinApp' OpIntAdd [mkVar' 0, mkConstant' (ConstInteger 1)] | ||
] | ||
] | ||
) | ||
( mkConstr | ||
(setInfoName "ofNat" mempty) | ||
tagOfNat | ||
[mkApp' (mkIdent' intToNatSym) (mkVar' 0)] | ||
) | ||
_ -> mkLambda' mkTypeInteger' $ mkVar' 0 |
53 changes: 18 additions & 35 deletions
53
src/Juvix/Compiler/Core/Translation/FromInternal/Builtins/Nat.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,25 @@ | ||
module Juvix.Compiler.Core.Translation.FromInternal.Builtins.Nat where | ||
|
||
import Data.HashMap.Strict qualified as HashMap | ||
import Juvix.Compiler.Core.Data | ||
import Juvix.Compiler.Core.Extra | ||
import Juvix.Compiler.Core.Info.NameInfo | ||
import Juvix.Compiler.Core.Language | ||
|
||
setupLiteralIntToNat :: Symbol -> InfoTable -> InfoTable | ||
setupLiteralIntToNat sym tab = | ||
tab | ||
{ _infoIdentifiers = HashMap.insert sym ii (tab ^. infoIdentifiers), | ||
_identContext = HashMap.insert sym node (tab ^. identContext), | ||
_infoLiteralIntToNat = Just sym | ||
} | ||
where | ||
ii = | ||
IdentifierInfo | ||
{ _identifierSymbol = sym, | ||
_identifierName = freshIdentName tab "intToNat", | ||
_identifierLocation = Nothing, | ||
_identifierArgsNum = 1, | ||
_identifierType = mkPi mempty (Binder "x" Nothing mkTypeInteger') targetType, | ||
_identifierIsExported = False, | ||
_identifierBuiltin = Nothing | ||
} | ||
node = | ||
case (tagZeroM, tagSucM, boolSymM) of | ||
(Just tagZero, Just tagSuc, Just boolSym) -> | ||
mkLambda' mkTypeInteger' $ | ||
mkIf' | ||
boolSym | ||
(mkBuiltinApp' OpEq [mkVar' 0, mkConstant' (ConstInteger 0)]) | ||
(mkConstr (setInfoName "zero" mempty) tagZero []) | ||
(mkConstr (setInfoName "suc" mempty) tagSuc [mkApp' (mkIdent' sym) (mkBuiltinApp' OpIntSub [mkVar' 0, mkConstant' (ConstInteger 1)])]) | ||
_ -> | ||
mkLambda' mkTypeInteger' $ mkVar' 0 | ||
targetType = maybe mkTypeInteger' (\s -> mkTypeConstr (setInfoName "Nat" mempty) s []) natSymM | ||
tagZeroM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinNatZero | ||
tagSucM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinNatSuc | ||
boolSymM = (^. inductiveSymbol) <$> lookupBuiltinInductive tab BuiltinBool | ||
natSymM = (^. inductiveSymbol) <$> lookupBuiltinInductive tab BuiltinNat | ||
-- | Returns the node representing a function Int -> Nat that is used to transform | ||
-- literal integers to builtin Nat. The symbol representing the literalIntToNat function is passed | ||
-- so that it can be called recusively. | ||
literalIntToNatNode :: Member InfoTableBuilder r => Symbol -> Sem r Node | ||
literalIntToNatNode sym = do | ||
tab <- getInfoTable | ||
let tagZeroM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinNatZero | ||
tagSucM = (^. constructorTag) <$> lookupBuiltinConstructor tab BuiltinNatSuc | ||
boolSymM = (^. inductiveSymbol) <$> lookupBuiltinInductive tab BuiltinBool | ||
return $ case (tagZeroM, tagSucM, boolSymM) of | ||
(Just tagZero, Just tagSuc, Just boolSym) -> | ||
mkLambda' mkTypeInteger' $ | ||
mkIf' | ||
boolSym | ||
(mkBuiltinApp' OpEq [mkVar' 0, mkConstant' (ConstInteger 0)]) | ||
(mkConstr (setInfoName "zero" mempty) tagZero []) | ||
(mkConstr (setInfoName "suc" mempty) tagSuc [mkApp' (mkIdent' sym) (mkBuiltinApp' OpIntSub [mkVar' 0, mkConstant' (ConstInteger 1)])]) | ||
_ -> mkLambda' mkTypeInteger' $ mkVar' 0 |