@@ -12,18 +12,30 @@ import Idris.Core.TT
1212
1313import Control.Applicative
1414import Control.Monad.State.Strict
15−import Data.Generics.Uniplate.Data (transformM)
15+import Data.Generics.Uniplate.Data (transformM, universe)
1616
1717-- | Desugar by changing x@y on lhs to let x = y in ... or rhs
18−desugarAs :: PTerm -> PTerm -> (PTerm, PTerm)
19−desugarAs lhs rhs
20− = let (lhs', pats) = runState (collectAs (replaceUnderscore lhs)) [] in
21− (lhs', bindPats pats rhs)
18+desugarAs :: PTerm -> PTerm -> [PDecl] -> (PTerm, PTerm, [PDecl])
19+desugarAs lhs rhs whereblock
20+ = (lhs', bindOnRight rhs pats, map (bindInWhereDecl pats) whereblock)
2221 where
23− bindPats :: [(Name, FC, PTerm)] -> PTerm -> PTerm
24− bindPats [] rhs = rhs
25− bindPats ((n, fc, tm) : ps) rhs
26− = PLet fc n NoFC Placeholder tm (bindPats ps rhs)
22+ (lhs', pats) = runState (collectAs (replaceUnderscore lhs)) []
23+
24+ bindOnRight :: PTerm -> [(Name, FC, PTerm)] -> PTerm
25+ bindOnRight = foldr (\(n, fc, tm) -> PLet fc n NoFC Placeholder tm)
26+
27+ bindInWhereDecl :: [(Name, FC, PTerm)] -> PDecl -> PDecl
28+ bindInWhereDecl pats (PClauses fc opts n clauses)
29+ = PClauses fc opts n $ map (bindInWhereClause pats) clauses
30+ bindInWhereDecl _ d = d
31+
32+ bindInWhereClause :: [(Name, FC, PTerm)] -> PClause -> PClause
33+ bindInWhereClause pats (PClause fc n lhs ws rhs wb)
34+ = let bound = [ n | (PRef _ _ n) <- universe lhs ]
35+ pats' = filter (not . (`elem` bound) . \(n,_,_) -> n) pats
36+ rhs' = bindOnRight rhs pats' in
37+ PClause fc n lhs ws rhs' $ map (bindInWhereDecl pats') wb
38+ bindInWhereClause _ c = c
2739
2840collectAs :: PTerm -> State [(Name, FC, PTerm)] PTerm
2941collectAs (PAs fc n tm) = do tm' <- collectAs tm