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

Add syntax for builtin list #2239

Merged
merged 7 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Juvix/Compiler/Asm/Translation/FromCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ fromCore :: Core.InfoTable -> InfoTable
fromCore tab =
InfoTable
{ _infoMainFunction = tab ^. Core.infoMain,
_infoFunctions = fmap (genCode tab) (tab ^. Core.infoFunctions),
_infoInductives = fmap translateInductiveInfo (tab ^. Core.infoInductives),
_infoConstrs = fmap translateConstructorInfo (tab ^. Core.infoConstructors)
_infoFunctions = genCode tab <$> tab ^. Core.infoFunctions,
_infoInductives = translateInductiveInfo <$> tab ^. Core.infoInductives,
_infoConstrs = translateConstructorInfo <$> tab ^. Core.infoConstructors
}

-- Generate code for a single function.
Expand Down
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Juvix.Compiler.Builtins
module Juvix.Compiler.Builtins.IO,
module Juvix.Compiler.Builtins.Int,
module Juvix.Compiler.Builtins.Bool,
module Juvix.Compiler.Builtins.List,
module Juvix.Compiler.Builtins.String,
module Juvix.Compiler.Builtins.Debug,
module Juvix.Compiler.Builtins.Control,
Expand All @@ -16,5 +17,6 @@ import Juvix.Compiler.Builtins.Debug
import Juvix.Compiler.Builtins.Effect
import Juvix.Compiler.Builtins.IO
import Juvix.Compiler.Builtins.Int
import Juvix.Compiler.Builtins.List
import Juvix.Compiler.Builtins.Nat
import Juvix.Compiler.Builtins.String
37 changes: 37 additions & 0 deletions src/Juvix/Compiler/Builtins/List.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Juvix.Compiler.Builtins.List where

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

registerListDef :: Member Builtins r => InductiveDef -> Sem r ()
registerListDef d = do
unless (isSmallUniverse' (d ^. inductiveType)) (error "Lists should be in the small universe")
registerBuiltin BuiltinList (d ^. inductiveName)
case d ^. inductiveConstructors of
[c1, c2] -> registerNil param c1 >> registerCons param c2
_ -> error "List should have exactly two constructors"
where
param :: VarName
param = case d ^. inductiveParameters of
[v] -> v ^. inductiveParamName
_ -> error "List should have exactly one type parameter"

registerNil :: Member Builtins r => VarName -> InductiveConstructorDef -> Sem r ()
registerNil a d@InductiveConstructorDef {..} = do
let nil = _inductiveConstructorName
ty = _inductiveConstructorType
list_ <- getBuiltinName (getLoc d) BuiltinList
let nilty = list_ @@ a
unless (ty === nilty) (error $ "nil has the wrong type " <> ppTrace ty <> " | " <> ppTrace nilty)
registerBuiltin BuiltinListNil nil

registerCons :: Member Builtins r => VarName -> InductiveConstructorDef -> Sem r ()
registerCons a d@InductiveConstructorDef {..} = do
let cons_ = _inductiveConstructorName
ty = _inductiveConstructorType
list_ <- getBuiltinName (getLoc d) BuiltinList
let consty = a --> list_ @@ a --> list_ @@ a
unless (ty === consty) (error "cons has the wrong type")
registerBuiltin BuiltinListCons cons_
5 changes: 5 additions & 0 deletions src/Juvix/Compiler/Concrete/Data/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ builtinConstructors = \case
BuiltinNat -> [BuiltinNatZero, BuiltinNatSuc]
BuiltinBool -> [BuiltinBoolTrue, BuiltinBoolFalse]
BuiltinInt -> [BuiltinIntOfNat, BuiltinIntNegSuc]
BuiltinList -> [BuiltinListNil, BuiltinListCons]

data BuiltinInductive
= BuiltinNat
| BuiltinBool
| BuiltinInt
| BuiltinList
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Data)

instance Hashable BuiltinInductive
Expand All @@ -54,6 +56,7 @@ instance Pretty BuiltinInductive where
BuiltinNat -> Str.nat
BuiltinBool -> Str.bool_
BuiltinInt -> Str.int_
BuiltinList -> Str.list

data BuiltinConstructor
= BuiltinNatZero
Expand All @@ -62,6 +65,8 @@ data BuiltinConstructor
| BuiltinBoolFalse
| BuiltinIntOfNat
| BuiltinIntNegSuc
| BuiltinListNil
| BuiltinListCons
deriving stock (Show, Eq, Ord, Generic, Data)

instance Hashable BuiltinConstructor
Expand Down
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Concrete/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import Juvix.Data.Keyword.All
kwAssign,
kwAt,
kwAxiom,
kwBracketL,
kwBracketR,
kwBuiltin,
kwCase,
kwColon,
Expand Down
Loading