-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeInference.cs
435 lines (410 loc) · 19.4 KB
/
TypeInference.cs
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
System.Language.TypeInference ( https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system )
Copyright (c) 2017 Cyril Jandia
https://ysharp.io/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL CYRIL JANDIA BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Cyril Jandia shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from Cyril Jandia.
Inquiries : ysharp {dot} design {at} gmail {dot} com
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System.Language.TypeInference
{
#region Abstract syntax tree
public abstract class Node
{
public static Const Const(object value) => new Const { Spec = value != null ? (!(value is IType) ? value.GetType().FullName : value) : typeof(void).FullName };
public static Var Var(string name) => Var(name, null);
public static Var Var(string name, object type) => new Var { Spec = name, Type = type };
public static Apply Apply(Node expr, Node[] args) => Apply(expr, args, null);
public static Apply Apply(Node expr, Node[] args, bool isAnnotation) => Apply(expr, args, null, isAnnotation);
public static Apply Apply(Node expr, Node[] args, object ctor) => Apply(expr, args, ctor, false);
public static Apply Apply(Node expr, Node[] args, object ctor, bool isAnnotation) => new Apply { Spec = expr, Args = args, Type = ctor, IsAnnotation = isAnnotation };
public static Abstract Abstract(Node[] args, Node body) => Abstract(args, null, body);
public static Abstract Abstract(Node[] args, object type, Node body) => new Abstract { Args = args, Body = body, Type = type };
public static Define Define(Var var, Node body) => new Define { Spec = var, Body = (body as Node) ?? Const(body) };
public static Let Let(Define[] defs, Node body) => new Let { Args = defs, Body = body };
public abstract IType Infer(ITypeSystem system, IEnvironment env, IList<IType> types);
public object Spec { get; private set; }
public Node[] Args { get; private set; }
public Node Body { get; private set; }
public object Type { get; private set; }
public bool IsAnnotation { get; private set; }
}
public class Const : Node
{
public override string ToString() => string.Concat("{", Spec, "}");
public override IType Infer(ITypeSystem system, IEnvironment env, IList<IType> types) => !(Spec is IType) ? system.Const(env, (string)Spec) : (IType)Spec;
}
public class Var : Node
{
public override string ToString() => Id;
public override IType Infer(ITypeSystem system, IEnvironment env, IList<IType> types) => system.Fresh(env[Id], types.ToArray());
public string Id => (string)Spec;
public bool IsConstructor => (Type is IType) && ((IType)Type).IsConstructor;
}
public class Abstract : Node
{
public override string ToString() => string.Format("( {0}){2} => {1}", Args.Length > 0 ? string.Concat(string.Join(" ", Args.Select(arg => arg.ToString()).ToArray()), " ") : string.Empty, Body, Type != null ? string.Concat(" : ", Type) : string.Empty);
public override IType Infer(ITypeSystem system, IEnvironment env, IList<IType> types)
{
var scope = env.Local(system);
var known = new List<IType>(types);
var args = new List<IType>();
foreach (var arg in Args)
{
IType type;
Var var;
if (!(arg is Define))
{
var = (Var)arg;
if (var.Type != null)
{
type = !(var.Type is IType) ? system.Const(scope, (string)var.Type) : (IType)var.Type;
}
else
{
type = system.NewGeneric();
known.Add(type);
}
scope[var.Id] = type;
}
else
{
var spec = ((Define)arg).Body;
var = (Var)((Define)arg).Spec;
type = system.NewGeneric();
system.Unify(type, system.Infer(scope, spec, known));
scope[var.Id] = type;
known.Add(type);
}
args.Add(type);
if (!type.Value.IsConstructor)
{
type.Value.Bind(var.Id);
}
}
args.Add(system.Infer(scope, Body is Let ? Body.Args[Body.Args.Length - 1] : Body, known));
if (Type != null)
{
system.Unify(args[args.Count - 1], !(Type is IType) ? system.Const(scope, (string)Type) : (IType)Type);
}
return system.NewType(TypeSystem.Function, args.ToArray());
}
}
public class Apply : Node
{
private bool IsFunction(IType type) => type != null ? (type.Constructor != null ? type.Constructor == TypeSystem.Function : IsFunction(type.Self)) : false;
private Node ToFormal(ITypeSystem system, IEnvironment env, IList<IType> types, Node arg)
{
Func<Var, IType> typed =
var =>
IsAnnotation && !env.ContainsKey(var.Id) ? env[var.Id] = system.NewGeneric() : env[var.Id];
var formal =
IsAnnotation ?
(
arg is Apply ?
Define((Var)arg.Args[0], Const(system.Infer(env, (Node)arg.Spec, types)))
:
arg is Var ? Define((Var)arg, Const(typed((Var)arg))) : arg
)
:
arg is Var ? Define((Var)arg, Const(typed((Var)arg))) : arg;
return formal;
}
private IType AsAnnotationType(ITypeSystem system, IEnvironment env, IList<IType> types)
{
IType ctor;
if
(
(Spec is Node) &&
(((ctor = ((Node)Spec).Type as IType)) != null) &&
(!IsFunction(ctor) || IsAnnotation)
)
{
Args.Select
(
(arg, i) =>
arg is Var ?
system.Infer(env, Define((Var)arg, Const(ctor[i])), types)
:
null
).
ToArray();
var type = system.NewType(ctor, Args.Select(arg => system.Infer(env, ToFormal(system, env, types, arg), types)).ToArray());
return type;
}
else
{
return null;
}
}
public override string ToString() => string.Format("( {0} {1})", Spec, Args.Length > 0 ? string.Concat(string.Join(" ", Args.Select(arg => arg.ToString()).ToArray()), " ") : string.Empty);
public override IType Infer(ITypeSystem system, IEnvironment env, IList<IType> types)
{
if (Type == null)
{
var annotation = AsAnnotationType(system, env, types);
if (annotation != null)
{
return annotation;
}
}
var args = Args.Select(arg => system.Infer(env, ToFormal(system, env, types, arg), types)).ToList();
var expr = (Node)Spec;
var self = system.Infer(env, expr, types);
IType @out;
if (Type != null)
{
var ctor = !(Type is IType) ? system.Const(env, (string)Type) : (IType)Type;
@out = system.Infer(env, Apply(Var(ctor.Id, ctor), args.Select(arg => Const(arg)).ToArray(), IsAnnotation), types);
}
else
{
@out = system.NewGeneric();
args.Add(@out);
system.Unify(system.NewType(TypeSystem.Function, args.ToArray()), self);
}
return @out;
}
}
public class Define : Node
{
public override string ToString() => string.Format("( {0} = {1} )", Spec, Body);
public override IType Infer(ITypeSystem system, IEnvironment env, IList<IType> types)
{
var known = new List<IType>(types);
var type = system.NewGeneric();
var var = (Var)Spec;
var scope = Body.IsAnnotation ? env.Local(system) : env;
env[var.Id] = type;
known.Add(type);
system.Unify(type, system.Infer(scope, Body, known));
return env[var.Id] = !type.Value.IsConstructor ? type.Value.Bind(var.Id) : type.Value;
}
}
public class Let : Node
{
public override string ToString() => string.Format("( ( {0}) {1} )", Args.Length > 0 ? string.Concat(string.Join(" ", Args.Select(arg => arg.ToString()).ToArray()), " ") : string.Empty, Body);
public override IType Infer(ITypeSystem system, IEnvironment env, IList<IType> types)
{
env = env.Local(system);
return Args.Select(define => system.Infer(env, define, types)).Concat(new[] { system.Infer(env, Body, types) }).Last();
}
}
#endregion
#region Type schemes
public interface IType
{
IType Bind(string name);
IType Constructor { get; }
string Id { get; }
IType[] Args { get; }
IType Self { get; }
Var Name { get; }
IType Value { get; }
bool IsConstructor { get; }
object Metadata { get; }
IType this[int index] { get; }
}
#endregion
#region Environment
public interface IEnvironment : IDictionary<string, IType>
{
IEnvironment Local(ITypeSystem system);
}
public class Environment : Dictionary<string, IType>, IEnvironment
{
protected virtual IType Get(string id)
{
if (id == null)
{
throw new ArgumentNullException("id", "cannot be null");
}
if (!ContainsKey(id))
{
throw new InvalidOperationException(string.Format("undefined {0}", id));
}
return base[id];
}
protected virtual void Set(string id, IType type)
{
if (id == null)
{
throw new ArgumentNullException("id", "cannot be null");
}
base[id] = type;
}
public Environment() : this(null) { }
public Environment(IDictionary<string, IType> outer) : base(outer ?? new Dictionary<string, IType>()) { }
public IEnvironment Local(ITypeSystem system) => system.NewEnvironment(this);
public new IType this[string id] { get { return Get(id); } set { Set(id, value); } }
}
#endregion
#region Type system
public interface ITypeSystem
{
IEnvironment NewEnvironment();
IEnvironment NewEnvironment(IDictionary<string, IType> outer);
IType Fresh(IType t, IType[] types);
IType Const(IEnvironment env, string ctor);
IType NewGeneric();
IType NewType(string id, IType[] args);
IType NewType(string id, IType[] args, object meta);
IType NewType(IType constructor, IType[] args);
IType NewType(IType constructor, IType[] args, object meta);
IType NewType(IType constructor, string id, IType[] args);
IType NewType(IType constructor, string id, IType[] args, object meta);
void Unify(IType t, IType s);
IType Infer(IEnvironment env, Node node);
IType Infer(IEnvironment env, Node node, IList<IType> types);
}
public class TypeSystem : ITypeSystem
{
internal abstract class Scheme : IType
{
protected Scheme(string id) { if ((this is Type) && (id == null)) { throw new ArgumentNullException("id", "cannot be null"); } Bind(Id = id); }
protected Scheme(string id, IType[] args) : this(id) { Args = args ?? new IType[0]; }
public override string ToString() => Id;
public IType Bind(string name) { Name = Node.Var(name, this); return this; }
public IType Constructor { get; protected set; }
public virtual string Id { get; protected set; }
public IType[] Args { get; private set; }
public IType Self { get; internal set; }
public Var Name { get; private set; }
public IType Value => Self != null ? Self.Value : this;
public bool IsConstructor => Constructor == this;
public object Metadata { get; protected set; }
public IType this[int index] => Args[index];
}
internal class Generic : Scheme
{
private string alpha;
private string Alpha() { var id = Uid; var sb = new StringBuilder(); while (id-- > 0) { var r = id % 26; var c = (char)(r + 97); sb.Insert(0, c); id = (id - r) / 26; } return sb.ToString(); }
private string GetName() => Self != null ? Self.Id : string.Concat('`', alpha ?? (alpha = Alpha()));
internal Generic(TypeSystem system) : base(null) { Uid = system.NewId(); }
internal readonly int Uid;
public override string ToString() => Self != null ? Self.ToString() : base.ToString();
public override string Id { get { return GetName(); } protected set { } }
}
internal class Type : Scheme
{
internal Type(IType constructor, string id, IType[] args, object meta) : base(id, args) { Constructor = constructor ?? this; Metadata = meta; }
public override string ToString() { var id = Args.Length > 0 ? Id : base.ToString(); return (Args.Length > 0 ? string.Format("{0}<{1}>", id, string.Concat(string.Join(", ", Args.Take(Args.Length - 1).Select(arg => arg.ToString())), (Args.Length > 1 ? ", " : string.Empty), Args[Args.Length - 1].ToString())) : id); }
}
private static IType Create(IType constructor, string id, IType[] args, object meta) => new Type(constructor, id, args, meta);
private static IType Prune(IType t)
{
Generic var = t as Generic;
return (var != null) && (var.Self != null) ? (var.Self = Prune(var.Self)) : t;
}
private static bool OccursIn(IType t, IType s) => ((s = Prune(s)) != t) ? (s is Type ? OccursIn(t, s.Args) : false) : true;
private static bool OccursIn(IType t, IType[] types) => types.Any(type => OccursIn(t, type));
private IType Fresh(IType t, IType[] types, IDictionary<int, IType> vars)
{
vars = vars ?? new Dictionary<int, IType>();
t = Prune(t);
var var = t as Generic;
var type = t as Type;
if (var != null)
{
if (!OccursIn(t, types))
{
if (!vars.ContainsKey(var.Uid))
{
vars[var.Uid] = NewGeneric();
}
return vars[var.Uid];
}
else
{
return t;
}
}
else if (type != null)
{
return NewType(type.Constructor, type.Id, type.Args.Select(arg => Fresh(arg, types, vars)).ToArray());
}
else
{
throw new InvalidOperationException(string.Format("unsupported {0}", t.GetType()));
}
}
private int id;
internal int NewId() => ++id;
public static readonly IType Function = Create(null, "Func", null, null);
#region ITypeSystem
public virtual IEnvironment NewEnvironment() => NewEnvironment(null);
public virtual IEnvironment NewEnvironment(IDictionary<string, IType> outer) => new Environment(outer);
public IType Fresh(IType t, IType[] types) => Fresh(t, types, null);
public IType Const(IEnvironment env, string ctor) => env[ctor];
public IType NewGeneric() => new Generic(this);
public IType NewType(string id, IType[] args) => NewType(id, args, null);
public IType NewType(string id, IType[] args, object meta) => NewType(null, id, args, meta);
public IType NewType(IType constructor, IType[] args) => NewType(constructor, args, null);
public IType NewType(IType constructor, IType[] args, object meta) => NewType(constructor, constructor.Id, args, meta);
public IType NewType(IType constructor, string id, IType[] args) => NewType(constructor, id, args, null);
public IType NewType(IType constructor, string id, IType[] args, object meta) => Create(constructor, id, args, meta);
public void Unify(IType t, IType s)
{
t = Prune(t);
s = Prune(s);
if (t is Generic)
{
if (t != s)
{
if (OccursIn(t, s))
{
throw new InvalidOperationException(string.Format("recursive unification of {0} in {1}", t, s));
}
((Generic)t).Self = s;
}
}
else if ((t is Type) && (s is Generic))
{
Unify(s, t);
}
else if ((t is Type) && (s is Type))
{
var t_type = (Type)t;
var s_type = (Type)s;
if ((t_type.Constructor.Id != s_type.Constructor.Id) || (t_type.Args.Length != s_type.Args.Length))
{
throw new InvalidOperationException(string.Format("{0} incompatible with {1}", t_type, s_type));
}
for (var i = 0; i < t_type.Args.Length; i++)
{
Unify(t_type.Args[i], s_type.Args[i]);
}
}
else
{
throw new InvalidOperationException(string.Format("undecided unification for {0} and {1}", t, s));
}
}
public IType Infer(IEnvironment env, Node node) => Infer(env, node, null);
public IType Infer(IEnvironment env, Node node, IList<IType> types) => node.Infer(this, env, types ?? new List<IType>());
#endregion
}
#endregion
}