Skip to content

Commit fdc06b5

Browse files
zafer-esenatomb
andauthored
Monomorphic Field and collection types (#4597)
Removes the type argument from `Field`, and from all collection types. All heap and collection elements are now always of type `Box`. This has the effect of very slightly increasing brittleness on some particularly brittle examples, though it has minimal effect on most code. The benefits of the simplification (and the opportunities it opens for future optimization) seem worth the tradeoff, however. By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license. --------- Co-authored-by: Aaron Tomb <[email protected]>
1 parent 29c4525 commit fdc06b5

File tree

44 files changed

+1710
-1802
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1710
-1802
lines changed

Source/DafnyCore/DafnyPrelude.bpl

Lines changed: 340 additions & 338 deletions
Large diffs are not rendered by default.

Source/DafnyCore/GeneratedFromDafny.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ public static Dafny.ISequence<__T> SetToSeq<__T>(Dafny.ISet<__T> s) {
921921
goto after__ASSIGN_SUCH_THAT_0;
922922
}
923923
}
924-
throw new System.Exception("assign-such-that search produced no value (line 7231)");
924+
throw new System.Exception("assign-such-that search produced no value (line 7247)");
925925
after__ASSIGN_SUCH_THAT_0:;
926926
_105_left = Dafny.Set<__T>.Difference(_105_left, Dafny.Set<__T>.FromElements(_106_x));
927927
xs = Dafny.Sequence<__T>.Concat(xs, Dafny.Sequence<__T>.FromElements(_106_x));

