Skip to content

Commit

Permalink
Implement wildcard constructor (#2550)
Browse files Browse the repository at this point in the history
- Closes #2549 

The implementation of wildcard constructors was previously done in the
arity checker. I did not realise I was missing it because there was not
tests for it that included typechecking (we were only checking
formatting).
  • Loading branch information
janmasrovira authored Dec 7, 2023
1 parent 69594ed commit 4fa1735
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,23 @@ checkPattern = go
whenJust name (\n -> addPatternVar n ty (argTy ^. paramName))
pat' <- case pat of
PatternVariable v -> addPatternVar v ty (argTy ^. paramName) $> pat
PatternWildcardConstructor {} -> impossible
PatternWildcardConstructor c -> checkWildcardConstructor pat ty c
PatternConstructorApp a -> goPatternConstructor pat ty a
return (set patternArgPattern pat' patArg)
where
checkWildcardConstructor :: Pattern -> Expression -> WildcardConstructor -> Sem r Pattern
checkWildcardConstructor pat ty w = do
let c = w ^. wildcardConstructor
numArgs <- length . constructorArgs . (^. constructorInfoType) <$> lookupConstructor c
holeArgs <- replicateM numArgs (genWildcard (getLoc w) Explicit)
let capp =
ConstructorApp
{ _constrAppConstructor = c,
_constrAppParameters = holeArgs,
_constrAppType = Nothing
}
goPatternConstructor pat ty capp

goPatternConstructor :: Pattern -> Expression -> ConstructorApp -> Sem r Pattern
goPatternConstructor pat ty a = do
s <- checkSaturatedInductive ty
Expand Down
4 changes: 4 additions & 0 deletions test/Typecheck/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ tests =
"Import a .juvix.md module in a .juvix.md file"
$(mkRelDir "MarkdownImport")
$(mkRelFile "C.juvix.md"),
posTest
"Typecheck constructor wildcard"
$(mkRelDir ".")
$(mkRelFile "ConstructorWildcard.juvix"),
posTestAbsDir
"Typecheck orphan file"
(relToProject $(mkRelDir "tests/WithoutPackageFile"))
Expand Down
16 changes: 16 additions & 0 deletions tests/positive/ConstructorWildcard.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module ConstructorWildcard;

type Bool :=
false | true;

type either (A B : Type) :=
left A
| right B;

isLeft {A B} : either A B → Bool
| left@{} := true
| right@{} := false;

not : Bool → Bool
| false@{} := true
| true := false;

0 comments on commit 4fa1735

Please sign in to comment.