-
Notifications
You must be signed in to change notification settings - Fork 18
/
recursionSchemes.scala
647 lines (563 loc) · 26.6 KB
/
recursionSchemes.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package gapt.grammars
import gapt.expr.formula.fol._
import gapt.expr._
import gapt.expr.formula.All
import gapt.expr.formula.And
import gapt.expr.formula.Atom
import gapt.expr.formula.Bottom
import gapt.expr.formula.Eq
import gapt.expr.formula.Formula
import gapt.expr.formula.Imp
import gapt.expr.formula.Neg
import gapt.expr.formula.Or
import gapt.expr.formula.Top
import gapt.expr.formula.fol.FOLAtom
import gapt.expr.formula.fol.FOLFormula
import gapt.expr.formula.fol.FOLVar
import gapt.expr.formula.hol._
import gapt.expr.subst.FOLSubstitution
import gapt.expr.subst.Substitution
import gapt.expr.ty.FunctionType
import gapt.expr.ty.TBase
import gapt.expr.ty.To
import gapt.expr.ty.arity
import gapt.expr.util.constants
import gapt.expr.util.expressionSize
import gapt.expr.util.freeVariables
import gapt.expr.util.rename
import gapt.expr.util.subTerms
import gapt.expr.util.syntacticMGU
import gapt.expr.util.syntacticMatching
import gapt.formats.babel.{ BabelExporter, BabelSignature, MapBabelSignature, Precedence }
import gapt.logic.hol.simplifyPropositional
import gapt.logic.hol.toNNF
import gapt.proofs.context.Context
import gapt.provers.maxsat.{ MaxSATSolver, bestAvailableMaxSatSolver }
import gapt.utils.{ Doc, Logger }
import scala.collection.mutable
case class Rule( lhs: Expr, rhs: Expr ) {
require( freeVariables( rhs ) subsetOf freeVariables( lhs ), s"$rhs has more free variables than $lhs" )
require( lhs.ty == rhs.ty, s"$lhs has different type than $rhs" )
def apply( term: Expr ): Option[Expr] =
syntacticMatching( lhs, term ).map( _( rhs ) )
def apply( subst: Substitution ): Rule =
Rule( subst( lhs ), subst( rhs ) )
override def toString: String = toSigRelativeString
def toSigRelativeString( implicit sig: BabelSignature ) = s"${lhs.toSigRelativeString} -> ${rhs.toSigRelativeString}"
}
private class RecursionSchemeExporter( unicode: Boolean, rs: RecursionScheme )
extends BabelExporter( unicode, rs.babelSignature ) {
import Doc._
def csep( docs: List[Doc] ): Doc = wordwrap( docs, "," )
def `export`(): String = {
val nonTerminals = rs.startSymbol +: ( rs.nonTerminals - rs.startSymbol ).toList.sortBy { _.name }
val ntDecl = group( "Non-terminals:" <> nest( line <> csep(
nonTerminals map { show( _, false, Map(), Map() )._1.inPrec( 0 ) } ) ) )
val tDecl = group( "Terminals:" <> nest( line <> csep(
rs.terminals.toList.sortBy { _.name } map { show( _, false, Map(), Map() )._1.inPrec( 0 ) } ) ) )
val knownTypes = ( rs.nonTerminals union rs.terminals ).map { c => c.name -> c }.toMap
val rules = group( stack( rs.rules.toList sortBy { _.toString } map {
case Rule( lhs, rhs ) =>
group( show( lhs, false, Map(), knownTypes )._1.inPrec( Precedence.impl ) </> nest( "→" </>
show( rhs, true, Map(), knownTypes )._1.inPrec( Precedence.impl ) ) )
} ) )
group( ntDecl </> tDecl <> line </> rules <> line ).render( lineWidth )
}
}
case class RecursionScheme( startSymbol: Const, nonTerminals: Set[Const], rules: Set[Rule] ) {
require( nonTerminals contains startSymbol )
rules foreach {
r =>
( r: @unchecked ) match {
case Rule( Apps( leftHead: Const, _ ), _ ) =>
require( nonTerminals contains leftHead )
}
}
def terminals: Set[Const] =
rules flatMap { case Rule( lhs, rhs ) => constants.nonLogical( lhs ) union constants.nonLogical( rhs ) } diff nonTerminals
def babelSignature = MapBabelSignature( terminals union nonTerminals )
def language: Set[Expr] = parametricLanguage()
def languageWithDummyParameters: Set[Expr] =
( startSymbol.ty: @unchecked ) match {
case FunctionType( _, argtypes ) =>
parametricLanguage( argtypes.zipWithIndex.map { case ( t, i ) => Const( s"dummy$i", t ) }: _* )
}
def rulesFrom( nonTerminal: Const ): Set[Rule] =
rules collect { case r @ Rule( Apps( `nonTerminal`, _ ), _ ) => r }
def parametricLanguage( params: Expr* ): Set[Expr] = {
require( params.size == arity( startSymbol ) )
generatedTerms( startSymbol( params: _* ) )
}
def generatedTerms( from: Expr ): Set[Expr] = {
val seen = mutable.Set[Expr]()
val gen = mutable.Set[Expr]()
def rewrite( t: Expr ): Unit = t match {
case _ if seen contains t => ()
case Apps( head: Const, args ) if nonTerminals contains head =>
rules foreach { _( t ) foreach rewrite }
seen += t
case _ =>
gen += t
}
rewrite( from )
gen.toSet
}
override def toString: String = new RecursionSchemeExporter( unicode = true, rs = this ).export()
}
object RecursionScheme {
def apply( startSymbol: Const, rules: ( Expr, Expr )* ): RecursionScheme =
apply( startSymbol, rules map { case ( from, to ) => Rule( from, to ) } toSet )
def apply( startSymbol: Const, nonTerminals: Set[Const], rules: ( Expr, Expr )* ): RecursionScheme =
RecursionScheme( startSymbol, nonTerminals, rules map { case ( from, to ) => Rule( from, to ) } toSet )
def apply( startSymbol: Const, rules: Set[Rule] ): RecursionScheme = {
val nonTerminals = rules.map {
r =>
( r: @unchecked ) match {
case Rule( Apps( head: Const, _ ), _ ) => head
}
} + startSymbol
RecursionScheme( startSymbol, nonTerminals, rules )
}
}
object preOrderTraversal {
def apply( term: Expr ): Seq[Expr] = term match {
case App( a, b ) => term +: ( apply( a ) ++ apply( b ) )
case _: Const | _: Var => Seq( term )
}
}
object canonicalVars {
def apply( term: Expr ): Expr =
FOLSubstitution( preOrderTraversal( term ).
collect { case v: FOLVar => v }.
distinct.
zipWithIndex.map { case ( v, i ) => v -> FOLVar( s"x$i" ) } )( term )
}
object TargetFilter {
type Type = ( Expr, Expr ) => Option[Boolean]
def default: Type = ( from: Expr, to: Expr ) =>
syntacticMatching( to, from ) map { _ => true }
}
class RecSchemGenLangFormula(
val recursionScheme: RecursionScheme,
val targetFilter: TargetFilter.Type = TargetFilter.default ) {
def ruleIncluded( rule: Rule ) = FOLAtom( s"${rule.lhs}->${rule.rhs}" )
def derivable( from: Expr, to: Expr ) = FOLAtom( s"$from=>$to" )
private val rulesPerNonTerminal = Map() ++ recursionScheme.rules.
groupBy { r =>
( r: @unchecked ) match {
case Rule( _, Apps( nt: Const, _ ) ) => nt
}
}.view.mapValues( _.toSeq ).toMap
def reverseMatches( against: Expr ) =
against match {
case Apps( nt: Const, _ ) => rulesPerNonTerminal.getOrElse( nt, Seq() ).flatMap { rule =>
val ( fvsRule, fvsAgainst ) = ( freeVariables( rule.lhs ), freeVariables( against ) )
val rule_ = if ( fvsRule intersect fvsAgainst nonEmpty )
rule( Substitution( rename( freeVariables( rule.lhs ), freeVariables( against ) ) ) )
else
rule
syntacticMGU( rule_.rhs, against ).headOption.map { unifier => canonicalVars( unifier( rule_.lhs ) ) -> rule }
}
}
type Target = ( Expr, Expr )
def apply( targets: Iterable[Target] ): FOLFormula = {
val edges = mutable.ArrayBuffer[( Target, Rule, Target )]()
val goals = mutable.Set[Target]()
val queue = mutable.Queue( targets.toSeq: _* )
val alreadyDone = mutable.Set[Target]()
while ( queue nonEmpty ) {
val target @ ( from, to ) = queue.dequeue()
if ( !alreadyDone( target ) )
reverseMatches( to ).foreach {
case ( newTo, rule ) =>
targetFilter( from, newTo ) match {
case Some( true ) =>
goals += ( from -> newTo )
edges += ( ( target, rule, from -> newTo ) )
case Some( false ) => ()
case None =>
edges += ( ( target, rule, from -> newTo ) )
queue enqueue ( from -> newTo )
}
}
alreadyDone += target
}
val reachable = mutable.Set[Target]( goals.toSeq: _* )
var changed = true
while ( changed ) {
changed = false
edges.foreach {
case ( a, r, b ) =>
if ( ( reachable contains b ) && !( reachable contains a ) ) {
reachable += a
changed = true
}
}
}
if ( !( targets.toSet subsetOf reachable ) ) return Bottom()
val edgesPerFrom = edges.groupBy( _._1 )
And( targets.toSeq.map { case ( from, to ) => derivable( from, to ) } ++ ( reachable collect {
case t @ ( from, to ) if !( goals contains t ) =>
Imp( derivable( from, to ), Or(
edgesPerFrom( t ) collect {
case ( _, r, b ) if goals contains b => ruleIncluded( r )
case ( _, r, b @ ( from_, to_ ) ) if reachable contains b =>
And( ruleIncluded( r ), derivable( from_, to_ ) )
} ) )
} ) ++ ( for (
( from1, to1 ) <- reachable;
( from2, to2 ) <- reachable if from1 == from2 && to1 != to2 if syntacticMatching( to2, to1 ).isDefined
) yield Imp( derivable( from1, to1 ), derivable( from1, to2 ) ) ) )
}
}
object minimizeRecursionScheme {
val logger = Logger( "minimizeRecursionScheme" ); import logger._
def apply( recSchem: RecursionScheme, targets: Iterable[( Expr, Expr )],
targetFilter: TargetFilter.Type = TargetFilter.default,
solver: MaxSATSolver = bestAvailableMaxSatSolver,
weight: Rule => Int = _ => 1 ) = {
val fvs = freeVariables( targets.map( _._1 ) ) union freeVariables( targets.map( _._2 ) )
val nameGen = rename.awayFrom( constants.nonLogical( targets.map( _._1 ) ) union constants.nonLogical( targets.map( _._2 ) ) )
val grounding = Substitution( for ( v @ Var( name, ty ) <- fvs ) yield v -> Const( nameGen fresh name, ty ) )
val targets_ = grounding( targets.toSet )
val formula = new RecSchemGenLangFormula( recSchem, targetFilter )
val hard = formula( targets_ )
debug( s"Logical complexity of the minimization formula: ${lcomp( simplifyPropositional( toNNF( hard ) ) )}" )
val soft = recSchem.rules map { rule => Neg( formula.ruleIncluded( rule ) ) -> weight( rule ) }
val interp = time( "maxsat" ) { solver.solve( hard, soft ).get }
RecursionScheme( recSchem.startSymbol, recSchem.nonTerminals,
recSchem.rules.filter { rule => interp( formula ruleIncluded rule ) } )
}
def viaInst( recSchem: RecursionScheme, targets: Iterable[( Expr, Expr )],
targetFilter: TargetFilter.Type = TargetFilter.default,
solver: MaxSATSolver = bestAvailableMaxSatSolver,
weight: Rule => Int = _ => 1 ) = {
val fvs = freeVariables( targets.map( _._1 ) ) union freeVariables( targets.map( _._2 ) )
val nameGen = rename.awayFrom( constants.nonLogical( targets.map( _._1 ) ) union constants.nonLogical( targets.map( _._2 ) ) )
val grounding = Substitution( for ( v @ Var( name, ty ) <- fvs ) yield v -> Const( nameGen fresh name, ty ) )
val targets_ = grounding( targets.toSet )
val instTerms = targets_.map { _._1 }.flatMap { case Apps( _, as ) => as }.flatMap { flatSubterms( _ ) }
val instRS = instantiateRS( recSchem, instTerms )
val formula = new RecSchemGenLangFormula( instRS, targetFilter )
val ruleCorrespondence = for ( ir <- instRS.rules.toSeq ) yield formula.ruleIncluded( ir ) --> Or(
for {
r <- recSchem.rules.toSeq
_ <- syntacticMatching( List( r.lhs -> ir.lhs, r.rhs -> ir.rhs ) )
} yield formula.ruleIncluded( r ) )
val hard = formula( targets_ ) & And( ruleCorrespondence )
debug( s"Logical complexity of the minimization formula: ${lcomp( simplifyPropositional( toNNF( hard ) ) )}" )
val soft = recSchem.rules map { rule => Neg( formula.ruleIncluded( rule ) ) -> weight( rule ) }
val interp = solver.solve( hard, soft ).get
RecursionScheme( recSchem.startSymbol, recSchem.nonTerminals,
recSchem.rules.filter { rule => interp( formula ruleIncluded rule ) } )
}
}
case class RecSchemTemplate( startSymbol: Const, template: Set[( Expr, Expr )] ) {
val nonTerminals: Set[Const] = template map { case ( Apps( nt: Const, _ ), _ ) => nt }
val isSubtermC = "is_subterm"
def isSubterm( v: Expr, t: Expr ): Formula =
Const( isSubtermC, v.ty ->: t.ty ->: To )( v, t ).asInstanceOf[Formula]
val canonicalArgs = nonTerminals map {
case nt @ Const( _, FunctionType( _, argTypes ), _ ) =>
nt -> argTypes.zipWithIndex.map { case ( t, i ) => Var( s"${nt}_$i", t ) }
} toMap
val states = canonicalArgs map { case ( nt, args ) => nt( args: _* ) }
val constraints: Map[( Const, Const ), Formula] = {
val cache = mutable.Map[( Const, Const ), Formula]()
def get( from: Const, to: Const ): Formula =
cache.getOrElseUpdate( from -> to, {
var postCond = if ( from == to )
And( canonicalArgs( from ).lazyZip( canonicalArgs( to ) ).map { Eq( _, _ ) } ) else Or( template collect {
case ( Apps( prev: Const, prevArgs ), Apps( `to`, toArgs ) ) if prev != to =>
def postCondition( preCond: Formula ): Formula = preCond match {
case Top() => Top()
case Bottom() => Bottom()
case And( a, b ) => And( postCondition( a ), postCondition( b ) )
case Or( a, b ) => Or( postCondition( a ), postCondition( b ) )
case Eq( a, b ) =>
prevArgs( canonicalArgs( prev ).indexOf( a ) ) match {
case v: Var =>
And( for ( ( toArg, canToArg: Var ) <- toArgs.lazyZip( canonicalArgs( to ) ).toSeq if v == toArg )
yield Eq( canToArg, b ) )
case constr =>
val vars = freeVariables( constr )
And( ( toArgs.toSeq zip canonicalArgs( to ) ).
collect {
case ( toArg: Var, canToArg ) if vars contains toArg =>
isSubterm( canToArg, b )
} )
}
case Apps( Const( `isSubtermC`, _, _ ), Seq( a, b ) ) =>
val vars = freeVariables( prevArgs( canonicalArgs( prev ).indexOf( a ) ) )
And( ( toArgs.toSeq zip canonicalArgs( to ) ).
collect {
case ( toArg: Var, canToArg ) if vars contains toArg =>
isSubterm( canToArg, b )
} )
}
postCondition( get( from, prev ) )
} toSeq )
val recCalls = template filter {
case ( Apps( `to`, _ ), Apps( `to`, _ ) ) => true
case _ => false
}
if ( recCalls nonEmpty ) {
val constArgs = canonicalArgs( to ).zipWithIndex filter {
case ( a, i ) =>
recCalls forall {
case ( Apps( _, callerArgs ), Apps( _, calleeArgs ) ) =>
callerArgs( i ) == calleeArgs( i )
}
} map { _._1 }
val structRecArgs = canonicalArgs( to ).zipWithIndex filter {
case ( a, i ) =>
recCalls forall {
case ( Apps( _, callerArgs ), Apps( _, calleeArgs ) ) =>
callerArgs( i ).find( calleeArgs( i ) ).nonEmpty
}
} map { _._1 }
def appRecConstr( p: Formula ): Formula = p match {
case Top() => Top()
case Bottom() => Bottom()
case Or( a, b ) => Or( appRecConstr( a ), appRecConstr( b ) )
case And( a, b ) => And( appRecConstr( a ), appRecConstr( b ) )
case Eq( a, b ) if constArgs contains a => Eq( a, b )
case Eq( a, b ) if structRecArgs contains a => isSubterm( a, b )
case Apps( Const( `isSubtermC`, _, _ ), Seq( a, b )
) if ( constArgs contains a ) || ( structRecArgs contains a ) =>
isSubterm( a, b )
case _ => Top()
}
postCond = appRecConstr( postCond )
}
simplifyPropositional( toNNF( postCond ) )
} )
( for ( from <- nonTerminals; to <- nonTerminals )
yield ( from, to ) -> get( from, to ) ) toMap
}
val constraintEvaluators: Map[( Const, Const ), ( Seq[Expr], Seq[Expr] ) => Boolean] =
constraints map {
case ( ( from, to ), constr ) =>
def mkEval( f: Formula ): ( ( Seq[Expr], Seq[Expr] ) => Boolean ) = f match {
case Top() => ( _, _ ) => true
case Bottom() => ( _, _ ) => false
case And( a, b ) =>
val aEval = mkEval( a )
val bEval = mkEval( b )
( x, y ) => aEval( x, y ) && bEval( x, y )
case Or( a, b ) =>
val aEval = mkEval( a )
val bEval = mkEval( b )
( x, y ) => aEval( x, y ) || bEval( x, y )
case Eq( b, a ) =>
val aIdx = canonicalArgs( from ).indexOf( a )
val bIdx = canonicalArgs( to ).indexOf( b )
require( aIdx >= 0 && bIdx >= 0 )
( x, y ) => syntacticMatching( y( bIdx ), x( aIdx ) ).isDefined
case Apps( Const( `isSubtermC`, _, _ ), Seq( b, a ) ) =>
val aIdx = canonicalArgs( from ).indexOf( a )
val bIdx = canonicalArgs( to ).indexOf( b )
require( aIdx >= 0 && bIdx >= 0 )
( x, y ) => ( expressionSize( y( bIdx ) ) <= expressionSize( x( aIdx ) ) + 1 ) &&
constants.nonLogical( y( bIdx ) ).subsetOf( constants.nonLogical( x( aIdx ) ) )
}
( from -> to ) -> mkEval( constr )
}
val targetFilter: TargetFilter.Type = ( from, to ) =>
TargetFilter.default( from, to ).orElse {
val Apps( fromNt: Const, fromArgs ) = from
val Apps( toNt: Const, toArgs ) = to
val constrValue = constraintEvaluators( fromNt -> toNt )( fromArgs, toArgs )
if ( constrValue ) None else Some( false )
}
def stableRecSchem( targets: Set[( Expr, Expr )] ): RecursionScheme = {
val neededVars = template flatMap { case ( from, to ) => freeVariables( from ) }
val allTerms = targets map { _._2 }
val topLevelStableTerms = stableTerms( allTerms, neededVars.toSeq ).filter( !_.isInstanceOf[Var] )
val argumentStableTerms = stableTerms(
allTerms
flatMap { case Apps( _, as ) => as }
flatMap { subTerms( _ ) }
filter { _.ty.isInstanceOf[TBase] },
neededVars.toSeq )
var rules = template.flatMap {
case ( from, to: Var ) =>
val allowedVars = freeVariables( from )
topLevelStableTerms.filter { st => freeVariables( st ) subsetOf allowedVars }.
map { Rule( from, _ ) }
case ( from, to ) =>
val allowedVars = freeVariables( from )
val templateVars = freeVariables( to ).diff( freeVariables( from ) )
templateVars.
foldLeft( Seq[Map[Var, Expr]]( Map() ) )( ( chosenValues, nextVar ) =>
for (
subst <- chosenValues;
st <- argumentStableTerms if st.ty == nextVar.ty && freeVariables( st ).subsetOf( allowedVars )
) yield subst + ( nextVar -> st ) ).
map( s => Rule( from, Substitution( s )( to ) ) )
}
// Filter out rules that only used variables that are passed unchanged from the startSymbol.
targets.map { case ( Apps( nt: Const, _ ), _ ) => nt }.toSeq match {
case Seq() => // empty language
case Seq( startSymbol ) =>
( nonTerminals - startSymbol ) foreach { nt =>
constraints( startSymbol -> nt ) match {
case And.nAry( constr ) =>
val identicalArgs = constr.collect {
case Eq( ntArg, startSymbolArg ) => canonicalArgs( nt ).indexOf( ntArg )
}.toSet
rules = rules filter {
case Rule( Apps( `nt`, args ), to ) =>
!freeVariables( to ).subsetOf( identicalArgs map { args( _ ) } collect { case v: Var => v } )
case _ => true
}
}
}
}
RecursionScheme( startSymbol, nonTerminals, rules )
}
def findMinimalCover(
targets: Set[( Expr, Expr )],
solver: MaxSATSolver = bestAvailableMaxSatSolver,
weight: Rule => Int = _ => 1 ): RecursionScheme = {
minimizeRecursionScheme( stableRecSchem( targets ), targets toSeq, targetFilter, solver, weight )
}
def findMinimalCoverViaInst(
targets: Set[( Expr, Expr )],
solver: MaxSATSolver = bestAvailableMaxSatSolver,
weight: Rule => Int = _ => 1 ): RecursionScheme = {
minimizeRecursionScheme.viaInst( stableRecSchem( targets ), targets toSeq, targetFilter, solver, weight )
}
}
object RecSchemTemplate {
def apply( startSymbol: Const, rules: ( Expr, Expr )* ): RecSchemTemplate =
RecSchemTemplate( startSymbol, rules toSet )
}
object recSchemToVTRATG {
def orderedNonTerminals( rs: RecursionScheme ): Seq[Const] = {
val ntDeps = rs.nonTerminals map { nt =>
nt -> ( rs rulesFrom nt map { _.rhs } flatMap { constants.nonLogical( _ ) } intersect rs.nonTerminals )
} toMap
var nts = Seq[Const]()
while ( rs.nonTerminals -- nts nonEmpty ) {
val Some( next ) = rs.nonTerminals -- nts find { nt => ntDeps( nt ) subsetOf nts.toSet }
nts = next +: nts
}
nts
}
def apply( recSchem: RecursionScheme ): VTRATG = {
val nameGen = rename.awayFrom( containedNames( recSchem ) )
val ntCorrespondence = orderedNonTerminals( recSchem ).reverse map {
case nt @ Const( name, FunctionType( _, argTypes ), _ ) =>
nt -> ( for ( ( t, i ) <- argTypes.zipWithIndex ) yield Var( nameGen.fresh( s"x_${name}_$i" ), t ) )
}
val ntMap = ntCorrespondence.toMap
val FunctionType( startSymbolType, _ ) = recSchem.startSymbol.ty
val startSymbol = Var( nameGen.fresh( s"x_${recSchem.startSymbol.name}" ), startSymbolType )
val nonTerminals = List( startSymbol ) +: ( ntCorrespondence map { _._2 } filter { _.nonEmpty } )
val productions = recSchem.rules map {
r =>
( r: @unchecked ) match {
case Rule( Apps( nt1: Const, vars1 ), Apps( nt2: Const, args2 )
) if recSchem.nonTerminals.contains( nt1 ) && recSchem.nonTerminals.contains( nt2 ) =>
val subst = Substitution( vars1.map( _.asInstanceOf[Var] ) zip ntMap( nt1 ) )
ntMap( nt2 ) -> args2.map( subst( _ ) )
case Rule( Apps( nt1: Const, vars1 ), rhs ) if recSchem.nonTerminals.contains( nt1 ) =>
val subst = Substitution( vars1.map( _.asInstanceOf[Var] ) zip ntMap( nt1 ) )
List( startSymbol ) -> List( subst( rhs ) )
}
}
VTRATG( startSymbol, nonTerminals, productions )
}
}
object simplePi1RecSchemTempl {
def apply( startSymbol: Expr, pi1QTys: Seq[TBase] )( implicit ctx: Context ): RecSchemTemplate = {
val nameGen = rename.awayFrom( ctx.constants )
val Apps( startSymbolNT: Const, startSymbolArgs ) = startSymbol
val FunctionType( instTT, startSymbolArgTys ) = startSymbolNT.ty
// TODO: handle strong quantifiers in conclusion correctly
val startSymbolArgs2 = for ( ( t, i ) <- startSymbolArgTys.zipWithIndex ) yield Var( s"x_$i", t )
val indLemmaNT = Const(
nameGen fresh "B",
FunctionType( instTT, startSymbolArgTys ++ startSymbolArgTys ++ pi1QTys ) )
val lhsPi1QArgs = for ( ( t, i ) <- pi1QTys.zipWithIndex ) yield Var( s"w_$i", t )
val rhsPi1QArgs = for ( ( t, i ) <- pi1QTys.zipWithIndex ) yield Var( s"v_$i", t )
val indLemmaRules = startSymbolArgTys.zipWithIndex.flatMap {
case ( indLemmaArgTy, indLemmaArgIdx ) =>
val indTy = indLemmaArgTy.asInstanceOf[TBase]
ctx.getConstructors( indTy ) match {
case None => Seq()
case Some( ctrs ) =>
ctrs flatMap { ctr =>
val FunctionType( _, ctrArgTys ) = ctr.ty
val ctrArgs = for ( ( t, i ) <- ctrArgTys.zipWithIndex )
yield Var( s"x_${indLemmaArgIdx}_$i", t )
val lhs = indLemmaNT( startSymbolArgs )(
startSymbolArgs2.take( indLemmaArgIdx ) )(
ctr( ctrArgs: _* ) )(
startSymbolArgs2.drop( indLemmaArgIdx + 1 ) )(
lhsPi1QArgs )
val recRules = ctrArgTys.zipWithIndex.filter { _._1 == indTy } map {
case ( ctrArgTy, ctrArgIdx ) =>
lhs -> indLemmaNT( startSymbolArgs )(
startSymbolArgs2.take( indLemmaArgIdx ) )(
ctrArgs( ctrArgIdx ) )(
startSymbolArgs2.drop( indLemmaArgIdx + 1 ) )(
rhsPi1QArgs )
}
recRules :+ ( lhs -> Var( "u", instTT ) )
}
}
}
RecSchemTemplate(
startSymbolNT,
indLemmaRules.toSet
+ ( startSymbolNT( startSymbolArgs ) ->
indLemmaNT( startSymbolArgs )( startSymbolArgs )( rhsPi1QArgs ) )
+ ( startSymbolNT( startSymbolArgs ) -> Var( "u", instTT ) )
+ ( indLemmaNT( startSymbolArgs )( startSymbolArgs2 )( lhsPi1QArgs ) -> Var( "u", instTT ) ) )
}
}
object qbupForRecSchem {
def canonicalRsLHS( recSchem: RecursionScheme )( implicit ctx: Context ): Set[Expr] =
recSchem.nonTerminals flatMap { nt =>
val FunctionType( To, argTypes ) = nt.ty
val args = for ( ( t, i ) <- argTypes.zipWithIndex ) yield Var( s"x$i", t )
recSchem.rulesFrom( nt ).flatMap {
case Rule( Apps( _, as ), _ ) => as.zipWithIndex.filterNot { _._1.isInstanceOf[Var] }.map { _._2 }
}.toSeq match {
case Seq() => Some( nt( args: _* ) )
case idcs =>
val newArgs = for ( ( _: TBase, idx ) <- argTypes.zipWithIndex )
yield if ( !idcs.contains( idx ) ) List( args( idx ) )
else {
val indTy = argTypes( idx ).asInstanceOf[TBase]
val Some( ctrs ) = ctx.getConstructors( indTy )
for {
ctr <- ctrs.toList
FunctionType( _, ctrArgTys ) = ctr.ty
} yield ctr(
( for ( ( t, i ) <- ctrArgTys.zipWithIndex ) yield Var( s"x${idx}_$i", t ) ): _* )
}
import cats.instances.list._
import cats.syntax.traverse._
newArgs.traverse( identity ).map( nt( _: _* ) )
}
}
def apply( recSchem: RecursionScheme, conj: Formula )( implicit ctx: Context ): Formula = {
def convert( term: Expr ): Formula = term match {
case Apps( ax, args ) if ax == recSchem.startSymbol => instantiate( conj, args )
case Apps( nt @ Const( name, ty, _ ), args ) if recSchem.nonTerminals contains nt =>
Atom( Var( s"X_$name", ty )( args: _* ) )
case formula: Formula => formula
}
val lhss = canonicalRsLHS( recSchem )
existentialClosure( And( for ( lhs <- lhss ) yield All.Block(
freeVariables( lhs ) toSeq,
formulaToSequent.pos( And( for {
Rule( lhs_, rhs ) <- recSchem.rules
subst <- syntacticMatching( lhs_, lhs )
} yield convert( subst( rhs ) ) )
--> convert( lhs ) ).toImplication ) ) )
}
}