1
1
mirror of https://github.com/anoma/juvix.git synced 2024-11-30 14:13:27 +03:00

Update Geb backend (#2436)

Changes in the printing of Lambda terms necessary for the use of the Juvix
Geb backend, changing the names of binary operations, adding a
constructor for natural numbers. Appropriately changes tests when
necessary.

---------

Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
This commit is contained in:
Artem Gureev 2023-10-06 21:37:08 +06:00 committed by GitHub
parent 9bed4bbadb
commit e7f5a83da3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
63 changed files with 191 additions and 196 deletions

View File

@ -73,7 +73,7 @@ eval morph =
MorphismBinop op -> evalBinop op
MorphismCase c -> evalCase c
MorphismFirst f -> evalFirst f
MorphismInteger i -> return $ GebValueMorphismInteger i
MorphismInteger i -> evalBitChoice i
MorphismLambda l -> evalLambda l
MorphismLeft m -> evalLeftInj m
MorphismPair p -> evalPair p
@ -83,6 +83,10 @@ eval morph =
MorphismVar x -> evalVar x
MorphismFail x -> evalFail x
evalBitChoice :: BitChoice -> Sem r GebValue
evalBitChoice n = do
return (GebValueMorphismInteger (n ^. bitChoice))
evalVar :: (EvalEffects r) => Var -> Sem r GebValue
evalVar var = do
ctx <- asks (^. envContext)
@ -303,12 +307,15 @@ valueFalse =
quote :: GebValue -> Morphism
quote = \case
GebValueClosure cls -> quoteClosure cls
GebValueMorphismInteger i -> MorphismInteger i
GebValueMorphismInteger i -> quoteBitChoice i
GebValueMorphismLeft m -> quoteValueMorphismLeft m
GebValueMorphismPair m -> quoteValueMorphismPair m
GebValueMorphismRight m -> quoteValueMorphismRight m
GebValueMorphismUnit -> MorphismUnit
quoteBitChoice :: Integer -> Morphism
quoteBitChoice n = MorphismInteger (BitChoice {_bitChoice = n})
quoteClosure :: ValueClosure -> Morphism
quoteClosure cls =
let env = map quote (toList (cls ^. valueClosureEnv))

View File

@ -33,6 +33,7 @@ allKeywords =
kwGebBinopMod,
kwGebBinopEq,
kwGebBinopLt,
kwGebBitChoice,
kwGebObjectInitial,
kwGebObjectTerminal,
kwGebObjectProduct,
@ -98,6 +99,9 @@ kwGebObjectInteger = asciiKw Str.gebInteger
kwGebVar :: Keyword
kwGebVar = asciiKw Str.gebVar
kwGebBitChoice :: Keyword
kwGebBitChoice = asciiKw Str.gebBitChoice
kwGebObjectInitial :: Keyword
kwGebObjectInitial = asciiKw Str.gebInitial

View File

@ -103,6 +103,11 @@ data Failure = Failure
}
deriving stock (Show, Eq, Generic)
newtype BitChoice = BitChoice
{ _bitChoice :: Integer
}
deriving stock (Show, Eq, Generic)
-- | Corresponds to the GEB type for terms (morphisms of the category): `stlc`
-- (https://github.com/anoma/geb/blob/main/src/specs/lambda.lisp).
data Morphism
@ -117,7 +122,7 @@ data Morphism
| MorphismLambda Lambda
| MorphismApplication Application
| MorphismVar Var
| MorphismInteger Integer
| MorphismInteger BitChoice
| MorphismBinop Binop
| MorphismFail Failure
deriving stock (Show, Eq, Generic)
@ -189,7 +194,7 @@ instance HasAtomicity Morphism where
MorphismLambda {} -> Aggregate appFixity
MorphismApplication {} -> Aggregate appFixity
MorphismVar {} -> Aggregate appFixity
MorphismInteger {} -> Atom
MorphismInteger {} -> Aggregate appFixity
MorphismBinop {} -> Aggregate appFixity
MorphismFail {} -> Aggregate appFixity
@ -231,3 +236,4 @@ makeLenses ''RightInj'
makeLenses ''Second
makeLenses ''TypedMorphism
makeLenses ''Var
makeLenses ''BitChoice

View File

@ -146,10 +146,15 @@ instance PrettyCode Morphism where
MorphismLambda x -> ppCode x
MorphismApplication x -> ppCode x
MorphismVar idx -> ppCode idx
MorphismInteger n -> return $ annotate AnnLiteralInteger (pretty n)
MorphismInteger n -> ppCode n
MorphismBinop x -> ppCode x
MorphismFail x -> ppCode x
instance PrettyCode BitChoice where
ppCode BitChoice {..} = do
let pnumb = (annotate AnnLiteralInteger (pretty _bitChoice))
return $ kwBitChoice <+> pnumb
instance PrettyCode Product where
ppCode Product {..} = do
left <- ppArg _productLeft

View File

@ -67,6 +67,9 @@ kwEq = keyword Str.gebEq
kwLt :: Doc Ann
kwLt = keyword Str.gebLt
kwBitChoice :: Doc Ann
kwBitChoice = keyword Str.gebBitChoice
kwInitial :: Doc Ann
kwInitial = keyword Str.gebInitial

View File

@ -157,7 +157,7 @@ fromCore tab = case tab ^. Core.infoMain of
convertConstant :: Core.Constant -> Trans Morphism
convertConstant Core.Constant {..} = case _constantValue of
Core.ConstInteger n -> return $ MorphismInteger n
Core.ConstInteger n -> return $ MorphismInteger (BitChoice {_bitChoice = n})
Core.ConstString {} -> unsupported
convertApp :: Core.App -> Trans Morphism

View File

@ -115,7 +115,6 @@ morphism :: ParsecS r Geb.Morphism
morphism =
P.label "<geb morphism>" $ do
morphismUnit
<|> Geb.MorphismInteger <$> morphismInteger
<|> parens
( morphismUnit
<|> Geb.MorphismAbsurd <$> morphismAbsurd
@ -130,6 +129,7 @@ morphism =
<|> Geb.MorphismVar <$> morphismVar
<|> Geb.MorphismBinop <$> morphismBinop
<|> Geb.MorphismFail <$> morphismFail
<|> Geb.MorphismInteger <$> morphismBitChoice
)
morphismList :: ParsecS r Geb.Morphism
@ -140,8 +140,12 @@ morphismList = parens $ do
parseNatural :: ParsecS r Integer
parseNatural = lexeme P.decimal
morphismInteger :: ParsecS r Integer
morphismInteger = parseNatural
morphismBitChoice :: ParsecS r Geb.BitChoice
morphismBitChoice = do
P.label "<geb MorphismBitChoice>" $ do
kw kwGebBitChoice <* space
_bitChoice <- parseNatural
return Geb.BitChoice {..}
opcode :: ParsecS r Geb.Opcode
opcode =

View File

@ -638,29 +638,32 @@ gebApp = "app"
gebVar :: (IsString s) => s
gebVar = "index"
gebBitChoice :: (IsString s) => s
gebBitChoice = "bit-choice"
gebAdd :: (IsString s) => s
gebAdd = "add"
gebAdd = "plus"
gebSub :: (IsString s) => s
gebSub = "sub"
gebSub = "minus"
gebMul :: (IsString s) => s
gebMul = "mul"
gebMul = "times"
gebDiv :: (IsString s) => s
gebDiv = "div"
gebDiv = "divide"
gebMod :: (IsString s) => s
gebMod = "mod"
gebMod = "modulo"
gebFail :: (IsString s) => s
gebFail = "err"
gebEq :: (IsString s) => s
gebEq = "eq"
gebEq = "lamb-eq"
gebLt :: (IsString s) => s
gebLt = "lt"
gebLt = "lamb-lt"
gebInitial :: (IsString s) => s
gebInitial = "so0"
@ -675,7 +678,7 @@ gebCoprod :: (IsString s) => s
gebCoprod = "coprod"
gebHom :: (IsString s) => s
gebHom = "so-hom-obj"
gebHom = "fun-type"
gebInteger :: (IsString s) => s
gebInteger = "int"

View File

@ -1 +1 @@
11
(bit-choice 11)

View File

@ -1 +1,2 @@
32
(bit-choice 32)

View File

@ -1 +1 @@
8
(bit-choice 8)

View File

@ -1 +1 @@
32
(bit-choice 32)

View File

@ -1 +1 @@
8
(bit-choice 8)

View File

@ -1 +1 @@
9
(bit-choice 9)

View File

@ -1 +1 @@
3
(bit-choice 3)

View File

@ -1 +1 @@
5050
(bit-choice 5050)

View File

@ -1 +1 @@
5050
(bit-choice 5050)

View File

@ -1 +1 @@
55
(bit-choice 55)

View File

@ -1 +1 @@
771
(bit-choice 771)

View File

@ -1 +1 @@
55
(bit-choice 55)

View File

@ -1 +1 @@
5050
(bit-choice 5050)

View File

@ -1 +1 @@
11
(bit-choice 11)

View File

@ -1 +1 @@
364
(bit-choice 364)

View File

@ -1 +1 @@
48830320
(bit-choice 48830320)

View File

@ -1 +1 @@
6386010
(bit-choice 6386010)

View File

@ -1 +1 @@
87
(bit-choice 87)

View File

@ -1 +1 @@
203
(bit-choice 203)

View File

@ -1 +1 @@
3
(bit-choice 3)

View File

@ -1 +1 @@
6077
(bit-choice 6077)

View File

@ -1 +1 @@
1
(bit-choice 1)

View File

@ -1 +1 @@
1
(bit-choice 1)

View File

@ -1 +1 @@
55
(bit-choice 55)

View File

@ -1 +1 @@
11
(bit-choice 11)

View File

@ -1 +1 @@
32
(bit-choice 32)

View File

@ -1 +1 @@
8
(bit-choice 8)

View File

@ -1 +1 @@
1
(bit-choice 1)

View File

@ -1 +1 @@
40
(bit-choice 40)

View File

@ -1 +1 @@
8
(bit-choice 8)

View File

@ -1 +1 @@
5
(bit-choice 5)

View File

@ -1 +1 @@
3
(bit-choice 3)

View File

@ -1 +1 @@
5050
(bit-choice 5050)

View File

@ -1 +1 @@
5050
(bit-choice 5050)

View File

@ -1 +1 @@
55
(bit-choice 55)

View File

@ -1 +1 @@
55
(bit-choice 55)

View File

@ -1 +1 @@
5050
(bit-choice 5050)

View File

@ -1 +1 @@
11
(bit-choice 11)

View File

@ -1 +1 @@
364
(bit-choice 364)

View File

@ -1 +1 @@
48830320
(bit-choice 48830320)

View File

@ -1 +1 @@
901
(bit-choice 901)

View File

@ -1 +1 @@
87
(bit-choice 87)

View File

@ -1 +1 @@
203
(bit-choice 203)

View File

@ -1 +1 @@
3
(bit-choice 3)

View File

@ -1 +1 @@
6295
(bit-choice 6295)

View File

@ -1 +1 @@
9
(bit-choice 9)

View File

@ -1,23 +1,21 @@
(typed
(app
(app
(app
(app
(lamb
(list
(fun-type
so1
so1))
(lamb
(list
(so-hom-obj
so1
so1))
(lamb
(list
int)
(index 1)))
(list
(lamb
(list
so1)
(index 0))))
int)
(index 1)))
(list
12345))
(lamb
(list
so1)
(index 0))))
(list
(unit)))
so1)
(bit-choice 12345)))
(list
(unit)))

