summaryrefslogtreecommitdiff
path: root/src/full/Agda/Syntax/Concrete/Operators.hs
blob: 2fa15468c8f316857a8a3ed359af950fdc12a9ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
{-# LANGUAGE CPP                 #-}
{-# LANGUAGE ScopedTypeVariables #-}

{-| The parser doesn't know about operators and parses everything as normal
    function application. This module contains the functions that parses the
    operators properly. For a stand-alone implementation of this see
    @src\/prototyping\/mixfix\/old@.

    It also contains the function that puts parenthesis back given the
    precedence of the context.
-}
module Agda.Syntax.Concrete.Operators
    ( parseApplication
    , parseModuleApplication
    , parseLHS
    , parsePattern
    , parsePatternSyn
    ) where

import Control.DeepSeq
import Control.Applicative
import Control.Monad

import Data.Either (partitionEithers)
import Data.Function
import Data.List
import Data.Maybe
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable (traverse)
import qualified Data.Traversable as Trav

import Agda.Syntax.Concrete.Pretty ()
import Agda.Syntax.Common hiding (Arg, Dom, NamedArg)
import qualified Agda.Syntax.Common as Common
import Agda.Syntax.Concrete hiding (appView)
import Agda.Syntax.Concrete.Operators.Parser
import qualified Agda.Syntax.Abstract.Name as A
import Agda.Syntax.Position
import Agda.Syntax.Fixity
import Agda.Syntax.Notation
import Agda.Syntax.Scope.Base
import Agda.Syntax.Scope.Monad

import Agda.TypeChecking.Monad.Base (typeError, TypeError(..), LHSOrPatSyn(..))
import Agda.TypeChecking.Monad.Benchmark (billSub)
import qualified Agda.TypeChecking.Monad.Benchmark as Bench
import Agda.TypeChecking.Monad.State (getScope)
import Agda.TypeChecking.Monad.Options

import Agda.Utils.Either
import Agda.Utils.ReadP
import Agda.Utils.List

#include "undefined.h"
import Agda.Utils.Impossible

---------------------------------------------------------------------------
-- * Building the parser
---------------------------------------------------------------------------

partsInScope :: FlatScope -> ScopeM (Set QName)
partsInScope flat = do
    (names, ops) <- localNames flat
    let xs = concatMap parts names ++ concatMap notationNames ops
    return $ Set.fromList xs
    where
        qual xs x = foldr Qual (QName x) xs
        parts q = parts' (init $ qnameParts q) (unqualify q)
        parts' ms (NoName _ _)   = []
        parts' ms x@(Name _ [_]) = [qual ms x]
                                   -- The first part should be qualified, but not the rest
        parts' ms x@(Name _ xs)  = qual ms x : qual ms (Name noRange [first]) : [ QName $ Name noRange [i] | i <- iparts ]
          where
            first:iparts = [ i | i@(Id {}) <- xs ]

type FlatScope = Map QName [AbstractName]

-- | Compute all unqualified defined names in scope and their fixities.
--   Note that overloaded names (constructors) can have several fixities.
--   Then we 'chooseFixity'. (See issue 1194.)
getDefinedNames :: [KindOfName] -> FlatScope -> [(QName, Fixity')]
getDefinedNames kinds names =
  [ (x, chooseFixity fixs)
  | (x, ds) <- Map.toList names
  , any ((`elem` kinds) . anameKind) ds
  , let fixs = map (A.nameFixity . A.qnameName . anameName) ds
  , not (null fixs)
  -- Andreas, 2013-03-21 see Issue 822
  -- Names can have different kinds, i.e., 'defined' and 'constructor'.
  -- We need to consider all names that have *any* matching kind,
  -- not only those whose first appearing kind is matching.
 ]

-- | Compute all names (first component) and operators (second component) in
--   scope.
localNames :: FlatScope -> ScopeM ([QName], [NewNotation])
localNames flat = do
  let defs = getDefinedNames allKindsOfNames flat
  locals <- notShadowedLocals <$> getLocalVars
  -- Note: Debug printout aligned with the one in buildParsers.
  reportSLn "scope.operators" 50 $ unlines
    [ "flat  = " ++ show flat
    , "defs  = " ++ show defs
    , "locals= " ++ show locals
    ]
  return $ split $ uniqOn fst $ map localOp locals ++ defs
  where
    localOp (x, y) = (QName x, A.nameFixity y)
    split ops = partitionEithers $ concatMap opOrNot ops

    opOrNot (q, Fixity' fx syn) = Left q : map Right (notaFromName ++ nota)
      where
        notaFromName = case unqualify q of
          Name _ [_] -> []
          x          -> [NewNotation q fx $ syntaxOf x]
        nota = if null syn then [] else [NewNotation q fx syn]

-- | Data structure filled in by @buildParsers@.
--   The top-level parser @pTop@ is of primary interest,
--   but @pArgs@ is used to convert module application
--   from concrete to abstract syntax.
data Parsers e = Parsers
  { pTop    :: ReadP e e
  , pApp    :: ReadP e e
  , pArgs   :: ReadP e [NamedArg e]
  , pNonfix :: ReadP e e
  , pAtom   :: ReadP e e
  }

data UseBoundNames = UseBoundNames | DontUseBoundNames

{-| Builds parser for operator applications from all the operators and function
    symbols in scope. When parsing a pattern we use 'DontUseBoundNames'.

    The effect is that operator parts (that are not constructor parts)
    can be used as atomic names in the pattern (so they can be
    rebound). See test/succeed/OpBind.agda for an example.

    To avoid problems with operators of the same precedence but different
    associativity we decide (completely arbitrary) to fix the precedences of
    operators with the same given precedence in the following order (from
    loosest to hardest):

    - non-associative

    - left associative

    - right associative

    - prefix

    - postfix

    This has the effect that if you mix operators with the same precedence but
    different associativity the parser won't complain. One could argue that
    this is a Bad Thing, but since it's not trivial to implement the check it
    will stay this way until people start complaining about it.
-}
buildParsers :: forall e. IsExpr e => Range -> FlatScope -> UseBoundNames -> ScopeM (Parsers e)
buildParsers r flat use = do
    (names, ops) <- localNames flat
    let cons = getDefinedNames [ConName, PatternSynName] flat
    reportSLn "scope.operators" 50 $ unlines
      [ "names = " ++ show names
      , "ops   = " ++ show ops
      , "cons  = " ++ show cons ]
    let conparts   = Set.fromList $ concatMap notationNames $ map oldToNewNotation cons
        opsparts   = Set.fromList $ concatMap notationNames $ ops
        allParts   = Set.union conparts opsparts
        connames   = Set.fromList $ map fst cons
        (non, fix) = partition nonfix ops
        set        = Set.fromList names
        isAtom   x = case use of
                       UseBoundNames     -> not (Set.member x allParts) || Set.member x set
                       DontUseBoundNames -> not (Set.member x conparts) || Set.member x connames
        -- If string is a part of notation, it cannot be used as an identifier,
        -- unless it is also used as an identifier. See issue 307.

    let chain = foldr ( $ )

    return $ Data.Function.fix $ \p -> Parsers
        { pTop    = chain (pApp p) (concatMap (mkP (pTop p)) (order fix))
        , pApp    = appP (pNonfix p) (pArgs p)
        , pArgs   = argsP (pNonfix p)
        , pNonfix = chain (pAtom p) (map (nonfixP . opP (pTop p)) non)
        , pAtom   = atomP isAtom
        }
    where
        level :: NewNotation -> Integer
        level = fixityLevel . notaFixity

        isinfixl, isinfixr, isinfix, nonfix, isprefix, ispostfix :: NewNotation -> Bool

        isinfixl (NewNotation _ (LeftAssoc _ _) syn)  = isInfix syn
        isinfixl _ = False

        isinfixr (NewNotation _ (RightAssoc _ _) syn) = isInfix syn
        isinfixr _ = False

        isinfix (NewNotation _ (NonAssoc _ _) syn)    = isInfix syn
        isinfix _ = False

        nonfix    (NewNotation _ _ syn) = notationKind syn == NonfixNotation
        isprefix  (NewNotation _ _ syn) = notationKind syn == PrefixNotation
        ispostfix (NewNotation _ _ syn) = notationKind syn == PostfixNotation

        isInfix :: Notation -> Bool
        isInfix syn = notationKind syn == InfixNotation

        -- | Group operators by precedence level
        order :: [NewNotation] -> [[NewNotation]]
        order = groupBy ((==) `on` level) . sortBy (compare `on` level)

        -- | Each element of the returned list takes the parser for an
        -- expression of higher precedence as parameter.
        mkP :: ReadP e e -> [NewNotation] -> [ReadP e e -> ReadP e e]
        mkP p0 ops = case concat [infx, inlfx, inrfx, prefx, postfx] of
            []      -> [id]
            fs      -> fs
            where
                inlfx   = fixP infixlP  isinfixl
                inrfx   = fixP infixrP  isinfixr
                infx    = fixP infixP   isinfix
                prefx   = fixP prefixP  isprefix
                postfx  = fixP postfixP ispostfix

                fixP :: (ReadP e (NewNotation,Range,[e]) -> ReadP e e -> ReadP e e) -> (NewNotation -> Bool) -> [ReadP e e -> ReadP e e]
                fixP f g =
                    case filter g ops of
                        []  -> []
                        ops -> [ f $ choice $ map (opP p0) ops ]


---------------------------------------------------------------------------
-- * Expression instances
---------------------------------------------------------------------------

instance IsExpr Expr where
    exprView e = case e of
        Ident x         -> LocalV x
        App _ e1 e2     -> AppV e1 e2
        OpApp r d es    -> OpAppV d es
        HiddenArg _ e   -> HiddenArgV e
        InstanceArg _ e -> InstanceArgV e
        Paren _ e       -> ParenV e
        Lam _ bs    e   -> LamV bs e
        Underscore{}    -> WildV e
        _               -> OtherV e
    unExprView e = case e of
        LocalV x      -> Ident x
        AppV e1 e2    -> App (fuseRange e1 e2) e1 e2
        OpAppV d es   -> OpApp (fuseRange d es) d es
        HiddenArgV e  -> HiddenArg (getRange e) e
        InstanceArgV e -> InstanceArg (getRange e) e
        ParenV e      -> Paren (getRange e) e
        LamV bs e     -> Lam (fuseRange bs e) bs e
        WildV e       -> e
        OtherV e      -> e


instance IsExpr Pattern where
    exprView e = case e of
        IdentP x      -> LocalV x
        AppP e1 e2    -> AppV e1 e2
        OpAppP r d es -> OpAppV d ((map . fmap . fmap) Ordinary es)
        HiddenP _ e   -> HiddenArgV e
        InstanceP _ e -> InstanceArgV e
        ParenP _ e    -> ParenV e
        WildP{}       -> WildV e
        _             -> OtherV e
    unExprView e = case e of
        LocalV x       -> IdentP x
        AppV e1 e2     -> AppP e1 e2
        OpAppV d es    -> let ess :: [NamedArg Pattern]
                              ess = (map . fmap . fmap) (fromOrdinary __IMPOSSIBLE__) es
                          in OpAppP (fuseRange d es) d ess
        HiddenArgV e   -> HiddenP (getRange e) e
        InstanceArgV e -> InstanceP (getRange e) e
        ParenV e       -> ParenP (getRange e) e
        LamV _ _       -> __IMPOSSIBLE__
        WildV e        -> e
        OtherV e       -> e

{- TRASH
instance IsExpr LHSCore where
    exprView e = case e of
        LHSHead f ps -> foldl AppV (LocalV f) $ map exprView ps
        LHSProj d ps1 e ps2 -> foldl AppV (LocalV d) $
          map exprView ps1 ++ exprView e : map exprView ps2
    unExprView e = LHSHead f ps
      where p :: Pattern
            p = unExprView
            (f, ps) = lhsArgs p
-}

---------------------------------------------------------------------------
-- * Helpers for pattern and lhs parsing
---------------------------------------------------------------------------

-- Andreas, 2011-11-24 moved here from ConcreteToAbstract
lhsArgs :: Pattern -> (Name, [NamedArg Pattern])
lhsArgs p = case lhsArgs' p of
              Just (x, args) -> (x, args)
              Nothing        -> __IMPOSSIBLE__

-- | @lhsArgs' p@ splits a lhs @f ps@, given as a pattern @p@,
--   into @(f, ps)@.
lhsArgs' :: Pattern -> Maybe (Name, [NamedArg Pattern])
lhsArgs' p = case patternAppView p of
    Common.Arg _ (Named _ (IdentP (QName x))) : ps -> Just (x, ps)
    _                                              -> Nothing

-- | View a pattern @p@ as a list @p0 .. pn@ where @p0@ is the identifier
--   (in most cases a constructor).
--
--  Pattern needs to be parsed already (operators resolved).
patternAppView :: Pattern -> [NamedArg Pattern]
patternAppView p = case p of
    AppP p arg    -> patternAppView p ++ [arg]
    OpAppP _ x ps -> defaultNamedArg (IdentP x) : ps
    ParenP _ p    -> patternAppView p
    RawAppP _ _   -> __IMPOSSIBLE__
    _             -> [ defaultNamedArg p ]


---------------------------------------------------------------------------
-- * Parse functions
---------------------------------------------------------------------------

-- | Returns the list of possible parses.
parsePat :: ReadP Pattern Pattern -> Pattern -> [Pattern]
parsePat prs p = case p of
    AppP p (Common.Arg info q) ->
        fullParen' <$> (AppP <$> parsePat prs p <*> (Common.Arg info <$> traverse (parsePat prs) q))
    RawAppP _ ps     -> fullParen' <$> (parsePat prs =<< parse prs ps)
    OpAppP r d ps    -> fullParen' . OpAppP r d <$> (mapM . traverse . traverse) (parsePat prs) ps
    HiddenP _ _      -> fail "bad hidden argument"
    InstanceP _ _    -> fail "bad instance argument"
    AsP r x p        -> AsP r x <$> parsePat prs p
    DotP r e         -> return $ DotP r e
    ParenP r p       -> fullParen' <$> parsePat prs p
    WildP _          -> return p
    AbsurdP _        -> return p
    LitP _           -> return p
    QuoteP _         -> return p
    IdentP _         -> return p


{- Implement parsing of copattern left hand sides, e.g.

  record Tree (A : Set) : Set where
    field
      label : A
      child : Bool -> Tree A

  -- corecursive function defined by copattern matching
  alternate : {A : Set}(a b : A) -> Tree A
  -- shallow copatterns
         label (alternate a b)              = a
         child (alternate a b) True         = alternate b a
  -- deep copatterns:
  label (child (alternate a b) False)       = b
  child (child (alternate a b) False) True  = alternate a b
  child (child (alternate a b) False) False = alternate a b

  Delivers an infinite tree

                   a
              b        b
            a   a    a   a
           b b b b  b b b b
                 ...

  Each lhs is a pattern tree with a distinct path of destructors
  ("child", "label") from the root to the defined symbol ("alternate").
  All branches besides this distinct path are patterns.

  Syntax.Concrete.LHSCore represents a lhs
   - the destructor path
   - the side patterns
   - the defined function symbol
   - the applied patterns
-}

type ParseLHS = Either Pattern (Name, LHSCore)

parseLHS' :: LHSOrPatSyn -> Maybe Name -> Pattern -> ScopeM ParseLHS
parseLHS' lhsOrPatSyn top p = do
    let ms = qualifierModules $ patternQNames p
    flat <- flattenScope ms <$> getScope
    parsers <- buildParsers (getRange p) flat DontUseBoundNames
    let patP = pTop parsers
    let cons = getNames [ConName, PatternSynName] flat
    let flds = getNames [FldName] flat
    case [ res | p' <- force $ parsePat patP p
               , res <- validPattern (PatternCheckConfig top cons flds) p' ] of
        [(p,lhs)] -> return lhs
        []        -> typeError $ NoParseForLHS lhsOrPatSyn p
        rs        -> typeError $ AmbiguousParseForLHS lhsOrPatSyn p $
                       map (fullParen . fst) rs
    where
        getNames kinds flat = map fst $ getDefinedNames kinds flat

        -- validPattern returns an empty or singleton list (morally a Maybe)
        validPattern :: PatternCheckConfig -> Pattern -> [(Pattern, ParseLHS)]
        validPattern conf p = case (classifyPattern conf p, top) of
            (Just r@(Left _), Nothing) -> [(p, r)] -- expect pattern
            (Just r@(Right _), Just{}) -> [(p, r)] -- expect lhs
            _ -> []

-- | Name sets for classifying a pattern.
data PatternCheckConfig = PatternCheckConfig
  { topName  :: Maybe Name  -- ^ name of defined symbol
  , conNames :: [QName]     -- ^ valid constructor names
  , fldNames :: [QName]     -- ^ valid field names
  }

-- | Returns zero or one classified patterns.
classifyPattern :: PatternCheckConfig -> Pattern -> Maybe ParseLHS
classifyPattern conf p =
  case patternAppView p of

    -- case @f ps@
    Common.Arg _ (Named _ (IdentP x@(QName f))) : ps | Just f == topName conf -> do
      guard $ all validPat ps
      return $ Right (f, LHSHead f ps)

    -- case @d ps@
    Common.Arg _ (Named _ (IdentP x)) : ps | x `elem` fldNames conf -> do
      -- ps0 :: [NamedArg ParseLHS]
      ps0 <- mapM classPat ps
      let (ps1, rest) = span (isLeft . namedArg) ps0
      (p2, ps3) <- uncons rest -- when (null rest): no field pattern or def pattern found
      guard $ all (isLeft . namedArg) ps3
      let (f, lhs)      = fromR p2
          (ps', _:ps'') = splitAt (length ps1) ps
      return $ Right (f, LHSProj x ps' lhs ps'')

    -- case: ordinary pattern
    _ -> do
      guard $ validConPattern (conNames conf) p
      return $ Left p

  where -- allNames = conNames conf ++ fldNames conf
        validPat = validConPattern (conNames conf) . namedArg
        classPat :: NamedArg Pattern -> Maybe (NamedArg ParseLHS)
        classPat = Trav.mapM (Trav.mapM (classifyPattern conf))
        fromR :: NamedArg (Either a (b, c)) -> (b, NamedArg c)
        fromR (Common.Arg info (Named n (Right (b, c)))) = (b, Common.Arg info (Named n c))
        fromR (Common.Arg info (Named n (Left  a     ))) = __IMPOSSIBLE__



-- | Parses a left-hand side, and makes sure that it defined the expected name.
--   TODO: check the arities of constructors. There is a possible ambiguity with
--   postfix constructors:
--      Assume _ * is a constructor. Then 'true *' can be parsed as either the
--      intended _* applied to true, or as true applied to a variable *. If we
--      check arities this problem won't appear.
parseLHS :: Name -> Pattern -> ScopeM LHSCore
parseLHS top p = do
  res <- parseLHS' IsLHS (Just top) p
  case res of
    Right (f, lhs) -> return lhs
    _ -> typeError $ NoParseForLHS IsLHS p

-- | Parses a pattern.
--   TODO: check the arities of constructors. There is a possible ambiguity with
--   postfix constructors:
--      Assume _ * is a constructor. Then 'true *' can be parsed as either the
--      intended _* applied to true, or as true applied to a variable *. If we
--      check arities this problem won't appear.
parsePattern :: Pattern -> ScopeM Pattern
parsePattern = parsePatternOrSyn IsLHS

parsePatternSyn :: Pattern -> ScopeM Pattern
parsePatternSyn = parsePatternOrSyn IsPatSyn

parsePatternOrSyn :: LHSOrPatSyn -> Pattern -> ScopeM Pattern
parsePatternOrSyn lhsOrPatSyn p = do
  res <- parseLHS' lhsOrPatSyn Nothing p
  case res of
    Left p -> return p
    _      -> typeError $ NoParseForLHS lhsOrPatSyn p

-- | Helper function for 'parseLHS' and 'parsePattern'.
validConPattern :: [QName] -> Pattern -> Bool
validConPattern cons p = case appView p of
    [_]           -> True
    IdentP x : ps -> elem x cons && all (validConPattern cons) ps
    [QuoteP _, _] -> True
    _             -> False
-- Andreas, 2012-06-04: I do not know why the following line was
-- the catch-all case.  It seems that the new catch-all works also
-- and is more logical.
--    ps            -> all (validConPattern cons) ps

-- | Helper function for 'parseLHS' and 'parsePattern'.
appView :: Pattern -> [Pattern]
appView p = case p of
    AppP p a         -> appView p ++ [namedArg a]
    OpAppP _ op ps   -> IdentP op : map namedArg ps
    ParenP _ p       -> appView p
    RawAppP _ _      -> __IMPOSSIBLE__
    HiddenP _ _      -> __IMPOSSIBLE__
    InstanceP _ _    -> __IMPOSSIBLE__
    _                -> [p]

-- | Collect all names in a pattern into a list of qualified names.
patternQNames :: Pattern -> [QName]
patternQNames p = case p of
  RawAppP _ ps     -> concatMap patternQNames ps
  IdentP q         -> [q]
  ParenP _ p       -> patternQNames p
  HiddenP _ p      -> patternQNames (namedThing p)
  InstanceP _ p    -> patternQNames (namedThing p)
  OpAppP r d ps    -> __IMPOSSIBLE__
  AppP{}           -> __IMPOSSIBLE__
  AsP r x p        -> patternQNames p
  AbsurdP{}        -> []
  WildP{}          -> []
  DotP{}           -> []
  LitP{}           -> []
  QuoteP{}         -> []

-- | Return all qualifiers occuring in a list of 'QName's.
--   Each qualifier is returned as a list of names, e.g.
--   for @Data.Nat._+_@ we return the list @[Data,Nat]@.
qualifierModules :: [QName] -> [[Name]]
qualifierModules qs =
  nub $ filter (not . null) $ map (init . qnameParts) qs

-- | Parse a list of expressions into an application.
parseApplication :: [Expr] -> ScopeM Expr
parseApplication [e] = return e
parseApplication es = do
    -- Build the parser
    let ms = qualifierModules [ q | Ident q <- es ]
    flat <- flattenScope ms <$> getScope
    -- Andreas, 2014-04-27 Time for building the parser is negligible
    p <- -- billSub [Bench.Parsing, Bench.Operators, Bench.BuildParser] $
      buildParsers (getRange es) flat UseBoundNames

    -- Parse
    case force $ parse (pTop p) es of
        [e] -> return e
        [] -> do
          -- When the parser fails and a name is not in scope, it is more
          -- useful to say that to the user rather than just "failed".
          inScope <- partsInScope flat
          case [ x | Ident x <- es, not (Set.member x inScope) ] of
              [] -> typeError $ NoParseForApplication es
              xs -> typeError $ NotInScope xs

        es' -> typeError $ AmbiguousParseForApplication es $ map fullParen es'

parseModuleIdentifier :: Expr -> ScopeM QName
parseModuleIdentifier (Ident m) = return m
parseModuleIdentifier e = typeError $ NotAModuleExpr e

parseRawModuleApplication :: [Expr] -> ScopeM (QName, [NamedArg Expr])
parseRawModuleApplication es = do
    let e : es_args = es
    m <- parseModuleIdentifier e

    -- Build the arguments parser
    let ms = qualifierModules [ q | Ident q <- es_args ]
    flat <- flattenScope ms <$> getScope
    p <- buildParsers (getRange es_args) flat UseBoundNames

    -- Parse
    case {-force $-} parse (pArgs p) es_args of -- TODO: not sure about forcing
        [as] -> return (m, as)
        [] -> do
          inScope <- partsInScope flat
          case [ x | Ident x <- es_args, not (Set.member x inScope) ] of
              [] -> typeError $ NoParseForApplication es
              xs -> typeError $ NotInScope xs

        ass -> do
          let f = fullParen . foldl (App noRange) (Ident m)
          typeError $ AmbiguousParseForApplication es
                    $ map f ass

-- | Parse an expression into a module application
--   (an identifier plus a list of arguments).
parseModuleApplication :: Expr -> ScopeM (QName, [NamedArg Expr])
parseModuleApplication (RawApp _ es) = parseRawModuleApplication es
parseModuleApplication (App r e1 e2) = do -- TODO: do we need this case?
    (m, args) <- parseModuleApplication e1
    return (m, args ++ [e2])
parseModuleApplication e = do
    m <- parseModuleIdentifier e
    return (m, [])

---------------------------------------------------------------------------
-- * Inserting parenthesis
---------------------------------------------------------------------------

fullParen :: IsExpr e => e -> e
fullParen e = case exprView $ fullParen' e of
    ParenV e    -> e
    e'          -> unExprView e'

fullParen' :: IsExpr e => e -> e
fullParen' e = case exprView e of
    LocalV _     -> e
    WildV _      -> e
    OtherV _     -> e
    HiddenArgV _ -> e
    InstanceArgV _ -> e
    ParenV _     -> e
    AppV e1 (Common.Arg info e2) -> par $ unExprView $ AppV (fullParen' e1) (Common.Arg info e2')
        where
            e2' = case argInfoHiding info of
                Hidden    -> e2
                Instance  -> e2
                NotHidden -> fullParen' <$> e2
    OpAppV x es -> par $ unExprView $ OpAppV x $ (map . fmap . fmap . fmap) fullParen' es
    LamV bs e -> par $ unExprView $ LamV bs (fullParen e)
    where
        par = unExprView . ParenV