String.hs 9.07 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
20
getFunctions :: ConvertFunctions
getFunctions
  = VF {
    variableType = getVariableType,
    envType = getEnvType,
    userTypes = getTypes,
    variableInstances = getVariableInstances,
    variableFunctions = getVariableFunctions,
21
22
    envFunctions = getEnvFunctions,
    nativeCode = freshVarFunctions
marton bognar's avatar
marton bognar committed
23
24
  }

25
26
27
28
29
30
31
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
32
getVariableType :: Language -> (Type, [Constructor])
marton bognar's avatar
marton bognar committed
33
getVariableType (nsd, _, _, _) = ("Variable", map (\ns -> Constr ('S' : nname ns) ["String"]) nsd)
34

35
36
37
38
39
40
41
42
43
44
45
46
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"]

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

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

_getCtorParams :: ConstructorDef -> [Parameter]
54
55
_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 ..]])]
56
  where
marton bognar's avatar
marton bognar committed
57
58
59
60
61
    consName = cname cons
    folds = cfolds cons
    lists = clists cons
    sorts = csorts cons
    hTypes = cnatives cons
62
63

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

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

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

73
ef = EF {
74
75
  paramForCtor = _getCtorParams,
  freeVarExprForVarCtor = _varCtorFreeVar,
76
  transformForAddAttr = _oneDeeper,
77
  includeBinders = True
78
}
79

80
81
82
83
84
85
86
-- | 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
87
88
89
90
    in Fn (sortName ++ "VarReplace") (map (\ctor ->
      ([VarParam "orig", VarParam "sub"] ++ _getCtorParams ctor, varReplaceCallForCtor ctor)
    ) ctors)
    : map (\ctx ->
91
      let sortOfCtxNamespace = sortNameForNamespaceName (xnamespace ctx) nsd
marton bognar's avatar
marton bognar committed
92
93
94
95
      in Fn (sortName ++ sortOfCtxNamespace ++ "Substitute") (map (\ctor ->
          ([VarParam "orig", VarParam "sub"] ++ _getCtorParams ctor, substExprForCtor sortName sortOfCtxNamespace ctor)
        ) ctors)
    ) inhCtxs
96
97
98
99
100
  ) 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
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

    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 ..]]
        )
142
      where
marton bognar's avatar
marton bognar committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        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
161
          where
marton bognar's avatar
marton bognar committed
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
            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)
203
          | sortHasCtxForSort (sortNameForIden iden ctor) sortOfCtxNamespace nsd ctxsBySname
marton bognar's avatar
marton bognar committed
204
205
206
            = if fromJust (lookup sortNameOfIden varAccessBySname)
                then if iden `elem` map fst folds
                  then FnCall "fmap" [FnCall fnName substParams, idenExpr]
207
                  else if iden `elem` map fst lists
marton bognar's avatar
marton bognar committed
208
                    then FnCall "map" [FnCall fnName substParams, idenExpr]
209
                    else FnCall fnName (substParams ++ [idenExpr])
marton bognar's avatar
marton bognar committed
210
211
212
213
214
215
216
217
218
                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