String.hs 11.6 KB
Newer Older
marton bognar's avatar
marton bognar committed
1
module Variable.String (getFunctions) where
2
3
4

import GeneralTerms
import Program
marton bognar's avatar
marton bognar committed
5
6
import Converter
import Variable.Common
7
import Utility
8

9
10
import Data.Maybe
import Data.List
11
import Debug.Trace
12

marton bognar's avatar
marton bognar committed
13
14
15
16
17
18
19
getFunctions :: ConvertFunctions
getFunctions
  = VF {
    variableType = getVariableType,
    userTypes = getTypes,
    variableInstances = getVariableInstances,
    variableFunctions = getVariableFunctions,
20
21
    envFunctions = getEnvFunctions,
    nativeCode = freshVarFunctions
marton bognar's avatar
marton bognar committed
22
23
  }

24
25
26
27
28
29
30
freshVarFunctions :: Language -> (Type, [Constructor]) -> [String]
freshVarFunctions _ varType
  = let ctors = snd varType
        names = map (\(Constr name _) -> tail name) ctors
    in ["fresh" ++ name ++ " x b = if not (x `elem` b) then x else head [S" ++ name ++ " ('v' : show n) | n <- [0..], not (S" ++ name ++ " ('v' : show n) `elem` b)]"
    | name <- names]

marton bognar's avatar
marton bognar committed
31
getVariableType :: Language -> (Type, [Constructor])
marton bognar's avatar
marton bognar committed
32
getVariableType (nsd, _, _, _) = ("Variable", map (\ns -> Constr ('S' : nname ns) ["String"]) nsd)
33

34
35
36
37
38
39
40
41
42
43
44
45
getTypes :: Language -> [(Type, [Constructor])]
getTypes (_, sd, _, _) = map (
    \(MkDefSort name _ cds _) -> (name, map getConstr cds)
  ) sd where
    getConstr :: ConstructorDef -> Constructor
    getConstr (MkDefConstructor n lists listSorts folds _ hTypes) =
      Constr n (map (\(_, s, f) -> "(" ++ f ++ " " ++ s ++ ")") folds ++ map (\(_, t) -> "[" ++ t ++ "]") lists ++ map snd listSorts ++ hTypes)
    getConstr (MkBindConstructor n lists listSorts folds (var, ns) _ hTypes) =
      Constr n ("Variable" : (map (\(_, s, f) -> "(" ++ f ++ " " ++ s ++ ")") folds ++ map (\(_, t) -> "[" ++ t ++ "]") lists ++ map snd listSorts ++ hTypes))
    getConstr (MkVarConstructor n _) =
      Constr n ["Variable"]

46
47
48
49
getVariableInstances :: (Type, [Constructor]) -> [(Type, Type, [Function])]
getVariableInstances _ = []

getVariableFunctions :: Language -> (Type, [Constructor]) -> [Function]
marton bognar's avatar
marton bognar committed
50
getVariableFunctions lan _ = mappingFunctions lan ef ++ freeVarFunctions lan ef ++ substFunctionsC lan
51
52

_getCtorParams :: ConstructorDef -> [Parameter]
53
54
_getCtorParams (MkVarConstructor consName _) = [ConstrParam consName [VarParam "var"]]
_getCtorParams cons = [ConstrParam consName ((map (\_ -> VarParam "b") (maybeToList (cbinder cons))) ++ firstToVarParams (dropFold folds ++ lists ++ sorts) ++ [VarParam (x ++ show n) | (x, n) <- zip hTypes [1 :: Int ..]])]
55
  where
marton bognar's avatar
marton bognar committed
56
57
58
59
60
    consName = cname cons
    folds = cfolds cons
    lists = clists cons
    sorts = csorts cons
    hTypes = cnatives cons
61
62

_varCtorFreeVar :: String -> Expression
63
_varCtorFreeVar name = IfExpr (FnCall "elem" [VarExpr "var", VarExpr "c"]) (ListExpr []) (ListExpr [VarExpr "var"])
64

65
66
67
_substExpr sname consName =
  IfExpr (EQExpr (VarExpr "var") (VarExpr "c"))
    (VarExpr "sub")
68
    (ConstrInst consName [VarExpr "var"])
69

