Set fresh name counter on loading Main ttc

Make sure this is the counter from the right namespace, or we might get
name clashes at the REPL
This commit is contained in:
Edwin Brady 2019-06-27 13:42:49 +01:00
parent ce78abaaef
commit d053a18977
32 changed files with 392 additions and 12 deletions

View File

@ -305,7 +305,7 @@ instantiate : {auto c : Ref Ctxt Defs} ->
Term newvars -> -- shrunk environment
Core ()
instantiate {newvars} loc env mname mref mdef locs otm tm
= do log 5 $ "Instantiating " ++ show tm ++ " in " ++ show newvars
= do logTerm 5 ("Instantiating in " ++ show newvars) tm
-- let Hole _ _ = definition mdef
-- | def => ufail {a=()} loc (show mname ++ " already resolved as " ++ show def)
case fullname mdef of

View File

@ -89,6 +89,13 @@ initUState = MkUState empty empty empty empty empty [] 0 0 []
export
data UST : Type where
export
resetNextVar : {auto u : Ref UST UState} ->
Core ()
resetNextVar
= do ust <- get UST
put UST (record { nextName = 0 } ust)
-- Generate a global name based on the given root, in the current namespace
export
genName : {auto c : Ref Ctxt Defs} ->
@ -354,6 +361,7 @@ newMeta {vars} fc rig env n ty nocyc
(newDef fc n rig [] hty Public (Hole (length env) False))
log 5 $ "Adding new meta " ++ show (n, fc, rig)
logTerm 10 ("New meta type " ++ show n) hty
defs <- get Ctxt
idx <- addDef n hole
addHoleName fc n idx
pure (idx, Meta fc n idx envArgs)
@ -385,6 +393,8 @@ newConstant {vars} fc rig env tm ty constrs
let defty = abstractEnvType fc env ty
cn <- genName "postpone"
let guess = newDef fc cn rig [] defty Public (Guess def constrs)
log 5 $ "Adding new constant " ++ show (cn, fc, rig)
logTerm 10 ("New constant type " ++ show cn) defty
idx <- addDef cn guess
addGuessName fc cn idx
pure (Meta fc cn idx envArgs)

View File

@ -184,11 +184,3 @@ main = do Right opts <- getCmdOpts
exit 1)
(\res => pure ())
else pure ()
locMain : List CLOpt -> IO ()
locMain opts = coreRun (stMain opts)
(\err : Error =>
do putStrLn ("Uncaught error: " ++ show err)
exit 1)
(\res => pure ())

View File

@ -193,6 +193,7 @@ buildDeps fname
mainttc <- getTTCFileName fname ".ttc"
log 10 $ "Reloading " ++ show mainttc
readAsMain mainttc
-- Load the associated metadata for interactive editing
mainttm <- getTTCFileName fname ".ttm"
log 10 $ "Reloading " ++ show mainttm

View File

