-
Notifications
You must be signed in to change notification settings - Fork 4
/
TypeSystem.fs
404 lines (375 loc) · 13.8 KB
/
TypeSystem.fs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/// <summary>
/// The Starling type system.
///
/// <para>
/// This module contains the <c>Typed</c> and <c>CTyped</c>
/// types, which implement the core of Starling's type system.
/// Included modules provide type checking, mapping between
/// different instances of <c>Typed</c> types, and convenience
/// functions for extracting types and values from <c>Typed</c>
/// objects.
/// </para>
///
/// <para>
/// In the documentation for this module, we use 'type'
/// to refer to types in the Starling object language, and
/// 'meta-type' to refer to types in F#.
/// </para>
/// </summary>
module Starling.Core.TypeSystem
open Chessie.ErrorHandling
open Starling.Utils
/// <summary>
/// A subtype definition.
/// </summary>
type Subtype =
| /// <summary>The subtype of this item is not fixed yet.</summary>
Indef
| /// <summary>This item has the 'normal' subtype.</summary>
Normal
| /// <summary>This item has a named subtype.</summary>
Named of string
/// <summary>
/// An extended type record for primitive types.
/// </summary>
type PrimTypeRec =
{ /// <summary>The subtype of this primitive type.</summary>
PrimSubtype : Subtype }
/// <summary>
/// An extended type record for array types.
/// </summary>
type ArrayTypeRec =
{ /// <summary>The element type of the array.</summary>
ElementType : Type
/// <summary>The length of the array.</summary>
Length : int option }
/// <summary>
/// A typed item.
/// </summary>
/// <typeparam name="Int">
/// The meta-type of the item when it is typed as <c>Int</c>.
/// </typeparam>
/// <typeparam name="Bool">
/// The meta-type of the item when it is typed as <c>Bool</c>.
/// </typeparam>
/// <typeparam name="Array">
/// The meta-type of the item when it is typed as <c>Array</c>.
/// </typeparam>
and Typed<'Int, 'Bool, 'Array> =
/// <summary>An item of integral type.</summary>
| Int of typerec : PrimTypeRec * value : 'Int
/// <summary>
/// An item of Boolean type.
/// </summary>
| Bool of typerec : PrimTypeRec * value: 'Bool
/// <summary>
/// An item of array type, annotated by the type of the array element
/// and an optional length.
/// </summary>
| Array of typerec : ArrayTypeRec * value : 'Array
override this.ToString() =
match this with
| Int (_, x) -> sprintf "I(%A)" x
| Bool (_, x) -> sprintf "B(%A)" x
| Array ({ ElementType = eltype; Length = length}, x) -> sprintf "A<%A, %A>(%A)" eltype length x
/// <summary>
/// A typed item where every type leads to the same meta-type.
/// </summary>
/// <typeparam name="T">
/// The meta-type to use for all <c>Typed</c> parameters.
/// </typeparam>
and CTyped<'T> = Typed<'T, 'T, 'T>
/// <summary>
/// A standalone type annotation.
/// </summary>
and Type = CTyped<unit>
/// <summary>
/// Functions for working with <c>Typed</c> values.
/// </summary>
[<AutoOpen>]
module Typed =
/// <summary>
/// Extracts the <c>Type</c> of a <c>Typed</c> item.
/// </summary>
/// <param name="typed">
/// The typed item.
/// </param>
/// <returns>
/// The item's <c>Type</c>.
/// </returns>
let typeOf : Typed<_, _, _> -> Type =
function
| Bool (t, _) -> Bool (t, ())
| Int (t, _) -> Int (t, ())
| Array (t, _) -> Array (t, ())
/// <summary>
/// Maps over the contents of a <see cref="CTyped"/>.
/// </summary>
/// <param name="f">The function to map.</param>
/// <param name="ctyped">The <see cref="CTyped"/> to map.</param>
/// <returns>
/// The result of applying <paramref name="f"/> on <paramref name="CTyped"/>.
/// </returns>
/// <typeparam name="Src">
/// The meta-type of items entering the map.
/// </typeparam>
/// <typeparam name="Dst">
/// The meta-type of items leaving the map.
/// </typeparam>
let mapCTyped (f : 'Src -> 'Dst) (ctyped : CTyped<'Src>) : CTyped<'Dst> =
match ctyped with
| Int (t, i) -> Int (t, f i)
| Bool (t, b) -> Bool (t, f b)
| Array (t, a) -> Array (t, f a)
/// <summary>
/// Combines a <c>Type</c> with an item to make it
/// <c>CTyped</c>.
/// </summary>
/// <param name="ty">
/// The type to use to mark the item.
/// </param>
/// <param name="item">
/// The item to be marked.
/// </param>
/// <typeparam name="a">
/// The meta-type of the item to type.
/// </typeparam>
/// <returns>
/// A <c>CTyped</c> with <paramref name="item"/> as its value.
/// </returns>
let withType (ty : Type) (item : 'a) : CTyped<'a> =
mapCTyped (fun _ -> item) ty
/// <summary>
/// Extracts the value of a <c>CTyped</c> item.
/// </summary>
/// <param name="typed">
/// The typed item.
/// </param>
/// <returns>
/// The item's value
/// </returns>
let valueOf (typed : CTyped<'a>) : 'a =
match typed with
| Int (_, a) | Bool (_, a) | Array (_, a) -> a
/// <summary>
/// Active pattern splitting a CTyped item into its item and type.
/// </summary>
let (|WithType|) x = (valueOf x, typeOf x)
/// <summary>
/// Active pattern extracting the root type of a typed item.
/// </summary>
let (|AnInt|ABool|AnArray|) (typed : Typed<'I, 'B, 'A>) =
match typed with
| Int _ -> AnInt
| Bool _ -> ABool
| Array ({ ElementType = eltype; Length = length }, _) -> AnArray (eltype, length)
/// <summary>
/// Active pattern extracting the full root type of a typed item.
/// </summary>
let (|AnIntR|ABoolR|AnArrayR|) (typed : Typed<'I, 'B, 'A>) =
match typed with
| Int (t, _) -> AnIntR t
| Bool (t, _) -> ABoolR t
| Array (t, _) -> AnArrayR t
/// <summary>
/// Tries to unify a list of primitive type records.
/// </summary>
/// <param name="xs">The possibly empty list of type records to check.</param>
/// <returns>
/// The unified record, if unification is possible; None otherwise.
/// </returns>
let rec unifyPrimTypeRecs (xs : PrimTypeRec list) : PrimTypeRec option =
match xs with
| [] -> Some { PrimSubtype = Indef }
| x::xs ->
(* TODO(CaptainHayashi): this is way too strong and order-dependent.
It's also somewhat broken, but in mostly inconsequential ways. *)
let foldRecs recSoFar nextRec =
match recSoFar with
| None -> None
| Some rs ->
match rs.PrimSubtype, nextRec.PrimSubtype with
| _, Indef -> Some rs
| Indef, _ -> Some nextRec
| Normal, Normal -> Some rs
| Named x, Named y when x = y -> Some rs
| _ -> None
List.fold foldRecs (Some x) xs
/// <summary>
/// Checks whether two primitive type records are compatible.
/// </summary>
/// <param name="x">The first type record to check.</param>
/// <param name="y">The second type record to check.</param>
/// <returns>
/// True if <paramref name="x"/> can be made compatible with
/// <paramref name="y"/>, or vice versa; false otherwise.
/// </returns>
let rec primTypeRecsCompatible (x : PrimTypeRec) (y : PrimTypeRec) : bool =
match unifyPrimTypeRecs [x; y] with
| Some _ -> true
| None -> false
/// <summary>
/// Tries to unify a list of primitive type records.
/// </summary>
/// <param name="xs">The possibly empty list of type records to check.</param>
/// <returns>
/// The unified record, if unification is possible; None otherwise.
/// </returns>
let rec unifyArrayTypeRecs (xs : ArrayTypeRec list) : ArrayTypeRec option =
// TODO(CaptainHayashi): add to this when PrimTypeRec expands.
match xs with
| [] -> None
| x::xs ->
(* TODO(CaptainHayashi): this is way too strong and order-dependent.
It's also somewhat broken, but in mostly inconsequential ways. *)
let foldRecs recSoFar nextRec =
match recSoFar with
| None -> None
| Some rs ->
// TODO(CaptainHayashi): unify element types.
match unifyTwoTypes rs.ElementType nextRec.ElementType with
| Some et ->
(* Currently, favour the type with the most defined length.
This should really pull the unified type from above
instead of taking one or the other. *)
match (rs.Length, nextRec.Length) with
| Some l, None | None, Some l ->
Some { ElementType = et; Length = Some l }
| None, None ->
Some { ElementType = et; Length = None }
| Some x, Some y when x = y ->
Some { ElementType = et; Length = Some x }
| _ -> None
| None -> None
List.fold foldRecs (Some x) xs
/// <summary>
/// Checks whether two array type records are compatible.
/// </summary>
/// <param name="x">The first type record to check.</param>
/// <param name="y">The second type record to check.</param>
/// <returns>
/// True if <paramref name="x"/> can be made compatible with
/// <paramref name="y"/>, or vice versa; false otherwise.
/// </returns>
and arrayTypeRecsCompatible (x : ArrayTypeRec) (y : ArrayTypeRec) : bool =
match unifyArrayTypeRecs [x; y] with
| Some _ -> true
| None -> false
/// <summary>
/// Tries to unify two types.
/// </summary>
/// <param name="x">The first type to unify.</param>
/// <param name="y">The second type to unify.</param>
/// <returns>
/// The unified record, if unification is possible; None otherwise.
/// </returns>
and unifyTwoTypes (x : Type) (y : Type) : Type option =
(* Types are can be unified when their base types are equal and their
extended type records are unifiable. *)
match (x, y) with
| (AnIntR xr, AnIntR yr) ->
Option.map (fun tr -> Int (tr, ())) (unifyPrimTypeRecs [xr; yr])
| (ABoolR xr, ABoolR yr) ->
Option.map (fun tr -> Bool (tr, ())) (unifyPrimTypeRecs [xr; yr])
| (AnArrayR xr, AnArrayR yr) ->
Option.map (fun tr -> Array (tr, ())) (unifyArrayTypeRecs [xr; yr])
| _ -> None
/// <summary>
/// Checks whether two types are compatible.
/// </summary>
/// <param name="x">The first type to check.</param>
/// <param name="y">The second type to check.</param>
/// <returns>
/// True if <paramref name="x"/> can be made compatible with
/// <paramref name="y"/>, or vice versa; false otherwise.
/// </returns>
and typesCompatible (x : Type) (y : Type) : bool =
match unifyTwoTypes x y with
| Some _ -> true
| None -> false
/// <summary>
/// Pretty printers for the type system.
/// </summary>
module Pretty =
open Starling.Core.Pretty
/// <summary>
/// Pretty-prints a type.
/// </summary>
let rec printType (ty : Type) : Doc =
match ty with
| AnIntR { PrimSubtype = Indef } -> String "int?"
| AnIntR { PrimSubtype = Normal } -> String "int"
| AnIntR { PrimSubtype = Named x } -> String x
| ABoolR { PrimSubtype = Indef } -> String "bool?"
| ABoolR { PrimSubtype = Normal } -> String "bool"
| ABoolR { PrimSubtype = Named x } -> String x
| AnArrayR { ElementType = eltype; Length = len } ->
parened
(String "array"
<+> printType eltype
<+> maybe (String "?") printInt len)
/// <summary>
/// Pretty-prints a typed item.
///
/// <para>
/// The item is printed as, for example, '(int foo)',
/// where 'int' is the type and 'foo' the result of printing
/// the inner item. If the pretty printer returns a no-op, then
/// no extra whitespace is generated.
/// </para>
/// </summary>
/// <param name="pInt">
/// Pretty printer for the inner item when the type is
/// <c>Int</c>.
/// </param>
/// <param name="pBool">
/// Pretty printer for the inner item when the type is
/// <c>Bool</c>.
/// </param>
/// <param name="typed">The typed item to print.</typed>
/// <typeparam name="Int">
/// The meta-type of <c>Int</c>-typed values.
/// </typeparam>
/// <typeparam name="Bool">
/// The meta-type of <c>Bool</c>-typed values.
/// </typeparam>
/// <returns>
/// A <see cref="Doc"/> capturing the typed value.
/// </returns>
let printTyped
(pInt : 'Int -> Doc)
(pBool : 'Bool -> Doc)
(pArray : Type -> int option -> 'Array -> Doc)
(typed : Typed<'Int, 'Bool, 'Array>) : Doc =
let typeDoc = printType (typeOf typed)
let valDoc =
match typed with
| Int (_, a) -> pInt a
| Bool (_, a) -> pBool a
| Array ({ ElementType = eltype; Length = length }, a) -> pArray eltype length a
let sexprContents =
match valDoc with
| Nop -> []
| doc -> [ doc ]
parened (hsep (typeDoc::sexprContents))
/// <summary>
/// Pretty-prints a ctyped item.
///
/// <para>
/// See <see cref="printTyped"/> for more information.
/// </para>
/// </summary>
/// <param name="pItem">
/// Pretty printer for the inner item.
/// </param>
/// <param name="ctyped">
/// The <c>CTyped</c> value to print.
/// </param>
/// <typeparam name="item">
/// The meta-type of ctyped values.
/// </typeparam>
/// <returns>
/// A printer <c>Doc</c> printing <paramref name="ctyped"/>.
/// </returns>
let printCTyped (pItem : 'item -> Doc) (ctyped : CTyped<'item>) : Doc =
printTyped pItem pItem (fun _ _ -> pItem) ctyped