Skip to content

Commit 271eef9

Browse files
Merge pull request #3 from alexhumphreys/feat/list/append
Add ListAppend
2 parents 1028cc9 + 438af83 commit 271eef9

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

Idrall/Check.idr

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ mutual
6464
aEquivHelper i ns1 (EListLit tx xs) ns2 (EListLit ty ys)
6565
= aEquivMaybe i ns1 tx ns2 ty &&
6666
aEquivList i ns1 xs ns2 ys
67+
aEquivHelper i ns1 (EListAppend w x) ns2 (EListAppend y z)
68+
= aEquivHelper i ns1 w ns2 y &&
69+
aEquivHelper i ns1 x ns2 z
6770
aEquivHelper _ _ _ _ _ = False
6871
-- TODO check if assert/equivalent should be in here
6972

@@ -144,8 +147,9 @@ mutual
144147
| NEquivalent Neutral Normal
145148
| NAssert Neutral
146149
| NApp Neutral Normal
147-
| NList Neutral
148150
| NBoolAnd Neutral Normal
151+
| NList Neutral
152+
| NListAppend Neutral Normal
149153

150154
Show Value where
151155
show (VLambda x y) = "(VLambda " ++ show x ++ " " ++ show y ++ ")"
@@ -168,6 +172,7 @@ mutual
168172
show (NAssert x) = "(NEquivalent " ++ show x ++ ")"
169173
show (NApp x y) = "(NApp " ++ show x ++ " " ++ show y ++ ")"
170174
show (NList x) = "(NList " ++ show x ++ ")"
175+
show (NListAppend x y) = "(NListAppend " ++ show x ++ " " ++ show y ++ ")"
171176
show (NBoolAnd x y) = "(NBoolAnd " ++ show x ++ " " ++ show y ++ ")"
172177

173178
extendEnv : Env -> Name -> Value -> Env
@@ -215,6 +220,7 @@ data Error
215220
| ReadBackError String
216221
| SortError
217222
| AssertError String
223+
| ListAppendError String
218224

219225
public export
220226
Show Error where
@@ -227,6 +233,7 @@ Show Error where
227233
show (ReadBackError x) = "ReadBackError: " ++ x
228234
show SortError = "SortError"
229235
show (AssertError str) = "AssertError" ++ str
236+
show (ListAppendError str) = "ListAppendError" ++ str
230237

231238
mutual
232239
partial
@@ -294,6 +301,10 @@ mutual
294301
ty' <- eval env ty
295302
vs <- mapListEither es (eval env)
296303
Right (VListLit (Just ty') vs)
304+
eval env (EListAppend x y) = do
305+
x' <- eval env x
306+
y' <- eval env y
307+
doListAppend x' y'
297308
eval env (ENaturalIsZero x)
298309
= do x' <- eval env x
299310
doNaturalIsZero x'
@@ -329,6 +340,13 @@ mutual
329340
= Right (VNeutral (VEquivalent x y) (NAssert v))
330341
doAssert x = Left (AssertError ("Assert error: " ++ show x))
331342

343+
doListAppend : Value -> Value -> Either Error Value
344+
doListAppend (VListLit x xs) (VListLit _ ys) =
345+
Right (VListLit x (xs ++ ys)) -- TODO dropping type info
346+
doListAppend (VNeutral (VList x) v) y =
347+
Right (VNeutral (VList x) (NListAppend v (Normal' (VList x) y)))
348+
doListAppend x y = Left (ListAppendError (show x ++ " " ++ show y))
349+
332350
-- fresh names
333351
nextName : Name -> Name
334352
nextName x = x ++ "'"
@@ -364,6 +382,10 @@ mutual
364382
readBackNeutral ctx (NList a) = do
365383
a' <- readBackNeutral ctx a
366384
Right (EList a')
385+
readBackNeutral ctx (NListAppend x y) = do
386+
x' <- readBackNeutral ctx x
387+
y' <- readBackNormal ctx y
388+
Right (EListAppend x' y')
367389

368390
readBackTyped : Ctx -> Ty -> Value -> Either Error Expr
369391
readBackTyped ctx (VPi dom ran) fun =
@@ -431,7 +453,7 @@ isBool ctx other = unexpected ctx "Not Bool" other
431453

432454
isList : Ctx -> Value -> Either Error ()
433455
isList ctx (VList x) = Right ()
434-
isList ctx other = unexpected ctx "Not Bool" other
456+
isList ctx other = unexpected ctx "Not List" other
435457

436458
isEquivalent : Ctx -> Value -> Either Error (Value, Value)
437459
isEquivalent ctx (VEquivalent x y) = Right (x, y)
@@ -603,3 +625,9 @@ mutual
603625
mapListEither xs (\e => check ctx e xTy)
604626
Right (xTy)
605627
synth ctx (EAssert other) = Left (AssertError ("Can't assert for expr: " ++ show other))
628+
synth ctx (EListAppend x y) = do
629+
xTy <- synth ctx x
630+
yTy <- synth ctx y
631+
isList ctx xTy
632+
convert ctx (VConst CType) xTy yTy
633+
Right (xTy)

Idrall/Expr.idr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ data Expr
5959
| EList Expr
6060
-- | > EList (Some e) [e', ...] ~ [] : List a
6161
| EListLit (Maybe Expr) (List Expr)
62+
-- | > x # y
63+
| EListAppend Expr Expr
6264

6365
export
6466
Show Expr where
@@ -81,3 +83,4 @@ Show Expr where
8183
show (EList x) = "(EList " ++ show x ++ ")"
8284
show (EListLit Nothing xs) = "(EListLit Nothing " ++ show xs ++ ")"
8385
show (EListLit (Just x) xs) = "(EListLit (Just " ++ show x ++ ") " ++ show xs ++ ")"
86+
show (EListAppend x y) = "(EListAppend " ++ show x ++ " " ++ show y ++ ")"

Idrall/Parser.idr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ table = [ [ Infix appl AssocLeft]
9595
, [ Infix (do token ":"; pure EAnnot) AssocLeft]
9696
, [ Infix (do (token "===" <|> token ""); pure EEquivalent) AssocLeft]
9797
, [ Prefix (do token "assert"; token ":"; pure EAssert)]
98-
, [ Infix (do token "&&"; pure EBoolAnd) AssocLeft]]
98+
, [ Infix (do token "&&"; pure EBoolAnd) AssocLeft]
99+
, [ Infix (do token "#"; pure EListAppend) AssocLeft]]
99100

100101
mutual
101102
letExpr : Parser Expr -- TODO handle type annotation

tests/Test.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ testAll = do
7979
putStrLn "done"
8080

8181
expectPass : List String
82-
expectPass = ["AssertTrivial", "Bool", "Function", "Natural", "True", "NaturalIsZero", "NaturalLiteral", "Let", "FunctionTypeTermTerm", "FunctionApplication", "Equivalence", "FunctionDependentType1", "List", "ListLiteralOne", "ListLiteralEmpty"]
82+
expectPass = ["AssertTrivial", "Bool", "Function", "Natural", "True", "NaturalIsZero", "NaturalLiteral", "Let", "FunctionTypeTermTerm", "FunctionApplication", "Equivalence", "FunctionDependentType1", "List", "ListLiteralOne", "ListLiteralEmpty", "OperatorListConcatenate"]
8383

8484
testGood : IO ()
8585
testGood

0 commit comments

Comments
 (0)