@@ -9,6 +9,7 @@ import Core.Evaluate
99import Control.Monad.State
1010import Data.List
1111import Data.Char
12+import Debug.Trace
1213
1314data IOption = IOption { opt_logLevel :: Int }
1415 deriving (Show, Eq)
@@ -22,7 +23,7 @@ defaultOpts = IOption 0
2223
2324data IState = IState { tt_ctxt :: Context,
2425 idris_infixes :: [FixDecl],
25− idris_implicits :: Ctxt ([Name], Int), -- implicits, arity
26+ idris_implicits :: Ctxt [PArg],
2627 idris_log :: String,
2728 idris_options :: IOption
2829 }
@@ -99,7 +100,7 @@ data PDecl' t = PFix Fixity [String] -- fixity declaration
99100 | PData (PData' t) -- data declaration
100101 deriving Functor
101102
102−data PClause' t = PClause Name t t
103+data PClause' t = PClause Name t t [PDecl]
103104 deriving Functor
104105
105106data PData' t = PDatadecl { d_name :: Name,
@@ -112,20 +113,27 @@ data PData' t = PDatadecl { d_name :: Name,
112113
113114type PDecl = PDecl' PTerm
114115type PData = PData' PTerm
115−type PClause = PClause' PTerm
116−-- High level language terms
116+type PClause = PClause' PTerm
117117
118+-- High level language terms
119+--
118120data PTerm = PQuote Raw
119121 | PRef Name
120122 | PLam Name PTerm PTerm
121123 | PPi Plicity Name PTerm PTerm
122124 | PLet Name PTerm PTerm PTerm -- not implemented yet
123− | PApp PTerm [(Name, PTerm)] [PTerm]
125+ | PApp PTerm [PArg]
124126 | PHidden PTerm -- irrelevant or hidden pattern
125127 | PSet
126128 | PConstant Const
127129 | Placeholder
128130
131+data PArg' t = PImp { pname :: Name, getTm :: t }
132+ | PExp { getTm :: t }
133+ deriving (Show, Functor)
134+
135+type PArg = PArg' PTerm
136+
129137-- Syntactic sugar info (TODO: namespaces, parameters, modules)
130138
131139data SyntaxInfo = Syn { using :: [(Name, PTerm)] }
@@ -151,14 +159,27 @@ instance Show PData where
151159 show d = showDImp False d
152160
153161showCImp :: Bool -> PClause -> String
154−showCImp impl (PClause n l r) = showImp impl l ++ " = " ++ showImp impl r
155−
162+showCImp impl (PClause n l r w) = showImp impl l ++ " = " ++ showImp impl r
163+ ++ " where " ++ show w
156164showDImp :: Bool -> PData -> String
157165showDImp impl (PDatadecl n ty cons)
158166 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
159167 ++ showSep "\n\t| "
160168 (map (\ (n, t) -> show n ++ " : " ++ showImp impl t) cons)
161169
170+getImps :: [PArg] -> [(Name, PTerm)]
171+getImps [] = []
172+getImps (PImp n tm : xs) = (n, tm) : getImps xs
173+getImps (_ : xs) = getImps xs
174+
175+getExps :: [PArg] -> [PTerm]
176+getExps [] = []
177+getExps (PExp tm : xs) = tm : getExps xs
178+getExps (_ : xs) = getExps xs
179+
180+getAll :: [PArg] -> [PTerm]
181+getAll = map getTm
182+
162183showImp :: Bool -> PTerm -> String
163184showImp impl tm = se 10 tm where
164185 se p (PQuote r) = "![" ++ show r ++ "]"
@@ -168,19 +189,21 @@ showImp impl tm = se 10 tm where
168189 | n `elem` allNamesIn sc = bracket p 2 $
169190 "(" ++ show n ++ " : " ++ se 10 ty ++
170191 ") -> " ++ se 10 sc
171− | otherwise = bracket p 2 $ se 10 ty ++ " -> " ++ se 10 sc
192+ | otherwise = bracket p 2 $ se 0 ty ++ " -> " ++ se 10 sc
172193 se p (PPi Imp n ty sc)
173194 | impl = bracket p 2 $ "{" ++ show n ++ " : " ++ se 10 ty ++
174195 "} -> " ++ se 10 sc
175196 | otherwise = se 10 sc
176− se p (PApp (PRef f) _ [])
197+ se p (PApp (PRef f) [])
177198 | not impl = show f
178− se p (PApp (PRef op@(UN [f:_])) _ [l, r])
179− | not impl && not (isAlpha f)
180− = bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
181− se p (PApp f imps args)
182− = bracket p 1 $ se 1 f ++ (if impl then concatMap siArg imps else "")
183− ++ concatMap seArg args
199+ se p (PApp (PRef op@(UN [f:_])) args)
200+ | length (getExps args) == 2 && not impl && not (isAlpha f)
201+ = let [l, r] = getExps args in
202+ bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
203+ se p (PApp f as)
204+ = let (imps, args) = (getImps as, getExps as) in
205+ bracket p 1 $ se 1 f ++ (if impl then concatMap siArg imps else "")
206+ ++ concatMap seArg args
184207 se p (PHidden tm) = "." ++ se 0 tm
185208 se p PSet = "Set"
186209 se p (PConstant c) = show c
@@ -197,8 +220,7 @@ allNamesIn tm = nub $ ni [] tm
197220 where
198221 ni env (PRef n)
199222 | not (n `elem` env) = [n]
200− ni env (PApp f is es) = ni env f ++ concatMap (ni env) (map snd is) ++
201− concatMap (ni env) es
223+ ni env (PApp f as) = ni env f ++ concatMap (ni env) (map getTm as)
202224 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
203225 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
204226 ni env (PHidden tm) = ni env tm
@@ -212,8 +234,7 @@ namesIn ist tm = nub $ ni [] tm
212234 = case lookupCtxt n (idris_implicits ist) of
213235 Nothing -> [n]
214236 _ -> []
215− ni env (PApp f is es) = ni env f ++ concatMap (ni env) (map snd is) ++
216− concatMap (ni env) es
237+ ni env (PApp f as) = ni env f ++ concatMap (ni env) (map getTm as)
217238 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
218239 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
219240 ni env (PHidden tm) = ni env tm
@@ -229,7 +250,7 @@ inferDecl = PDatadecl inferTy
229250 PPi Exp (MN 0 "a") (PRef (MN 0 "A"))
230251 (PRef inferTy)))]
231252
232−infTerm t = PApp (PRef inferCon) [(MN 0 "A", Placeholder)] [t]
253+infTerm t = PApp (PRef inferCon) [PImp (MN 0 "A") Placeholder, PExp t]
233254infP = P (TCon 0) inferTy (Set 0)
234255
235256getInferTerm, getInferType :: Term -> Term
@@ -246,52 +267,53 @@ getInferType (App (App _ ty) _) = ty
246267-- Add implicit Pi bindings for any names in the term which appear in an
247268-- argument position.
248269
249−implicitise :: SyntaxInfo -> IState -> PTerm -> (PTerm, ([Name], Int))
270+implicitise :: SyntaxInfo -> IState -> PTerm -> (PTerm, [PArg])
250271implicitise syn ist tm
251272 = let uvars = using syn
252− (declimps, a, ns) = execState (imps [] tm) ([], 0, []) in
273+ (declimps, ns) = execState (imps True [] tm) ([], []) in
253274 if null ns
254− then (tm, (reverse declimps, a))
275+ then (tm, reverse declimps)
255276 else implicitise syn ist (pibind uvars ns tm)
256277 where
257− imps env (PApp f is es)
258− = do (decls, a, ns) <- get
259− let isn = concatMap (namesIn ist) (map snd is)
260− let esn = concatMap (namesIn ist) es
261− put (decls, a, nub (ns ++ ((isn ++ esn) \\ (env ++ decls))))
262− imps env (PPi Imp n ty sc)
263− = do imps env ty
264− (impdecls, a, ns) <- get
265− put (n:impdecls, a, ns)
266− imps (n:env) sc
267− imps env (PPi Exp n ty sc)
268− = do imps env ty
269− (impdecls, a, ns) <- get
270− put (impdecls, a+1, ns)
271− imps (n:env) sc
272− imps env (PLam n ty sc)
273− = do imps env ty
274− imps (n:env) sc
275− imps env (PHidden tm) = imps env tm
276− imps env _ = return ()
278+ imps top env (PApp f as)
279+ = do (decls, ns) <- get
280+ let isn = concatMap (namesIn ist) (map getTm as)
281+ put (decls, nub (ns ++ (isn \\ (env ++ map fst (getImps decls)))))
282+ imps top env (PPi Imp n ty sc)
283+ = do let isn = namesIn ist ty \\ [n]
284+ (decls , ns) <- get
285+ put (PImp n ty : decls, nub (ns ++ (isn \\ (env ++ map fst (getImps decls)))))
286+ imps True (n:env) sc
287+ imps top env (PPi Exp n ty sc)
288+ = do let isn = (namesIn ist ty ++ case sc of
289+ (PRef x) -> namesIn ist sc
290+ _ -> []) \\ [n]
291+ (decls, ns) <- get -- ignore decls in HO types
292+ put (PExp ty : decls, nub (ns ++ (isn \\ (env ++ map fst (getImps decls)))))
293+ imps True (n:env) sc
294+ imps top env (PLam n ty sc)
295+ = do imps False env ty
296+ imps False (n:env) sc
297+ imps top env (PHidden tm) = imps False env tm
298+ imps top env _ = return ()
277299
278300 pibind using [] sc = sc
279301 pibind using (n:ns) sc = case lookup n using of
280302 Just ty -> PPi Imp n ty (pibind using ns sc)
281303 Nothing -> PPi Imp n Placeholder (pibind using ns sc)
282304
305+-- Add implicit arguments in function calls
306+
283307addImpl :: IState -> PTerm -> PTerm
284308addImpl ist ptm = ai [] ptm
285309 where
286− ai env (PRef f) = aiFn env (PRef f) [] []
287− ai env (PApp (PRef f) is es)
288− = let is' = map (\ (n, tm) -> (n, ai env tm)) is
289− es' = map (ai env) es in
290− aiFn env (PRef f) is' es'
291− ai env (PApp f is es) = let f' = ai env f
292− is' = map (\ (n, tm) -> (n, ai env tm)) is
293− es' = map (ai env) es in
294− aiFn env f' is' es'
310+ ai env (PRef f) = aiFn env (PRef f) []
311+ ai env (PApp (PRef f) as)
312+ = let as' = map (fmap (ai env)) as in
313+ aiFn env (PRef f) as'
314+ ai env (PApp f as) = let f' = ai env f
315+ as' = map (fmap (ai env)) as in
316+ aiFn env f' as'
295317 ai env (PLam n ty sc) = let ty' = ai env ty
296318 sc' = ai (n:env) sc in
297319 PLam n ty' sc'
@@ -301,23 +323,31 @@ addImpl ist ptm = ai [] ptm
301323 ai env (PHidden tm) = PHidden (ai env tm)
302324 ai env tm = tm
303325
304− aiFn env (PRef f) is es | not (f `elem` env)
326+ aiFn env (PRef f) as | not (f `elem` env)
305327 = case lookupCtxt f (idris_implicits ist) of
306− Just (ns, a) -> pApp a (PRef f) (insertImpl ns is) es
307− Nothing -> pApp 1 (PRef f) is es
308− aiFn env f is es = pApp 1 f is es
328+ Just ns -> pApp (length ns) (PRef f) (insertImpl ns as)
329+ Nothing -> pApp 1 (PRef f) as
330+ aiFn env f as = pApp 1 f as
309331
310− pApp a f [] [] = f
311− pApp a f is es = let rest = drop a es in
312− appRest (PApp f is (take a es)) rest
332+ pApp a f [] = f
333+ pApp a f as = let rest = drop a as in
334+ appRest (PApp f (take a as)) rest
313335
314336 appRest f [] = f
315− appRest f (a : as) = appRest (PApp f [] [a]) as
316−
317− insertImpl [] given = []
318− insertImpl (n:ns) given
319− = case lookup n given of
320− Just val -> (n, val) : insertImpl ns given
321− Nothing -> (n, Placeholder) : insertImpl ns given
322−
337+ appRest f (a : as) = appRest (PApp f [a]) as
338+
339+ insertImpl :: [PArg] -> [PArg] -> [PArg]
340+ insertImpl (PExp ty : ps) (PExp tm : given) =
341+ PExp tm : insertImpl ps given
342+ insertImpl (PImp n ty : ps) given =
343+ case find n given [] of
344+ Just (tm, given') -> PImp n ty : insertImpl ps given'
345+ Nothing -> PImp n Placeholder : insertImpl ps given
346+ insertImpl expected [] = []
347+ insertImpl [] given = given
348+
349+ find n [] acc = Nothing
350+ find n (PImp n' t : gs) acc
351+ | n == n' = Just (t, reverse acc ++ gs)
352+ find n (g : gs) acc = find n gs (g : acc)
323353