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

Support 16.0.0 features #53

Merged
merged 4 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 dhall-lang
Submodule dhall-lang updated 54 files
+1 −6 docs/howtos/How-to-integrate-Dhall.md
+11 −11 standard/beta-normalization.md
+14 −8 standard/dhall.abnf
+2 −0 standard/imports.md
+12 −35 standard/type-inference.md
+1 −0 tests/import/data/file with spaces.txt
+1 −0 tests/import/success/unit/FilenameWithSpacesA.dhall
+1 −0 tests/import/success/unit/FilenameWithSpacesB.dhall
+1 −0 tests/import/success/unit/QuotedPathA.dhall
+3 −0 tests/import/success/unit/QuotedPathB.dhall
+16 −0 tests/normalization/success/regression/UnsaturatedBuiltinsA.dhall
+3 −0 tests/normalization/success/regression/UnsaturatedBuiltinsB.dhall
+1 −0 tests/parser/failure/unit/WithPrecedence1.dhall
+1 −0 tests/parser/failure/unit/WithPrecedence2.dhall
+1 −0 tests/parser/failure/unit/WithPrecedence3.dhall
+0 −0 tests/parser/success/unit/WithPrecedence1A.dhall
+0 −0 tests/parser/success/unit/WithPrecedence1B.dhallb
+0 −0 tests/parser/success/unit/WithPrecedence1B.diag
+1 −0 tests/parser/success/unit/WithPrecedence2A.dhall
+ tests/parser/success/unit/WithPrecedence2B.dhallb
+1 −0 tests/parser/success/unit/WithPrecedence2B.diag
+1 −0 tests/parser/success/unit/WithPrecedence3A.dhall
+ tests/parser/success/unit/WithPrecedence3B.dhallb
+1 −0 tests/parser/success/unit/WithPrecedence3B.diag
+1 −0 tests/parser/success/unit/operators/PrecedenceEquivalenceA.dhall
+1 −0 tests/parser/success/unit/operators/PrecedenceEquivalenceB.dhallb
+1 −0 tests/parser/success/unit/operators/PrecedenceEquivalenceB.diag
+0 −1 tests/type-inference/failure/mixedUnions.dhall
+0 −1 tests/type-inference/failure/unit/RecordMixedKinds3.dhall
+0 −1 tests/type-inference/failure/unit/RightBiasedRecordMergeMixedKinds2.dhall
+0 −1 tests/type-inference/failure/unit/RightBiasedRecordMergeMixedKinds3.dhall
+1 −1 tests/type-inference/success/unit/AssertSimpleA.dhall
+0 −0 tests/type-inference/success/unit/RecordMixedKinds2A.dhall
+0 −0 tests/type-inference/success/unit/RecordMixedKinds2B.dhall
+0 −0 tests/type-inference/success/unit/RecordMixedKindsA.dhall
+0 −0 tests/type-inference/success/unit/RecordMixedKindsB.dhall
+0 −0 tests/type-inference/success/unit/RecordTypeMixedKinds2A.dhall
+0 −0 tests/type-inference/success/unit/RecordTypeMixedKinds2B.dhall
+0 −0 tests/type-inference/success/unit/RecordTypeMixedKinds3A.dhall
+0 −0 tests/type-inference/success/unit/RecordTypeMixedKinds3B.dhall
+0 −0 tests/type-inference/success/unit/RecordTypeMixedKindsA.dhall
+0 −0 tests/type-inference/success/unit/RecordTypeMixedKindsB.dhall
+0 −0 tests/type-inference/success/unit/RecursiveRecordMergeMixedKindsA.dhall
+0 −0 tests/type-inference/success/unit/RecursiveRecordMergeMixedKindsB.dhall
+0 −0 tests/type-inference/success/unit/RightBiasedRecordMergeMixedKindsA.dhall
+0 −0 tests/type-inference/success/unit/RightBiasedRecordMergeMixedKindsB.dhall
+0 −0 tests/type-inference/success/unit/UnionTypeMixedKinds1A.dhall
+1 −0 tests/type-inference/success/unit/UnionTypeMixedKinds1B.dhall
+0 −0 tests/type-inference/success/unit/UnionTypeMixedKinds2A.dhall
+1 −0 tests/type-inference/success/unit/UnionTypeMixedKinds2B.dhall
+0 −0 tests/type-inference/success/unit/UnionTypeMixedKinds3A.dhall
+1 −0 tests/type-inference/success/unit/UnionTypeMixedKinds3B.dhall
+1 −0 tests/type-inference/success/unit/UnionTypeMixedKinds4A.dhall
+1 −0 tests/type-inference/success/unit/UnionTypeMixedKinds4B.dhall
26 changes: 13 additions & 13 deletions modules/core/src/main/java/org/dhallj/core/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

