-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1170 from informalsystems/gabriela/row-simplifica…
…tion Consolidate row simplification utility
- Loading branch information
Showing
7 changed files
with
140 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* ---------------------------------------------------------------------------------- | ||
* Copyright (c) Informal Systems 2023. All rights reserved. | ||
* Licensed under the Apache 2.0. | ||
* See License.txt in the project root for license information. | ||
* --------------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* Simplification for quint types | ||
* | ||
* @author Gabriela Moreira | ||
* | ||
* @module | ||
*/ | ||
|
||
import { IRTransformer, transformRow, transformType } from '../ir/IRTransformer' | ||
import { Row } from '../ir/quintTypes' | ||
import { TypeScheme } from './base' | ||
|
||
/** | ||
* Simplifies a type scheme by flattening all type rows. | ||
* | ||
* A row like `{ a: int | { b: bool | r } }` becomes | ||
* `{ a: int, b: bool | r }`. | ||
* | ||
* @param typeScheme - The type scheme to be simplified | ||
* | ||
* @returns The simplified type scheme | ||
*/ | ||
export function simplify(typeScheme: TypeScheme): TypeScheme { | ||
const simplifier = new RowSimplifier() | ||
return { ...typeScheme, type: transformType(simplifier, typeScheme.type) } | ||
} | ||
|
||
/** | ||
* Simplifies a row type. | ||
* | ||
* A row like `{ a: int | { b: bool | r } }` becomes | ||
* `{ a: int, b: bool | r }`. | ||
* | ||
* @param row - The tow to be simplified | ||
* | ||
* @returns The simplified row | ||
*/ | ||
export function simplifyRow(row: Row): Row { | ||
const simplifier = new RowSimplifier() | ||
return transformRow(simplifier, row) | ||
} | ||
|
||
class RowSimplifier implements IRTransformer { | ||
exitRow(row: Row): Row { | ||
if (row.kind !== 'row') { | ||
return row | ||
} | ||
|
||
if (row.fields.length === 0) { | ||
return row.other | ||
} | ||
|
||
if (row.other.kind !== 'row') { | ||
return row | ||
} | ||
|
||
return { ...row, fields: row.fields.concat(row.other.fields), other: row.other.other } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, it } from 'mocha' | ||
import { assert } from 'chai' | ||
import { typeSchemeToString } from '../../src/types/printing' | ||
import { simplify } from '../../src/types/simplification' | ||
import { toScheme } from '../../src/types/base' | ||
import { QuintRecordType } from '../../src' | ||
|
||
describe('simplify', () => { | ||
it('flattens nested row tails', () => { | ||
// { a: int | { b: bool | { c: str | v } } } | ||
const type: QuintRecordType = { | ||
kind: 'rec', | ||
fields: { | ||
kind: 'row', | ||
fields: [{ fieldName: 'a', fieldType: { kind: 'int' } }], | ||
other: { | ||
kind: 'row', | ||
fields: [{ fieldName: 'b', fieldType: { kind: 'bool' } }], | ||
other: { | ||
kind: 'row', | ||
fields: [{ fieldName: 'c', fieldType: { kind: 'str' } }], | ||
other: { kind: 'var', name: 'v' }, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const simplifiedType = simplify(toScheme(type)) | ||
|
||
assert.deepEqual(typeSchemeToString(simplifiedType), '{ a: int, b: bool, c: str | v }') | ||
}) | ||
|
||
it('simplifies rows with empty fields', () => { | ||
// { | { b: bool | { c: str | v } } } | ||
const type: QuintRecordType = { | ||
kind: 'rec', | ||
fields: { | ||
kind: 'row', | ||
fields: [], | ||
other: { | ||
kind: 'row', | ||
fields: [{ fieldName: 'b', fieldType: { kind: 'bool' } }], | ||
other: { | ||
kind: 'row', | ||
fields: [{ fieldName: 'c', fieldType: { kind: 'str' } }], | ||
other: { kind: 'var', name: 'v' }, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const simplifiedType = simplify(toScheme(type)) | ||
|
||
assert.deepEqual(typeSchemeToString(simplifiedType), '{ b: bool, c: str | v }') | ||
}) | ||
}) |