1(*
2    Copyright (c) 2000
3        Cambridge University Technical Services Limited
4
5    Modified David C. J. Matthews 2009, 2015.
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License version 2.1 as published by the Free Software Foundation.
10    
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15    
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19*)
20
21signature STRUCTVALSIG =
22sig
23    type location =
24        { file: string, startLine: FixedInt.int, startPosition: FixedInt.int,
25          endLine: FixedInt.int, endPosition: FixedInt.int }
26    val inBasis: location
27
28    type codetree and level
29    type univTable
30
31    (* Types *)
32  
33    datatype 'a possRef = FrozenRef of 'a | VariableRef of 'a ref
34    val pling: 'a possRef -> 'a
35    val updatePR: 'a possRef * 'a -> unit
36  
37    (* Standard type constructors. *)
38  
39    type typeVarForm
40    type uniqueId
41    
42    type typeIdDescription = { location: location, name: string, description: string }
43    type references =
44        {
45            exportedRef: bool ref, localRef: location list ref,
46            recursiveRef: (location * string) list ref
47        } option
48    val makeRef: unit -> references
49
50    datatype typeId =
51        TypeId of { access: valAccess, description: typeIdDescription, idKind: typeIdKind }
52
53    and typeIdKind =
54        Free of { uid: uniqueId, allowUpdate: bool, arity: int  }
55    |   Bound of { offset: int, eqType: bool possRef, isDatatype: bool, arity: int }
56    |   TypeFn of typeVarForm list * types
57
58        (* A type is the union of these different cases. *)
59    and types = 
60        TypeVar of typeVarForm
61
62    |   TypeConstruction of
63        {
64            name:  string,
65            constr: typeConstrs,
66            args:  types list,
67            locations: locationProp list
68        }
69
70    |   FunctionType of
71        { 
72            arg:    types,
73            result: types
74        }
75
76    |   LabelledType  of labelledRec
77
78    |   OverloadSet   of
79        {
80            typeset: typeConstrs list
81        }
82
83    |   BadType
84  
85    |   EmptyType
86
87    and typeConstrs = 
88        TypeConstrs of
89        {
90            name:       string,
91            typeVars:   typeVarForm list,
92            identifier: typeId,
93            locations:  locationProp list (* Location of declaration *)
94        }
95
96    and labelFieldList =
97        FieldList of string list * bool (* True if this is frozen *)
98    |   FlexibleList of labelFieldList ref
99
100    and valAccess =
101        Global   of codetree
102    |   Local    of { addr: int ref, level: level ref }
103    |   Selected of { addr: int,     base:  valAccess }
104    |   Formal   of int
105    |   Overloaded of typeDependent (* Values only. *)
106
107    (* Structures. *)
108    and structVals = 
109        Struct of
110        {
111            name:   string,
112            signat: signatures,
113            access: valAccess,
114            locations: locationProp list
115        }
116
117    and signatures =
118        Signatures of
119        { 
120            name:               string,
121            tab:                univTable,
122            typeIdMap:          int -> typeId,
123            firstBoundIndex:    int,
124            boundIds:           typeId list,
125            locations:          locationProp list
126        }
127 
128    and functors =
129        Functor of
130        {
131            name:       string,
132            arg:        structVals,
133            result:     signatures,
134            access:     valAccess,
135            locations:  locationProp list
136        }
137
138    (* Values. *)
139    and typeDependent =
140        Print
141    |   GetPretty
142    |   MakeString
143    |   AddPretty
144    |   Equal
145    |   NotEqual
146    |   AddOverload
147    |   TypeDep
148    |   GetLocation
149
150    and values =
151        Value of
152        {
153            name: string,
154            typeOf: types,
155            access: valAccess,
156            class: valueClass,
157            locations: locationProp list,
158            references: references,
159            instanceTypes: types list ref option
160        }
161
162    (* Classes of values. *)
163    and valueClass =
164        ValBound
165    |   PattBound
166    |   Exception
167    |   Constructor of { nullary: bool, ofConstrs: int }
168
169    (* Location properties.  A value may have some or all of these. *)
170    and locationProp =
171        DeclaredAt of location
172    |   OpenedAt of location
173    |   StructureAt of location
174    |   SequenceNo of FixedInt.int
175
176    withtype labelledRec =
177    {
178        (* Fields actually present in this record.  If this was flexible at some
179           stage there may be extra fields listed in the full field list. *)
180        recList: { name: string, typeof: types } list,
181        (* The names of all the fields including extra fields. *)
182        fullList: labelFieldList
183    }
184
185
186    (* type identifiers. *)
187    val isEquality:   typeId -> bool
188    val offsetId:     typeId -> int
189    val idAccess:     typeId -> valAccess
190    val sameTypeId:   typeId * typeId -> bool
191    val setEquality:  typeId * bool -> unit
192
193    val basisDescription: string -> typeIdDescription
194    val makeFreeId: int * valAccess * bool * typeIdDescription -> typeId
195    val makeFreeIdEqUpdate: int * valAccess * bool * typeIdDescription -> typeId
196    val makeBoundId: int * valAccess * int * bool * bool * typeIdDescription -> typeId
197    val makeBoundIdWithEqUpdate: int * valAccess * int * bool * bool * typeIdDescription -> typeId
198    val makeTypeFunction: typeIdDescription * (typeVarForm list * types) -> typeId
199    
200    (* Types *)
201    val badType:   types
202    val emptyType: types
203
204    val isBad:     types -> bool
205    val isEmpty:   types -> bool
206
207    val recordFields   : labelledRec -> string list
208    val recordIsFrozen : labelledRec -> bool
209
210    val tcName:            typeConstrs -> string
211    val tcArity:           typeConstrs -> int
212    val tcTypeVars:        typeConstrs -> typeVarForm list
213    val tcEquality:        typeConstrs -> bool
214    val tcSetEquality:     typeConstrs * bool -> unit
215    val tcIdentifier:      typeConstrs -> typeId
216    val tcLocations:       typeConstrs -> locationProp list
217    val tcIsAbbreviation:  typeConstrs -> bool
218
219    val makeTypeConstructor:
220        string * typeVarForm list * typeId * locationProp list -> typeConstrs
221
222    datatype typeConstrSet = (* A type constructor with its, possible, value constructors. *)
223        TypeConstrSet of typeConstrs * values list
224
225    val tsConstr: typeConstrSet -> typeConstrs
226    val tsConstructors: typeConstrSet -> values list
227
228    val tvLevel:        typeVarForm -> int
229    val tvEquality:     typeVarForm -> bool
230    val tvPrintity:     typeVarForm -> bool
231    val tvNonUnifiable: typeVarForm -> bool
232    val tvValue:        typeVarForm -> types
233    val tvSetValue:     typeVarForm * types -> unit
234
235    val sameTv: typeVarForm * typeVarForm -> bool
236
237    val makeTv:
238        {value: types, level: int, equality: bool, nonunifiable: bool, printable: bool } -> typeVarForm
239
240    val generalisable: int
241
242    (* Access to values, structures etc. *)
243    val makeGlobal:   codetree -> valAccess
244    val makeLocal:    unit -> valAccess
245    val makeSelected: int * structVals -> valAccess
246
247    val vaGlobal:   valAccess -> codetree
248    val vaLocal:    valAccess -> { addr: int ref, level: level ref }
249
250    val makeEmptyGlobal:   string -> structVals
251    val makeGlobalStruct:  string * signatures * codetree * locationProp list -> structVals
252    val makeLocalStruct:   string * signatures * locationProp list -> structVals
253    val makeFormalStruct:  string * signatures * int * locationProp list -> structVals
254
255    val makeSelectedStruct: structVals * structVals * locationProp list -> structVals
256
257    (* Functors *)
258    val makeFunctor: string * structVals * signatures * valAccess * locationProp list -> functors
259
260    (* Signatures *)
261    val makeSignatureTable: unit -> univTable
262    val makeSignature: string * univTable * int * locationProp list * (int -> typeId) * typeId list -> signatures
263
264    (* Values. *)
265    val valName: values -> string
266    val valTypeOf: values -> types
267    val undefinedValue: values
268    val isUndefinedValue: values -> bool
269    val isConstructor: values -> bool
270    val isValueConstructor: values -> bool
271
272    val makeOverloaded: string * types * typeDependent -> values
273    val makeValueConstr: string * types * bool * int * valAccess * locationProp list -> values
274
275    (* Infix status *)
276    datatype infixity = 
277        Infix of int
278    |   InfixR of int
279    |   Nonfix
280
281    datatype fixStatus = FixStatus of string * infixity
282
283    datatype env =
284        Env of
285        {
286            lookupVal:    string -> values option,
287            lookupType:   string -> typeConstrSet option,
288            lookupFix:    string -> fixStatus option,
289            lookupStruct: string -> structVals option,
290            lookupSig:    string -> signatures option,
291            lookupFunct:  string -> functors option,
292            enterVal:     string * values      -> unit,
293            enterType:    string * typeConstrSet -> unit,
294            enterFix:     string * fixStatus   -> unit,
295            enterStruct:  string * structVals  -> unit,
296            enterSig:     string * signatures  -> unit,
297            enterFunct:   string * functors    -> unit,
298            allValNames:  unit -> string list
299        }
300
301    val makeEnv: univTable -> env
302
303    val valueVar:      values      Universal.tag
304    val typeConstrVar: typeConstrSet Universal.tag
305    val fixVar:        fixStatus   Universal.tag
306    val structVar:     structVals  Universal.tag
307    val signatureVar:  signatures  Universal.tag
308    val functorVar:    functors    Universal.tag
309
310    (* Types that can be shared. *)
311    structure Sharing:
312    sig
313        type codetree   = codetree
314        and  signatures = signatures
315        and  types      = types
316        and  values     = values
317        and  typeId     = typeId
318        and  structVals = structVals
319        and  valAccess  = valAccess
320        and  typeConstrs= typeConstrs
321        and  typeConstrSet=typeConstrSet
322        and  env        = env
323        and  univTable  = univTable
324        and  fixStatus  = fixStatus
325        and  infixity   = infixity
326        and  functors   = functors
327        and  locationProp = locationProp
328        and  typeVarForm = typeVarForm
329        and  level = level
330    end
331end;
332
333