summaryrefslogtreecommitdiff
path: root/src/GF/Grammar/AppPredefined.hs
blob: cfb6baf1d92cc845ceda2412b5754123dd9ef8ee (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
----------------------------------------------------------------------
-- |
-- Module      : AppPredefined
-- Maintainer  : AR
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/10/06 14:21:34 $ 
-- > CVS $Author: aarne $
-- > CVS $Revision: 1.13 $
--
-- Predefined function type signatures and definitions.
-----------------------------------------------------------------------------

module GF.Grammar.AppPredefined (isInPredefined, typPredefined, appPredefined
		     ) where

import GF.Infra.Ident
import GF.Data.Operations
import GF.Grammar.Predef
import GF.Grammar.Grammar
import GF.Grammar.Macros
import GF.Grammar.PrGrammar (prt,prt_,prtBad)
import qualified Data.ByteString.Char8 as BS

-- predefined function type signatures and definitions. AR 12/3/2003.

isInPredefined :: Ident -> Bool
isInPredefined = err (const True) (const False) . typPredefined

typPredefined :: Ident -> Err Type
typPredefined f
  | f == cInt       = return typePType
  | f == cFloat     = return typePType
  | f == cErrorType = return typeType
  | f == cInts      = return $ mkFunType [typeInt] typePType
  | f == cPBool     = return typePType
  | f == cError     = return $ mkFunType [typeStr] typeError  -- non-can. of empty set
  | f == cPFalse    = return $ typePBool
  | f == cPTrue     = return $ typePBool
  | f == cDp        = return $ mkFunType [typeInt,typeTok] typeTok
  | f == cDrop      = return $ mkFunType [typeInt,typeTok] typeTok
  | f == cEqInt     = return $ mkFunType [typeInt,typeInt] typePBool
  | f == cLessInt   = return $ mkFunType [typeInt,typeInt] typePBool
  | f == cEqStr     = return $ mkFunType [typeTok,typeTok] typePBool
  | f == cLength    = return $ mkFunType [typeTok] typeInt
  | f == cOccur     = return $ mkFunType [typeTok,typeTok] typePBool
  | f == cOccurs    = return $ mkFunType [typeTok,typeTok] typePBool
  | f == cPlus      = return $ mkFunType [typeInt,typeInt] (typeInt)
----  "read"   -> (P : Type) -> Tok -> P
  | f == cShow      = return $ mkProd -- (P : PType) -> P -> Tok
    ([(varP,typePType),(identW,Vr varP)],typeStr,[]) 
  | f == cToStr     = return $ mkProd -- (L : Type)  -> L -> Str
    ([(varL,typeType),(identW,Vr varL)],typeStr,[]) 
  | f == cMapStr    = return $ mkProd -- (L : Type)  -> (Str -> Str) -> L -> L
    ([(varL,typeType),(identW,mkFunType [typeStr] typeStr),(identW,Vr varL)],Vr varL,[])
  | f == cTake      = return $ mkFunType [typeInt,typeTok] typeTok
  | f == cTk        = return $ mkFunType [typeInt,typeTok] typeTok
  | otherwise       = prtBad "unknown in Predef:" f

varL :: Ident
varL = identC (BS.pack "L")

varP :: Ident
varP = identC (BS.pack "P")

appPredefined :: Term -> Err (Term,Bool)
appPredefined t = case t of
  App f x0 -> do
   (x,_) <- appPredefined x0
   case f of
    -- one-place functions
    Q mod f | mod == cPredef ->
      case x of
        (K s) | f == cLength -> retb $ EInt $ toInteger $ length s
        _                    -> retb t

    -- two-place functions
    App (Q mod f) z0 | mod == cPredef -> do
     (z,_) <- appPredefined z0
     case (norm z, norm x) of
      (EInt i, K s)    | f == cDrop    -> retb $ K (drop (fi i) s)
      (EInt i, K s)    | f == cTake    -> retb $ K (take (fi i) s)
      (EInt i, K s)    | f == cTk      -> retb $ K (take (max 0 (length s - fi i)) s)
      (EInt i, K s)    | f == cDp      -> retb $ K (drop (max 0 (length s - fi i)) s)
      (K s,    K t)    | f == cEqStr   -> retb $ if s == t then predefTrue else predefFalse
      (K s,    K t)    | f == cOccur   -> retb $ if substring s t then predefTrue else predefFalse
      (K s,    K t)    | f == cOccurs  -> retb $ if any (flip elem t) s then predefTrue else predefFalse
      (EInt i, EInt j) | f == cEqInt   -> retb $ if i==j then predefTrue else predefFalse
      (EInt i, EInt j) | f == cLessInt -> retb $ if i<j  then predefTrue else predefFalse
      (EInt i, EInt j) | f == cPlus    -> retb $ EInt $ i+j
      (_,      t)      | f == cShow    -> retb $ foldr C Empty $ map K $ words $ prt t
      (_,      K s)    | f == cRead    -> retb $ Cn (identC (BS.pack s)) --- because of K, only works for atomic tags
      (_,      t)      | f == cToStr   -> trm2str t >>= retb
      _ -> retb t ---- prtBad "cannot compute predefined" t

    -- three-place functions
    App (App (Q mod f) z0) y0 | mod == cPredef -> do
     (y,_) <- appPredefined y0
     (z,_) <- appPredefined z0
     case (z, y, x) of
      (ty,op,t) | f == cMapStr -> retf $ mapStr ty op t
      _ -> retb t ---- prtBad "cannot compute predefined" t

    _ -> retb t ---- prtBad "cannot compute predefined" t
  _ -> retb t
                  ---- should really check the absence of arg variables
 where
   retb t = return (retc t,True)  -- no further computing needed
   retf t = return (retc t,False) -- must be computed further
   retc t = case t of
     K [] -> t
     K s  -> foldr1 C (map K (words s))
     _    -> t
   norm t = case t of
     Empty -> K []
     C u v -> case (norm u,norm v) of
       (K x,K y) -> K (x +++ y)
       _ -> t
     _ -> t
   fi = fromInteger

-- read makes variables into constants

predefTrue = Q cPredef cPTrue
predefFalse = Q cPredef cPFalse

substring :: String -> String -> Bool
substring s t = case (s,t) of
  (c:cs, d:ds) -> (c == d && substring cs ds) || substring s ds
  ([],_) -> True
  _ -> False

trm2str :: Term -> Err Term
trm2str t = case t of
  R ((_,(_,s)):_) -> trm2str s
  T _ ((_,s):_)   -> trm2str s
  TSh _ ((_,s):_) -> trm2str s
  V _ (s:_)       -> trm2str s
  C _ _         -> return $ t
  K _           -> return $ t
  S c _         -> trm2str c
  Empty         -> return $ t
  _ -> prtBad "cannot get Str from term" t

-- simultaneous recursion on type and term: type arg is essential!
-- But simplify the task by assuming records are type-annotated
-- (this has been done in type checking)
mapStr :: Type -> Term -> Term -> Term
mapStr ty f t = case (ty,t) of
  _ | elem ty [typeStr,typeTok] -> App f t
  (_,        R ts)    -> R [(l,mapField v)   | (l,v) <- ts]
  (Table a b,T ti cs) -> T ti [(p,mapStr b f v) | (p,v) <- cs]
  _    -> t 
 where
   mapField (mty,te) = case mty of
     Just ty -> (mty,mapStr ty f te)
     _ -> (mty,te)