-
Notifications
You must be signed in to change notification settings - Fork 403
[ new ] Dot inferred patterns in erased functions #3722
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
Open
spcfox
wants to merge
12
commits into
idris-lang:main
Choose a base branch
from
spcfox:dot-inferred
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8120733
[ test ] Add test for erased function with bad case tree
spcfox d6f0bab
[ new ] Dot inferred patterns in erased functions
spcfox dd8cec9
[ fix ] Support `{}` in dotting
spcfox bd9d7c4
[ new ] Recursive dottting algorithm
spcfox 7a9c913
[ fix ] Apply dotting to primitive functions
spcfox bc98d56
[ fix ] Normalise types in `TTImp.ProcessDef.Dot`
spcfox 02ef1b3
[ refactor ] Refactor the `TTImp.ProcessDef.Dot` module
spcfox 93f3339
[ fix ] Support matching on arrow types in dotting
spcfox 5e8e9f8
[ fix ] Support @-pattern in dotting
spcfox e14492e
[ fix ] Support `IAlternative` in dotting
spcfox 9aea37d
[ fix ] Match on erased arguments in erased fragment
spcfox eb44711
[ admin ] Update CHANGELOG
spcfox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| module TTImp.ProcessDef.Dot | ||
|
|
||
| import Core.Context | ||
| import Core.Env | ||
| import Core.Normalise | ||
| import Core.Value | ||
|
|
||
| import TTImp.TTImp | ||
| import TTImp.Elab.App | ||
|
|
||
| import Data.DPair | ||
| import Data.SnocList | ||
|
|
||
| isPattern : Term vars -> Bool | ||
| isPattern (App {}) = True | ||
| isPattern (Ref {}) = True | ||
| isPattern (TDelay {}) = True | ||
| isPattern (PrimVal {}) = True | ||
| isPattern (TType {}) = True | ||
| isPattern (Bind _ _ (Pi {}) _) = True | ||
| isPattern _ = False | ||
|
|
||
| isImplicit : RawImp -> Bool | ||
| isImplicit (Implicit {}) = True | ||
| isImplicit (IAs _ _ _ _ tm) = isImplicit tm | ||
| isImplicit _ = False | ||
|
|
||
| dot : FC -> Term vars -> Term vars | ||
| dot _ tm@(Erased {}) = tm | ||
| dot fc tm = if isPattern tm | ||
| then Erased fc $ Dotted tm | ||
| else tm | ||
|
|
||
| parameters {auto c : Ref Ctxt Defs} {vars : Scope} | ||
| (defs : Defs) (nest : NestedNames vars) (env : Env Term vars) | ||
| export | ||
| dotInferred : (topLevel : Bool) -> | ||
| RawImp -> | ||
| Term vars -> | ||
| Core (Term vars) | ||
|
|
||
| dotIfInferred : FC -> RawImp -> Term vars -> | ||
| Core (Term vars) | ||
| dotIfInferred fc (IAs _ _ _ _ rawArg) (As fc' s n arg) | ||
| = As fc' s n <$> dotIfInferred fc rawArg arg | ||
| dotIfInferred fc rawArg arg | ||
| = if isImplicit rawArg | ||
| then pure $ dot fc arg | ||
| else dotInferred False rawArg arg | ||
|
|
||
| addDots : (acc : SnocList (FC, Term vars)) -> | ||
| (ty : NF vars) -> | ||
| (args : List (FC, Term vars)) -> | ||
| (expargs : List RawImp) -> | ||
| (autoargs : List RawImp) -> | ||
| (namedargs : List (Name, RawImp)) -> | ||
| Core (List (FC, Term vars)) | ||
| addDots acc (NBind _ n (Pi _ _ Explicit _) sc) ((fc, arg) :: args) (rawArg :: exps) autos named | ||
| = addDots (acc :< (fc, !(dotIfInferred fc rawArg arg))) | ||
| !(sc defs $ toClosure defaultOpts env arg) | ||
| args exps autos named | ||
| addDots acc (NBind _ n (Pi _ _ AutoImplicit _) sc) ((fc, arg) :: args) exps (rawArg :: autos) named | ||
| = addDots (acc :< (fc, !(dotIfInferred fc rawArg arg))) | ||
| !(sc defs $ toClosure defaultOpts env arg) | ||
| args exps autos named | ||
| addDots acc (NBind _ n (Pi _ _ Explicit _) sc) ((fc, arg) :: args) [] autos named | ||
| = do (arg', named') <- case findNamed n named of | ||
| Nothing => case findBindAllExpPattern named of | ||
| Nothing => throw $ InternalError "Impossible happened: explicit argument not found." | ||
| Just rawArg => pure (!(dotIfInferred fc rawArg arg), named) | ||
| Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named') | ||
| addDots (acc :< (fc, arg')) | ||
| !(sc defs $ toClosure defaultOpts env arg') | ||
| args [] autos named' | ||
| addDots acc (NBind _ n (Pi _ _ AutoImplicit _) sc) ((fc, arg) :: args) exps [] named | ||
| = do (arg', named') <- case findNamed n named of | ||
| Nothing => pure (dot fc arg, named) | ||
| Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named') | ||
| addDots (acc :< (fc, arg')) | ||
| !(sc defs $ toClosure defaultOpts env arg') | ||
| args exps [] named' | ||
| addDots acc (NBind _ n (Pi _ _ Implicit _) sc) ((fc, arg) :: args) exps autos named | ||
| = do (arg', named') <- case findNamed n named of | ||
| Nothing => pure (dot fc arg, named) | ||
| Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named') | ||
| addDots (acc :< (fc, arg')) | ||
| !(sc defs $ toClosure defaultOpts env arg') | ||
| args exps autos named' | ||
| addDots acc (NBind _ n (Pi _ _ (DefImplicit defArg) _) sc) ((fc, arg) :: args) exps autos named | ||
| = do (arg', named') <- case findNamed n named of | ||
| Nothing => pure (dot fc arg, named) | ||
| Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named') | ||
| addDots (acc :< (fc, arg')) | ||
| !(sc defs $ toClosure defaultOpts env arg') | ||
| args exps autos named' | ||
| addDots acc ty [] [] [] named | ||
| = if null $ filter (not . isBindAllExpPattern . fst) named | ||
| then pure $ toList acc | ||
| else throw $ InternalError "Impossible happened: arguments mismatch." | ||
| addDots _ _ _ _ _ _ | ||
| = throw $ InternalError $ "Impossible happened: arguments mismatch." | ||
|
|
||
| skipArgs : Nat -> | ||
| (acc : SnocList (FC, Term vars)) -> | ||
| (args : List (FC, Term vars)) -> | ||
| (ty : NF vars) -> | ||
| Core (SnocList (FC, Term vars), List (FC, Term vars), NF vars) | ||
| skipArgs Z acc args ty = pure (acc, args, ty) | ||
| skipArgs (S n) acc ((fc, arg) :: args) (NBind _ _ (Pi {}) sc) | ||
| = skipArgs n (acc :< (fc, arg)) args !(sc defs $ toClosure defaultOpts env arg) | ||
| skipArgs _ _ __ | ||
| = throw $ InternalError "Impossible happened: expected nested argument." | ||
|
|
||
| dotInferred topLevel (IAlternative fc ty alts) tm | ||
| = do let (Ref _ _ nm) = getFn tm | ||
| | _ => pure tm | ||
| let Just alt = pickAlt !(toFullNames nm) alts | ||
| | _ => pure tm | ||
| dotInferred topLevel alt tm | ||
| where | ||
| pickAlt : Name -> List RawImp -> Maybe RawImp | ||
| pickAlt nm [] = Nothing | ||
| pickAlt nm (x :: xs) | ||
| = do let (IVar _ nm') = getFn x | ||
| | _ => Nothing | ||
| if nm == nm' | ||
| then Just x | ||
| else pickAlt nm xs | ||
| dotInferred topLevel raw tm = go raw [] [] [] | ||
| where | ||
| -- Don't dot primitives functions in argument position | ||
| needsDot : Name -> Core Bool | ||
| needsDot nm = if topLevel | ||
| then pure $ True | ||
| else pure $ not $ isPrimName !getPrimitiveNames nm | ||
|
|
||
| go : RawImp -> | ||
| (expargs : List RawImp) -> | ||
| (autoargs : List RawImp) -> | ||
| (namedargs : List (Name, RawImp)) -> | ||
| Core (Term vars) | ||
| go (IApp fc fn arg) exps autos named | ||
| = go fn (arg :: exps) autos named | ||
| go (IWithApp fc fn arg) exps autos named | ||
| = go fn (arg :: exps) autos named | ||
| go (IAutoApp fc fn arg) exps autos named | ||
| = go fn exps (arg :: autos) named | ||
| go (INamedApp fc fn nm arg) exps autos named | ||
| = go fn exps autos ((nm, arg) :: named) | ||
| go (IVar _ rawName) exps autos named | ||
| = do let (fn@(Ref fc t nm), args) = getFnArgsWithFC tm | ||
| | _ => pure tm | ||
| Just def <- lookupCtxtExact nm (gamma defs) | ||
| | Nothing => undefinedName fc nm | ||
| True <- needsDot rawName | ||
| | _ => pure tm | ||
| -- TODO: remove `the` after fix idris-lang/Idris2#3418 | ||
| let skip = maybe 0 (the (_ -> _) $ \(_, n, _) => length n) $ | ||
| lookup nm (names nest) <|> lookup rawName (names nest) | ||
| -- ^ Local block put raw name in `NestedNames` | ||
| -- while parameters put `Resolved`. | ||
| -- So, we need to check both | ||
| tynf <- nf defs env $ embed $ type def | ||
| (skipped, rest, ty') <- skipArgs skip [<] args tynf | ||
| args' <- addDots skipped ty' rest exps autos named | ||
| pure $ applyStackWithFC fn args' | ||
| go (IPi _ _ _ _ aty _) [] [] [] | ||
| = case tm of | ||
| Bind fc n (Pi bfc r p ty) sc | ||
| => pure $ Bind fc n (Pi bfc r p !(dotIfInferred bfc aty ty)) sc | ||
| _ => throw $ InternalError "Expected Pi type, got \{show tm}" | ||
| go (IPi _ _ _ _ aty _) exps autos named | ||
| = throw $ InternalError "Unexpected arguments Pi-type with arguments: \{show raw}" | ||
| go _ _ _ _ = pure tm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import Data.Vect | ||
|
|
||
| 0 foo : Vect n a -> Int | ||
| foo [x, y, z] = 1 | ||
| foo _ = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 1/1: Building Main (Main.idr) | ||
| Main> 0 Main.foo | ||
| Arguments [{arg:1}, {arg:2}, {arg:3}] | ||
| Compile time tree: case {arg:3} of | ||
| (::) {e:1} {e:2} {e:3} {e:4} => case {e:4} of | ||
| (::) {e:5} {e:6} {e:7} {e:8} => case {e:8} of | ||
| (::) {e:9} {e:10} {e:11} {e:12} => case {e:12} of | ||
| Nil {e:13} => 1 | ||
| _ => 2 | ||
| _ => 2 | ||
| _ => 2 | ||
| _ => 2 | ||
| Erasable args: [0, 1] | ||
| Inferrable args: [0, 1] | ||
| Flags: covering | ||
| Main> | ||
| Bye for now! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| :di foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| . ../../../testutils.sh | ||
|
|
||
| idris2 Main.idr < input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import Data.Vect | ||
|
|
||
| -- Check that `TTImp.ProcessDef.Dot` properly handles | ||
| -- functions in parameters and where clauses | ||
|
|
||
| parameters (str : String) | ||
| 0 foo : Vect n a -> Int | ||
| foo [x, y, z] = 1 | ||
| foo _ = 2 | ||
|
|
||
| bar : String -> () | ||
| bar str = () | ||
| where | ||
| -- We can't print case tree for nested function, | ||
| -- so we just check that this function compiles | ||
| 0 baz : Vect n a -> Int | ||
| baz [x, y, z] = 1 | ||
| baz _ = 2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't remove it in this PR because the coverage checker in
mainbranch will reject this function.