Container.hs revision dd5e505efb3641bf74ade0ba3adabf4ceaf94e60
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder{- |
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyModule : $Header$
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyDescription : Generic handling of some data structures
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyCopyright : (c) Hendrik Iben, Uni Bremen 2005-2006
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyMaintainer : hiben@informatik.uni-bremen.de
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyStability : provisional
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyPortability : portable
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reillyabstraction over data-containers (lists, sets, maps...)
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly-}
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reillymodule OMDoc.Container
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly (
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly Container(..)
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly ,con_convert
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder ,con_map
66bc8d6e69cde43f1ccbeb76104cf7b8038acd6cChristian Maeder ,processSubContents
66bc8d6e69cde43f1ccbeb76104cf7b8038acd6cChristian Maeder ,pSCStrip
e90dc723887d541f809007ae81c9bb73ced9592eChristian Maeder )
d5833d2ee7bafcbf2fdd2bdfd9a728c769b100c7Christian Maeder where
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reillyimport qualified Data.Map as Map
66bc8d6e69cde43f1ccbeb76104cf7b8038acd6cChristian Maederimport qualified Data.Set as Set
a00461fcf7432205a79a0f12dbe6c1ebc58bc000Christian Maederimport qualified Common.Lib.Rel as Rel
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly-- | Container-Class
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reillyclass Container a b | a -> b where
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder getItems::a->[b]
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder fromItems::[b]->a
7830e8fa7442fb7452af7ecdba102bc297ae367eChristian Maeder
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder-- | Container-Conversion
d5833d2ee7bafcbf2fdd2bdfd9a728c769b100c7Christian Maedercon_convert::(Container q i, Container r i)=>q->r
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reillycon_convert c = fromItems (getItems c)
c0833539c8cf577dd3f2497792fbdd818442744cChristian Maeder
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly-- | Container-Mapping
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reillycon_map::(Container q i, Container r j)=>(i->j)->q->r
7830e8fa7442fb7452af7ecdba102bc297ae367eChristian Maedercon_map f = fromItems . (map f) . getItems
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
fa373bc327620e08861294716b4454be8d25669fChristian Maeder-- Lists are containers
036ecbd8f721096321f47cf6a354a9d1bf3d032fChristian Maederinstance Container [a] a where
fa373bc327620e08861294716b4454be8d25669fChristian Maeder getItems = id
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder fromItems = id
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly-- Sets are containers
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reillyinstance (Ord a)=>Container (Set.Set a) a where
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly getItems = Set.toList
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly fromItems = Set.fromList
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder-- Maps are containers
648fe1220044aac847acbdfbc4155af5556063ebChristian Maederinstance (Ord a)=>Container (Map.Map a b) (a,b) where
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder getItems = Map.toList
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder fromItems = Map.fromList
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder-- Relations are containers
648fe1220044aac847acbdfbc4155af5556063ebChristian Maederinstance (Ord a)=>Container (Rel.Rel a) (a,a) where
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder getItems = Rel.toList
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder fromItems = Rel.fromList
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder-- | use this function to process containers that are stored in other containers
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder-- - think map key->container - and return container with containers of processed
4314e26a12954cb1c9be4dea10aa8103edac5bbbChristian Maeder-- items. the trick is that the key association is the same as long as the
d5833d2ee7bafcbf2fdd2bdfd9a728c769b100c7Christian Maeder-- processing function does not alter the key (but it may do so)
d5833d2ee7bafcbf2fdd2bdfd9a728c769b100c7Christian Maeder-- the processing function needs to take an initial status and the final status
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly-- will be returned
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'ReillyprocessSubContents::
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly (Ord k, Container a (k, p), Container p q
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder , Container t r, Container b (k, t) )=>
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder (s->[(k, q)]->([(k, r)], s))->s->a->(b, s)
648fe1220044aac847acbdfbc4155af5556063ebChristian MaederprocessSubContents
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder subprocess
d5833d2ee7bafcbf2fdd2bdfd9a728c769b100c7Christian Maeder startvalue
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder container =
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly let
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly allitems = getItems container
fa373bc327620e08861294716b4454be8d25669fChristian Maeder tagged = concatMap (\(k,c) -> map (\i -> (k,i)) (getItems c)) allitems
fa373bc327620e08861294716b4454be8d25669fChristian Maeder (processeditems, finalstatus) = subprocess startvalue tagged
fa373bc327620e08861294716b4454be8d25669fChristian Maeder sorted =
fa373bc327620e08861294716b4454be8d25669fChristian Maeder foldl (\sorted' (k,i) -> insertAtKey (k,i) sorted' )
fa373bc327620e08861294716b4454be8d25669fChristian Maeder []
fa373bc327620e08861294716b4454be8d25669fChristian Maeder processeditems
fa373bc327620e08861294716b4454be8d25669fChristian Maeder kconpairs = map (\(k,l) -> (k,fromItems l)) sorted
fa373bc327620e08861294716b4454be8d25669fChristian Maeder in
fa373bc327620e08861294716b4454be8d25669fChristian Maeder (fromItems kconpairs, finalstatus)
fa373bc327620e08861294716b4454be8d25669fChristian Maeder where
fa373bc327620e08861294716b4454be8d25669fChristian Maeder insertAtKey::(Eq k)=>(k,v)->[(k,[v])]->[(k,[v])]
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder insertAtKey (k,v) [] = [(k,[v])]
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder insertAtKey (k,v) ((lk,l):r) =
d5833d2ee7bafcbf2fdd2bdfd9a728c769b100c7Christian Maeder if k == lk then (lk,v:l):r else (lk,l):(insertAtKey (k,v) r)
7857a35e3af533dfbd0f0e18638ebd211e6358a0Christian Maeder
7857a35e3af533dfbd0f0e18638ebd211e6358a0Christian Maeder-- strip-function for using processSubContents
7857a35e3af533dfbd0f0e18638ebd211e6358a0Christian MaederpSCStrip::(a->b)->(z,a)->b
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'ReillypSCStrip f (_,a) = f a
648fe1220044aac847acbdfbc4155af5556063ebChristian Maeder