summaryrefslogtreecommitdiff
path: root/src/GF/OldParsing/GeneralChart.hs
blob: 1d51da02530fc9ad83a2e6ce7f7c15927b53df1c (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
----------------------------------------------------------------------
-- |
-- Module      : GeneralChart
-- Maintainer  : Peter Ljunglöf
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/04/11 13:52:53 $ 
-- > CVS $Author: peb $
-- > CVS $Revision: 1.1 $
--
-- Simple implementation of deductive chart parsing
-----------------------------------------------------------------------------


module GF.OldParsing.GeneralChart
    (-- * Type definition
     Chart,
     -- * Main functions
     chartLookup,
     buildChart,
     -- * Probably not needed
     emptyChart,
     chartMember,
     chartInsert,
     chartList,
     addToChart
    ) where

-- import Trace 

import GF.Data.RedBlackSet

-- main functions

chartLookup :: (Ord item, Ord key) => Chart item key -> key -> [item]
buildChart  :: (Ord item, Ord key) => (item -> key) -> 
	       [Chart item key -> item -> [item]] -> [item] -> [item]

buildChart keyof rules axioms     = chartList (addItems axioms emptyChart)
    where addItems []             = id
	  addItems (item:items)   = addItems items . addItem item

          -- addItem item | trace ("+ "++show item++"\n") False = undefined
          addItem item            = addToChart item (keyof item) 
				    (\chart -> foldr (consequence item) chart rules)

          consequence item rule chart = addItems (rule chart item) chart

-- probably not needed

emptyChart  :: (Ord item, Ord key) => Chart item key
chartMember :: (Ord item, Ord key) => Chart item key -> item -> key -> Bool
chartInsert :: (Ord item, Ord key) => Chart item key -> item -> key -> Maybe (Chart item key)
chartList   :: (Ord item, Ord key) => Chart item key -> [item]
addToChart  :: (Ord item, Ord key) => item -> key -> (Chart item key -> Chart item key) -> Chart item key -> Chart item key

addToChart item key after chart = maybe chart after (chartInsert chart item key)


--------------------------------------------------------------------------------
-- key charts as red/black trees

newtype Chart         item key = KC (RedBlackMap key item)
    deriving Show

emptyChart                     = KC rbmEmpty
chartMember (KC tree) item key = rbmElem key item tree
chartInsert (KC tree) item key = fmap KC (rbmInsert key item tree)
chartLookup (KC tree)      key = rbmLookup key tree
chartList   (KC tree)          = concatMap snd (rbmList tree)
--------------------------------------------------------------------------------}


{--------------------------------------------------------------------------------
-- key charts as unsorted association lists -- OBSOLETE!

newtype Chart          item key = SC [(key, item)]

emptyChart = SC []
chartMember (SC chart) item key = (key,item) `elem` chart
chartInsert (SC chart) item key = if (key,item) `elem` chart then Nothing else Just (SC ((key,item):chart))
chartLookup (SC chart)      key = [ item | (key',item) <- chart, key == key' ]
chartList   (SC chart)          = map snd chart
--------------------------------------------------------------------------------}