View File

@ -1,22 +1,18 @@
(typed
(app
(lamb
(list
(coprod
int
int))
(case-on
(index 0)
(right
int
1)
(left
int
2)))
(app
(lamb
(list
(coprod
int
int))
(case-on
(index 0)
(right
int
(bit-choice 1))
(left
int
3)))
(coprod
int
int))
(bit-choice 2))))
(list
(left
int
(bit-choice 3))))

View File

@ -1,14 +1,12 @@
(typed
(app
(lamb
(list
(prod
int
int))
(fst
(index 0)))
(app
(lamb
(list
(pair
10
20)))
int)
(prod
int
int))
(fst
(index 0)))
(list
(pair
(bit-choice 10)
(bit-choice 20))))

View File

@ -1,21 +1,15 @@
(typed
(app
(app
(lamb
(list
(fun-type
so1
so1))
(lamb
(list
(so-hom-obj
so1
so1))
(lamb
(list
int)
(index 1)))
(list
(lamb
(list
so1)
(index 0))))
(so-hom-obj
int
(so-hom-obj
so1
so1)))
int)
(index 1)))
(list
(lamb
(list
so1)
(index 0))))

View File

@ -1,11 +1,9 @@
(typed
(app
(lamb
(list
int)
(index 0))
(app
(lamb
(list
(add
1000
2000)))
int)
int)
(index 0))
(list
(plus
(bit-choice 1000)
(bit-choice 2000))))

