Skip to content

Commit 57a57f0

Browse files
Improve the Lisa code generator from SCProof
1 parent 15bf010 commit 57a57f0

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

lisa-utils/src/main/scala/lisa/utils/ProofsConverter.scala

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object ProofsConverter {
2121
* @param f formula to convert
2222
* @return Scala code representing the formula in string format
2323
*/
24-
private def any2code(a: K.Sequent | K.Formula | K.Term): String = (a match
24+
def any2code(a: K.Sequent | K.Formula | K.Term): String = (a match
2525
case sq: K.Sequent => asFront(sq)
2626
case form: K.Formula => asFront(form)
2727
case term: K.Term => asFront(term)
@@ -109,7 +109,7 @@ object ProofsConverter {
109109
* @param proof proof to extract formulas from
110110
* @return set of formulas
111111
*/
112-
private def extractFormulasFromProof(proof: K.SCProof): Set[K.Formula] =
112+
def extractFormulasFromProof(proof: K.SCProof): Set[K.Formula] =
113113
proof.steps.foldLeft(Set.empty[K.Formula])((prev, next) => {
114114
prev ++ (next match
115115
case sp @ SCSubproof(subproof, _) => extractFormulasFromProof(subproof)
@@ -122,37 +122,40 @@ object ProofsConverter {
122122
})
123123

124124
/**
125-
* Extracts all variables, functions, formula variables, predicates and connectors from a set of formulas
125+
* Extracts all symbols: variables, functions, formula variables, predicates and connectors from a set of formulas
126126
*
127127
* @param formulas set of formulas to extract variables from
128128
* @return tuple of sets of variables, functions, formula variables, predicates and connectors
129129
*/
130-
private def extractVariables(
130+
def extractSymbols(
131131
formulas: Set[K.Formula]
132132
): (Set[K.VariableLabel], Set[K.SchematicFunctionLabel], Set[K.VariableFormulaLabel], Set[K.SchematicPredicateLabel], Set[K.SchematicConnectorLabel]) =
133133
def extractVariablesAux(
134134
formula: K.Formula
135135
): (Set[K.VariableLabel], Set[K.SchematicFunctionLabel], Set[K.VariableFormulaLabel], Set[K.SchematicPredicateLabel], Set[K.SchematicConnectorLabel]) =
136-
var variableSet = formula.schematicTermLabels.collect { case v: K.VariableLabel => v }
137-
var functionSet = formula.schematicTermLabels.collect { case f: K.SchematicFunctionLabel => f }
138-
var formulaVariableSet = formula.schematicAtomicLabels.collect { case v: K.VariableFormulaLabel => v }
139-
var predicateSet = formula.schematicAtomicLabels.collect { case p: K.SchematicPredicateLabel => p }
140-
var connectorSet = formula.schematicConnectorLabels.collect { case c: K.SchematicConnectorLabel => c }
136+
val variableSet = formula.schematicTermLabels.collect { case v: K.VariableLabel => v } ++ formula.freeVariables ++ formula.freeSchematicTermLabels.collect { case v: K.VariableLabel => v }
137+
// val constantSet = formula.constantTermLabels.collect { case c: K.ConstantFunctionLabel if c.arity == 0 => c }
138+
val functionSet = formula.schematicTermLabels.collect { case f: K.SchematicFunctionLabel => f } ++ formula.freeSchematicTermLabels.collect { case f: K.SchematicFunctionLabel => f }
139+
// val constantFunctionSet = formula.constantTermLabels.collect { case c: K.ConstantFunctionLabel if c.arity > 0 => c }
140+
val formulaVariableSet = formula.schematicAtomicLabels.collect { case v: K.VariableFormulaLabel => v } ++ formula.freeVariableFormulaLabels
141+
val predicateSet = formula.schematicAtomicLabels.collect { case p: K.SchematicPredicateLabel => p }
142+
// val constantPredicateSet = formula.constantAtomicLabels
143+
val connectorSet = formula.schematicConnectorLabels.collect { case c: K.SchematicConnectorLabel => c }
141144
(variableSet, functionSet, formulaVariableSet, predicateSet, connectorSet)
142145

143-
formulas.foldLeft(
144-
(Set.empty[K.VariableLabel], Set.empty[K.SchematicFunctionLabel], Set.empty[K.VariableFormulaLabel], Set.empty[K.SchematicPredicateLabel], Set.empty[K.SchematicConnectorLabel])
145-
)((prev, next) => {
146-
val (variableSet, functionSet, formulaVariableSet, predicateSet, connectorSet) = prev
147-
val (variableSet_, functionSet_, formulaVariableSet_, predicateSet_, connectorSet_) = extractVariablesAux(next)
148-
(
149-
variableSet ++ variableSet_,
150-
functionSet ++ functionSet_,
151-
formulaVariableSet ++ formulaVariableSet_,
152-
predicateSet ++ predicateSet_,
153-
connectorSet ++ connectorSet_
154-
)
155-
})
146+
formulas.foldLeft((Set.empty[K.VariableLabel], Set.empty[K.SchematicFunctionLabel], Set.empty[K.VariableFormulaLabel], Set.empty[K.SchematicPredicateLabel], Set.empty[K.SchematicConnectorLabel]))(
147+
(prev, next) => {
148+
val (variableSet, functionSet, formulaVariableSet, predicateSet, connectorSet) = prev
149+
val (variableSet_, functionSet_, formulaVariableSet_, predicateSet_, connectorSet_) = extractVariablesAux(next)
150+
(
151+
variableSet ++ variableSet_,
152+
functionSet ++ functionSet_,
153+
formulaVariableSet ++ formulaVariableSet_,
154+
predicateSet ++ predicateSet_,
155+
connectorSet ++ connectorSet_
156+
)
157+
}
158+
)
156159

157160
/**
158161
* Generates a valid Scala/Lisa code to declare variables, functions, formula variables, predicates and connectors
@@ -164,7 +167,7 @@ object ProofsConverter {
164167
* @return Scala code representing the variables in string format
165168
*/
166169
private def generateVariablesCode(formulas: Set[K.Formula], accessibility: String): String =
167-
val (variableSet, functionSet, formulaVariableSet, predicateSet, connectorSet) = extractVariables(formulas)
170+
val (variableSet, functionSet, formulaVariableSet, predicateSet, connectorSet) = extractSymbols(formulas)
168171
val access = if accessibility != "" then accessibility.strip() + " " else ""
169172
(variableSet.map(v => access + s"val ${v.id} = variable").toList.sorted ++
170173
functionSet.map(f => access + s"val ${f.id} = function[${f.arity}]").toList.sorted ++
@@ -181,7 +184,7 @@ object ProofsConverter {
181184
*
182185
* @return Scala code representing the variables in string format
183186
*/
184-
private def generateVariablesCode(proof: K.SCProof, accessibility: String = "private"): String =
187+
def generateVariablesCode(proof: K.SCProof, accessibility: String = "private"): String =
185188
generateVariablesCode(extractFormulasFromProof(proof), accessibility)
186189

187190
/**
@@ -193,7 +196,9 @@ object ProofsConverter {
193196
* @return Scala code representing the theorem in string format
194197
*/
195198
private def generateTheoremCode(name: String, proof: K.SCProof): String = {
196-
s"val $name = Theorem(\n" +
199+
// lowercase and underscore-separated version of the theorem name
200+
val filteredName = "[A-Za-z0-9]+".r.findAllIn(name).mkString("_").toLowerCase
201+
s"val $filteredName = Theorem(\n" +
197202
indent(any2code(proof.conclusion)) +
198203
s"\n) {\n" +
199204
indent(scproof2code(proof)) +

0 commit comments

Comments
 (0)