Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Populate runtimes with all built-in declarations #4658

Merged
merged 52 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
5d4a536
Expose the nat issue
robin-aws Oct 12, 2023
ee1f458
Not emitting nat working for Java, C# and go
robin-aws Oct 12, 2023
1467945
Better solution (—include-system-module)
robin-aws Oct 12, 2023
e0321a5
Fix Java assumption that _System is always compiled before default mo…
robin-aws Oct 12, 2023
80f4a69
Generalize EmitRuntimeSource
robin-aws Oct 12, 2023
cac5f6d
Get separate-compilation test case to pass
robin-aws Oct 12, 2023
3f43125
Fix Go System module naming
robin-aws Oct 12, 2023
ae4824d
Hook up build verification for Go
robin-aws Oct 13, 2023
122bf3d
Hook up Java (ugh)
robin-aws Oct 13, 2023
3a6f48c
Hook up C# (partial)
robin-aws Oct 13, 2023
ddd92b9
Revert "Hook up C# (partial)"
robin-aws Oct 13, 2023
0da417a
Remove {:compile false} from System declarations
robin-aws Oct 13, 2023
423662b
Revert "Revert "Hook up C# (partial)""
robin-aws Oct 13, 2023
a81d0ab
Revert "Revert "Revert "Hook up C# (partial)"""
robin-aws Oct 13, 2023
4ae4a3b
Hook up for C# (better this time, still not perfect)
robin-aws Oct 13, 2023
f58fe08
Move generated files into separate directory
robin-aws Oct 20, 2023
0d9ac87
Fix Java compilation of tuples, correctly populate Tuple1
robin-aws Oct 20, 2023
129b08f
Unify runtime testing workflow better
robin-aws Oct 20, 2023
dcd038c
Fill in arrows and arrays up to 20
robin-aws Oct 20, 2023
96c3bae
Add Python Makefile
robin-aws Oct 20, 2023
29f26b9
Don’t compile partial or total function declarations (broken)
robin-aws Oct 23, 2023
7303e87
Temporarily add tuples back to break circular dependency
robin-aws Oct 24, 2023
cd9167b
Only arity <= 16 for functions and arrays
robin-aws Oct 24, 2023
5b86ff3
Update Go
robin-aws Oct 24, 2023
51cb7eb
Put Dafny-generated source under obj
robin-aws Oct 24, 2023
0facbbf
More progress
robin-aws Oct 24, 2023
980d12f
Better option to solve ugliness in C# etc
robin-aws Oct 24, 2023
cc2a370
Use _System module for Java
robin-aws Oct 24, 2023
1e013e6
Remove system module arity tracking in Java compiler
robin-aws Oct 24, 2023
39c71e5
Two more tweaks for Java
robin-aws Oct 24, 2023
72d5ea6
Legacy UI for —system-module option, missing _Tuple1 in C#
robin-aws Oct 24, 2023
26a7214
Update verbose translation tests
robin-aws Oct 24, 2023
67fb7c1
Merge branch 'master' of https://github.com/dafny-lang/dafny into git…
robin-aws Oct 24, 2023
7b6258b
FuncExtensions looks unused, let’s find out… :)
robin-aws Oct 25, 2023
06dc8c7
Hook up C++ and Rust (not fully working yet I suspect)
robin-aws Oct 25, 2023
dc012ae
Revert "FuncExtensions looks unused, let’s find out… :)"
robin-aws Oct 25, 2023
c3c2ee2
Fix internal builtins in C#
robin-aws Oct 25, 2023
86bc5e8
Adding —include-runtime:false for the benefit of C# testing
robin-aws Oct 26, 2023
d518542
No point in compiling arrow types, SinglePassCompiler ignores all val…
robin-aws Oct 26, 2023
8a41a9f
Fix —include-runtime:false in MultiBackendTest for C#
robin-aws Oct 26, 2023
2429f0c
Omit C+ and Rust for now, update expect files that print resolved pro…
robin-aws Oct 26, 2023
639b9e6
Whitespace (and exclude another generated-from-Dafny file)
robin-aws Oct 26, 2023
d9d3713
Mechanisms to check the built-in limits
robin-aws Oct 26, 2023
dbc369f
More complete error checking
robin-aws Oct 27, 2023
ca730f8
Document #if ISDAFNYRUNTIMELIB, typos
robin-aws Oct 31, 2023
dc0b8eb
Documentation in user guide and features table
robin-aws Oct 31, 2023
4812b89
Merge branch 'master' into github-issue-511
robin-aws Oct 31, 2023
582fe2c
Sigh…whitespace
robin-aws Oct 31, 2023
ab8c23b
Merge branch 'github-issue-511' of github.com:robin-aws/dafny into gi…
robin-aws Oct 31, 2023
761cbf8
PR feedback, release note
robin-aws Nov 1, 2023
b783675
Merge branch 'master' into github-issue-511
robin-aws Nov 1, 2023
f4adb04
Merge branch 'master' into github-issue-511
keyboardDrummer Nov 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/msbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
run: dotnet build Source/Dafny.sln
- name: Check whitespace and style
working-directory: dafny
run: dotnet tool run dotnet-format -w -s error --check Source/Dafny.sln --exclude DafnyCore/Scanner.cs --exclude DafnyCore/Parser.cs --exclude DafnyCore/GeneratedFromDafny.cs
run: dotnet tool run dotnet-format -w -s error --check Source/Dafny.sln --exclude DafnyCore/Scanner.cs --exclude DafnyCore/Parser.cs --exclude DafnyCore/GeneratedFromDafny.cs --exclude DafnyRuntime/DafnyRuntimeSystemModule.cs
- name: Create NuGet package (just to make sure it works)
run: dotnet pack --no-build dafny/Source/Dafny.sln
- name: Check uniformity of integration tests that exercise backends
Expand Down
25 changes: 17 additions & 8 deletions .github/workflows/runtime-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,27 @@ jobs:
cd ./Source/DafnyCore
make test
make check-format
- name: Test DafnyRuntime (C#)
run: |
cd ./Source/DafnyRuntime
make all
- name: Test DafnyRuntimeDafny
run: |
cd ./Source/DafnyRuntime/DafnyRuntimeDafny
make test
make check-format
make all
- name: Test DafnyRuntimeGo
run: |
cd ./Source/DafnyRuntime/DafnyRuntimeGo
make test
# This isn't strictly necessary since the Java runtime has to be built into a jar,
# which also runs the tests.
# But I prefer the simplicity of testing every testable runtime in this workflow
make all
- name: Test DafnyRuntimeJava
run: ./Source/DafnyRuntime/DafnyRuntimeJava/gradlew -p ./Source/DafnyRuntime/DafnyRuntimeJava test

run: |
cd ./Source/DafnyRuntime/DafnyRuntimeJava
make all
- name: Test DafnyRuntimePython
run: |
cd ./Source/DafnyRuntime/DafnyRuntimePython
make all
- name: Test DafnyRuntimeJs
run: |
cd ./Source/DafnyRuntime/DafnyRuntimeJs
make all
5 changes: 4 additions & 1 deletion Source/DafnyCore/AST/ShouldCompileOrVerify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public static bool ShouldCompile(this ModuleDefinition module, CompilationData p
}

if (module.FullName == "_System") {
return true;
return program.Options.SystemModuleTranslationMode != CommonOptionBag.SystemModuleMode.Omit;
}
if (program.Options.SystemModuleTranslationMode == CommonOptionBag.SystemModuleMode.Populate) {
return false;
}

if (module is DefaultModuleDefinition) {
Expand Down
35 changes: 34 additions & 1 deletion Source/DafnyCore/AST/SystemModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public TupleTypeDecl TupleType(IToken tok, int dims, bool allowCreationOfNewType
nonGhostTupleTypeDecl = TupleType(tok, nonGhostDims, allowCreationOfNewType);
}

tt = new TupleTypeDecl(argumentGhostness, SystemModule, nonGhostTupleTypeDecl, DontCompile());
tt = new TupleTypeDecl(argumentGhostness, SystemModule, nonGhostTupleTypeDecl, null);
if (tt.NonGhostDims > MaxNonGhostTupleSizeUsed) {
MaxNonGhostTupleSizeToken = tok;
MaxNonGhostTupleSizeUsed = tt.NonGhostDims;
Expand Down Expand Up @@ -490,6 +490,39 @@ public void ResolveValueTypeDecls(ProgramResolver programResolver) {
}
}
}

public void CheckHasAllTupleNonGhostDimsUpTo(int max) {
var allNeededDims = Enumerable.Range(0, max + 1).ToHashSet();
var allDeclaredDims = tupleTypeDecls.Keys
.Select(argumentGhostness => argumentGhostness.Count(ghost => !ghost))
.Distinct()
.ToHashSet();
if (!allDeclaredDims.SetEquals(allNeededDims)) {
throw new ArgumentException(@$"Not all tuple types declared between 0 and {max}!
needed: {allNeededDims.Comma()}
declared: {allDeclaredDims.Comma()}");
}
}

public void CheckHasAllArrayDimsUpTo(int max) {
var allNeededDims = Enumerable.Range(1, max).ToHashSet();
var allDeclaredDims = arrayTypeDecls.Keys.ToHashSet();
if (!allDeclaredDims.SetEquals(allNeededDims)) {
throw new ArgumentException(@$"Not all array types declared between 1 and {max}!
needed: {allNeededDims.Comma()}
declared: {allDeclaredDims.Comma()}");
}
}

public void CheckHasAllArrowAritiesUpTo(int max) {
var allNeededArities = Enumerable.Range(0, max + 1).ToHashSet();
var allDeclaredArities = ArrowTypeDecls.Keys.ToHashSet();
if (!allDeclaredArities.SetEquals(allNeededArities)) {
throw new ArgumentException(@$"Not all arrow types declared between 0 and {max}
needed: {allNeededArities.Comma()}
declared: {allDeclaredArities.Comma()}");
}
}
}

enum ValuetypeVariety {
Expand Down
3 changes: 3 additions & 0 deletions Source/DafnyCore/AST/TypeDeclarations/TupleTypeDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ private TupleTypeDecl(ModuleDefinition systemModule, List<TypeParameter> typeArg
}
}
this.EqualitySupport = argumentGhostness.Contains(true) ? ES.Never : ES.ConsultTypeArguments;

// TODO: Correct?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this TODO

ParentTypeInformation = new InheritanceInformationClass();
}
private static List<TypeParameter> CreateCovariantTypeParameters(int dims) {
Contract.Requires(0 <= dims);
Expand Down
42 changes: 37 additions & 5 deletions Source/DafnyCore/Compilers/CSharp/Compiler-Csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public class CsharpCompiler : SinglePassCompiler {

public override IReadOnlySet<Feature> UnsupportedFeatures => new HashSet<Feature> {
Feature.SubsetTypeTests,
Feature.TuplesWiderThan20
Feature.TuplesWiderThan20,
Feature.ArraysWithMoreThan16Dims,
Feature.ArrowsWithMoreThan16Arguments
};

public CsharpCompiler(DafnyOptions options, ErrorReporter reporter) : base(options, reporter) {
Expand Down Expand Up @@ -71,17 +73,26 @@ protected override void EmitHeader(Program program, ConcreteSyntaxTree wr) {
wr.WriteLine("// Optionally, you may want to include compiler switches like");
wr.WriteLine("// /debug /nowarn:162,164,168,183,219,436,1717,1718");
wr.WriteLine();
if (program.Options.SystemModuleTranslationMode == CommonOptionBag.SystemModuleMode.Populate) {
wr.WriteLine("#if ISDAFNYRUNTIMELIB");
}
wr.WriteLine("using System;");
wr.WriteLine("using System.Numerics;");
wr.WriteLine("using System.Collections;");
if (program.Options.SystemModuleTranslationMode == CommonOptionBag.SystemModuleMode.Populate) {
wr.WriteLine("#endif");
}
Synthesize = ProgramHasMethodsWithAttr(program, "synthesize");
if (Synthesize) {
CsharpSynthesizer.EmitImports(wr);
}
EmitDafnySourceAttribute(program, wr);

if (program.Options.SystemModuleTranslationMode != CommonOptionBag.SystemModuleMode.Populate) {
EmitDafnySourceAttribute(program, wr);
}

if (Options.IncludeRuntime) {
ReadRuntimeSystem(program, "DafnyRuntime.cs", wr);
EmitRuntimeSource("DafnyRuntimeCsharp", wr, false);
}

}
Expand Down Expand Up @@ -119,8 +130,25 @@ void EmitDafnySourceAttribute(Program program, ConcreteSyntaxTree wr) {
}

protected override void EmitBuiltInDecls(SystemModuleManager systemModuleManager, ConcreteSyntaxTree wr) {
if (systemModuleManager.MaxNonGhostTupleSizeUsed > 20) {
UnsupportedFeatureError(systemModuleManager.MaxNonGhostTupleSizeToken, Feature.TuplesWiderThan20);
switch (Options.SystemModuleTranslationMode) {
case CommonOptionBag.SystemModuleMode.Omit: {
CheckCommonSytemModuleLimits(systemModuleManager);
break;
}
case CommonOptionBag.SystemModuleMode.Populate: {
CheckSystemModulePopulatedToCommonLimits(systemModuleManager);
break;
}
}

// The declarations below would normally be omitted if we aren't compiling the system module,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like these declarations could be not-internal and only as part of the system module

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, but realized FuncExtensions in particular is tricky because they are extension methods on the System.Func family of delegates, and extension methods only ever apply within the current assembly. Given that I didn't bother spending the time moving the Array helper functions either. I'm adding a comment about this here though.

Copy link
Member

@keyboardDrummer keyboardDrummer Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extension methods only ever apply within the current assembly

I don't get what you mean. You can use extensions methods defined in a library for sure.

// but they are all marked as "internal", so they have to be included in each separately-compiled assembly.
// Instead we just make sure to guard them with "#if ISDAFNYRUNTIMELIB" when compiling the system module,
// so they don't become duplicates when --include-runtime is used.
// See comment at the top of DafnyRuntime.cs.

if (Options.SystemModuleTranslationMode == CommonOptionBag.SystemModuleMode.Populate) {
wr.WriteLine("#if ISDAFNYRUNTIMELIB");
}

var dafnyNamespace = CreateModule("Dafny", false, false, null, wr);
Expand All @@ -129,6 +157,10 @@ protected override void EmitBuiltInDecls(SystemModuleManager systemModuleManager
CsharpSynthesizer.EmitMultiMatcher(dafnyNamespace);
}
EmitFuncExtensions(systemModuleManager, wr);

if (Options.SystemModuleTranslationMode == CommonOptionBag.SystemModuleMode.Populate) {
wr.WriteLine("#endif");
}
}

// Generates casts for functions of those arities present in the program, like:
Expand Down
2 changes: 1 addition & 1 deletion Source/DafnyCore/Compilers/CSharp/Synthesizer-Csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void SynthesizeExpression(ConcreteSyntaxTree wr, ForallExpr forallExpr,
/// </summary>
internal static void EmitMultiMatcher(ConcreteSyntaxTree dafnyNamespace) {
const string multiMatcher = @"
class MultiMatcher {
internal class MultiMatcher {

private readonly Func<object[], bool> predicate;
private readonly int argumentCount;
Expand Down
7 changes: 3 additions & 4 deletions Source/DafnyCore/Compilers/Cplusplus/Compiler-cpp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public CppCompiler(DafnyOptions options, ErrorReporter reporter, ReadOnlyCollect
Feature.RunAllTests,
Feature.MethodSynthesis,
Feature.UnicodeChars,
Feature.ConvertingValuesToStrings
Feature.ConvertingValuesToStrings,
Feature.BuiltinsInRuntime
};

private List<DatatypeDecl> datatypeDecls = new();
Expand Down Expand Up @@ -118,10 +119,8 @@ protected override void EmitHeader(Program program, ConcreteSyntaxTree wr) {
this.classDeclsWr = headerFileWr.Fork();
this.hashWr = headerFileWr.Fork();

var rt = wr.NewFile("DafnyRuntime.h");

if (Options.IncludeRuntime) {
ReadRuntimeSystem(program, "DafnyRuntime.h", rt);
EmitRuntimeSource("DafnyRuntimeCpp", wr);
}

}
Expand Down
8 changes: 1 addition & 7 deletions Source/DafnyCore/Compilers/GoLang/Compiler-go.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,10 @@ protected override void EmitHeader(Program program, ConcreteSyntaxTree wr) {
EmitImports(wr, out RootImportWriter, out RootImportDummyWriter);

if (Options.IncludeRuntime) {
var rt = wr.NewFile("dafny/dafny.go");
ReadRuntimeSystem(program, "DafnyRuntime.go", rt);
rt = wr.NewFile("dafny/dafnyFromDafny.go");
ReadRuntimeSystem(program, "DafnyRuntimeFromDafny.go", rt);
EmitRuntimeSource("DafnyRuntimeGo", wr);
}
}

protected override void EmitBuiltInDecls(SystemModuleManager systemModuleManager, ConcreteSyntaxTree wr) {
}

private string DafnyTypeDescriptor => $"{HelperModulePrefix}TypeDescriptor";

// The implementation of seq<T> is now in DafnyRuntimeDafny/src/dafnyRuntime.dfy.
Expand Down
Loading
Loading