summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Grammar/Lookup.hs
blob: 074f0c5ec5ae4ddbabf4cbbba39df1c4b10e8d57 (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
{-# LANGUAGE PatternGuards #-}
----------------------------------------------------------------------
-- |
-- Module      : Lookup
-- Maintainer  : AR
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/10/27 13:21:53 $ 
-- > CVS $Author: aarne $
-- > CVS $Revision: 1.15 $
--
-- Lookup in source (concrete and resource) when compiling.
--
-- lookup in resource and concrete in compiling; for abstract, use 'Look'
-----------------------------------------------------------------------------

module GF.Grammar.Lookup (
           lookupIdent,
           lookupIdentInfo,
           lookupOrigInfo,
           allOrigInfos,
           lookupResDef,
	       lookupResType, 
           lookupOverload,
	       lookupParamValues, 
	       allParamValues,
	       lookupAbsDef, 
	       lookupLincat, 
	       lookupFunType,
	       lookupCatContext
	      ) where

import GF.Data.Operations
import GF.Infra.Ident
import GF.Infra.Modules
import GF.Grammar.Macros
import GF.Grammar.Grammar
import GF.Grammar.Printer
import GF.Grammar.Predef
import GF.Grammar.Lockfield

import Data.List (nub,sortBy)
import Control.Monad
import Text.PrettyPrint

-- whether lock fields are added in reuse
lock c = lockRecType c -- return
unlock c = unlockRecord c -- return

-- to look up a constant etc in a search tree --- why here? AR 29/5/2008
lookupIdent :: Ident -> BinTree Ident b -> Err b
lookupIdent c t =
  case lookupTree showIdent c t of
    Ok v  -> return v
    Bad _ -> Bad ("unknown identifier" +++ showIdent c)

lookupIdentInfo :: ModInfo Ident a -> Ident -> Err a
lookupIdentInfo mo i = lookupIdent i (jments mo)

lookupResDef :: SourceGrammar -> Ident -> Ident -> Err Term
lookupResDef gr m c
  | isPredefCat c = lock c defLinType 
  | otherwise     = look m c
  where 
    look m c = do
      mo <- lookupModule gr m
      info <- lookupIdentInfo mo c
      case info of
        ResOper _ (Just t) -> return t
        ResOper _ Nothing  -> return (Q m c)
        CncCat (Just ty) _ _ -> lock c ty
        CncCat _ _ _         -> lock c defLinType
      
        CncFun (Just (cat,_,_)) (Just tr) _ -> unlock cat tr
        CncFun _                (Just tr) _ -> return tr

        AnyInd _ n        -> look n c
        ResParam _ _      -> return (QC m c)
        ResValue _        -> return (QC m c)
        _   -> Bad $ render (ppIdent c <+> text "is not defined in resource" <+> ppIdent m)

lookupResType :: SourceGrammar -> Ident -> Ident -> Err Type
lookupResType gr m c = do
  mo <- lookupModule gr m
  info <- lookupIdentInfo mo c
  case info of
    ResOper (Just t) _ -> return t

    -- used in reused concrete
    CncCat _ _ _ -> return typeType
    CncFun (Just (cat,cont,val)) _ _ -> do
          val' <- lock cat val 
          return $ mkProd cont val' []
    AnyInd _ n        -> lookupResType gr n c
    ResParam _ _      -> return typePType
    ResValue t        -> return t
    _   -> Bad $ render (ppIdent c <+> text "has no type defined in resource" <+> ppIdent m)

lookupOverload :: SourceGrammar -> Ident -> Ident -> Err [([Type],(Type,Term))]
lookupOverload gr m c = do
    mo <- lookupModule gr m
    info <- lookupIdentInfo mo c
    case info of
      ResOverload os tysts -> do
            tss <- mapM (\x -> lookupOverload gr x c) os
            return $ [let (args,val) = typeFormCnc ty in (map (\(b,x,t) -> t) args,(val,tr)) | 
                      (ty,tr) <- tysts] ++ 
                     concat tss

      AnyInd _ n  -> lookupOverload gr n c
      _   -> Bad $ render (ppIdent c <+> text "is not an overloaded operation")

-- | returns the original 'Info' and the module where it was found
lookupOrigInfo :: SourceGrammar -> Ident -> Ident -> Err (Ident,Info)
lookupOrigInfo gr m c = do
  mo <- lookupModule gr m
  info <- lookupIdentInfo mo c
  case info of
    AnyInd _ n  -> lookupOrigInfo gr n c
    i           -> return (m,i)

allOrigInfos :: SourceGrammar -> Ident -> [(Ident,Info)]
allOrigInfos gr m = errVal [] $ do
  mo <- lookupModule gr m
  return [(c,i) | (c,_) <- tree2list (jments mo), Ok (_,i) <- [look c]]
  where
    look = lookupOrigInfo gr m

lookupParamValues :: SourceGrammar -> Ident -> Ident -> Err [Term]
lookupParamValues gr m c = do
  (_,info) <- lookupOrigInfo gr m c
  case info of
    ResParam _ (Just pvs) -> return pvs
    _                     -> Bad $ render (ppIdent c <+> text "has no parameter values defined in resource" <+> ppIdent m)

allParamValues :: SourceGrammar -> Type -> Err [Term]
allParamValues cnc ptyp = case ptyp of
     _ | Just n <- isTypeInts ptyp -> return [EInt i | i <- [0..n]]
     QC p c -> lookupParamValues cnc p c
     Q  p c -> lookupResDef cnc p c >>= allParamValues cnc
     RecType r -> do
       let (ls,tys) = unzip $ sortByFst r
       tss <- mapM (allParamValues cnc) tys
       return [R (zipAssign ls ts) | ts <- combinations tss]
     _ -> Bad (render (text "cannot find parameter values for" <+> ppTerm Unqualified 0 ptyp))
  where
    -- to normalize records and record types
    sortByFst = sortBy (\ x y -> compare (fst x) (fst y))

lookupAbsDef :: SourceGrammar -> Ident -> Ident -> Err (Maybe Int,Maybe [Equation])
lookupAbsDef gr m c = errIn (render (text "looking up absdef of" <+> ppIdent c)) $ do
  mo <- lookupModule gr m
  info <- lookupIdentInfo mo c
  case info of
    AbsFun _ a d -> return (a,d)
    AnyInd _ n   -> lookupAbsDef gr n c
    _            -> return (Nothing,Nothing)

lookupLincat :: SourceGrammar -> Ident -> Ident -> Err Type
lookupLincat gr m c | isPredefCat c = return defLinType --- ad hoc; not needed?
lookupLincat gr m c = do
  mo <- lookupModule gr m
  info <- lookupIdentInfo mo c
  case info of
    CncCat (Just t) _ _ -> return t
    AnyInd _ n          -> lookupLincat gr n c
    _                   -> Bad (render (ppIdent c <+> text "has no linearization type in" <+> ppIdent m))

-- | this is needed at compile time
lookupFunType :: SourceGrammar -> Ident -> Ident -> Err Type
lookupFunType gr m c = do
  mo <- lookupModule gr m
  info <- lookupIdentInfo mo c
  case info of
    AbsFun (Just t) _ _  -> return t
    AnyInd _ n           -> lookupFunType gr n c
    _                    -> Bad (render (text "cannot find type of" <+> ppIdent c))

-- | this is needed at compile time
lookupCatContext :: SourceGrammar -> Ident -> Ident -> Err Context
lookupCatContext gr m c = do
  mo <- lookupModule gr m
  info <- lookupIdentInfo mo c
  case info of
    AbsCat (Just co) _ -> return co
    AnyInd _ n         -> lookupCatContext gr n c
    _                  -> Bad (render (text "unknown category" <+> ppIdent c))