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

Fix arity checker bug #1546

Merged
merged 6 commits into from
Sep 26, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Juvix.Compiler.Internal.Translation.FromInternal.Analysis.ArityChecking.C
where

import Juvix.Compiler.Internal.Extra
import Juvix.Compiler.Internal.Pretty
import Juvix.Compiler.Internal.Translation.FromAbstract.Data.Context
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.ArityChecking.Data.LocalVars
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.ArityChecking.Data.Types
Expand Down Expand Up @@ -319,7 +320,7 @@ checkLambda ari (Lambda cl) = Lambda <$> mapM goClause cl
-- TODO. think what to do in this case
return (pats, as)
(_ : _, []) -> case rest of
ArityRestUnit -> error "too many patterns in lambda"
ArityRestUnit -> error ("too many patterns in lambda: " <> ppTrace (Lambda cl) <> "\n" <> prettyText ari)
ArityRestUnknown -> return (pats, [])

idenArity :: Members '[Reader LocalVars, Reader InfoTable] r => Iden -> Sem r Arity
Expand Down Expand Up @@ -422,10 +423,10 @@ checkExpression hintArity expr = case expr of
toArity = \case
ParamExplicit a -> a
ParamImplicit -> ArityUnit
mapM
(secondM (uncurry checkExpression))
[(i', (a, e')) | (a, (i', e')) <- zip (argsAris ++ repeat ArityUnknown) args]
>>= addHoles i hintArity ari
argsWithHoles :: [(IsImplicit, Expression)] <- addHoles i hintArity ari args
let argsWithAris :: [(IsImplicit, (Arity, Expression))]
argsWithAris = [(i', (a, e')) | (a, (i', e')) <- zip (argsAris ++ repeat ArityUnknown) argsWithHoles]
mapM (secondM (uncurry checkExpression)) argsWithAris
addHoles ::
Interval ->
Arity ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Juvix.Compiler.Internal.Translation.FromInternal.Analysis.ArityChecking.Data.Types where

import Juvix.Prelude
import Juvix.Prelude.Pretty

data Arity
= ArityUnit
Expand Down Expand Up @@ -74,3 +75,32 @@ foldArity UnfoldedArity {..} = go _ufoldArityParams
l = case a of
ParamExplicit e -> ParamExplicit e
ParamImplicit -> ParamImplicit

instance HasAtomicity FunctionArity where
atomicity = const (Aggregate funFixity)

instance HasAtomicity Arity where
atomicity = \case
ArityUnit -> Atom
ArityUnknown -> Atom
ArityFunction f -> atomicity f

instance Pretty ArityParameter where
pretty = \case
ParamImplicit -> "{𝟙}"
ParamExplicit f -> pretty f

instance Pretty FunctionArity where
pretty f@(FunctionArity l r) =
parensCond (atomParens (const True) (atomicity f) funFixity) (pretty l)
<> " → "
<> pretty r
where
parensCond :: Bool -> Doc a -> Doc a
parensCond t d = if t then parens d else d

instance Pretty Arity where
pretty = \case
ArityUnit -> "𝟙"
ArityUnknown -> "?"
ArityFunction f -> pretty f
10 changes: 10 additions & 0 deletions tests/positive/Internal/Lambda.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,14 @@ zipWith := λ {_ nil _ := nil;
t : {A : Type} → {B : Type} → ({X : Type} → List X) → List A × List B;
t := id {({X : Type} → List X) → _} λ { f := f {_} , f {_} };

inductive Box (A : Type) {
b : A → Box A;
};

x : Box ((A : Type) → A → A);
x := b λ {A a := a};

t1 : {A : Type} → Box ((A : Type) → A → A) → A → A;
t1 {A} := λ {(b f) := f A};

end;