Skip to content

Commit 63eb388

Browse files
authored
Parametric Sorts (#7)
* Added support for parametric sorts and correct conversion of identifiers with numeral arguments. Added shadowJar to gradle config to make fat JAR directly executable. * Added readme note pointing to parser version.
1 parent 3bc05db commit 63eb388

File tree

10 files changed

+155
-91
lines changed

10 files changed

+155
-91
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ problem, then dumps it to standard output:
6767
$ java -jar semgus-java.jar max2-exp.sem.json
6868
```
6969

70+
The current JSON format is in line with the 2-23-24 edition of Semgus-Parser which introduced support for parametric sorts.
71+
7072
For more information, check out all the declarations and accompanying JavaDocs in the source code.

build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
java
33
`maven-publish`
4+
id("com.github.johnrengelman.shadow") version "7.0.0"
45
}
56

67
group = "org.semgus"
@@ -38,6 +39,15 @@ tasks.getByName<Jar>("jar") {
3839
}
3940
}
4041

42+
tasks.shadowJar {
43+
configurations = listOf(project.configurations.getByName("runtimeClasspath")) // Include runtime dependencies in shadow JAR
44+
manifest {
45+
attributes(
46+
"Main-Class" to "org.semgus.java.Main"
47+
)
48+
}
49+
}
50+
4151
publishing {
4252
publications {
4353
create<MavenPublication>("maven") {

src/main/java/org/semgus/java/event/EventParser.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ private static DeclareFunctionEvent parseDeclareFunction(JSONObject eventDto) th
164164
String name = JsonUtils.getString(eventDto, "name");
165165
JSONObject rankDto = JsonUtils.getObject(eventDto, "rank");
166166

167-
Identifier returnType;
168-
List<Identifier> argumentTypes;
167+
Sort returnType;
168+
List<Sort> argumentTypes;
169169
try {
170-
returnType = Identifier.deserializeAt(rankDto, "returnSort");
170+
returnType = Sort.deserializeAt(rankDto, "returnSort");
171171
JSONArray argumentTypesDto = JsonUtils.getArray(rankDto, "argumentSorts");
172172
try {
173-
argumentTypes = Identifier.deserializeList(argumentTypesDto);
173+
argumentTypes = Sort.deserializeList(argumentTypesDto);
174174
} catch (DeserializationException e) {
175175
throw e.prepend("argumentSorts");
176176
}
@@ -246,9 +246,9 @@ private static DefineDatatypeEvent parseDefineDatatype(JSONObject eventDto) thro
246246
String constructorName = JsonUtils.getString(constructorDto, "name");
247247
JSONArray argumentTypesDto = JsonUtils.getArray(constructorDto, "children");
248248

249-
List<Identifier> argumentTypes;
249+
List<Sort> argumentTypes;
250250
try {
251-
argumentTypes = Identifier.deserializeList(argumentTypesDto);
251+
argumentTypes = Sort.deserializeList(argumentTypesDto);
252252
} catch (DeserializationException e) {
253253
throw e.prepend("children");
254254
}
@@ -331,10 +331,10 @@ private static HornClauseEvent parseHornClause(JSONObject eventDto) throws Deser
331331
"constructor");
332332
}
333333

334-
// parse constructor arg type identifiers
335-
List<Identifier> constructorArgTypes;
334+
// parse constructor arg type sorts
335+
List<Sort> constructorArgTypes;
336336
try {
337-
constructorArgTypes = Identifier.deserializeList(constructorArgTypesDto);
337+
constructorArgTypes = Sort.deserializeList(constructorArgTypesDto);
338338
} catch (DeserializationException e) {
339339
throw e.prepend("constructor.argumentSorts");
340340
}

src/main/java/org/semgus/java/event/SmtSpecEvent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.semgus.java.event;
22

3-
import org.semgus.java.object.Identifier;
3+
import org.semgus.java.object.Sort;
44
import org.semgus.java.object.SmtTerm;
55
import org.semgus.java.object.TypedVar;
66

@@ -21,8 +21,8 @@ public sealed interface SmtSpecEvent extends SpecEvent {
2121
*/
2222
record DeclareFunctionEvent(
2323
String name,
24-
Identifier returnType,
25-
List<Identifier> argumentTypes
24+
Sort returnType,
25+
List<Sort> argumentTypes
2626
) implements SmtSpecEvent {
2727
// NO-OP
2828
}
@@ -37,7 +37,7 @@ record DeclareFunctionEvent(
3737
*/
3838
record DefineFunctionEvent(
3939
String name,
40-
Identifier returnType,
40+
Sort returnType,
4141
List<TypedVar> arguments,
4242
SmtTerm body
4343
) implements SmtSpecEvent {
@@ -76,7 +76,7 @@ record DefineDatatypeEvent(String name, List<Constructor> constructors) implemen
7676
* @param name The name of the constructor.
7777
* @param argumentTypes The types of the arguments to the constructor.
7878
*/
79-
public record Constructor(String name, List<Identifier> argumentTypes) {
79+
public record Constructor(String name, List<Sort> argumentTypes) {
8080
// NO-OP
8181
}
8282

src/main/java/org/semgus/java/object/Identifier.java

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,50 +52,14 @@ public static Identifier deserialize(Object idDtoRaw) throws DeserializationExce
5252
throw new DeserializationException("Identifier must either be a string or an array!");
5353
}
5454

55-
/**
56-
* Deserializes an identifier from the SemGuS JSON format at a given key in a parent JSON object.
57-
*
58-
* @param parentDto The parent JSON object.
59-
* @param key The key whose value should be deserialized.
60-
* @return The deserialized identifier.
61-
* @throws DeserializationException If the value at {@code key} is not a valid representation of an identifier.
62-
*/
63-
public static Identifier deserializeAt(JSONObject parentDto, String key) throws DeserializationException {
64-
Object identifierDto = JsonUtils.get(parentDto, key);
65-
try {
66-
return deserialize(identifierDto);
67-
} catch (DeserializationException e) {
68-
throw e.prepend(key);
69-
}
70-
}
71-
72-
/**
73-
* Deserializes a list of identifiers from a JSON array.
74-
*
75-
* @param idsDto The JSON array of identifiers.
76-
* @return The list of the deserialized identifiers.
77-
* @throws DeserializationException If {@code idsDto} is not an array of valid representations of identifiers.
78-
*/
79-
public static List<Identifier> deserializeList(JSONArray idsDto) throws DeserializationException {
80-
Identifier[] ids = new Identifier[idsDto.size()];
81-
for (int i = 0; i < ids.length; i++) {
82-
try {
83-
ids[i] = deserialize(idsDto.get(i));
84-
} catch (DeserializationException e) {
85-
throw e.prepend(i);
86-
}
87-
}
88-
return Arrays.asList(ids);
89-
}
90-
9155
@Override
9256
public String toString() {
9357
if (indices.length == 0) {
9458
return name;
9559
}
96-
StringBuilder sb = new StringBuilder("(").append(name);
60+
StringBuilder sb = new StringBuilder("(_ ").append(name);
9761
for (Index index : indices) {
98-
sb.append(" ").append(index);
62+
sb.append(" ").append(index.toString());
9963
}
10064
return sb.append(")").toString();
10165
}
@@ -113,26 +77,10 @@ public sealed interface Index {
11377
* @throws DeserializationException If {@code indexDtoRaw} is not a valid representation of an index value.
11478
*/
11579
static Index deserialize(Object indexDtoRaw) throws DeserializationException {
116-
if (indexDtoRaw instanceof String index) {
117-
return new NString(index);
118-
} else if (indexDtoRaw instanceof Long index) {
80+
if (indexDtoRaw instanceof Long index) {
11981
return new NInt(index.intValue());
12082
}
121-
throw new DeserializationException("Identifier index must either be a string or integer constant!");
122-
}
123-
124-
/**
125-
* A string index value.
126-
*
127-
* @param value The string value.
128-
*/
129-
record NString(String value) implements Index {
130-
131-
@Override
132-
public String toString() {
133-
return "\"" + value + "\"";
134-
}
135-
83+
throw new DeserializationException("Identifier index must be an integer constant!");
13684
}
13785

13886
/**

src/main/java/org/semgus/java/object/RelationApp.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public static RelationApp deserialize(JSONObject relAppDto) throws Deserializati
3535
sigDto.size(), args.size()));
3636
}
3737

38-
// deserialize type identifiers
39-
List<Identifier> types;
38+
// deserialize type sorts
39+
List<Sort> types;
4040
try {
41-
types = Identifier.deserializeList(sigDto);
41+
types = Sort.deserializeList(sigDto);
4242
} catch (DeserializationException e) {
4343
throw e.prepend("signature");
4444
}

src/main/java/org/semgus/java/object/SmtContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public String toString() {
5252
* @param name The name of the constructor.
5353
* @param argumentTypes The types of the constructor's arguments.
5454
*/
55-
public record Constructor(String name, List<Identifier> argumentTypes) {
55+
public record Constructor(String name, List<Sort> argumentTypes) {
5656

5757
@Override
5858
public String toString() {
@@ -61,7 +61,7 @@ public String toString() {
6161
}
6262

6363
StringBuilder sb = new StringBuilder("(").append(name);
64-
for (Identifier argumentType : argumentTypes) {
64+
for (Sort argumentType : argumentTypes) {
6565
sb.append(" ").append(argumentType);
6666
}
6767
return sb.append(")").toString();

src/main/java/org/semgus/java/object/SmtTerm.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ static SmtTerm deserializeAt(JSONObject parentDto, String key) throws Deserializ
6969
* @throws DeserializationException If {@code termDto} is not a valid representation of a function application.
7070
*/
7171
private static SmtTerm deserializeApplication(JSONObject termDto) throws DeserializationException {
72-
// deserialize function and return type identifiers
73-
Identifier id = Identifier.deserializeAt(termDto, "name");
74-
Identifier returnType = Identifier.deserializeAt(termDto, "returnSort");
72+
// deserialize function and return type sorts
73+
Sort id = Sort.deserializeAt(termDto, "name");
74+
Sort returnType = Sort.deserializeAt(termDto, "returnSort");
7575

7676
// zip together argument terms and argument types
7777
JSONArray argTypes = JsonUtils.getArray(termDto, "argumentSorts");
@@ -84,9 +84,9 @@ private static SmtTerm deserializeApplication(JSONObject termDto) throws Deseria
8484
Application.TypedTerm[] argTerms = new Application.TypedTerm[argTypes.size()];
8585
for (int i = 0; i < argTerms.length; i++) {
8686
// deserialize type
87-
Identifier type;
87+
Sort type;
8888
try {
89-
type = Identifier.deserialize(argTypes.get(i));
89+
type = Sort.deserialize(argTypes.get(i));
9090
} catch (DeserializationException e) {
9191
throw e.prepend("argumentSorts." + i);
9292
}
@@ -122,7 +122,7 @@ private static SmtTerm deserializeQuantifier(JSONObject termDto, Quantifier.Type
122122
JSONObject bindingDto = bindingsDto.get(i);
123123
try {
124124
bindings[i] = new TypedVar(JsonUtils.getString(bindingDto, "name"),
125-
Identifier.deserializeAt(bindingDto, "sort"));
125+
Sort.deserializeAt(bindingDto, "sort"));
126126
} catch (DeserializationException e) {
127127
throw e.prepend("bindings." + i);
128128
}
@@ -180,7 +180,7 @@ private static SmtTerm deserializeMatch(JSONObject termDto) throws Deserializati
180180
* @throws DeserializationException If {@code termDto} is not a valid representation of a variable.
181181
*/
182182
private static SmtTerm deserializeVariable(JSONObject termDto) throws DeserializationException {
183-
return new Variable(JsonUtils.getString(termDto, "name"), Identifier.deserializeAt(termDto, "sort"));
183+
return new Variable(JsonUtils.getString(termDto, "name"), Sort.deserializeAt(termDto, "sort"));
184184
}
185185

186186
/**
@@ -261,11 +261,11 @@ private static int readHexChar(char hexChar) throws DeserializationException {
261261
/**
262262
* Represents a function application in an SMT formula.
263263
*
264-
* @param name The identifier for the function.
264+
* @param name The Sort for the function.
265265
* @param returnType The function's return type.
266266
* @param arguments The arguments to the function.
267267
*/
268-
record Application(Identifier name, Identifier returnType, List<TypedTerm> arguments) implements SmtTerm {
268+
record Application(Sort name, Sort returnType, List<TypedTerm> arguments) implements SmtTerm {
269269

270270
@Override
271271
public String toString() {
@@ -279,7 +279,7 @@ public String toString() {
279279
* @param type The argument type.
280280
* @param term The subterm being passed as an argument.
281281
*/
282-
public record TypedTerm(Identifier type, SmtTerm term) {
282+
public record TypedTerm(Sort type, SmtTerm term) {
283283

284284
@Override
285285
public String toString() {
@@ -398,9 +398,9 @@ public String toString() {
398398
* Represents a variable in an SMT formula.
399399
*
400400
* @param name The name of the variable.
401-
* @param type The identifier for the type of the variable.
401+
* @param type The Sort for the type of the variable.
402402
*/
403-
record Variable(String name, Identifier type) implements SmtTerm {
403+
record Variable(String name, Sort type) implements SmtTerm {
404404

405405
@Override
406406
public String toString() {

0 commit comments

Comments
 (0)