-
Notifications
You must be signed in to change notification settings - Fork 18
/
LLKProofParser.scala
240 lines (201 loc) · 10.3 KB
/
LLKProofParser.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package gapt.formats.llk
import gapt.expr._
import gapt.formats.llk.ast.LambdaAST
import gapt.proofs.HOLSequent
import gapt.proofs.lk.LKProof
import java.io.FileReader
import gapt.expr.formula.Atom
import gapt.expr.formula.Formula
import gapt.expr.ty.Ty
import gapt.formats.InputFile
/**
* An extended proof database allows to label subproofs by formulas. It provides mappings from formulas to proofs
* additionally to the list of pairs.
*/
case class ExtendedProofDatabase(
eproofs: Map[Formula, LKProof],
eaxioms: Map[Formula, Formula],
edefinitions: Map[Const, Expr] ) {
val proofs = eproofs.map( x =>
x._1 match {
case Atom( Const( sym, _, _ ), _ ) => ( sym.toString, x._2 )
case Atom( Var( sym, _ ), _ ) => ( sym.toString, x._2 )
} ).toList
val Definitions = edefinitions
val axioms = eaxioms.values.toList map ( x => HOLSequent( Nil, x :: Nil ) )
def proof( name: String ) = proofs.find( _._1 == name ).get._2
}
/**
* The abstract class for tokens of an llk proof. TTokens represent type declarations, ATokens represent axiom
* and definition declarations, and RTokens represent a rule inference.
*/
abstract class Token
/**
* A TToken represents an LLK type declaration.
*
* @param decltype either "VARDEC" or "CONSTDEC"
* @param names a list of symbol names
* @param types the assigned type
*/
case class TToken( decltype: String, names: List[String], types: Ty ) extends Token
/**
* An AToken represents an Axiom declaration or Definition declaration.
*
* @param rule either "AXIOMDEF", "PREDDEF" or"FUNDEF"
* @param name the (unique) name of the definition/axiom
* @param antecedent the antecedent of the declaration sequent (not yet typechecked)
* @param succedent the succedent of the declaration sequent (not yet typechecked)
*/
case class AToken( rule: String, name: Option[LambdaAST], antecedent: List[LambdaAST], succedent: List[LambdaAST] ) extends Token
/**
* A RToken represents a rule application.
*
* @param rule One out of "AX", "ALLL", "ALLR", "EXL", "EXR", "ANDL", "ANDR", "ORL", "ORR", "IMPL", "IMPR", "NEGL",
* "NEGR", "CUT", "EQL", "EQR", "WEAKL", "WEAKR", "CONTRL", "CONTRR", "DEF", "BETA", "INSTAXIOM"
* @param name quantifier rules allow optional specification of the subsitution term, definitions and axiom instantiations
* take the referenced declaration, etc.
* @param antecedent the antecedent of the declaration sequent (not yet typechecked)
* @param succedent the antecedent of the declaration sequent (not yet typechecked)
* @param sub some rules like axiom instantiation specify substitutions, which are passed as list of var-term pairs
*/
case class RToken( rule: String, name: Option[LambdaAST], antecedent: List[LambdaAST],
succedent: List[LambdaAST], sub: List[( ast.Var, LambdaAST )] ) extends Token
class HybridLatexParserException( m: String, t: Throwable ) extends Exception( m, t ) {
def this( m: String ) = this( m, null )
}
/**
* This code works around some limitations of latex syntax and adds alternative syntax for abstraction, application and
* adds support of some macros used in the n-tape proof.
*/
trait LatexReplacementParser extends DeclarationParser {
override lazy val abs: PackratParser[LambdaAST] =
( "\\lambda" ~ atom2 ~ formula ) ^^ { case _ ~ v ~ f => ast.Abs( v, f ) }
override lazy val appOrAtomWeq: PackratParser[LambdaAST] =
( parens( "@" ~> rep1( formula ) ) |
( "\\apply" ~> "{" ~> rep1( formula ) <~ "}" ) ) ^^ { x =>
x match {
case List( elem ) => elem
case list => ast.App( list )
}
} | atomWeq
lazy val latexmacros: PackratParser[LambdaAST] =
( "\\ite{" ~> formula <~ "}" ) ~ ( "{" ~> formula <~ "}" ) ~ ( "{" ~> formula <~ "}" ) ^^ {
case f1 ~ f2 ~ f3 => ast.App( List( ast.Var( "ite" ), f1, f2, f3 ) )
} |
( "\\benc{" ~> formula <~ "}" ) ^^ {
case f1 => ast.App( List( ast.Var( "be" ), f1 ) )
} |
( "\\ienc{" ~> formula <~ "}" ) ^^ {
case f1 => ast.App( List( ast.Var( "ie" ), f1 ) )
}
override lazy val atom: PackratParser[LambdaAST] =
latexmacros | atom1 | atom2 | topbottom
override lazy val iatom2: PackratParser[LambdaAST] = iatom3 ~ """([+\-]|\\bm)""".r ~ iatom3 ^^ {
_ match {
case t1 ~ "\\bm" ~ t2 => ast.App( List( ast.Var( "bm" ), t1, t2 ) ) //TODO: change to \dotdiv and modify prooftool etc
case t1 ~ sym ~ t2 => ast.App( List( ast.Var( sym ), t1, t2 ) )
}
} | iatom3
//accept latex connectives
override lazy val implication: PackratParser[LambdaAST] = ( dis_or_con ~ ( "<->" | "->" | "<-" | "\\impl" ) ~ dis_or_con ) ^^ {
r =>
( r: @unchecked ) match {
case f ~ "->" ~ g => ast.Imp( f, g )
case f ~ "\\impl" ~ g => ast.Imp( f, g )
case f ~ "<-" ~ g => ast.Imp( g, f )
case f ~ "<->" ~ g => ast.And( ast.Imp( f, g ), ast.Imp( g, f ) )
}
} | dis_or_con
override lazy val disjunction: PackratParser[LambdaAST] =
( conlit ~ ( ( "|" | "\\lor" ) ~> disjunction ) ^^ { case f ~ g => ast.Or( f, g ) } ) | conlit
override lazy val conjunction: PackratParser[LambdaAST] =
( qliteral ~ ( ( "&" | "\\land" ) ~> conjunction ) ^^ { case f ~ g => ast.And( f, g ) } ) | qliteral
override lazy val allformula_ : PackratParser[LambdaAST] =
( ( "all" | "\\forall" ) ~> atom2 ~ ( allformula_ | exformula_ | formula ) ) ^^ { case v ~ f => ast.All( v, f ) }
override lazy val exformula_ : PackratParser[LambdaAST] =
( ( "exists" | "\\exists" ) ~> atom2 ~ ( allformula_ | exformula_ | formula ) ) ^^ { case v ~ f => ast.Exists( v, f ) }
override lazy val negation: PackratParser[LambdaAST] =
( ( """(-|\\neg)""".r ) ~> literal2 ^^ { x => ast.Neg( x ) } ) | absOrAtomWeq
lazy val reservedset = Set( "\\neg", "\\land", "\\lor", "\\impl", "\\forall", "\\exists" )
override lazy val atomsymb: Parser[String] = atomsymb2.^?(
{ case x if !( reservedset contains x ) => x },
( x => "error: \\neg,\\land,\\lor,\\impl,\\forall,\\exists are reserved names" ) )
lazy val atomsymb2: Parser[String] = atomregexp
}
object LLKProofParser extends LLKProofParser {
def apply( file: InputFile ): ExtendedProofDatabase = createLKProof( parse( file.read ) )
}
class LLKProofParser extends DeclarationParser with LatexReplacementParser with TokenToLKConverter {
def parse( in: CharSequence ): List[Token] = {
parseAll( rules, in ) match {
case Success( r, _ ) => r
case failure: NoSuccess =>
throw new HybridLatexParserException(
"Error parsing Hybrid Latex/LK at position " + failure.next.pos + ": " + failure.msg )
}
}
lazy val rules: PackratParser[List[Token]] = rep1( ( rule1 | rule2 | rule3 | decl | comment ) )
lazy val comment: PackratParser[RToken] = ( "%" ~> "[^%]*".r <~ "%" ) ^^ { _ =>
RToken( "COMMENT", None, Nil, Nil, Nil )
}
lazy val rule1: PackratParser[RToken] = ( ( "\\" ~> "CONTINUEWITH" <~ "{" ) ~ atom <~ "}" ) ^^ {
_ match {
case cw ~ atom => RToken( cw, Some( atom ), Nil, Nil, Nil )
}
}
lazy val rule2: PackratParser[Token] = ( "\\" ~>
"(AX|AND[LR]|OR[LR]|IMP[LR]|NEG[LR]|EQ[LR]|WEAK[LR]|CONTR[LR]|CUT|DEF|BETA|PREDDEF|FUNDEF|TAUTCOMPLETION|AUTOPROP)".r
<~ "{" ) ~ ( repsep( formula, "," ) <~ "}" ) ~ ( "{" ~> repsep( formula, "," ) <~ "}" ) ^^ {
_ match {
case ( name @ "PREDDEF" ) ~ de ~ to => AToken( name, None, de, to )
case ( name @ "FUNDEF" ) ~ de ~ to => AToken( name, None, de, to )
case name ~ antecedent ~ succedent => RToken( name, None, antecedent, succedent, Nil )
}
}
lazy val rule3: PackratParser[Token] = rule3a | rule3b
lazy val rule3a: PackratParser[Token] = ( "\\" ~> "(ALL[LR]|EX[LR]|INSTLEMMA|CONTINUEFROM|AXIOMDEC)".r
<~ "{" <~ "([^}]+:)?".r ) ~ ( opt( formula ) <~ "}" ) ~ ( "{" ~> repsep( formula, "," ) <~ "}" ) ~ ( "{" ~> repsep( formula, "," ) <~ "}" ) ^^ {
_ match {
case ( name @ "AXIOMDEC" ) ~ arg1 ~ antecedent ~ succedent => AToken( name, arg1, antecedent, succedent )
case name ~ arg1 ~ antecedent ~ succedent => RToken( name, arg1, antecedent, succedent, Nil )
}
}
lazy val rule3b: PackratParser[Token] = ( "\\" ~> "(INSTAXIOM|EQAXIOM)".r
<~ "{" ) ~ ( opt( "([^:}]+:)?".r ) ~> opt( "sub" ~> parens( substitution ) ) ) ~ ( opt( formula ) <~ "}" ) ~
( "{" ~> repsep( formula, "," ) <~ "}" ) ~ ( "{" ~> repsep( formula, "," ) <~ "}" ) ^^ {
_ match {
case name ~ Some( sub ) ~ arg1 ~ antecedent ~ succedent => /* println("Parsed sub:"+sub); */ RToken( name, arg1, antecedent, succedent, sub )
case name ~ None ~ arg1 ~ antecedent ~ succedent => RToken( name, arg1, antecedent, succedent, Nil )
}
}
lazy val substitution: PackratParser[List[( ast.Var, ast.LambdaAST )]] = rep1sep( single_substitution, "," )
lazy val single_substitution: PackratParser[( ast.Var, ast.LambdaAST )] =
( atomsymb ~ ( "=" ~> formula ) ) ^^ { case x ~ y => ( ast.Var( x ), y ) }
lazy val decl: PackratParser[TToken] = ( "\\" ~> "(CONSTDEC|VARDEC)".r <~ "{" ) ~
( rep1sep( symbolnames, "," ) <~ "}" ) ~ ( "{" ~> complexType <~ "}" ) ^^ {
r =>
( r: @unchecked ) match {
case "CONSTDEC" ~ namest ~ ( types: Ty ) => TToken( "CONST", namest, types )
case "VARDEC" ~ namest ~ ( types: Ty ) => TToken( "VAR", namest, types )
}
}
def splitAtOutermostComma( s: String ): List[String] = splitAtOutermostComma( s, "", List(), 0 ).reverse
def splitAtOutermostComma( s: String, current: String, acc: List[String], level: Int ): List[String] = {
if ( s.isEmpty ) {
require( level == 0, "Problem splitting a list of formulas apart: there are still " + level + " parenthesis left open!" )
if ( current.isEmpty ) acc else current :: acc
} else {
s.head match {
case '(' =>
splitAtOutermostComma( s.tail, current + s.head, acc, level + 1 )
case ')' =>
require( level > 0, "Problem splitting a list of formulas apart: trying to close parenthesis without corresponding open one! " + s )
splitAtOutermostComma( s.tail, current + s.head, acc, level - 1 )
case ',' if ( level == 0 ) =>
splitAtOutermostComma( s.tail, "", current :: acc, level )
case _ =>
splitAtOutermostComma( s.tail, current + s.head, acc, level )
}
}
}
}