Source/DafnyCore/Verifier/BoogieGenerator.BoogieFactory.cs

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -295,30 +295,24 @@ Bpl.NAryExpr FunctionCall(Bpl.IToken tok, BuiltinFunction f, Bpl.Type typeInstan
295295

296296
case BuiltinFunction.SetCard:
297297
Contract.Assert(args.Length == 1);
298-
Contract.Assert(typeInstantiation == null);
299298
return FunctionCall(tok, "Set#Card", Bpl.Type.Int, args);
300299
case BuiltinFunction.SetEmpty: {
301300
Contract.Assert(args.Length == 0);
302-
Contract.Assert(typeInstantiation != null);
303-
Bpl.Type resultType = predef.SetType(tok, true, typeInstantiation);
301+
Bpl.Type resultType = predef.SetType;
304302
return Bpl.Expr.CoerceType(tok, FunctionCall(tok, "Set#Empty", resultType, args), resultType);
305303
}
306304
case BuiltinFunction.SetUnionOne:
307305
Contract.Assert(args.Length == 2);
308-
Contract.Assert(typeInstantiation != null);
309-
return FunctionCall(tok, "Set#UnionOne", predef.SetType(tok, true, typeInstantiation), args);
306+
return FunctionCall(tok, "Set#UnionOne", predef.SetType, args);
310307
case BuiltinFunction.SetUnion:
311308
Contract.Assert(args.Length == 2);
312-
Contract.Assert(typeInstantiation != null);
313-
return FunctionCall(tok, "Set#Union", predef.SetType(tok, true, typeInstantiation), args);
309+
return FunctionCall(tok, "Set#Union", predef.SetType, args);
314310
case BuiltinFunction.SetIntersection:
315311
Contract.Assert(args.Length == 2);
316-
Contract.Assert(typeInstantiation != null);
317-
return FunctionCall(tok, "Set#Intersection", predef.SetType(tok, true, typeInstantiation), args);
312+
return FunctionCall(tok, "Set#Intersection", predef.SetType, args);
318313
case BuiltinFunction.SetDifference:
319314
Contract.Assert(args.Length == 2);
320-
Contract.Assert(typeInstantiation != null);
321-
return FunctionCall(tok, "Set#Difference", predef.SetType(tok, true, typeInstantiation), args);
315+
return FunctionCall(tok, "Set#Difference", predef.SetType, args);
322316
case BuiltinFunction.SetEqual:
323317
Contract.Assert(args.Length == 2);
324318
Contract.Assert(typeInstantiation == null);
@@ -333,148 +327,114 @@ Bpl.NAryExpr FunctionCall(Bpl.IToken tok, BuiltinFunction f, Bpl.Type typeInstan
333327
return FunctionCall(tok, "Set#Disjoint", Bpl.Type.Bool, args);
334328
case BuiltinFunction.ISetEmpty: {
335329
Contract.Assert(args.Length == 0);
336-
Contract.Assert(typeInstantiation != null);
337-
Bpl.Type resultType = predef.SetType(tok, false, typeInstantiation);
330+
Bpl.Type resultType = predef.ISetType;
338331
return Bpl.Expr.CoerceType(tok, FunctionCall(tok, "ISet#Empty", resultType, args), resultType);
339332
}
340333
case BuiltinFunction.ISetUnionOne:
341334
Contract.Assert(args.Length == 2);
342-
Contract.Assert(typeInstantiation != null);
343-
return FunctionCall(tok, "ISet#UnionOne", predef.SetType(tok, false, typeInstantiation), args);
335+
return FunctionCall(tok, "ISet#UnionOne", predef.ISetType, args);
344336
case BuiltinFunction.ISetUnion:
345337
Contract.Assert(args.Length == 2);
346-
Contract.Assert(typeInstantiation != null);
347-
return FunctionCall(tok, "ISet#Union", predef.SetType(tok, false, typeInstantiation), args);
338+
return FunctionCall(tok, "ISet#Union", predef.ISetType, args);
348339
case BuiltinFunction.ISetIntersection:
349340
Contract.Assert(args.Length == 2);
350-
Contract.Assert(typeInstantiation != null);
351-
return FunctionCall(tok, "ISet#Intersection", predef.SetType(tok, false, typeInstantiation), args);
341+
return FunctionCall(tok, "ISet#Intersection", predef.ISetType, args);
352342
case BuiltinFunction.ISetDifference:
353343
Contract.Assert(args.Length == 2);
354-
Contract.Assert(typeInstantiation != null);
355-
return FunctionCall(tok, "ISet#Difference", predef.SetType(tok, false, typeInstantiation), args);
344+
return FunctionCall(tok, "ISet#Difference", predef.ISetType, args);
356345
case BuiltinFunction.ISetEqual:
357346
Contract.Assert(args.Length == 2);
358-
Contract.Assert(typeInstantiation == null);
359347
return FunctionCall(tok, "ISet#Equal", Bpl.Type.Bool, args);
360348
case BuiltinFunction.ISetSubset:
361349
Contract.Assert(args.Length == 2);
362-
Contract.Assert(typeInstantiation == null);
363350
return FunctionCall(tok, "ISet#Subset", Bpl.Type.Bool, args);
364351
case BuiltinFunction.ISetDisjoint:
365352
Contract.Assert(args.Length == 2);
366-
Contract.Assert(typeInstantiation == null);
367353
return FunctionCall(tok, "ISet#Disjoint", Bpl.Type.Bool, args);
368354
case BuiltinFunction.MultiSetCard:
369355
Contract.Assert(args.Length == 1);
370-
Contract.Assert(typeInstantiation == null);
371356
return FunctionCall(tok, "MultiSet#Card", Bpl.Type.Int, args);
372357
case BuiltinFunction.MultiSetEmpty: {
373358
Contract.Assert(args.Length == 0);
374-
Contract.Assert(typeInstantiation != null);
375-
Bpl.Type resultType = predef.MultiSetType(tok, typeInstantiation);
359+
Bpl.Type resultType = predef.MultiSetType;
376360
return Bpl.Expr.CoerceType(tok, FunctionCall(tok, "MultiSet#Empty", resultType, args), resultType);
377361
}
378362
case BuiltinFunction.MultiSetUnionOne:
379363
Contract.Assert(args.Length == 2);
380-
Contract.Assert(typeInstantiation != null);
381-
return FunctionCall(tok, "MultiSet#UnionOne", predef.MultiSetType(tok, typeInstantiation), args);
364+
return FunctionCall(tok, "MultiSet#UnionOne", predef.MultiSetType, args);
382365
case BuiltinFunction.MultiSetUnion:
383366
Contract.Assert(args.Length == 2);
384-
Contract.Assert(typeInstantiation != null);
385-
return FunctionCall(tok, "MultiSet#Union", predef.MultiSetType(tok, typeInstantiation), args);
367+
return FunctionCall(tok, "MultiSet#Union", predef.MultiSetType, args);
386368
case BuiltinFunction.MultiSetIntersection:
387369
Contract.Assert(args.Length == 2);
388-
Contract.Assert(typeInstantiation != null);
389-
return FunctionCall(tok, "MultiSet#Intersection", predef.MultiSetType(tok, typeInstantiation), args);
370+
return FunctionCall(tok, "MultiSet#Intersection", predef.MultiSetType, args);
390371
case BuiltinFunction.MultiSetDifference:
391372
Contract.Assert(args.Length == 2);
392-
Contract.Assert(typeInstantiation != null);
393-
return FunctionCall(tok, "MultiSet#Difference", predef.MultiSetType(tok, typeInstantiation), args);
373+
return FunctionCall(tok, "MultiSet#Difference", predef.MultiSetType, args);
394374
case BuiltinFunction.MultiSetEqual:
395375
Contract.Assert(args.Length == 2);
396-
Contract.Assert(typeInstantiation == null);
397376
return FunctionCall(tok, "MultiSet#Equal", Bpl.Type.Bool, args);
398377
case BuiltinFunction.MultiSetSubset:
399378
Contract.Assert(args.Length == 2);
400-
Contract.Assert(typeInstantiation == null);
401379
return FunctionCall(tok, "MultiSet#Subset", Bpl.Type.Bool, args);
402380
case BuiltinFunction.MultiSetDisjoint:
403381
Contract.Assert(args.Length == 2);
404-
Contract.Assert(typeInstantiation == null);
405382
return FunctionCall(tok, "MultiSet#Disjoint", Bpl.Type.Bool, args);
406383
case BuiltinFunction.MultiSetFromSet:
407384
Contract.Assert(args.Length == 1);
408-
Contract.Assert(typeInstantiation != null);
409-
return FunctionCall(tok, "MultiSet#FromSet", predef.MultiSetType(tok, typeInstantiation), args);
385+
return FunctionCall(tok, "MultiSet#FromSet", predef.MultiSetType, args);
410386
case BuiltinFunction.MultiSetFromSeq:
411387
Contract.Assert(args.Length == 1);
412-
Contract.Assert(typeInstantiation != null);
413-
return FunctionCall(tok, "MultiSet#FromSeq", predef.MultiSetType(tok, typeInstantiation), args);
388+
return FunctionCall(tok, "MultiSet#FromSeq", predef.MultiSetType, args);
414389
case BuiltinFunction.IsGoodMultiSet:
415390
Contract.Assert(args.Length == 1);
416-
Contract.Assert(typeInstantiation == null);
417391
return FunctionCall(tok, "$IsGoodMultiSet", Bpl.Type.Bool, args);
418392

419393
case BuiltinFunction.SeqLength:
420394
Contract.Assert(args.Length == 1);
421-
Contract.Assert(typeInstantiation == null);
422395
return FunctionCall(tok, "Seq#Length", Bpl.Type.Int, args);
423396
case BuiltinFunction.SeqEmpty: {
424397
Contract.Assert(args.Length == 0);
425-
Contract.Assert(typeInstantiation != null);
426-
Bpl.Type resultType = predef.SeqType(tok, typeInstantiation);
398+
Bpl.Type resultType = predef.SeqType;
427399
return Bpl.Expr.CoerceType(tok, FunctionCall(tok, "Seq#Empty", resultType, args), resultType);
428400
}
429401
case BuiltinFunction.SeqBuild:
430402
Contract.Assert(args.Length == 2);
431-
Contract.Assert(typeInstantiation != null);
432-
return FunctionCall(tok, "Seq#Build", predef.SeqType(tok, typeInstantiation), args);
403+
return FunctionCall(tok, "Seq#Build", predef.SeqType, args);
433404
case BuiltinFunction.SeqAppend:
434405
Contract.Assert(args.Length == 2);
435-
Contract.Assert(typeInstantiation != null);
436-
return FunctionCall(tok, "Seq#Append", predef.SeqType(tok, typeInstantiation), args);
406+
return FunctionCall(tok, "Seq#Append", predef.SeqType, args);
437407
case BuiltinFunction.SeqIndex:
438408
Contract.Assert(args.Length == 2);
439-
Contract.Assert(typeInstantiation != null);
440-
return FunctionCall(tok, "Seq#Index", typeInstantiation, args);
409+
return FunctionCall(tok, "Seq#Index", predef.BoxType, args);
441410
case BuiltinFunction.SeqUpdate:
442411
Contract.Assert(args.Length == 3);
443-
Contract.Assert(typeInstantiation != null);
444-
return FunctionCall(tok, "Seq#Update", predef.SeqType(tok, typeInstantiation), args);
412+
return FunctionCall(tok, "Seq#Update", predef.SeqType, args);
445413
case BuiltinFunction.SeqContains:
446414
Contract.Assert(args.Length == 2);
447-
Contract.Assert(typeInstantiation == null);
448415
return FunctionCall(tok, "Seq#Contains", Bpl.Type.Bool, args);
449416
case BuiltinFunction.SeqDrop:
450417
Contract.Assert(args.Length == 2);
451-
Contract.Assert(typeInstantiation != null);
452-
return FunctionCall(tok, "Seq#Drop", predef.SeqType(tok, typeInstantiation), args);
418+
return FunctionCall(tok, "Seq#Drop", predef.SeqType, args);
453419
case BuiltinFunction.SeqTake:
454420
Contract.Assert(args.Length == 2);
455-
Contract.Assert(typeInstantiation != null);
456-
return FunctionCall(tok, "Seq#Take", predef.SeqType(tok, typeInstantiation), args);
421+
return FunctionCall(tok, "Seq#Take", predef.SeqType, args);
457422
case BuiltinFunction.SeqEqual:
458423
Contract.Assert(args.Length == 2);
459-
Contract.Assert(typeInstantiation == null);
460424
return FunctionCall(tok, "Seq#Equal", Bpl.Type.Bool, args);
461425
case BuiltinFunction.SeqSameUntil:
462426
Contract.Assert(args.Length == 3);
463-
Contract.Assert(typeInstantiation == null);
464427
return FunctionCall(tok, "Seq#SameUntil", Bpl.Type.Bool, args);
465428
case BuiltinFunction.SeqFromArray:
466429
Contract.Assert(args.Length == 2);
467-
Contract.Assert(typeInstantiation != null);
468430
return FunctionCall(tok, "Seq#FromArray", typeInstantiation, args);
469431
case BuiltinFunction.SeqRank:
470432
Contract.Assert(args.Length == 1);
471-
Contract.Assert(typeInstantiation == null);
472433
return FunctionCall(tok, "Seq#Rank", Bpl.Type.Int, args);
473434

474435
case BuiltinFunction.MapEmpty: {
475436
Contract.Assert(args.Length == 0);
476-
Contract.Assert(typeInstantiation != null);
477-
Bpl.Type resultType = predef.MapType(tok, true, typeInstantiation, typeInstantiation); // use 'typeInstantiation' (which is really always just BoxType anyway) as both type arguments
437+
Bpl.Type resultType = predef.MapType;
478438
return Bpl.Expr.CoerceType(tok, FunctionCall(tok, "Map#Empty", resultType, args), resultType);
479439
}
480440
case BuiltinFunction.MapCard:
@@ -489,7 +449,7 @@ Bpl.NAryExpr FunctionCall(Bpl.IToken tok, BuiltinFunction f, Bpl.Type typeInstan
489449
return FunctionCall(tok, "Map#Elements", typeInstantiation, args);
490450
case BuiltinFunction.MapGlue:
491451
Contract.Assert(args.Length == 3);
492-
return FunctionCall(tok, "Map#Glue", predef.MapType(tok, true, predef.BoxType, predef.BoxType), args);
452+
return FunctionCall(tok, "Map#Glue", predef.MapType, args);
493453
case BuiltinFunction.MapEqual:
494454
Contract.Assert(args.Length == 2);
495455
Contract.Assert(typeInstantiation == null);
@@ -505,8 +465,7 @@ Bpl.NAryExpr FunctionCall(Bpl.IToken tok, BuiltinFunction f, Bpl.Type typeInstan
505465

506466
case BuiltinFunction.IMapEmpty: {
507467
Contract.Assert(args.Length == 0);
508-
Contract.Assert(typeInstantiation != null);
509-
Bpl.Type resultType = predef.MapType(tok, false, typeInstantiation, typeInstantiation); // use 'typeInstantiation' (which is really always just BoxType anyway) as both type arguments
468+
Bpl.Type resultType = predef.IMapType;
510469
return Bpl.Expr.CoerceType(tok, FunctionCall(tok, "IMap#Empty", resultType, args), resultType);
511470
}
512471
case BuiltinFunction.IMapDomain:
@@ -517,7 +476,7 @@ Bpl.NAryExpr FunctionCall(Bpl.IToken tok, BuiltinFunction f, Bpl.Type typeInstan
517476
return FunctionCall(tok, "IMap#Elements", typeInstantiation, args);
518477
case BuiltinFunction.IMapGlue:
519478
Contract.Assert(args.Length == 3);
520-
return FunctionCall(tok, "IMap#Glue", predef.MapType(tok, false, predef.BoxType, predef.BoxType), args);
479+
return FunctionCall(tok, "IMap#Glue", predef.IMapType, args);
521480
case BuiltinFunction.IMapEqual:
522481
Contract.Assert(args.Length == 2);
523482
Contract.Assert(typeInstantiation == null);
@@ -526,11 +485,11 @@ Bpl.NAryExpr FunctionCall(Bpl.IToken tok, BuiltinFunction f, Bpl.Type typeInstan
526485
case BuiltinFunction.IndexField:
527486
Contract.Assert(args.Length == 1);
528487
Contract.Assert(typeInstantiation == null);
529-
return FunctionCall(tok, "IndexField", predef.FieldName(tok, predef.BoxType), args);
488+
return FunctionCall(tok, "IndexField", predef.FieldName(tok), args);
530489
case BuiltinFunction.MultiIndexField:
531490
Contract.Assert(args.Length == 2);
532491
Contract.Assert(typeInstantiation == null);
533-
return FunctionCall(tok, "MultiIndexField", predef.FieldName(tok, predef.BoxType), args);
492+
return FunctionCall(tok, "MultiIndexField", predef.FieldName(tok), args);
534493

535494
case BuiltinFunction.Box:
536495
Contract.Assert(args.Length == 1);
@@ -582,7 +541,7 @@ Bpl.NAryExpr FunctionCall(Bpl.IToken tok, BuiltinFunction f, Bpl.Type typeInstan
582541
case BuiltinFunction.FieldOfDecl:
583542
Contract.Assert(args.Length == 2);
584543
Contract.Assert(typeInstantiation != null);
585-
return FunctionCall(tok, "FieldOfDecl", predef.FieldName(tok, typeInstantiation), args);
544+
return FunctionCall(tok, "FieldOfDecl", predef.FieldName(tok), args);
586545
case BuiltinFunction.FDim:
587546
Contract.Assert(args.Length == 1);
588547
Contract.Assert(typeInstantiation != null);

Source/DafnyCore/Verifier/BoogieGenerator.DataTypes.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ private Bpl.Function AddDataTypeConstructor(DatatypeDecl dt, DatatypeCtor ctor,
582582
bvs.Add(dVar);
583583
var ie = new Bpl.IdentifierExpr(arg.tok, dVar);
584584
var f = finite ? BuiltinFunction.MapDomain : BuiltinFunction.IMapDomain;
585-
var domain = FunctionCall(arg.tok, f, predef.MapType(arg.tok, finite, predef.BoxType, predef.BoxType),
585+
var domain = FunctionCall(arg.tok, f, finite ? predef.MapType : predef.IMapType,
586586
args[i]);
587587
var inDomain = Bpl.Expr.SelectTok(arg.tok, domain, FunctionCall(arg.tok, BuiltinFunction.Box, null, ie));
588588
var lhs = FunctionCall(ctor.tok, BuiltinFunction.DtRank, null, ie);
@@ -601,11 +601,11 @@ private Bpl.Function AddDataTypeConstructor(DatatypeDecl dt, DatatypeCtor ctor,
601601
bvs.Add(bxVar);
602602
var ie = new Bpl.IdentifierExpr(arg.tok, bxVar);
603603
var f = finite ? BuiltinFunction.MapDomain : BuiltinFunction.IMapDomain;
604-
var domain = FunctionCall(arg.tok, f, predef.MapType(arg.tok, finite, predef.BoxType, predef.BoxType),
604+
var domain = FunctionCall(arg.tok, f, finite ? predef.MapType : predef.IMapType,
605605
args[i]);
606606
var inDomain = Bpl.Expr.SelectTok(arg.tok, domain, ie);
607607
var ef = finite ? BuiltinFunction.MapElements : BuiltinFunction.IMapElements;
608-
var element = FunctionCall(arg.tok, ef, predef.MapType(arg.tok, finite, predef.BoxType, predef.BoxType),
608+
var element = FunctionCall(arg.tok, ef, finite ? predef.MapType : predef.IMapType,
609609
args[i]);
610610
var elmt = Bpl.Expr.SelectTok(arg.tok, element, ie);
611611
var unboxElmt = FunctionCall(arg.tok, BuiltinFunction.Unbox, predef.DatatypeType, elmt);

Source/DafnyCore/Verifier/BoogieGenerator.Decreases.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ void ComputeLessEq(IToken tok, Type ty0, Type ty1, Bpl.Expr e0, Bpl.Expr e1, out
287287
b1 = e1;
288288
} else {
289289
// for maps, compare their domains as sets
290-
b0 = FunctionCall(tok, BuiltinFunction.MapDomain, predef.MapType(tok, true, predef.BoxType, predef.BoxType), e0);
291-
b1 = FunctionCall(tok, BuiltinFunction.MapDomain, predef.MapType(tok, true, predef.BoxType, predef.BoxType), e1);
290+
b0 = FunctionCall(tok, BuiltinFunction.MapDomain, predef.MapType, e0);
291+
b1 = FunctionCall(tok, BuiltinFunction.MapDomain, predef.MapType, e1);
292292
}
293293
eq = FunctionCall(tok, BuiltinFunction.SetEqual, null, b0, b1);
294294
less = ProperSubset(tok, b0, b1);
@@ -303,8 +303,8 @@ void ComputeLessEq(IToken tok, Type ty0, Type ty1, Bpl.Expr e0, Bpl.Expr e1, out
303303
} else {
304304
Contract.Assert(!((MapType)ty0).Finite);
305305
// for maps, compare their domains as sets
306-
b0 = FunctionCall(tok, BuiltinFunction.IMapDomain, predef.MapType(tok, false, predef.BoxType, predef.BoxType), e0);
307-
b1 = FunctionCall(tok, BuiltinFunction.IMapDomain, predef.MapType(tok, false, predef.BoxType, predef.BoxType), e1);
306+
b0 = FunctionCall(tok, BuiltinFunction.IMapDomain, predef.MapType, e0);
307+
b1 = FunctionCall(tok, BuiltinFunction.IMapDomain, predef.MapType, e1);
308308
}
309309
eq = FunctionCall(tok, BuiltinFunction.ISetEqual, null, b0, b1);
310310
less = Bpl.Expr.False;

0 commit comments

Comments
 (0)