View File

@ -1,14 +1,10 @@
(typed
(case-on
(right
so1
(unit))
(right
so1
(unit))
(left
so1
(unit)))
(coprod
(case-on
(right
so1
so1))
(unit))
(right
so1
(unit))
(left
so1
(unit)))

View File

@ -1,13 +1,7 @@
(typed
(lamb
(list
so1)
(lamb
(list
so1)
(lamb
(list
so1)
(index 1)))
(so-hom-obj
so1
(so-hom-obj
so1
so1)))
(index 1)))

View File

@ -1,12 +1,6 @@
(typed
(lamb
(list
so1)
(left
so1
(unit)))
(so-hom-obj
(lamb
(list
so1)
(left
so1
(coprod
so1
so1)))
(unit)))

View File

@ -55,7 +55,7 @@ tests:
- dev
- geb
- repl
stdin: ':t (mul 2 3)'
stdin: ':t (times (bit-choice 2) (bit-choice 3))'
stdout:
contains: 'int'
exit-status: 0
@ -78,7 +78,7 @@ tests:
- dev
- geb
- repl
stdin: '(add 2 (mul 3 4))'
stdin: '(plus (bit-choice 2) (times (bit-choice 3) (bit-choice 4)))'
stdout:
contains: |
14
@ -133,25 +133,19 @@ tests:
args:
- Geb/positive/app-lambda.geb
stdout: |
(typed
(app
(app
(lamb
(list
(fun-type
so1
so1))
(lamb
(list
(so-hom-obj
so1
so1))
(lamb
(list
int)
(index 1)))
(list
(lamb
(list
so1)
(index 0))))
(so-hom-obj
int
(so-hom-obj
so1
so1)))
int)
(index 1)))
(list
(lamb
(list
so1)
(index 0))))
exit-status: 0