1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-11 08:25:46 +03:00

Fix de Brujin indexing of lambda arguments (#1727)

A lambda:

```
\ { v0 := b0 ; v1 := b1 ; ... ; vn := bn }
```

should be translated to:

```
λ? (λ? ... (λ? (match ?$0, ?$1, ... , ?$n with ...)))
```

i.e the de Brujin index of the values in the match always start from 0.

Fixes: https://github.com/anoma/juvix/issues/1695
This commit is contained in:
Paul Cadman 2023-01-16 11:13:39 +00:00 committed by GitHub
parent d5bd3d4fa1
commit 0ac9eb1ab4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 10 deletions

View File

@ -298,19 +298,13 @@ goLambda l = do
local
(over indexTableVarsNum (+ nPatterns))
(mapM goLambdaClause (l ^. Internal.lambdaClauses))
values' <- values
let match = mkMatch' (fromList values') (toList ms)
return $ foldr (\_ n -> mkLambda' n) match values'
let values = take nPatterns (mkVar' <$> [0 ..])
match = mkMatch' (fromList values) (toList ms)
return $ foldr (\_ n -> mkLambda' n) match values
where
nPatterns :: Int
nPatterns = length (l ^. Internal.lambdaClauses . _head1 . Internal.lambdaPatterns)
values :: Sem r [Node]
values = do
varsNum <- asks (^. indexTableVarsNum)
let vs = take nPatterns [varsNum ..]
return (mkVar' <$> vs)
goAxiomInductive ::
forall r.
Members '[InfoTableBuilder, Reader InternalTyped.TypesTable, Reader Internal.InfoTable] r =>

View File

@ -163,5 +163,10 @@ tests =
"Builtin Inductive type"
$(mkRelDir ".")
$(mkRelFile "BuiltinInductive.juvix")
$(mkRelFile "out/BuiltinInductive.out")
$(mkRelFile "out/BuiltinInductive.out"),
PosTest
"Higher Order Lambda"
$(mkRelDir ".")
$(mkRelFile "HigherOrderLambda.juvix")
$(mkRelFile "out/HigherOrderLambda.out")
]

View File

@ -0,0 +1,14 @@
module HigherOrderLambda;
open import Stdlib.Prelude;
map' : {A : Type} → {B : Type} → (A → B) → List A → List B;
map' f := \{ nil := nil; (h :: t) := f h :: map' f t};
lst : List Nat;
lst := zero :: one :: nil;
main : IO;
main := printNatLn (foldr (+) zero (map' ((+) one) lst));
end;

View File

@ -0,0 +1 @@
suc (suc (suc zero))