Skip to content

Commit

Permalink
Check all the type parameter names are different when declaring an in…
Browse files Browse the repository at this point in the history
…ductive type (#1377)

* Fix #1334

* Rename error type
  • Loading branch information
jonaprieto committed Jul 15, 2022
1 parent 753c5d5 commit d11605a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Juvix/Syntax/Concrete/Scoped/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ data ScoperError
| ErrMultipleCompileBlockSameName MultipleCompileBlockSameName
| ErrMultipleCompileRuleSameBackend MultipleCompileRuleSameBackend
| ErrWrongKindExpressionCompileBlock WrongKindExpressionCompileBlock
| ErrDuplicateInductiveParameterName DuplicateInductiveParameterName
deriving stock (Show)

instance ToGenericError ScoperError where
Expand All @@ -58,3 +59,4 @@ instance ToGenericError ScoperError where
ErrMultipleCompileBlockSameName e -> genericError e
ErrMultipleCompileRuleSameBackend e -> genericError e
ErrWrongKindExpressionCompileBlock e -> genericError e
ErrDuplicateInductiveParameterName e -> genericError e
24 changes: 24 additions & 0 deletions src/Juvix/Syntax/Concrete/Scoped/Error/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,27 @@ ambiguousMessage n es =
<> "It could be any of:"
<> line
<> indent' (vsep (map ppCode es))

newtype DuplicateInductiveParameterName = DuplicateInductiveParameterName
{ _duplicateInductiveParameterName :: Symbol
}
deriving stock (Show)

makeLenses ''DuplicateInductiveParameterName

instance ToGenericError DuplicateInductiveParameterName where
genericError e =
GenericError
{ _genericErrorLoc = i,
_genericErrorMessage = prettyError msg,
_genericErrorIntervals = [i]
}
where
param = e ^. duplicateInductiveParameterName
i = getLoc param
msg =
"Invalid name"
<+> ppCode param
<> "."
<> line
<> "Inductive parameter names can not be repeated."
33 changes: 20 additions & 13 deletions src/Juvix/Syntax/Concrete/Scoped/Scoper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -429,24 +429,31 @@ withParams ::
[InductiveParameter 'Parsed] ->
([InductiveParameter 'Scoped] -> Sem r a) ->
Sem r a
withParams xs a = go [] xs
withParams xs a = go [] [] xs
where
go :: [InductiveParameter 'Scoped] -> [InductiveParameter 'Parsed] -> Sem r a
go inductiveParameters' params =
go :: [InductiveParameter 'Scoped] -> [Symbol] -> [InductiveParameter 'Parsed] -> Sem r a
go inductiveParameters' usedNames params =
case params of
-- All params have been checked
[] -> a inductiveParameters'
-- More params to check
(InductiveParameter {..} : ps) -> do
inductiveParameterType' <- checkParseExpressionAtoms _inductiveParameterType
inductiveParameterName' <- freshVariable _inductiveParameterName
let param' =
InductiveParameter
{ _inductiveParameterType = inductiveParameterType',
_inductiveParameterName = inductiveParameterName'
}
withBindLocalVariable (LocalVariable inductiveParameterName') $
go (inductiveParameters' ++ [param']) ps
-- All params have been checked
[] -> a inductiveParameters'
if
| _inductiveParameterName `elem` usedNames ->
throw
( ErrDuplicateInductiveParameterName
(DuplicateInductiveParameterName _inductiveParameterName)
)
| otherwise -> do
inductiveParameterName' <- freshVariable _inductiveParameterName
let param' =
InductiveParameter
{ _inductiveParameterType = inductiveParameterType',
_inductiveParameterName = inductiveParameterName'
}
withBindLocalVariable (LocalVariable inductiveParameterName') $
go (inductiveParameters' ++ [param']) (_inductiveParameterName : usedNames) ps

checkInductiveDef ::
forall r.
Expand Down
7 changes: 7 additions & 0 deletions test/Scope/Negative.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ scoperErrorTests =
"WrongKindExpressionCompileBlock.juvix"
$ \case
ErrWrongKindExpressionCompileBlock {} -> Nothing
_ -> wrongError,
NegTest
"A type parameter name occurs twice when declaring an inductive type"
"."
"DuplicateInductiveParameterName.juvix"
$ \case
ErrDuplicateInductiveParameterName {} -> Nothing
_ -> wrongError
]

Expand Down
5 changes: 5 additions & 0 deletions tests/negative/DuplicateInductiveParameterName.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module DuplicateInductiveParameterName;

inductive T (A : Type) (B : Type) (A : Type) {};

end;

0 comments on commit d11605a

Please sign in to comment.