@@ -16,6 +16,7 @@ import Idris.Core.Evaluate
16import Idris.Core.Unify
16import Idris.Core.Unify
17import Idris.Core.Typecheck (check)
17import Idris.Core.Typecheck (check)
18import Idris.ErrReverse (errReverse)
18import Idris.ErrReverse (errReverse)
19+import Idris.ElabQuasiquote (extractUnquotes)
20import Control.Applicative ((<$>))
21import Control.Applicative ((<$>))
21import Control.Monad
22import Control.Monad
@@ -630,11 +631,22 @@ elab ist info pattern opts fn tm
630 elab' ina t
631 elab' ina t
631 unifyLog False
632 unifyLog False
632 elab' ina (PQuasiquote t)
633 elab' ina (PQuasiquote t)
634+ = do -- First elaborate the unquoted subterms, replacing them with
635+ -- fresh names in the quasiquoted term
636+ (t, unq) <- extractUnquotes t
637+ mapM_ elabUnquote unq
638+ let unquoteNames = map fst unq
633− = do -- Save the old state - we need a fresh proof state for this to
640+ -- Save the old state - we need a fresh proof state for this to
634 -- avoid capture
641 -- avoid capture
635 ctxt <- get_context
642 ctxt <- get_context
636 saveState
643 saveState
645+ newProof (sMN 0 "q") ctxt $
637− updatePS (const $ newProof (sMN 0 "q") ctxt (P Ref (sNS (sUN "TT") ["Reflection", "Language"]) Erased))
646+ P Ref (sNS (sUN "TT") ["Reflection", "Language"]) Erased)
648+ -- Re-add the unquotes
649+ mapM_ (flip claim (Var (sNS (sUN "TT") ["Reflection", "Language"]))) unquoteNames
639 -- Establish holes for the type and value of the term to be
651 -- Establish holes for the type and value of the term to be
640 -- quasiquoted
652 -- quasiquoted
@@ -654,14 +666,20 @@ elab ist info pattern opts fn tm
654 -- We now have an elaborated term. Reflect it and solve the
666 -- We now have an elaborated term. Reflect it and solve the
655 -- original goal
667 -- original goal
656 env <- get_env
668 env <- get_env
657− let qTerm = fmap (reflect . binderVal) $ lookup nTm env
669+ let qTerm = fmap (reflectQuote unquoteNames . binderVal) $ lookup nTm env
658 case qTerm of
670 case qTerm of
659 Just q -> do loadState
671 Just q -> do loadState
662 Nothing -> fail "Broken elaboration of quasiquote"
674 Nothing -> fail "Broken elaboration of quasiquote"
663−
675+ where elabUnquote (n, tm)
676+ = do qn <- getNameFrom (sMN 0 "unquotHole")
677+ let tt = sNS (sUN "TT") ["Reflection", "Language"]
679+ letbind n (Var tt) (Var qn)
664− elab' ina (PUnquote t) = fail "Elaboration of unquotes not yet implemented"
682+ elab' ina (PUnquote t) = fail "Found unquote outside of quasiquote"
665 elab' ina x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
683 elab' ina x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
667 isScr :: PTerm -> (Name, Binder Term) -> (Name, (Bool, Binder Term))
685 isScr :: PTerm -> (Name, Binder Term) -> (Name, (Bool, Binder Term))
@@ -1474,21 +1492,26 @@ reflCall funName args
1475-- | Lift a term into its Language.Reflection.TT representation
1493-- | Lift a term into its Language.Reflection.TT representation
1476reflect :: Term -> Raw
1494reflect :: Term -> Raw
1477−reflect (P nt n t)
1495+reflect = reflectQuote []
1497+-- | Create a reflected term, but leave refs to the provided name intact
1498+reflectQuote :: [Name] -> Term -> Raw
1499+reflectQuote unq (P nt n t)
1500+ | n `elem` unq = Var n
1478− = reflCall "P" [reflectNameType nt, reflectName n, reflect t]
1501+ | otherwise = reflCall "P" [reflectNameType nt, reflectName n, reflectQuote unq t]
1479−reflect (V n)
1502+reflectQuote unq (V n)
1480 = reflCall "V" [RConstant (I n)]
1503 = reflCall "V" [RConstant (I n)]
1481−reflect (Bind n b x)
1504+reflectQuote unq (Bind n b x)
1482− = reflCall "Bind" [reflectName n, reflectBinder b, reflect x]
1505+ = reflCall "Bind" [reflectName n, reflectBinder b, reflectQuote unq x]
1483−reflect (App f x)
1506+reflectQuote unq (App f x)
1484− = reflCall "App" [reflect f, reflect x]
1507+ = reflCall "App" [reflectQuote unq f, reflectQuote unq x]
1485−reflect (Constant c)
1508+reflectQuote unq (Constant c)
1486 = reflCall "TConst" [reflectConstant c]
1509 = reflCall "TConst" [reflectConstant c]
1487−reflect (Proj t i)
1510+reflectQuote unq (Proj t i)
1488− = reflCall "Proj" [reflect t, RConstant (I i)]
1511+ = reflCall "Proj" [reflectQuote unq t, RConstant (I i)]
1489−reflect (Erased) = Var (reflm "Erased")
1512+reflectQuote unq (Erased) = Var (reflm "Erased")
1490−reflect (Impossible) = Var (reflm "Impossible")
1513+reflectQuote unq (Impossible) = Var (reflm "Impossible")
1491−reflect (TType exp) = reflCall "TType" [reflectUExp exp]
1514+reflectQuote unq (TType exp) = reflCall "TType" [reflectUExp exp]
1493reflectNameType :: NameType -> Raw
1516reflectNameType :: NameType -> Raw
1494reflectNameType (Bound) = Var (reflm "Bound")
1517reflectNameType (Bound) = Var (reflm "Bound")
@@ -1516,24 +1539,27 @@ reflectName (NErased) = Var (reflm "NErased")
1516reflectName n = Var (reflm "NErased") -- special name, not yet implemented
1539reflectName n = Var (reflm "NErased") -- special name, not yet implemented
1518reflectBinder :: Binder Term -> Raw
1541reflectBinder :: Binder Term -> Raw
1519−reflectBinder (Lam t)
1542+reflectBinder = reflectBinderQuote []
1544+reflectBinderQuote :: [Name] -> Binder Term -> Raw
1545+reflectBinderQuote unq (Lam t)
1520− = reflCall "Lam" [Var (reflm "TT"), reflect t]
1546+ = reflCall "Lam" [Var (reflm "TT"), reflectQuote unq t]
1521−reflectBinder (Pi t)
1547+reflectBinderQuote unq (Pi t)
1522− = reflCall "Pi" [Var (reflm "TT"), reflect t]
1548+ = reflCall "Pi" [Var (reflm "TT"), reflectQuote unq t]
1523−reflectBinder (Let x y)
1549+reflectBinderQuote unq (Let x y)
1524− = reflCall "Let" [Var (reflm "TT"), reflect x, reflect y]
1550+ = reflCall "Let" [Var (reflm "TT"), reflectQuote unq x, reflectQuote unq y]
1525−reflectBinder (NLet x y)
1551+reflectBinderQuote unq (NLet x y)
1526− = reflCall "NLet" [Var (reflm "TT"), reflect x, reflect y]
1552+ = reflCall "NLet" [Var (reflm "TT"), reflectQuote unq x, reflectQuote unq y]
1527−reflectBinder (Hole t)
1553+reflectBinderQuote unq (Hole t)
1528− = reflCall "Hole" [Var (reflm "TT"), reflect t]
1554+ = reflCall "Hole" [Var (reflm "TT"), reflectQuote unq t]
1529−reflectBinder (GHole _ t)
1555+reflectBinderQuote unq (GHole _ t)
1530− = reflCall "GHole" [Var (reflm "TT"), reflect t]
1556+ = reflCall "GHole" [Var (reflm "TT"), reflectQuote unq t]
1531−reflectBinder (Guess x y)
1557+reflectBinderQuote unq (Guess x y)
1532− = reflCall "Guess" [Var (reflm "TT"), reflect x, reflect y]
1558+ = reflCall "Guess" [Var (reflm "TT"), reflectQuote unq x, reflectQuote unq y]
1533−reflectBinder (PVar t)
1559+reflectBinderQuote unq (PVar t)
1534− = reflCall "PVar" [Var (reflm "TT"), reflect t]
1560+ = reflCall "PVar" [Var (reflm "TT"), reflectQuote unq t]
1535−reflectBinder (PVTy t)
1561+reflectBinderQuote unq (PVTy t)
1536− = reflCall "PVTy" [Var (reflm "TT"), reflect t]
1562+ = reflCall "PVTy" [Var (reflm "TT"), reflectQuote unq t]
1538reflectConstant :: Const -> Raw
1564reflectConstant :: Const -> Raw
1539reflectConstant c@(I _) = reflCall "I" [RConstant c]
1565reflectConstant c@(I _) = reflCall "I" [RConstant c]