1-- Copyright (C) 2009 Florent Becker
2--
3-- This program is free software; you can redistribute it and/or modify
4-- it under the terms of the GNU General Public License as published by
5-- the Free Software Foundation; either version 2, or (at your option)
6-- any later version.
7--
8-- This program is distributed in the hope that it will be useful,
9-- but WITHOUT ANY WARRANTY; without even the implied warranty of
10-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11-- GNU General Public License for more details.
12--
13-- You should have received a copy of the GNU General Public License
14-- along with this program; see the file COPYING.  If not, write to
15-- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16-- Boston, MA 02110-1301, USA.
17
18module Darcs.Patch.Witnesses.WZipper
19    ( FZipper(..)
20    , focus
21    , leftmost
22    , left
23    , rightmost
24    , right
25    , jokers
26    , clowns
27    , flToZipper
28    , lengthFZ
29    , nullFZ
30    , toEnd
31    , toStart
32    )
33where
34
35import Darcs.Prelude
36
37import Darcs.Patch.Witnesses.Ordered
38    ( FL(..)
39    , RL(..)
40    , nullFL
41    , nullRL
42    , lengthFL
43    , lengthRL
44    , reverseFL
45    , reverseRL
46    , (+<+)
47    , (+>+)
48    )
49import Darcs.Patch.Witnesses.Sealed(Sealed2(..), Sealed(..), FlippedSeal(..))
50
51-- forward zipper
52data FZipper a wX wZ where
53    FZipper :: RL a wX wY -> FL a wY wZ -> FZipper a wX wZ
54
55-- Constructors
56flToZipper :: FL a wX wY -> FZipper a wX wY
57flToZipper = FZipper NilRL
58
59--destructors
60nullFZ :: FZipper a wX wY -> Bool
61nullFZ (FZipper l r) = nullRL l && nullFL r
62
63lengthFZ :: FZipper a wX wY -> Int
64lengthFZ (FZipper l r) = lengthRL l + lengthFL r
65
66focus :: FZipper a wX wY -> Maybe (Sealed2 a)
67focus (FZipper _ (x :>: _)) = Just $ Sealed2 x
68focus _ = Nothing
69
70-- | \"Clowns to the left of me, jokers to the right.  Here I am, stuck
71--   in the middle of you\"
72--   <http://en.wikipedia.org/wiki/Stuck_in_the_Middle>
73clowns :: FZipper a wX wY -> Sealed ((RL a) wX)
74clowns (FZipper l _) = Sealed l
75
76-- | See 'clowns'
77jokers :: FZipper a wX wY -> FlippedSeal (FL a) wY
78jokers (FZipper _ r) = FlippedSeal r
79
80rightmost :: FZipper p wX wY -> Bool
81rightmost (FZipper _ NilFL) = True
82rightmost _ = False
83
84right :: FZipper p wX wY -> FZipper p wX wY
85right (FZipper l (m:>:r)) = FZipper (l :<: m) r
86right x@(FZipper _ NilFL) = x
87
88leftmost :: FZipper p wX wY -> Bool
89leftmost (FZipper NilRL _) = True
90leftmost _ = False
91
92left :: FZipper p wX wY -> FZipper p wX wY
93left (FZipper (l :<: m) r) = FZipper l (m :>: r)
94left x@(FZipper NilRL _) = x
95
96toEnd :: FZipper p wX wY -> FZipper p wX wY
97toEnd (FZipper l r) = FZipper (l +<+ reverseFL r) NilFL
98
99toStart :: FZipper p wX wY -> FZipper p wX wY
100toStart (FZipper l r) = FZipper NilRL $ reverseRL l +>+ r
101