70
71
_oneDeeper namespace expr = FnCall "concat" [ListExpr (ListExpr [VarExpr "b"] : expr)]

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
_applyInhCtxsToAttrs :: SortName -> ConstructorDef -> (IdenName, [AttributeDef]) -> [(SortName, [Context])] -> Expression
_applyInhCtxsToAttrs sname ctor (iden, idenAttrs) ctxsBySname
 = let inhCtxs = (inhCtxsForSortName (sortNameForIden iden ctor) ctxsBySname)
   in foldr (\ctx rest -> fromMaybe rest (applyOneCtx ctx rest)) (VarExpr "c") inhCtxs
  where
    -- | Runs `applyOneAttr` if the identifier's attribute definitions
    -- contain an assignment to an instance of the given context
    applyOneCtx :: Context -> Expression -> Maybe Expression
    applyOneCtx ctx param
      | isJust attrForCtx = applyOneAttr (fromJust attrForCtx) [param]
      | otherwise = Nothing
      where
        attrForCtx = find (\(left, _) -> linst left == xinst ctx) idenAttrs

        -- | Apply the necessary wrap based on the attribute assignment type
        applyOneAttr :: AttributeDef -> [Expression] -> Maybe Expression
        applyOneAttr (_, RightLHS _) _ = Nothing
        applyOneAttr (l, RightAdd expr _) params
          = return (_oneDeeper (xnamespace ctx) (nextStep ++ params))
          where
            nextStep = maybeToList (applyOneAttr (l, expr) [])
        applyOneAttr (_, RightSub iden context) params
          = if elem iden (map fst lists) || elem iden (map fst folds)
              then if isJust attrsForIden
                then return (FnCall ("generateHnat" ++ xnamespace ctx) (FnCall "length" (VarExpr iden : nextStep) : params))
                else return (FnCall ("generateHnat" ++ xnamespace ctx) (FnCall "length" [VarExpr iden] : params))
              else if isJust attrsForIden
                then return (FnCall ("addToEnvironment" ++ fromJust (lookup iden sorts) ++ context) ((VarExpr iden : nextStep) ++ params))
                else return (FnCall ("addToEnvironment" ++ fromJust (lookup iden sorts) ++ context) (VarExpr iden : params))
          where
            newAttrs = filter (\(left, _) ->
                let iden = liden left
                    ctxsForSort = fromJust (lookup sname ctxsBySname)
                    ctxsForIdenSort = fromJust (lookup (sortNameForIden iden ctor) ctxsBySname)
                in (iden == "" && any (\ctx -> linst left == xinst ctx) ctxsForSort) || any (\ctx -> linst left == xinst ctx) ctxsForIdenSort
              ) (cattrs ctor)
            attrsForIden = find (\(left, _) -> liden left == iden) newAttrs
            nextStep = maybeToList (applyOneAttr (fromJust attrsForIden) [])
            lists = clists ctor
            folds = dropFold $ cfolds ctor
            sorts = csorts ctor

114
ef = EF {
115
116
  paramForCtor = _getCtorParams,
  freeVarExprForVarCtor = _varCtorFreeVar,
117
  applyInhCtxsToAttrs = _applyInhCtxsToAttrs,
118
  includeBinders = True
119
}
120

121
122
123
124
125
126
127
-- | Generates the following for sorts with variable access:
-- * Substitute functions for every sort that is related to the given sort by
-- the first sort having a context with a variable of the type of the second sort
substFunctionsC :: Language -> [Function]
substFunctionsC (nsd, sd, _, _) =
  concatMap (\(MkDefSort sortName ctxs ctors rewrite) ->
    let inhCtxs = [INH x y | INH x y <- ctxs]
marton bognar's avatar
marton bognar committed
128
129
130
131
    in Fn (sortName ++ "VarReplace") (map (\ctor ->
      ([VarParam "orig", VarParam "sub"] ++ _getCtorParams ctor, varReplaceCallForCtor ctor)
    ) ctors)
    : map (\ctx ->
132
      let sortOfCtxNamespace = sortNameForNamespaceName (xnamespace ctx) nsd
marton bognar's avatar
marton bognar committed
133
134
135
136
      in Fn (sortName ++ sortOfCtxNamespace ++ "Substitute") (map (\ctor ->
          ([VarParam "orig", VarParam "sub"] ++ _getCtorParams ctor, substExprForCtor sortName sortOfCtxNamespace ctor)
        ) ctors)
    ) inhCtxs
137
138
139
140
141
  ) sortsWithVarAccess
  where
    ctxsBySname = map snameAndCtxs sd
    varAccessBySname = varAccessBySortName sd
    sortsWithVarAccess = filter (\sort -> isJust (lookup (sname sort) varAccessBySname)) sd
