forked from idris-lang/Idris2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ elab ] Add an ability to inspect in which function we currently are
- Loading branch information
Showing
12 changed files
with
384 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,37 @@ | ||
module CurrFn | ||
|
||
import Language.Reflection | ||
|
||
%default total | ||
|
||
%language ElabReflection | ||
|
||
logCurrFn : Elab () | ||
logCurrFn = logMsg "elab" 0 "current fn: \{show !getCurrentFn}" | ||
|
||
%runElab logCurrFn | ||
|
||
f : Nat -> Nat | ||
f x = case %runElab logCurrFn of () => x + 4 | ||
|
||
f' : Nat -> Nat | ||
f' x = x + case %runElab logCurrFn of () => 4 | ||
|
||
f'' : Nat -> Nat | ||
f'' Z = 4 | ||
f'' (S x) = f $ case %runElab logCurrFn of () => x | ||
f''' : Nat -> Nat | ||
f''' x = case x of | ||
Z => 4 | ||
S x => f $ case %runElab logCurrFn of () => x | ||
|
||
n : Nat -> Nat | ||
n x = x + f x where | ||
f : Nat -> Nat | ||
f x = case %runElab logCurrFn of () => x + 4 | ||
|
||
w : Nat -> Nat | ||
w x with (x + 5) | ||
_ | Z = x + case %runElab logCurrFn of () => 4 | ||
_ | S n = f $ case %runElab logCurrFn of () => n |
This file contains 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,61 @@ | ||
module InspectRec | ||
|
||
import Control.Monad.Writer | ||
|
||
import Language.Reflection | ||
|
||
%default total | ||
|
||
%language ElabReflection | ||
|
||
isFunc : NameInfo -> Bool | ||
isFunc $ MkNameInfo Func = True | ||
isFunc _ = False | ||
|
||
inspectRec : Elaboration m => (desc : String) -> a -> m a | ||
inspectRec desc val = do | ||
expr <- quote val | ||
fns <- getCurrentFn | ||
let _ : Monoid Bool := Any | ||
rec <- execWriterT $ flip mapMTTImp expr $ \case | ||
e@(IVar fc nm) => do | ||
is <- getInfo nm | ||
let is = fst <$> filter (isFunc . snd) is | ||
refs <- map join $ for is $ \n => (n::) <$> getReferredFns n | ||
tell $ any (\fn => any (==fn) refs) fns | ||
pure e | ||
e => pure e | ||
logMsg "elab" 0 "case \{desc}: \{the String $ if rec then "recursive" else "non-recursive"}" | ||
pure val | ||
|
||
---------------------------------------------- | ||
|
||
dec : Nat -> Nat | ||
dec Z = %runElab inspectRec "zero case" 0 | ||
dec (S n) = %runElab inspectRec "non-zero case" n | ||
|
||
---------------------------------------------- | ||
|
||
simpleRec : Nat -> Nat | ||
simpleRec Z = %runElab inspectRec "base case" 10 | ||
simpleRec (S n) = %runElab inspectRec "next cast" $ simpleRec n | ||
|
||
---------------------------------------------- | ||
|
||
mutualRec1 : Nat -> Nat | ||
mutualRec2 : Nat -> Nat | ||
|
||
mutualRec1 Z = %runElab inspectRec "mutual rec 1, base" 11 | ||
mutualRec1 (S n) = %runElab inspectRec "mutual rec 1, next" $ mutualRec2 n | ||
|
||
mutualRec2 Z = %runElab inspectRec "mutual rec 2, base" 12 | ||
mutualRec2 (S n) = %runElab inspectRec "mutual rec 2, next" $ mutualRec1 n | ||
|
||
---------------------------------------------- | ||
|
||
nestedMutRec1 : Nat -> Nat | ||
nestedMutRec1 Z = %runElab inspectRec "nestedMut rec 1, base" 11 | ||
nestedMutRec1 (S n) = %runElab inspectRec "nestedMut rec 1, next" $ nestedMutRec2 n where | ||
nestedMutRec2 : Nat -> Nat | ||
nestedMutRec2 Z = %runElab inspectRec "nestedMut rec 2, base" 12 | ||
nestedMutRec2 (S n) = %runElab inspectRec "nestedMut rec 2, next" $ nestedMutRec1 n |
This file contains 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 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,51 @@ | ||
module RefDefsDeep | ||
|
||
import Language.Reflection | ||
|
||
import RefDefs | ||
|
||
%default total | ||
|
||
%language ElabReflection | ||
|
||
logCurrFn : Elab () | ||
logCurrFn = do | ||
currFn <- getCurrentFn | ||
logMsg "elab" 0 "=== current fn: \{show currFn} ===" | ||
|
||
%runElab logCurrFn | ||
|
||
f : Nat -> Nat | ||
f x = case %runElab logCurrFn of () => x + 4 | ||
|
||
f' : Nat -> Nat | ||
f' x = x + case %runElab logCurrFn of () => 4 | ||
|
||
f'' : Nat -> Nat | ||
f'' Z = 4 | ||
f'' (S x) = f $ case %runElab logCurrFn of () => x | ||
f''' : Nat -> Nat | ||
f''' x = case x of | ||
Z => 4 | ||
S x => f $ case %runElab logCurrFn of () => x | ||
|
||
n : Nat -> Nat | ||
n x = x + f x where | ||
f : Nat -> Nat | ||
f x = case %runElab logCurrFn of () => x + 4 | ||
|
||
w : Nat -> Nat | ||
w x with (x + 5) | ||
_ | Z = x + case %runElab logCurrFn of () => 4 | ||
_ | S n = f $ case %runElab logCurrFn of () => n | ||
|
||
%runElab | ||
traverse_ printRefDefs | ||
[ `{f} | ||
, `{f'} | ||
, `{f''} | ||
, `{f'''} | ||
, `{n} | ||
, `{w} | ||
] |
Oops, something went wrong.