~ubuntu-branches/ubuntu/utopic/haskell-uulib/utopic

« back to all changes in this revision

Viewing changes to src/UU/Parsing/Perms.hs

  • Committer: Bazaar Package Importer
  • Author(s): Arjan Oosting
  • Date: 2006-11-18 16:24:30 UTC
  • Revision ID: james.westby@ubuntu.com-20061118162430-24ddyj27kj0uk17v
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module UU.Parsing.Perms(Perms(), pPerms, pPermsSep, succeedPerms, (~*~), (~$~)) where
 
2
 
 
3
import UU.Parsing
 
4
import Maybe
 
5
 
 
6
-- =======================================================================================
 
7
-- ===== PERMUTATIONS ================================================================
 
8
-- =======================================================================================
 
9
 
 
10
newtype Perms p a = Perms (Maybe (p a), [Br p a])
 
11
data Br p a = forall b. Br (Perms p (b -> a)) (p b)
 
12
 
 
13
instance IsParser p s => Functor (Perms p) where
 
14
  fmap f (Perms (mb, bs)) = Perms (fmap (f<$>) mb, map (fmap f) bs)
 
15
 
 
16
instance IsParser p s => Functor (Br p) where
 
17
  fmap f (Br perm p) = Br (fmap (f.) perm) p 
 
18
 
 
19
(~*~) :: IsParser p s => Perms p (a -> b) -> p a -> Perms p b
 
20
perms ~*~ p = perms `add` (getzerop p, getonep p)
 
21
 
 
22
(~$~) :: IsParser p s => (a -> b) -> p a -> Perms p b
 
23
f     ~$~ p = succeedPerms f ~*~ p
 
24
 
 
25
succeedPerms :: IsParser p s => a -> Perms p a
 
26
succeedPerms x = Perms (Just (pLow x), []) 
 
27
 
 
28
add :: IsParser p s => Perms p (a -> b) -> (Maybe (p a),Maybe (p a)) -> Perms p b
 
29
add b2a@(Perms (eb2a, nb2a)) bp@(eb, nb)
 
30
 =  let changing :: IsParser p s => (a -> b) -> Perms p a -> Perms p b
 
31
        f `changing` Perms (ep, np) = Perms (fmap (f <$>) ep, [Br ((f.) `changing` pp) p | Br pp p <- np])
 
32
    in Perms
 
33
      ( do { f <- eb2a
 
34
           ; x <- eb
 
35
           ; return (f <*>  x)
 
36
           }
 
37
      ,  (case nb of
 
38
          Nothing     -> id
 
39
          Just pb     -> (Br b2a  pb:)
 
40
        )[ Br ((flip `changing` c) `add`  bp) d |  Br c d <- nb2a]
 
41
      )
 
42
 
 
43
pPerms :: IsParser p s => Perms p a -> p a 
 
44
pPerms (Perms (empty,nonempty))
 
45
 = foldl (<|>) (fromMaybe pFail empty) [ (flip ($)) <$> p <*> pPerms pp
 
46
                                       | Br pp  p <- nonempty
 
47
                                       ]
 
48
 
 
49
pPermsSep :: IsParser p s => p x -> Perms p a -> p a
 
50
pPermsSep (sep :: p z) perm = p2p (pSucceed ()) perm
 
51
 where  p2p :: IsParser p s => p x -> Perms p a -> p a
 
52
        p2p fsep (Perms (mbempty, nonempties)) = 
 
53
                let empty          = fromMaybe  pFail mbempty
 
54
                    pars (Br t p)  = flip ($) <$ fsep <*> p <*> p2p sep t
 
55
                in foldr (<|>) empty (map pars nonempties)              
 
56
        p2p_sep =  p2p sep                   
 
 
b'\\ No newline at end of file'