Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
inox
Commits
4cc65ae3
Commit
4cc65ae3
authored
14 years ago
by
Ali Sinan Köksal
Browse files
Options
Downloads
Patches
Plain Diff
An implementation of InductionTactic and its example use in ListWithSize
parent
a678eae0
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/purescala/InductionTactic.scala
+63
-1
63 additions, 1 deletion
src/purescala/InductionTactic.scala
testcases/ListWithSize.scala
+4
-0
4 additions, 0 deletions
testcases/ListWithSize.scala
with
67 additions
and
1 deletion
src/purescala/InductionTactic.scala
+
63
−
1
View file @
4cc65ae3
...
@@ -2,13 +2,75 @@ package purescala
...
@@ -2,13 +2,75 @@ package purescala
import
purescala.Common._
import
purescala.Common._
import
purescala.Trees._
import
purescala.Trees._
import
purescala.TypeTrees._
import
purescala.Definitions._
import
purescala.Definitions._
class
InductionTactic
(
reporter
:
Reporter
)
extends
DefaultTactic
(
reporter
)
{
class
InductionTactic
(
reporter
:
Reporter
)
extends
DefaultTactic
(
reporter
)
{
override
val
description
=
"Induction tactic for suitable functions"
override
val
description
=
"Induction tactic for suitable functions"
override
val
shortDescription
=
"induction"
override
val
shortDescription
=
"induction"
private
def
singleAbsClassDef
(
args
:
VarDecls
)
:
Option
[
AbstractClassDef
]
=
{
val
filtered
=
args
.
filter
(
arg
=>
arg
.
getType
match
{
case
AbstractClassType
(
_
)
=>
true
case
_
=>
false
})
if
(
filtered
.
size
!=
1
)
None
else
(
filtered
.
head
.
getType
match
{
case
AbstractClassType
(
classDef
)
=>
Some
(
classDef
)
case
_
=>
scala
.
Predef
.
error
(
"This should not happen."
)
})
}
private
def
selectorsOfParentType
(
parentType
:
ClassType
,
ccd
:
CaseClassDef
,
expr
:
Expr
)
:
Seq
[
Expr
]
=
{
val
childrenOfSameType
=
ccd
.
fields
.
filter
(
field
=>
field
.
getType
==
parentType
)
for
(
field
<-
childrenOfSameType
)
yield
{
CaseClassSelector
(
ccd
,
expr
,
field
.
id
).
setType
(
parentType
)
}
}
override
def
generatePostconditions
(
funDef
:
FunDef
)
:
Seq
[
VerificationCondition
]
=
{
override
def
generatePostconditions
(
funDef
:
FunDef
)
:
Seq
[
VerificationCondition
]
=
{
Seq
(
new
VerificationCondition
(
BooleanLiteral
(
false
),
funDef
,
VCKind
.
Postcondition
,
this
))
assert
(
funDef
.
body
.
isDefined
)
val
defaultPost
=
super
.
generatePostconditions
(
funDef
)
singleAbsClassDef
(
funDef
.
args
)
match
{
case
Some
(
classDef
)
=>
val
prec
=
funDef
.
precondition
val
post
=
funDef
.
postcondition
val
body
=
matchToIfThenElse
(
funDef
.
body
.
get
)
val
arg
=
funDef
.
args
.
head
val
argAsVar
=
arg
.
toVariable
if
(
post
.
isEmpty
)
{
Seq
.
empty
}
else
{
val
children
=
classDef
.
knownChildren
val
conditionsForEachChild
=
(
for
(
child
<-
classDef
.
knownChildren
)
yield
(
child
match
{
case
ccd
@
CaseClassDef
(
id
,
prnt
,
vds
)
=>
val
selectors
=
selectorsOfParentType
(
classDefToClassType
(
classDef
),
ccd
,
argAsVar
)
// if no subtrees of parent type, assert property for base case
val
resFresh
=
FreshIdentifier
(
"result"
,
true
).
setType
(
body
.
getType
)
val
bodyAndPostForArg
=
Let
(
resFresh
,
body
,
replace
(
Map
(
ResultVariable
()
->
Variable
(
resFresh
)),
matchToIfThenElse
(
post
.
get
)))
val
conditionForChild
=
if
(
selectors
.
size
==
0
)
bodyAndPostForArg
else
{
val
inductiveHypothesis
=
(
for
(
sel
<-
selectors
)
yield
{
val
resFresh
=
FreshIdentifier
(
"result"
,
true
).
setType
(
body
.
getType
)
val
bodyAndPost
=
Let
(
resFresh
,
replace
(
Map
(
argAsVar
->
sel
),
body
),
replace
(
Map
(
ResultVariable
()
->
Variable
(
resFresh
),
argAsVar
->
sel
),
matchToIfThenElse
(
post
.
get
)))
bodyAndPost
})
Implies
(
And
(
inductiveHypothesis
),
bodyAndPostForArg
)
}
Implies
(
CaseClassInstanceOf
(
ccd
,
argAsVar
),
conditionForChild
)
case
_
=>
error
(
"Abstract class has non-case class subtype."
)
}))
println
(
"Induction tactic yields the following vc:"
)
println
(
And
(
conditionsForEachChild
))
Seq
(
new
VerificationCondition
(
And
(
conditionsForEachChild
),
funDef
,
VCKind
.
Postcondition
,
this
))
}
case
None
=>
reporter
.
warning
(
"Induction tactic currently supports exactly one argument of abstract class type"
)
defaultPost
}
}
}
}
}
This diff is collapsed.
Click to expand it.
testcases/ListWithSize.scala
+
4
−
0
View file @
4cc65ae3
import
scala.collection.immutable.Set
import
scala.collection.immutable.Set
import
funcheck.Annotations._
import
funcheck.Annotations._
import
funcheck.Utils._
object
ListWithSize
{
object
ListWithSize
{
sealed
abstract
class
List
sealed
abstract
class
List
...
@@ -49,6 +50,9 @@ object ListWithSize {
...
@@ -49,6 +50,9 @@ object ListWithSize {
case
Cons
(
x
,
xs
)
=>
nilAppend
(
xs
)
case
Cons
(
x
,
xs
)
=>
nilAppend
(
xs
)
})
ensuring
(
res
=>
res
&&
append
(
l
,
Nil
())
==
l
)
})
ensuring
(
res
=>
res
&&
append
(
l
,
Nil
())
==
l
)
@induct
def
nilAppendInductive
(
l
:
List
)
:
Boolean
=
(
append
(
l
,
Nil
())
==
l
)
holds
// unclear if we needed this--it was meant to force folding
// unclear if we needed this--it was meant to force folding
def
appendFold
(
x
:
Int
,
xs
:
List
,
ys
:
List
)
:
Boolean
=
{
def
appendFold
(
x
:
Int
,
xs
:
List
,
ys
:
List
)
:
Boolean
=
{
true
true
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment