Skip to content

Commit

Permalink
Merge branch 'master' into fix-4823-general-traits-bug-extends
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelMayer authored Jan 8, 2024
2 parents d9b6c7a + 880ba3f commit 696ab51
Show file tree
Hide file tree
Showing 68 changed files with 1,671 additions and 1,013 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/nightly-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,3 @@ jobs:
ref: master
secrets:
nuget_api_key: ${{ secrets.NUGET_API_KEY }}

nightly-build-for-4-4:
if: github.repository_owner == 'dafny-lang'
uses: ./.github/workflows/nightly-build-reusable.yml
with:
ref: 4.4
publish-prerelease: true
secrets:
nuget_api_key: ${{ secrets.NUGET_API_KEY }}
2 changes: 1 addition & 1 deletion Source/DafnyCore/AST/Formatting.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module {:extern "Microsoft"} {:options "-functionSyntax:4"} Microsoft {
ensures allocated(x) {
}

/** Prints the entire program while fixing identation, based on
/** Prints the entire program while fixing indentation, based on
1) indentation information provided by the IIndentationFormatter reindent
2) Reindentation algorithm provided by the same reindent */
method ReindentProgramFromFirstToken(firstToken: IToken, reindent: IIndentationFormatter) returns (s: CsString)
Expand Down
2 changes: 2 additions & 0 deletions Source/DafnyCore/AST/Members/ICodeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,6 @@ public interface RedirectingTypeDecl : ICallable {
SubsetTypeDecl.WKind WitnessKind { get; }
Expression/*?*/ Witness { get; } // non-null iff WitnessKind is Compiled or Ghost
FreshIdGenerator IdGenerator { get; }

[FilledInDuringResolution] bool ConstraintIsCompilable { get; set; }
}
1 change: 1 addition & 0 deletions Source/DafnyCore/AST/SystemModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public SystemModuleManager(DafnyOptions options) {
NatDecl = new SubsetTypeDecl(RangeToken.NoToken, new Name("nat"),
new TypeParameter.TypeParameterCharacteristics(TypeParameter.EqualitySupportValue.InferredRequired, Type.AutoInitInfo.CompilableValue, false),
new List<TypeParameter>(), SystemModule, bvNat, natConstraint, SubsetTypeDecl.WKind.CompiledZero, null, ax);
((RedirectingTypeDecl)NatDecl).ConstraintIsCompilable = true;
SystemModule.SourceDecls.Add(NatDecl);
// create trait 'object'
ObjectDecl = new TraitDecl(RangeToken.NoToken, new Name("object"), SystemModule, new List<TypeParameter>(), new List<MemberDecl>(), DontCompile(), false, null);
Expand Down
7 changes: 7 additions & 0 deletions Source/DafnyCore/AST/TypeDeclarations/NewtypeDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public class NewtypeDecl : TopLevelDeclWithMembers, RevealableTypeDecl, Redirect
public SubsetTypeDecl.WKind WitnessKind { get; set; } = SubsetTypeDecl.WKind.CompiledZero;
public Expression/*?*/ Witness { get; set; } // non-null iff WitnessKind is Compiled or Ghost
[FilledInDuringResolution] public NativeType NativeType; // non-null for fixed-size representations (otherwise, use BigIntegers for integers)

[FilledInDuringResolution] bool RedirectingTypeDecl.ConstraintIsCompilable { get; set; }

[FilledInDuringResolution] public bool NativeTypeRangeImpliesAllConstraints; // indicates that all values in the range of the native type are values of the newtype
[FilledInDuringResolution] public bool TargetTypeCoversAllBitPatterns; // "target complete" -- indicates that any bit pattern that can fill the target type is a value of the newtype

public NewtypeDecl(RangeToken rangeToken, Name name, ModuleDefinition module, Type baseType, List<Type> parentTraits,
List<MemberDecl> members, Attributes attributes, bool isRefining)
: base(rangeToken, name, module, new List<TypeParameter>(), members, attributes, isRefining, parentTraits) {
Expand All @@ -24,6 +30,7 @@ public NewtypeDecl(RangeToken rangeToken, Name name, ModuleDefinition module, Ty
Contract.Requires(isRefining ^ (baseType != null));
Contract.Requires(members != null);
BaseType = baseType;
this.NewSelfSynonym();
}
public NewtypeDecl(RangeToken rangeToken, Name name, ModuleDefinition module, BoundVar bv, Expression constraint,
SubsetTypeDecl.WKind witnessKind, Expression witness, List<Type> parentTraits, List<MemberDecl> members, Attributes attributes, bool isRefining)
Expand Down
4 changes: 3 additions & 1 deletion Source/DafnyCore/AST/TypeDeclarations/SubsetTypeDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public class SubsetTypeDecl : TypeSynonymDecl, RedirectingTypeDecl, ICanAutoReve
public enum WKind { CompiledZero, Compiled, Ghost, OptOut, Special }
public readonly WKind WitnessKind;
public Expression/*?*/ Witness; // non-null iff WitnessKind is Compiled or Ghost
[FilledInDuringResolution] public bool ConstraintIsCompilable;
[FilledInDuringResolution] public bool CheckedIfConstraintIsCompilable = false; // Set to true lazily by the Resolver when the Resolver fills in "ConstraintIsCompilable".

[FilledInDuringResolution] bool RedirectingTypeDecl.ConstraintIsCompilable { get; set; }

public SubsetTypeDecl(RangeToken rangeToken, Name name, TypeParameter.TypeParameterCharacteristics characteristics, List<TypeParameter> typeArgs, ModuleDefinition module,
BoundVar id, Expression constraint, WKind witnessKind, Expression witness,
Attributes attributes)
Expand Down
6 changes: 6 additions & 0 deletions Source/DafnyCore/AST/TypeDeclarations/TypeSynonymDeclBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public Type RhsWithArgumentIgnoringScope(List<Type> typeArgs) {
ModuleDefinition RedirectingTypeDecl.Module { get { return EnclosingModuleDefinition; } }
BoundVar RedirectingTypeDecl.Var { get { return null; } }
Expression RedirectingTypeDecl.Constraint { get { return null; } }

bool RedirectingTypeDecl.ConstraintIsCompilable {
get => false;
set => throw new NotSupportedException();
}

SubsetTypeDecl.WKind RedirectingTypeDecl.WitnessKind { get { return SubsetTypeDecl.WKind.CompiledZero; } }
Expression RedirectingTypeDecl.Witness { get { return null; } }
FreshIdGenerator RedirectingTypeDecl.IdGenerator { get { return IdGenerator; } }
Expand Down
134 changes: 39 additions & 95 deletions Source/DafnyCore/AST/Types/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,15 @@ public Type UseInternalSynonym() {
[System.Diagnostics.Contracts.Pure]
public abstract bool Equals(Type that, bool keepConstraints = false);

public bool IsBoolType { get { return NormalizeExpand() is BoolType; } }
public bool IsCharType { get { return NormalizeExpand() is CharType; } }
public bool IsIntegerType { get { return NormalizeExpand() is IntType; } }
public bool IsRealType { get { return NormalizeExpand() is RealType; } }
public bool IsBigOrdinalType { get { return NormalizeExpand() is BigOrdinalType; } }
public bool IsBitVectorType { get { return AsBitVectorType != null; } }
public bool IsStringType { get { return AsSeqType?.Arg.IsCharType == true; } }
public BitvectorType AsBitVectorType { get { return NormalizeExpand() as BitvectorType; } }
public bool IsBoolType => NormalizeExpand() is BoolType;
public bool IsCharType => NormalizeExpand() is CharType;
public bool IsIntegerType => NormalizeExpand() is IntType;
public bool IsRealType => NormalizeExpand() is RealType;
public bool IsBigOrdinalType => NormalizeExpand() is BigOrdinalType;
public bool IsBitVectorType => AsBitVectorType != null;
public bool IsStringType => AsSeqType?.Arg.IsCharType == true;
public BitvectorType AsBitVectorType => NormalizeExpand() as BitvectorType;

public bool IsNumericBased() {
var t = NormalizeExpand();
return t.IsIntegerType || t.IsRealType || t.AsNewtype != null;
Expand Down Expand Up @@ -702,58 +703,32 @@ public bool IsArrowTypeWithoutPreconditions {
}
}
}
public bool IsArrowType {
get { return AsArrowType != null; }
}
public ArrowType AsArrowType {
get {
var t = NormalizeExpand();
return t as ArrowType;
}
}
public bool IsArrowType => AsArrowType != null;

public ArrowType AsArrowType => NormalizeExpand() as ArrowType;

public bool IsMapType => NormalizeExpand() is MapType { Finite: true };

public bool IsIMapType => NormalizeExpand() is MapType { Finite: false };

public bool IsISetType => NormalizeExpand() is SetType { Finite: false };

public bool IsMapType {
get {
var t = NormalizeExpand() as MapType;
return t != null && t.Finite;
}
}
public bool IsIMapType {
get {
var t = NormalizeExpand() as MapType;
return t != null && !t.Finite;
}
}
public bool IsISetType {
get {
var t = NormalizeExpand() as SetType;
return t != null && !t.Finite;
}
}
public NewtypeDecl AsNewtype {
get {
var udt = NormalizeExpand() as UserDefinedType;
return udt == null ? null : udt.ResolvedClass as NewtypeDecl;
return udt?.ResolvedClass as NewtypeDecl;
}
}
public TypeSynonymDecl AsTypeSynonym {
get {
var udt = this as UserDefinedType; // note, it is important to use 'this' here, not 'this.NormalizeExpand()'
if (udt == null) {
return null;
} else {
return udt.ResolvedClass as TypeSynonymDecl;
}
return udt?.ResolvedClass as TypeSynonymDecl;
}
}
public InternalTypeSynonymDecl AsInternalTypeSynonym {
get {
var udt = this as UserDefinedType; // note, it is important to use 'this' here, not 'this.NormalizeExpand()'
if (udt == null) {
return null;
} else {
return udt.ResolvedClass as InternalTypeSynonymDecl;
}
return udt?.ResolvedClass as InternalTypeSynonymDecl;
}
}
public RedirectingTypeDecl AsRedirectingType {
Expand All @@ -769,83 +744,52 @@ public RedirectingTypeDecl AsRedirectingType {
public RevealableTypeDecl AsRevealableType {
get {
var udt = this as UserDefinedType;
if (udt == null) {
return null;
} else {
return (udt.ResolvedClass as RevealableTypeDecl);
}
}
}
public bool IsRevealableType {
get { return AsRevealableType != null; }
}
public bool IsDatatype {
get {
return AsDatatype != null;
return udt?.ResolvedClass as RevealableTypeDecl;
}
}
public bool IsRevealableType => AsRevealableType != null;

public bool IsDatatype => AsDatatype != null;

public DatatypeDecl AsDatatype {
get {
var udt = NormalizeExpand() as UserDefinedType;
if (udt == null) {
return null;
} else {
return udt.ResolvedClass as DatatypeDecl;
}
}
}
public bool IsIndDatatype {
get {
return AsIndDatatype != null;
return udt?.ResolvedClass as DatatypeDecl;
}
}
public bool IsIndDatatype => AsIndDatatype != null;

public IndDatatypeDecl AsIndDatatype {
get {
var udt = NormalizeExpand() as UserDefinedType;
if (udt == null) {
return null;
} else {
return udt.ResolvedClass as IndDatatypeDecl;
}
}
}
public bool IsCoDatatype {
get {
return AsCoDatatype != null;
return udt?.ResolvedClass as IndDatatypeDecl;
}
}
public bool IsCoDatatype => AsCoDatatype != null;

public CoDatatypeDecl AsCoDatatype {
get {
var udt = NormalizeExpand() as UserDefinedType;
if (udt == null) {
return null;
} else {
return udt.ResolvedClass as CoDatatypeDecl;
}
return udt?.ResolvedClass as CoDatatypeDecl;
}
}
public bool InvolvesCoDatatype {
get {
return IsCoDatatype; // TODO: should really check structure of the type recursively
}
}
public bool IsTypeParameter {
get {
return AsTypeParameter != null;
}
}
public bool IsInternalTypeSynonym {
get { return AsInternalTypeSynonym != null; }
}
public bool IsTypeParameter => AsTypeParameter != null;

public bool IsInternalTypeSynonym => AsInternalTypeSynonym != null;

public TypeParameter AsTypeParameter {
get {
var ct = NormalizeExpandKeepConstraints() as UserDefinedType;
return ct?.ResolvedClass as TypeParameter;
}
}
public bool IsAbstractType {
get { return AsAbstractType != null; }
}
public bool IsAbstractType => AsAbstractType != null;

public AbstractTypeDecl AsAbstractType {
get {
var udt = this.Normalize() as UserDefinedType; // note, it is important to use 'this.Normalize()' here, not 'this.NormalizeExpand()'
Expand Down
2 changes: 2 additions & 0 deletions Source/DafnyCore/Compilers/CSharp/Compiler-Csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3291,6 +3291,8 @@ protected override void EmitConversionExpr(ConversionExpr e, bool inLetExprBody,
} else {
Contract.Assert(false, $"not implemented for C#: {e.E.Type} -> {e.ToType}");
}
} else if (e.E.Type.Equals(e.ToType) || e.E.Type.AsNewtype != null || e.ToType.AsNewtype != null) {
wr.Append(Expr(e.E, inLetExprBody, wStmts));
} else {
Contract.Assert(false, $"not implemented for C#: {e.E.Type} -> {e.ToType}");
}
Expand Down
7 changes: 5 additions & 2 deletions Source/DafnyCore/Compilers/Cplusplus/Compiler-cpp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2356,11 +2356,14 @@ protected override void EmitConversionExpr(ConversionExpr e, bool inLetExprBody,
wr.Write(".{0}()", Capitalize(GetNativeTypeName(nt)));
}
}
} else {
Contract.Assert(e.E.Type.IsBigOrdinalType);
} else if (e.E.Type.IsBigOrdinalType) {
Contract.Assert(e.ToType.IsNumericBased(Type.NumericPersuasion.Int));
// identity will do
wr.Append(Expr(e.E, inLetExprBody, wStmts));
} else {
// identity will do
Contract.Assert(e.E.Type.Equals(e.ToType) || e.E.Type.AsNewtype != null || e.ToType.AsNewtype != null);
wr.Append(Expr(e.E, inLetExprBody, wStmts));
}
}

Expand Down
2 changes: 2 additions & 0 deletions Source/DafnyCore/Compilers/GoLang/Compiler-go.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3576,6 +3576,8 @@ protected override void EmitConversionExpr(ConversionExpr e, bool inLetExprBody,
wr.Write(".{0}()", Capitalize(GetNativeTypeName(nt)));
}
}
} else if (e.E.Type.Equals(e.ToType) || e.E.Type.AsNewtype != null || e.ToType.AsNewtype != null) {
wr.Append(Expr(e.E, inLetExprBody, wStmts));
} else {
Contract.Assert(false, $"not implemented for go: {e.E.Type} -> {e.ToType}");
}
Expand Down
2 changes: 2 additions & 0 deletions Source/DafnyCore/Compilers/Java/Compiler-java.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4186,6 +4186,8 @@ protected override void EmitConversionExpr(ConversionExpr e, bool inLetExprBody,
} else {
Contract.Assert(false, $"not implemented for java: {fromType} -> {toType}");
}
} else if (e.E.Type.Equals(e.ToType) || e.E.Type.AsNewtype != null || e.ToType.AsNewtype != null) {
wr.Append(Expr(e.E, inLetExprBody, wStmts));
} else {
Contract.Assert(false, $"not implemented for java: {fromType} -> {toType}");
}
Expand Down
2 changes: 2 additions & 0 deletions Source/DafnyCore/Compilers/JavaScript/Compiler-js.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2432,6 +2432,8 @@ protected override void EmitConversionExpr(ConversionExpr e, bool inLetExprBody,
if (e.ToType.IsCharType) {
wr.Write(").toNumber())");
}
} else if (e.E.Type.Equals(e.ToType) || e.E.Type.AsNewtype != null || e.ToType.AsNewtype != null) {
wr.Append(Expr(e.E, inLetExprBody, wStmts));
} else {
Contract.Assert(false, $"not implemented for javascript: {e.E.Type} -> {e.ToType}");
}
Expand Down
16 changes: 14 additions & 2 deletions Source/DafnyCore/Compilers/Python/PythonBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace Microsoft.Dafny.Compilers;

Expand Down Expand Up @@ -46,6 +47,17 @@ private static string FindModuleName(string externFilename) {
return Path.GetExtension(externFilename) == ".py" ? Path.GetFileNameWithoutExtension(externFilename) : null;
}

private static readonly Dictionary<OSPlatform, string> PlatformDefaults = new() {
{OSPlatform.Linux, "python3"},
{OSPlatform.Windows, "python"},
{OSPlatform.FreeBSD, "python3"},
{OSPlatform.OSX, "python3"},
};
private static string DefaultPythonCommand => PlatformDefaults.SingleOrDefault(
kv => RuntimeInformation.IsOSPlatform(kv.Key),
new(OSPlatform.Linux, "python3")
).Value;

bool CopyExternLibraryIntoPlace(string externFilename, string mainProgram, TextWriter outputWriter) {
// Grossly, we need to look in the file to figure out where to put it
var moduleName = FindModuleName(externFilename);
Expand Down Expand Up @@ -88,7 +100,7 @@ public override bool CompileTargetProgram(string dafnyProgramName, string target
}
}
if (!runAfterCompile) {
var psi = PrepareProcessStartInfo("python3");
var psi = PrepareProcessStartInfo(DefaultPythonCommand);
psi.Arguments = $"-m compileall -q {Path.GetDirectoryName(targetFilename)}";
return 0 == RunProcess(psi, outputWriter, outputWriter, "Error while compiling Python files.");
}
Expand All @@ -99,7 +111,7 @@ public override bool RunTargetProgram(string dafnyProgramName, string targetProg
string targetFilename, ReadOnlyCollection<string> otherFileNames, object compilationResult, TextWriter outputWriter,
TextWriter errorWriter) {
Contract.Requires(targetFilename != null || otherFileNames.Count == 0);
var psi = PrepareProcessStartInfo("python3", Options.MainArgs.Prepend(targetFilename));
var psi = PrepareProcessStartInfo(DefaultPythonCommand, Options.MainArgs.Prepend(targetFilename));
psi.EnvironmentVariables["PYTHONIOENCODING"] = "utf8";
return 0 == RunProcess(psi, outputWriter, errorWriter);
}
Expand Down
Loading

0 comments on commit 696ab51

Please sign in to comment.