/** Represents a Dhall operator. */
public enum Operator {
OR("||", 2, true),
AND("&&", 6, true),
EQUALS("==", 11, true),
NOT_EQUALS("!=", 12, true),
PLUS("+", 3, false),
TIMES("*", 10, false),
TEXT_APPEND("++", 4, false),
LIST_APPEND("#", 5, false),
COMBINE("\u2227", 7, false),
PREFER("\u2afd", 8, false),
COMBINE_TYPES("\u2a53", 9, false),
IMPORT_ALT("?", 1, false),
EQUIVALENT("\u2261", 13, false),
OR("||", 3, true),
AND("&&", 7, true),
EQUALS("==", 12, true),
NOT_EQUALS("!=", 13, true),
PLUS("+", 4, false),
TIMES("*", 11, false),
TEXT_APPEND("++", 5, false),
LIST_APPEND("#", 6, false),
COMBINE("\u2227", 8, false),
PREFER("\u2afd", 9, false),
COMBINE_TYPES("\u2a53", 10, false),
IMPORT_ALT("?", 2, false),
EQUIVALENT("\u2261", 1, false),
COMPLETE("::", 0, false);

private static final Operator[] values = values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private static Expr applyLambdas(Expr base, final List<Expr> args) {
for (int j = i; j < args.size(); j++) {
currentLambda = Expr.makeApplication(currentLambda, args.get(j));
}
currentLambda = currentLambda.accept(BetaNormalize.instance);
}

return currentLambda;
Expand All @@ -38,24 +39,24 @@ private static final Expr booleanToExpr(boolean value) {
return value ? Expr.Constants.TRUE : Expr.Constants.FALSE;
}

static final Expr apply(Expr base, final List<Expr> args) {
static final Expr apply(Expr base, List<Expr> args) {
String builtIn = Expr.Util.asBuiltIn(base);

if (builtIn != null) {
if (builtIn.equals("Natural/fold")) {
if (builtIn.equals("Natural/fold") && args.size() >= 4) {
Expr result = naturalFold(base, args);

if (result != null) {
return result;
}

} else if (builtIn.equals("List/fold") && args.size() > 1) {
} else if (builtIn.equals("List/fold") && args.size() >= 5) {
Expr result = listFold(base, args);

if (result != null) {
return result;
}
} else if (builtIn.equals("Optional/fold") && args.size() > 1) {
} else if (builtIn.equals("Optional/fold") && args.size() >= 5) {
Expr result = optionalFold(base, args);

if (result != null) {
Expand Down Expand Up @@ -327,118 +328,54 @@ private static List<Expr> drop(List<Expr> args, int count) {
return result;
}

static final Expr naturalFold(Expr base, final List<Expr> args) {
private static final Expr naturalFold(Expr base, List<Expr> args) {
BigInteger firstAsNaturalLiteral = Expr.Util.asNaturalLiteral(args.get(0));

if (firstAsNaturalLiteral != null) {
List<Expr> newArgs = new ArrayList<Expr>(4);

// Temporarily necessary to match a bug in the Haskell implementation.
if (args.size() == 1) {
newArgs.add(args.get(0));
newArgs.add(Expr.makeIdentifier("natural"));
newArgs.add(Expr.makeIdentifier("succ"));
newArgs.add(Expr.makeIdentifier("zero"));
} else if (args.size() == 2) {
newArgs.add(args.get(0));
newArgs.add(args.get(1));
newArgs.add(Expr.makeIdentifier("succ"));
newArgs.add(Expr.makeIdentifier("zero"));
} else if (args.size() == 3) {
newArgs.add(args.get(0));
newArgs.add(args.get(1));
newArgs.add(args.get(2).increment("zero"));
newArgs.add(Expr.makeIdentifier("zero"));
} else {
newArgs.addAll(args);
}

Expr applied = null;
if (firstAsNaturalLiteral.equals(BigInteger.ZERO)) {
applied = newArgs.get(3);
applied = args.get(3);
} else {
applied =
Expr.makeApplication(
newArgs.get(2),
args.get(2),
Expr.makeApplication(
Expr.makeApplication(
Expr.makeApplication(
Expr.makeApplication(
Expr.Constants.NATURAL_FOLD,
Expr.makeNaturalLiteral(
firstAsNaturalLiteral.subtract(BigInteger.ONE))),
newArgs.get(1)),
newArgs.get(2)),
newArgs.get(3)))
args.get(1)),
args.get(2)),
args.get(3)))
.accept(BetaNormalize.instance);
}

if (applied == null) {
return null;
}

if (args.size() == 1) {
return Expr.makeLambda(
"natural",
Expr.Constants.TYPE,
Expr.makeLambda(
"succ",
Expr.makePi(Expr.makeIdentifier("natural"), Expr.makeIdentifier("natural")),
Expr.makeLambda("zero", Expr.makeIdentifier("natural"), applied)));
} else if (args.size() == 2) {
return Expr.makeLambda(
"succ",
Expr.makePi(newArgs.get(1), newArgs.get(1)),
Expr.makeLambda("zero", newArgs.get(1).increment("succ"), applied));
} else if (args.size() == 3) {
return Expr.makeLambda("zero", newArgs.get(1), applied);
} else if (args.size() == 4) {
if (args.size() == 4) {
return applied;
} else {
return Expr.makeApplication(applied, drop(newArgs, 4)).accept(BetaNormalize.instance);
return Expr.makeApplication(applied, drop(args, 4)).accept(BetaNormalize.instance);
}
}
return null;
}

static final Expr listFold(Expr base, final List<Expr> args) {
List<Expr> newArgs = new ArrayList<Expr>(5);

if (args.size() == 2) {
newArgs.add(args.get(0).increment("list").increment("cons").increment("nil"));
newArgs.add(args.get(1).increment("list").increment("cons").increment("nil"));
newArgs.add(Expr.makeIdentifier("list"));
newArgs.add(Expr.makeIdentifier("cons"));
newArgs.add(Expr.makeIdentifier("nil"));
} else if (args.size() == 3) {
newArgs.add(args.get(0).increment("cons").increment("nil"));
newArgs.add(args.get(1).increment("cons").increment("nil"));
newArgs.add(args.get(2).increment("cons").increment("nil"));
newArgs.add(Expr.makeIdentifier("cons"));
newArgs.add(Expr.makeIdentifier("nil"));
} else if (args.size() == 4) {
newArgs.add(args.get(0).increment("nil"));
newArgs.add(args.get(1).increment("nil"));
newArgs.add(args.get(2).increment("nil"));
newArgs.add(args.get(3).increment("nil"));
newArgs.add(Expr.makeIdentifier("nil"));
} else {
newArgs.addAll(args);
}

List<Expr> listArg = Expr.Util.asListLiteral(newArgs.get(1));
private static final Expr listFold(Expr base, List<Expr> args) {
List<Expr> listArg = Expr.Util.asListLiteral(args.get(1));

if (listArg != null) {

Expr applied = null;
if (!listArg.isEmpty()) {
Expr head = listArg.get(0);

Expr tail;
if (listArg.size() == 1) {
tail =
Expr.makeEmptyListLiteral(Expr.makeApplication(Expr.Constants.LIST, newArgs.get(0)));

tail = Expr.makeEmptyListLiteral(Expr.makeApplication(Expr.Constants.LIST, args.get(0)));
} else {
List<Expr> listArgTail = new ArrayList<>(listArg);
listArgTail.remove(0);
Expand All @@ -447,100 +384,44 @@ static final Expr listFold(Expr base, final List<Expr> args) {
}
applied =
Expr.makeApplication(
Expr.makeApplication(newArgs.get(3), head),
Expr.makeApplication(args.get(3), head),
Expr.makeApplication(
Expr.makeApplication(
Expr.makeApplication(
Expr.makeApplication(
Expr.makeApplication(Expr.Constants.LIST_FOLD, newArgs.get(0)),
Expr.makeApplication(Expr.Constants.LIST_FOLD, args.get(0)),
tail),
newArgs.get(2)),
newArgs.get(3)),
newArgs.get(4)))
args.get(2)),
args.get(3)),
args.get(4)))
.accept(BetaNormalize.instance);
} else {
applied = newArgs.get(4);
applied = args.get(4);
}

if (args.size() == 2) {
return Expr.makeLambda(
"list",
Expr.Constants.TYPE,
Expr.makeLambda(
"cons",
Expr.makePi(
newArgs.get(0),
Expr.makePi(Expr.makeIdentifier("list"), Expr.makeIdentifier("list"))),
Expr.makeLambda("nil", Expr.makeIdentifier("list"), applied)));
} else if (args.size() == 3) {
return Expr.makeLambda(
"cons",
Expr.makePi(newArgs.get(0), Expr.makePi(newArgs.get(2), newArgs.get(2))),
Expr.makeLambda("nil", newArgs.get(2).increment("cons"), applied));
} else if (args.size() == 4) {
return Expr.makeLambda("nil", newArgs.get(2).increment("cons"), applied);
} else if (args.size() == 5) {
if (args.size() == 5) {
return applied;
} else {
return Expr.makeApplication(applied, drop(newArgs, 5)).accept(BetaNormalize.instance);
return Expr.makeApplication(applied, drop(args, 5)).accept(BetaNormalize.instance);
}
}
return null;
}

static final Expr optionalFold(Expr base, final List<Expr> args) {
List<Expr> newArgs = new ArrayList<Expr>(5);

if (args.size() == 2) {
newArgs.add(args.get(0).increment("optional").increment("some").increment("none"));
newArgs.add(args.get(1).increment("optional").increment("some").increment("none"));
newArgs.add(Expr.makeIdentifier("optional"));
newArgs.add(Expr.makeIdentifier("some"));
newArgs.add(Expr.makeIdentifier("none"));
} else if (args.size() == 3) {
newArgs.add(args.get(0).increment("some").increment("none"));
newArgs.add(args.get(1).increment("some").increment("none"));
newArgs.add(args.get(2).increment("some").increment("none"));
newArgs.add(Expr.makeIdentifier("some"));
newArgs.add(Expr.makeIdentifier("none"));
} else if (args.size() == 4) {
newArgs.add(args.get(0).increment("none"));
newArgs.add(args.get(1).increment("none"));
newArgs.add(args.get(2).increment("none"));
newArgs.add(args.get(3).increment("none"));
newArgs.add(Expr.makeIdentifier("none"));
} else {
newArgs.addAll(args);
}

Expr someArg = Expr.Util.getSomeArg(newArgs.get(1));
Expr noneArg = Expr.Util.getNoneArg(newArgs.get(1));
private static final Expr optionalFold(Expr base, List<Expr> args) {
Expr someArg = Expr.Util.getSomeArg(args.get(1));
Expr noneArg = Expr.Util.getNoneArg(args.get(1));

if (someArg != null || noneArg != null) {
Expr applied =
(someArg != null)
? (Expr.makeApplication(newArgs.get(3), someArg).accept(BetaNormalize.instance))
: newArgs.get(4);

if (args.size() == 2) {
return Expr.makeLambda(
"optional",
Expr.Constants.TYPE,
Expr.makeLambda(
"some",
Expr.makePi(Expr.makeIdentifier("optional"), Expr.makeIdentifier("optional")),
Expr.makeLambda("none", Expr.makeIdentifier("optional"), applied)));
} else if (args.size() == 3) {
return Expr.makeLambda(
"some",
Expr.makePi(newArgs.get(2), newArgs.get(2)),
Expr.makeLambda("none", newArgs.get(2).increment("some"), applied));
} else if (args.size() == 4) {
return Expr.makeLambda("none", newArgs.get(2).increment("some"), applied);
} else if (newArgs.size() == 5) {
? (Expr.makeApplication(args.get(3), someArg).accept(BetaNormalize.instance))
: args.get(4);

if (args.size() == 5) {
return applied;
} else {
return Expr.makeApplication(applied, drop(newArgs, 5)).accept(BetaNormalize.instance);
return Expr.makeApplication(applied, drop(args, 5)).accept(BetaNormalize.instance);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,13 @@ public final Expr onRecordType(Iterable<Entry<String, Expr>> fields, int size) {
throw TypeCheckFailure.makeFieldDuplicateError(fieldName);
}

Universe universe = Universe.fromExpr(field.getValue().accept(this));
Expr type = field.getValue().accept(this);
Universe universe = Universe.fromExpr(type);

if (universe != null) {
max = max.max(universe);
} else {
throw TypeCheckFailure.makeFieldTypeError(fieldName);
throw TypeCheckFailure.makeFieldTypeError(fieldName, type);
}
}

Expand All @@ -215,46 +216,32 @@ public final Expr onRecordType(Iterable<Entry<String, Expr>> fields, int size) {

@Override
public final Expr onUnionType(Iterable<Entry<String, Expr>> fields, int size) {
if (size == 0) {
return Constants.TYPE;
} else {
Set<String> seen = new HashSet<>();
Universe firstUniverse = null;
Set<String> fieldNamesSeen = new HashSet<>(size);

for (Entry<String, Expr> field : fields) {
String fieldName = field.getKey();
Universe max = Universe.TYPE;

if (!seen.contains(fieldName)) {
seen.add(fieldName);
for (Entry<String, Expr> field : fields) {
String fieldName = field.getKey();

Expr alternativeType = field.getValue();
if (!fieldNamesSeen.add(fieldName)) {
throw TypeCheckFailure.makeAlternativeDuplicateError(fieldName);
}

if (alternativeType != null) {
Universe universe = Universe.fromExpr(alternativeType.accept(this));
Expr alternativeType = field.getValue();

if (universe != null) {
if (firstUniverse == null) {
firstUniverse = universe;
} else {
if (universe != firstUniverse) {
throw TypeCheckFailure.makeAlternativeTypeMismatchError(alternativeType);
}
}
} else {
throw TypeCheckFailure.makeAlternativeTypeError(alternativeType);
}
}
if (alternativeType != null) {
Expr type = alternativeType.accept(this);
Universe universe = Universe.fromExpr(type);

if (universe != null) {
max = max.max(universe);
} else {
throw TypeCheckFailure.makeAlternativeDuplicateError(fieldName);
throw TypeCheckFailure.makeAlternativeTypeError(fieldName, type);
}
}

if (firstUniverse == null) {
return Constants.TYPE;
} else {
return firstUniverse.toExpr();
}
}

return max.toExpr();
}

@Override
Expand Down
Loading