Skip to content

Commit

Permalink
assert builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Sep 9, 2024
1 parent f47b9b0 commit 144b314
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Juvix/Compiler/Builtins/Assert.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Juvix.Compiler.Builtins.Assert where

import Juvix.Compiler.Internal.Builtins
import Juvix.Compiler.Internal.Extra
import Juvix.Prelude

checkAssert :: (Members '[Reader BuiltinsTable, Error ScoperError, NameIdGen] r) => FunctionDef -> Sem r ()
checkAssert f = do
let ftype = f ^. funDefType
u = ExpressionUniverse smallUniverseNoLoc
l = getLoc f
bool_ <- getBuiltinNameScoper (getLoc f) BuiltinBool
valueT <- freshVar l "valueT"
let freeVars = hashSet [valueT]
unless ((ftype ==% (u <>--> bool_ --> valueT --> valueT)) freeVars) $
builtinsErrorText (getLoc f) "assert must be of type {Value : Type} -> Bool -> Value -> Value"
10 changes: 9 additions & 1 deletion src/Juvix/Compiler/Concrete/Data/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ instance Serialize BuiltinConstructor
instance NFData BuiltinConstructor

data BuiltinFunction
= BuiltinNatPlus
= BuiltinAssert
| BuiltinNatPlus
| BuiltinNatSub
| BuiltinNatMul
| BuiltinNatUDiv
Expand Down Expand Up @@ -163,6 +164,7 @@ instance NFData BuiltinFunction

instance Pretty BuiltinFunction where
pretty = \case
BuiltinAssert -> Str.assert
BuiltinNatPlus -> Str.natPlus
BuiltinNatSub -> Str.natSub
BuiltinNatMul -> Str.natMul
Expand Down Expand Up @@ -368,6 +370,7 @@ isNatBuiltin = \case
BuiltinNatLt -> True
BuiltinNatEq -> True
--
BuiltinAssert -> False
BuiltinBoolIf -> False
BuiltinBoolOr -> False
BuiltinBoolAnd -> False
Expand Down Expand Up @@ -403,6 +406,7 @@ isIntBuiltin = \case
BuiltinIntLe -> True
BuiltinIntLt -> True
--
BuiltinAssert -> False
BuiltinNatPlus -> False
BuiltinNatSub -> False
BuiltinNatMul -> False
Expand All @@ -425,6 +429,7 @@ isCastBuiltin = \case
BuiltinFromNat -> True
BuiltinFromInt -> True
--
BuiltinAssert -> False
BuiltinIntEq -> False
BuiltinIntPlus -> False
BuiltinIntSubNat -> False
Expand Down Expand Up @@ -463,6 +468,7 @@ isIgnoredBuiltin f
.&&. (not . isIntBuiltin)
.&&. (not . isCastBuiltin)
.&&. (/= BuiltinMonadBind)
.&&. (/= BuiltinAssert)
$ f

explicit :: Bool
Expand Down Expand Up @@ -495,6 +501,8 @@ isIgnoredBuiltin f
BuiltinNatEq -> False
-- Monad
BuiltinMonadBind -> False
-- Assert
BuiltinAssert -> False
-- Ignored
BuiltinBoolIf -> True
BuiltinBoolOr -> True
Expand Down
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Core/Translation/Stripped/FromCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fromCore fsize tab =
BuiltinIntLt -> False
BuiltinSeq -> False
BuiltinMonadBind -> True -- TODO revise
BuiltinAssert -> True
BuiltinFromNat -> True
BuiltinFromInt -> True

Expand Down
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Internal/Translation/FromConcrete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Data.HashSet qualified as HashSet
import Data.IntMap.Strict qualified as IntMap
import Data.List.NonEmpty qualified as NonEmpty
import Juvix.Compiler.Builtins
import Juvix.Compiler.Builtins.Assert
import Juvix.Compiler.Builtins.Pair
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S
import Juvix.Compiler.Concrete.Extra qualified as Concrete
Expand Down Expand Up @@ -537,6 +538,7 @@ checkBuiltinFunction ::
BuiltinFunction ->
Sem r ()
checkBuiltinFunction d f = localBuiltins $ case f of
BuiltinAssert -> checkAssert d
BuiltinNatPlus -> checkNatPlus d
BuiltinNatSub -> checkNatSub d
BuiltinNatMul -> checkNatMul d
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Extra/Strings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ ioSequence = "IO-sequence"
ioReadline :: (IsString s) => s
ioReadline = "IO-readline"

assert :: (IsString s) => s
assert = "assert"

natPrint :: (IsString s) => s
natPrint = "nat-print"

Expand Down

0 comments on commit 144b314

Please sign in to comment.