marton bognar's avatar
marton bognar committed
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

    freeVariablesCall :: ConstructorDef -> (IdenName, SortName) -> Expression
    freeVariablesCall ctor (iden, idenSort)
      = if iden `elem` map fst folds
          then FnCall "concat" [FnCall "fmap" [FnCall fnName substParams, idenExpr]]
          else if iden `elem` map fst lists
            then FnCall "concat" [FnCall "map" [FnCall fnName substParams, idenExpr]]
            else FnCall fnName (substParams ++ [idenExpr])
      where
        folds = dropFold (cfolds ctor)
        lists = clists ctor
        fnName = "freeVariables" ++ idenSort
        idenExpr = VarExpr iden
        substParams = [ListExpr []]

    varReplaceCall :: ConstructorDef -> [Expression] -> IdenName -> Expression
    varReplaceCall ctor params iden
      = if iden `elem` map fst folds
          then FnCall "fmap" [FnCall fnName params, idenExpr]
          else if iden `elem` map fst lists
            then FnCall "map" [FnCall fnName params, idenExpr]
            else FnCall fnName (params ++ [idenExpr])
      where
        folds = dropFold (cfolds ctor)
        lists = clists ctor
        fnName = (sortNameForIden iden ctor ++ "VarReplace")
        idenExpr = VarExpr iden

    varReplaceCallForCtor :: ConstructorDef -> Expression
    varReplaceCallForCtor (MkVarConstructor ctorName _) =
      IfExpr (EQExpr (VarExpr "var") (VarExpr "orig"))
        (ConstrInst ctorName [VarExpr "sub"])
        (ConstrInst ctorName [VarExpr "var"])
    varReplaceCallForCtor ctor =
      ConstrInst
        (cname ctor)
        (
          binder
          ++ map varReplaceCallForIden idensAndAttrs
          ++ [VarExpr (x ++ show n) | (x, n) <- zip (cnatives ctor) [1 :: Int ..]]
        )
183
      where
marton bognar's avatar
marton bognar committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
        binder = if isBind ctor
          then [FnCall
            ("fresh" ++ snd (fromJust (cbinder ctor)))
            [VarExpr "b", FnCall "concat" [ListExpr (ListExpr [VarExpr "sub"] : map (freeVariablesCall ctor) (folds ++ lists ++ csorts ctor))]]]
          else []
        idensAndAttrs = attrsByIden ctor
        folds = dropFold (cfolds ctor)
        lists = clists ctor

        varReplaceCallForIden :: (IdenName, [AttributeDef]) -> Expression
        varReplaceCallForIden (iden, idenAttrs)
          = if fromJust (lookup sortNameOfIden varAccessBySname)
              then if iden `elem` map fst folds
                then FnCall "fmap" [FnCall fnName substParams, idenExpr]
                else if iden `elem` map fst lists
                  then FnCall "map" [FnCall fnName substParams, idenExpr]
                  else FnCall fnName (substParams ++ [idenExpr])
              else idenExpr
202
          where
marton bognar's avatar
marton bognar committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
            fnName = sortNameForIden iden ctor ++ "VarReplace"
            idenExpr = if null binder
              then VarExpr iden
              else varReplaceCall ctor [VarExpr "b", head binder] iden
            substParams = [VarExpr "orig", VarExpr "sub"]
            sortNameOfIden = sortNameForIden iden ctor

    substExprForCtor :: SortName -> SortName -> ConstructorDef -> Expression
    substExprForCtor sortName sortOfCtxNamespace (MkVarConstructor ctorName _)
      | sortName == sortOfCtxNamespace
        = IfExpr (EQExpr (VarExpr "var") (VarExpr "orig"))
            (VarExpr "sub")
            (ConstrInst ctorName [VarExpr "var"])
      | otherwise = ConstrInst ctorName [VarExpr "var"]
    substExprForCtor sortName sortOfCtxNamespace ctor =
      ConstrInst
        (cname ctor)
        (
          binder
          ++ map substCallForIden idensAndAttrs
          ++ [VarExpr (x ++ show n) | (x, n) <- zip (cnatives ctor) [1 :: Int ..]]
        )
      where
        binder = if isBind ctor
          then [FnCall
            ("fresh" ++ snd (fromJust (cbinder ctor)))
            [VarExpr "b", FnCall "concat" [
              ListExpr (
                map
                  (freeVariablesCall ctor)
                  (("sub", sortOfCtxNamespace) : folds ++ lists ++ csorts ctor)
              )
            ]]]
          else []
        idensAndAttrs = attrsByIden ctor
        folds = dropFold (cfolds ctor)
        lists = clists ctor

        -- | Construct a mapping function call for an identifier
        substCallForIden :: (IdenName, [AttributeDef]) -> Expression
        substCallForIden (iden, idenAttrs)
244
          | sortHasCtxForSort (sortNameForIden iden ctor) sortOfCtxNamespace nsd ctxsBySname
marton bognar's avatar
marton bognar committed
245
246
247
            = if fromJust (lookup sortNameOfIden varAccessBySname)
                then if iden `elem` map fst folds
                  then FnCall "fmap" [FnCall fnName substParams, idenExpr]
248
                  else if iden `elem` map fst lists
marton bognar's avatar
marton bognar committed
249
                    then FnCall "map" [FnCall fnName substParams, idenExpr]
250
                    else FnCall fnName (substParams ++ [idenExpr])
marton bognar's avatar
marton bognar committed
251
252
253
254
255
256
257
258
259
                else idenExpr
          | otherwise = VarExpr iden
          where
            fnName = sortNameForIden iden ctor ++ sortOfCtxNamespace ++ "Substitute"
            idenExpr = if null binder
              then VarExpr iden
              else varReplaceCall ctor [VarExpr "b", head binder] iden
            substParams = [VarExpr "orig", VarExpr "sub"]
            sortNameOfIden = sortNameForIden iden ctor