@@ -97,7 +97,7 @@ elabVal info tm
97 tclift $ recheck ctxt [] vtm
97 tclift $ recheck ctxt [] vtm
99elabClause :: ElabInfo -> FC -> PClause -> Idris (Term, Term)
99elabClause :: ElabInfo -> FC -> PClause -> Idris (Term, Term)
100−elabClause info fc (PClause fname lhs rhs whereblock)
100+elabClause info fc (PClause fname lhs withs rhs whereblock)
101 = do ctxt <- getContext
101 = do ctxt <- getContext
102 -- Build the LHS as an "Infer", and pull out its type and
102 -- Build the LHS as an "Infer", and pull out its type and
103 -- pattern bindings
103 -- pattern bindings
@@ -116,28 +116,19 @@ elabClause info fc (PClause fname lhs rhs whereblock)
116 -- Now build the RHS, using the type of the LHS as the goal.
116 -- Now build the RHS, using the type of the LHS as the goal.
117 logLvl 2 (showImp True rhs)
117 logLvl 2 (showImp True rhs)
118 ctxt <- getContext -- new context with where block added
118 ctxt <- getContext -- new context with where block added
119+ ((rhs', defer), _) <-
119− ((rhs', defer), _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
120+ tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
120− (do pbinds lhs_tm
121+ (do pbinds lhs_tm
121− (_, d) <- erun fc (build winfo False rhs)
122+ (_, d) <- erun fc (build winfo False rhs)
122− psolve lhs_tm
123+ psolve lhs_tm
123− tt <- get_term
124+ tt <- get_term
124− return (tt, d))
125+ return (tt, d))
125 logLvl 2 $ "---> " ++ show rhs'
126 logLvl 2 $ "---> " ++ show rhs'
126 when (not (null defer)) $ iLOG $ "DEFERRED " ++ show defer
127 when (not (null defer)) $ iLOG $ "DEFERRED " ++ show defer
127 addDeferred defer
128 addDeferred defer
128 (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
129 (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
129 return (clhs, crhs)
130 return (clhs, crhs)
131− pbinds (Bind n (PVar t) sc) = do attack; patbind n
133− pbinds tm = return ()
135− psolve (Bind n (PVar t) sc) = do solve; psolve sc
136− psolve tm = return ()
138− pvars ist (Bind n (PVar t) sc) = (n, delab ist t) : pvars ist sc
141 pinfo ns ps i
132 pinfo ns ps i
142 = let ds = concatMap declared ps
133 = let ds = concatMap declared ps
143 newps = params info ++ ns
134 newps = params info ++ ns
@@ -151,6 +142,103 @@ elabClause info fc (PClause fname lhs rhs whereblock)
151 _ -> MN i (show n)) . l
142 _ -> MN i (show n)) . l
145+elabClause info fc (PWith fname lhs withs wval withblock)
146+ = do ctxt <- getContext
147+ -- Build the LHS as an "Infer", and pull out its type and
149+ ((lhs', dlhs), _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
150+ (erun fc (build info True (infTerm lhs)))
151+ let lhs_tm = getInferTerm lhs'
152+ let lhs_ty = getInferType lhs'
153+ let ret_ty = getRetTy lhs_ty
154+ logLvl 3 (show lhs_tm)
155+ (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
156+ logLvl 5 ("Checked " ++ show clhs)
157+ logLvl 5 ("Checking " ++ show wval)
158+ let bargs = getPBtys lhs_tm
159+ -- Elaborate wval in this context
160+ ((wval', defer), _) <-
161+ tclift $ elaborate ctxt (MN 0 "withRHS")
162+ (bindTyArgs PVTy bargs infP)
164+ -- TODO: may want where here - see winfo abpve
165+ (_', d) <- erun fc (build info False (infTerm wval))
170+ (cwval, cwvalty) <- tclift $ recheck ctxt [] (getInferTerm wval')
171+ logLvl 3 ("With type " ++ show cwvalty ++ "\nRet type " ++ show ret_ty)
173+ -- build a type declaration for the new function:
174+ -- (ps : Xs) -> (withval : cwvalty) -> ret_ty
175+ let wtype = bindTyArgs Pi (bargs ++ [(MN 0 "warg", getRetTy cwvalty)]) ret_ty
176+ logLvl 3 ("New function type " ++ show wtype)
177+ let wname = MN windex (show fname)
178+ addDeferred [(wname, wtype)]
180+ -- in the subdecls, lhs becomes:
181+ -- fname pats | wpat [rest]
182+ -- ==> fname' ps wpat [rest]
183+ wb <- mapM (mkAuxC wname (map fst bargs)) withblock
184+ logLvl 5 ("with block " ++ show wb)
185+ mapM_ (elabDecl info) wb
187+ -- rhs becomes: fname' ps wval
188+ let rhs = PApp fc (PRef fc wname) (map (PExp . (PRef fc) . fst) bargs ++
190+ logLvl 3 ("New RHS " ++ show rhs)
191+ ctxt <- getContext -- New context with block added
192+ ((rhs', defer), _) <-
193+ tclift $ elaborate ctxt (MN 0 "wpatRHS") clhsty
195+ (_, d) <- erun fc (build info False rhs)
200+ (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
203+ mkAuxC wname ns (PClauses fc n cs)
204+ | n == fname = do cs' <- mapM (mkAux wname ns) cs
205+ return $ PClauses fc wname cs'
206+ | otherwise = fail $ "with clause uses wrong function name " ++ show n
207+ mkAuxC wname ns d = return $ d
209+ mkAux wname ns (PClause n tm (w:ws) rhs wheres)
210+ = do lhs <- updateLHS n wname ns tm w
211+ return $ PClause wname lhs ws rhs wheres
212+ mkAux wname ns (PWith n tm (w:ws) wval wheres)
213+ = do lhs <- updateLHS n wname ns tm w
214+ return $ PWith wname lhs ws wval wheres
216+ updateLHS n wname ns (PApp fc (PRef fc' n') args) w
217+ = return $ PApp fc (PRef fc' wname)
218+ (map (PExp . (PRef fc')) ns ++ [PExp w])
219+ updateLHS n wname ns tm w = fail $ "Not implemented " ++ show tm
221+pbinds (Bind n (PVar t) sc) = do attack; patbind n
223+pbinds tm = return ()
225+pbty (Bind n (PVar t) sc) tm = Bind n (PVTy t) (pbty sc tm)
228+getPBtys (Bind n (PVar t) sc) = (n, t) : getPBtys sc
231+getRetTy (Bind n (PVar _) sc) = getRetTy sc
232+getRetTy (Bind n (PVTy _) sc) = getRetTy sc
233+getRetTy (Bind n (Pi _) sc) = getRetTy sc
236+psolve (Bind n (PVar t) sc) = do solve; psolve sc
237+psolve tm = return ()
239+pvars ist (Bind n (PVar t) sc) = (n, delab ist t) : pvars ist sc
154-- TODO: Also build a 'binary' version of each declaration for fast reloading
242-- TODO: Also build a 'binary' version of each declaration for fast reloading
156elabDecl :: ElabInfo -> PDecl -> Idris ()
244elabDecl :: ElabInfo -> PDecl -> Idris ()
@@ -215,8 +303,8 @@ elab info pattern tm = do elab' tm
215 elab' (PPair fc l r) = try (elab' (PApp fc (PRef fc pairTy)
303 elab' (PPair fc l r) = try (elab' (PApp fc (PRef fc pairTy)
216 [PExp l,PExp r]))
304 [PExp l,PExp r]))
217 (elab' (PApp fc (PRef fc pairCon)
305 (elab' (PApp fc (PRef fc pairCon)
218− [PImp (MN 0 "a") Placeholder,
306+ [PImp (MN 0 "A") Placeholder,
219− PImp (MN 0 "a") Placeholder,
307+ PImp (MN 0 "B") Placeholder,
220 PExp l, PExp r]))
308 PExp l, PExp r]))
221 elab' (PRef fc n) | pattern && not (inparamBlock n)
309 elab' (PRef fc n) | pattern && not (inparamBlock n)
222 = erun fc $
310 = erun fc $