@ -122,11 +122,21 @@ readAsMain fname
| Nothing => throw (InternalError "Already loaded")
replNS <- getNS
extendAs replNS replNS syn
ustm <- get UST
traverse (\ mimp =>
do let m = fst mimp
let as = snd (snd mimp)
fname <- nsToPath emptyFC m
readModule False emptyFC True False m as) more
-- We're in the namespace from the first TTC, so use the next name
-- from that for the fresh metavariable name generation
-- TODO: Maybe we should record this per namespace, since this is
-- a little bit of a hack? Or maybe that will have too much overhead.
ust <- get UST
put UST (record { nextName = nextName ustm } ust)
setNS replNS
addPrelude : List Import -> List Import
@ -241,6 +251,7 @@ process buildmsg file
-- but may fail for other reasons, so we still need to catch
-- other possible errors
catch (do initHash
resetNextVar
fn <- getTTCFileName file ".ttc"
Just errs <- logTime ("Elaborating " ++ file) $
processMod file fn buildmsg mod res

View File

@ -549,7 +549,8 @@ processCatch cmd
u' <- get UST
s' <- get Syn
o' <- get ROpts
catch (do r <- process cmd
catch (do ust <- get UST
r <- process cmd
commit
pure r)
(\err => do put Ctxt c'

View File

@ -447,7 +447,7 @@ checkApp rig elabinfo nest env fc (IVar fc' n) expargs impargs exp
etynf <- normaliseHoles defs env ety
pure (Just !(toFullNames etynf)))
exp
pure ("Checking application of " ++ show n ++
pure ("Checking application of " ++ show !(getFullName n) ++
" to " ++ show expargs ++ "\n\tFunction type " ++
(show !(toFullNames fnty)) ++ "\n\tExpected app type "
++ show exptyt))

View File

@ -57,6 +57,8 @@ yaffleMain fname args
do makeBuildDirectory (pathToNS (working_dir d) fname)
writeToTTC () !(getTTCFileName fname ".ttc")
coreLift $ putStrLn "Written TTC"
ust <- get UST
repl {c} {u}
{-

View File

@ -23,7 +23,8 @@ ttimpTests
idrisTests : List String
idrisTests
= ["basic001", "basic002", "basic003",
= ["basic001", "basic002", "basic003", "basic004", "basic005",
"basic006", "basic007",
"coverage001", "coverage002",
"error001", "error002", "error003", "error004", "error005",
"error006",

View File

@ -0,0 +1,45 @@
-- a mini prelude
module Stuff
public export
data Bool = True | False
public export
not : Bool -> Bool
not True = False
not False = True
public export
data Maybe a = Nothing | Just a
public export
intToBool : Int -> Bool
intToBool 0 = False
intToBool x = True
public export
ifThenElse : Bool -> Lazy a -> Lazy a -> a
ifThenElse True t e = t
ifThenElse False t e = e
public export
data Nat = Z | S Nat
public export
fromInteger : Integer -> Nat
fromInteger x = ifThenElse (intToBool (prim__eq_Integer x 0))
Z (S (fromInteger (prim__sub_Integer x 1)))
public export
plus : Nat -> Nat -> Nat
plus Z y = y
plus (S k) y = S (plus k y)
infixr 5 ::
public export
data List a = Nil | (::) a (List a)
data Eq : a -> b -> Type where
Refl : (x : a) -> Eq x x

View File

@ -0,0 +1,20 @@
module Wheres
import Stuff
reverse : List a -> List a
reverse xs = rev' Nil xs
where
rev' : List a -> List a -> List a
rev' acc Nil = acc
rev' acc (x :: xs) = rev' (x :: acc) xs
foo : Int -> Int
foo x = case isLT of
Yes => prim__mul_Int x 2
No => prim__mul_Int x 4
where
data MyLT = Yes | No
isLT : MyLT
isLT = ifThenElse (intToBool (prim__lt_Int x 20)) Yes No

View File

@ -0,0 +1,7 @@
1/2: Building Stuff (Stuff.idr)
2/2: Building Wheres (Wheres.idr)
Welcome to Idris 2 version 0.0. Enjoy yourself!
Wheres> [3, 2, 1]
Wheres> 8
Wheres> 84
Wheres> Bye for now!

View File

@ -0,0 +1,5 @@
reverse (1 :: 2 :: 3 :: Nil)
foo 4
foo 21
:q

3
tests/idris2/basic004/run Executable file
View File

@ -0,0 +1,3 @@
$1 --no-prelude Wheres.idr < input
rm -rf build

View File

@ -0,0 +1,2 @@
foo : ? -> Int
foo x = 42

View File

@ -0,0 +1,6 @@
1/1: Building NoInfer (NoInfer.idr)
NoInfer.idr:1:7--1:9:Unsolved holes:
Main.{_:1} introduced at NoInfer.idr:1:7--1:9
Welcome to Idris 2 version 0.0. Enjoy yourself!
Main> Bye for now!

3
tests/idris2/basic005/run Executable file
View File

@ -0,0 +1,3 @@
echo ':q' | $1 --no-prelude NoInfer.idr
rm -rf build

View File

@ -0,0 +1,12 @@
import Stuff
foo : Int -> Maybe Int
foo 0 = Nothing
foo x = Just (prim__mul_Int x 2)
testLet : Int -> Int
testLet x = let y = prim__mul_Int x 2
Just ans = foo y
| Nothing => 99
z = prim__mul_Int ans 2
in z

View File

@ -0,0 +1,61 @@
-- a mini prelude
module Stuff
public export
data Bool = True | False
public export
not : Bool -> Bool
not True = False
not False = True
public export
data Maybe a = Nothing | Just a
public export
intToBool : Int -> Bool
intToBool 0 = False
intToBool x = True
public export
ifThenElse : Bool -> Lazy a -> Lazy a -> a
ifThenElse True t e = t
ifThenElse False t e = e
public export
data Nat = Z | S Nat
public export
fromInteger : Integer -> Nat
fromInteger x = ifThenElse (intToBool (prim__eq_Integer x 0))
Z (S (fromInteger (prim__sub_Integer x 1)))
public export
plus : Nat -> Nat -> Nat
plus Z y = y
plus (S k) y = S (plus k y)
infixr 5 ::
public export
data List a = Nil | (::) a (List a)
public export
data Equal : a -> b -> Type where
Refl : (0 x : a) -> Equal x x
public export
data Unit = MkUnit
public export
data Pair : Type -> Type -> Type where
MkPair : {0 a, b : Type} -> (1 x : a) -> (1 y : b) -> Pair a b
public export
data Unrestricted : Type -> Type where
Un : (x : a) -> Unrestricted a
public export
the : (a : Type) -> a -> a
the _ x = x

View File

@ -0,0 +1,6 @@
1/2: Building Stuff (Stuff.idr)
2/2: Building PMLet (PMLet.idr)
Welcome to Idris 2 version 0.0. Enjoy yourself!
Main> 99
Main> 64
Main> Bye for now!

View File

@ -0,0 +1,3 @@
testLet 0
testLet 8
:q

3
tests/idris2/basic006/run Executable file
View File

@ -0,0 +1,3 @@
$1 --no-prelude PMLet.idr < input
rm -rf build

View File

@ -0,0 +1,15 @@
import Stuff
(>>=) : (Maybe a) -> (a -> Maybe b) -> Maybe b
(>>=) Nothing k = Nothing
(>>=) (Just x) k = k x
mthings : (foo : Maybe (Maybe Int)) -> (bar : Maybe (Maybe Int)) -> Maybe Int
mthings mx my
= do Just x <- mx
| Nothing => Just 0
Just y <- my
| Nothing => Just 1
let z : Int -> Int
z x = prim__add_Int x y
Just (z (prim__add_Int x x))

View File

@ -0,0 +1,61 @@
-- a mini prelude
module Stuff
public export
data Bool = True | False
public export
not : Bool -> Bool
not True = False
not False = True
public export
data Maybe a = Nothing | Just a
public export
intToBool : Int -> Bool
intToBool 0 = False
intToBool x = True
public export
ifThenElse : Bool -> Lazy a -> Lazy a -> a
ifThenElse True t e = t
ifThenElse False t e = e
public export
data Nat = Z | S Nat
public export
fromInteger : Integer -> Nat
fromInteger x = ifThenElse (intToBool (prim__eq_Integer x 0))
Z (S (fromInteger (prim__sub_Integer x 1)))
public export
plus : Nat -> Nat -> Nat
plus Z y = y
plus (S k) y = S (plus k y)
infixr 5 ::
public export
data List a = Nil | (::) a (List a)
public export
data Equal : a -> b -> Type where
Refl : (0 x : a) -> Equal x x
public export
data Unit = MkUnit
public export
data Pair : Type -> Type -> Type where
MkPair : {0 a, b : Type} -> (1 x : a) -> (1 y : b) -> Pair a b
public export
data Unrestricted : Type -> Type where
Un : (x : a) -> Unrestricted a
public export
the : (a : Type) -> a -> a
the _ x = x

View File

@ -0,0 +1,9 @@
1/2: Building Stuff (Stuff.idr)
2/2: Building DoLocal (DoLocal.idr)
Welcome to Idris 2 version 0.0. Enjoy yourself!
Main> Just 1
Main> Just 0
Main> Just 94
Main> Nothing
Main> Nothing
Main> Bye for now!

View File

@ -0,0 +1,6 @@
mthings (Just (Just 20)) (Just Nothing)
mthings (Just Nothing) (Just (Just 54))
mthings (Just (Just 20)) (Just (Just 54))
mthings (Just (Just 20)) Nothing
mthings Nothing (Just (Just 54))
:q

3
tests/idris2/basic007/run Executable file
View File

@ -0,0 +1,3 @@
$1 --no-prelude DoLocal.idr < input
rm -rf build

View File

@ -0,0 +1,18 @@
data Bool = False | True
data Nat = Z | S Nat
not : Bool -> Bool
not False = True
not True = False
isZero : Nat -> Bool
isZero Z = True
isZero (S k) = False
isOdd : Nat -> Bool
isOdd Z = False
isOdd (S k) = not (isOdd k)
testZ : Nat -> String
testZ x = if isZero x then "Zero" else
if isOdd x then "Odd" else "Even"

View File

@ -0,0 +1,61 @@
-- a mini prelude
module Stuff
public export
data Bool = True | False
public export
not : Bool -> Bool
not True = False
not False = True
public export
data Maybe a = Nothing | Just a
public export
intToBool : Int -> Bool
intToBool 0 = False
intToBool x = True
public export
ifThenElse : Bool -> Lazy a -> Lazy a -> a
ifThenElse True t e = t
ifThenElse False t e = e
public export
data Nat = Z | S Nat
public export
fromInteger : Integer -> Nat
fromInteger x = ifThenElse (intToBool (prim__eq_Integer x 0))
Z (S (fromInteger (prim__sub_Integer x 1)))
public export
plus : Nat -> Nat -> Nat
plus Z y = y
plus (S k) y = S (plus k y)
infixr 5 ::
public export
data List a = Nil | (::) a (List a)
public export
data Equal : a -> b -> Type where
Refl : (0 x : a) -> Equal x x
public export
data Unit = MkUnit
public export
data Pair : Type -> Type -> Type where
MkPair : {0 a, b : Type} -> (1 x : a) -> (1 y : b) -> Pair a b
public export
data Unrestricted : Type -> Type where
Un : (x : a) -> Unrestricted a
public export
the : (a : Type) -> a -> a
the _ x = x

View File

@ -0,0 +1,6 @@
1/1: Building If (If.idr)
Welcome to Idris 2 version 0.0. Enjoy yourself!
Main> "Zero"
Main> "Odd"
Main> "Even"
Main> Bye for now!

View File

@ -0,0 +1,4 @@
testZ Z
testZ (S (S (S Z)))
testZ (S (S (S (S Z))))
:q

3
tests/idris2/basic008/run Executable file
View File

@ -0,0 +1,3 @@
$1 --no-prelude If.idr < input
rm -rf build