Skip to content

Commit

Permalink
Add do notation (#2937)
Browse files Browse the repository at this point in the history
- Closes #2355
- Depends on #2943 

Example:
```
minusOne : Nat -> Maybe Nat
  | zero := nothing
  | (suc n) := just n;


minusThree (n : Nat) : Maybe Nat :=
  do {
    x1 <- minusOne n;
    x2 <- minusOne x1;
    let
      x2' : Nat := x2;
    in
    x3 <- minusOne x2';
    pure x3;
  };
```
  • Loading branch information
janmasrovira authored Aug 21, 2024
1 parent 41450a8 commit eb0922a
Show file tree
Hide file tree
Showing 21 changed files with 689 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Juvix.Compiler.Builtins
module Juvix.Compiler.Builtins.Cairo,
module Juvix.Compiler.Builtins.Byte,
module Juvix.Compiler.Builtins.ByteArray,
module Juvix.Compiler.Builtins.Monad,
)
where

Expand All @@ -28,5 +29,6 @@ import Juvix.Compiler.Builtins.IO
import Juvix.Compiler.Builtins.Int
import Juvix.Compiler.Builtins.List
import Juvix.Compiler.Builtins.Maybe
import Juvix.Compiler.Builtins.Monad
import Juvix.Compiler.Builtins.Nat
import Juvix.Compiler.Builtins.String
7 changes: 7 additions & 0 deletions src/Juvix/Compiler/Builtins/Monad.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Juvix.Compiler.Builtins.Monad where

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

checkMonadBind :: forall r. FunctionDef -> Sem r ()
checkMonadBind _ = return ()
117 changes: 113 additions & 4 deletions src/Juvix/Compiler/Concrete/Data/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ data BuiltinFunction
| BuiltinFromNat
| BuiltinFromInt
| BuiltinSeq
| BuiltinMonadBind
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Data)

instance Hashable BuiltinFunction
Expand Down Expand Up @@ -189,6 +190,7 @@ instance Pretty BuiltinFunction where
BuiltinFromNat -> Str.fromNat
BuiltinFromInt -> Str.fromInt
BuiltinSeq -> Str.builtinSeq
BuiltinMonadBind -> Str.builtinMonadBind

data BuiltinAxiom
= BuiltinNatPrint
Expand Down Expand Up @@ -365,7 +367,26 @@ isNatBuiltin = \case
BuiltinNatLe -> True
BuiltinNatLt -> True
BuiltinNatEq -> True
_ -> False
--
BuiltinBoolIf -> False
BuiltinBoolOr -> False
BuiltinBoolAnd -> False
BuiltinIntEq -> False
BuiltinIntPlus -> False
BuiltinIntSubNat -> False
BuiltinIntNegNat -> False
BuiltinIntNeg -> False
BuiltinIntMul -> False
BuiltinIntDiv -> False
BuiltinIntMod -> False
BuiltinIntSub -> False
BuiltinIntNonNeg -> False
BuiltinIntLe -> False
BuiltinIntLt -> False
BuiltinFromNat -> False
BuiltinFromInt -> False
BuiltinSeq -> False
BuiltinMonadBind -> False

isIntBuiltin :: BuiltinFunction -> Bool
isIntBuiltin = \case
Expand All @@ -381,13 +402,101 @@ isIntBuiltin = \case
BuiltinIntNonNeg -> True
BuiltinIntLe -> True
BuiltinIntLt -> True
_ -> False
--
BuiltinNatPlus -> False
BuiltinNatSub -> False
BuiltinNatMul -> False
BuiltinNatUDiv -> False
BuiltinNatDiv -> False
BuiltinNatMod -> False
BuiltinNatLe -> False
BuiltinNatLt -> False
BuiltinNatEq -> False
BuiltinBoolIf -> False
BuiltinBoolOr -> False
BuiltinBoolAnd -> False
BuiltinFromNat -> False
BuiltinFromInt -> False
BuiltinSeq -> False
BuiltinMonadBind -> False

isCastBuiltin :: BuiltinFunction -> Bool
isCastBuiltin = \case
BuiltinFromNat -> True
BuiltinFromInt -> True
_ -> False
--
BuiltinIntEq -> False
BuiltinIntPlus -> False
BuiltinIntSubNat -> False
BuiltinIntNegNat -> False
BuiltinIntNeg -> False
BuiltinIntMul -> False
BuiltinIntDiv -> False
BuiltinIntMod -> False
BuiltinIntSub -> False
BuiltinIntNonNeg -> False
BuiltinIntLe -> False
BuiltinIntLt -> False
BuiltinNatPlus -> False
BuiltinNatSub -> False
BuiltinNatMul -> False
BuiltinNatUDiv -> False
BuiltinNatDiv -> False
BuiltinNatMod -> False
BuiltinNatLe -> False
BuiltinNatLt -> False
BuiltinNatEq -> False
BuiltinBoolIf -> False
BuiltinBoolOr -> False
BuiltinBoolAnd -> False
BuiltinSeq -> False
BuiltinMonadBind -> False

isIgnoredBuiltin :: BuiltinFunction -> Bool
isIgnoredBuiltin f = not (isNatBuiltin f) && not (isIntBuiltin f) && not (isCastBuiltin f)
isIgnoredBuiltin f
| spec == explicit = spec
| otherwise = impossible
where
spec :: Bool
spec =
(not . isNatBuiltin)
.&&. (not . isIntBuiltin)
.&&. (not . isCastBuiltin)
.&&. (/= BuiltinMonadBind)
$ f

explicit :: Bool
explicit = case f of
-- Cast
BuiltinFromNat -> False
BuiltinFromInt -> False
-- Int
BuiltinIntEq -> False
BuiltinIntPlus -> False
BuiltinIntSubNat -> False
BuiltinIntNegNat -> False
BuiltinIntNeg -> False
BuiltinIntMul -> False
BuiltinIntDiv -> False
BuiltinIntMod -> False
BuiltinIntSub -> False
BuiltinIntNonNeg -> False
BuiltinIntLe -> False
BuiltinIntLt -> False
-- Nat
BuiltinNatPlus -> False
BuiltinNatSub -> False
BuiltinNatMul -> False
BuiltinNatUDiv -> False
BuiltinNatDiv -> False
BuiltinNatMod -> False
BuiltinNatLe -> False
BuiltinNatLt -> False
BuiltinNatEq -> False
-- Monad
BuiltinMonadBind -> False
-- Ignored
BuiltinBoolIf -> True
BuiltinBoolOr -> True
BuiltinBoolAnd -> True
BuiltinSeq -> True
4 changes: 4 additions & 0 deletions src/Juvix/Compiler/Concrete/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Juvix.Data.Keyword.All
kwCase,
kwCoercion,
kwColon,
kwDo,
kwElse,
kwEnd,
kwEq,
Expand All @@ -51,6 +52,7 @@ import Juvix.Data.Keyword.All
kwIterator,
kwLambda,
kwLeft,
kwLeftArrow,
kwLet,
kwMapsTo,
kwModule,
Expand Down Expand Up @@ -88,6 +90,7 @@ reservedKeywords =
kwAxiom,
kwCase,
kwColon,
kwDo,
kwElse,
kwEnd,
kwHiding,
Expand All @@ -97,6 +100,7 @@ reservedKeywords =
kwIn,
kwInductive,
kwLambda,
kwLeftArrow,
kwLet,
kwModule,
kwOf,
Expand Down
Loading

0 comments on commit eb0922a

Please sign in to comment.