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
bbbc470b
Commit
bbbc470b
authored
9 years ago
by
Nicolas Voirol
Committed by
Ravi
9 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Absolute value function for int32 types (termination)
parent
064fa2a0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/scala/leon/termination/StructuralSize.scala
+39
-13
39 additions, 13 deletions
src/main/scala/leon/termination/StructuralSize.scala
with
39 additions
and
13 deletions
src/main/scala/leon/termination/StructuralSize.scala
+
39
−
13
View file @
bbbc470b
...
@@ -16,23 +16,51 @@ trait StructuralSize {
...
@@ -16,23 +16,51 @@ trait StructuralSize {
private
val
sizeCache
:
MutableMap
[
TypeTree
,
FunDef
]
=
MutableMap
.
empty
private
val
sizeCache
:
MutableMap
[
TypeTree
,
FunDef
]
=
MutableMap
.
empty
// function absBigInt(x: BigInt): BigInt = if (x >= 0) x else -x
/* Absolute value for BigInt type
val
typedAbsBigIntFun
=
makeAbsFun
(
IntegerType
,
"absBigInt"
,
e
=>
UMinus
(
e
),
InfiniteIntegerLiteral
(
0
))
*
* def absBigInt(x: BigInt): BigInt = if (x >= 0) x else -x
// function absInt(x: Int): Int = if (x >= 0) x else -x
*/
val
typedAbsIntFun
=
makeAbsFun
(
Int32Type
,
"absInt"
,
e
=>
BVUMinus
(
e
),
IntLiteral
(
0
))
val
typedAbsBigIntFun
:
TypedFunDef
=
{
val
x
=
FreshIdentifier
(
"x"
,
IntegerType
,
alwaysShowUniqueID
=
true
)
def
makeAbsFun
(
tp
:
TypeTree
,
name
:
String
,
uminus
:
Expr
=>
Expr
,
zero
:
Expr
)
:
TypedFunDef
=
{
val
absFun
=
new
FunDef
(
FreshIdentifier
(
"absBigInt"
,
alwaysShowUniqueID
=
true
),
Seq
(),
Seq
(
ValDef
(
x
)),
IntegerType
)
val
x
=
FreshIdentifier
(
"x"
,
tp
,
alwaysShowUniqueID
=
true
)
val
absFun
=
new
FunDef
(
FreshIdentifier
(
name
,
alwaysShowUniqueID
=
true
),
Seq
(),
Seq
(
ValDef
(
x
)),
tp
)
absFun
.
body
=
Some
(
IfExpr
(
absFun
.
body
=
Some
(
IfExpr
(
GreaterEquals
(
Variable
(
x
),
zero
),
GreaterEquals
(
Variable
(
x
),
InfiniteIntegerLiteral
(
0
)
),
Variable
(
x
),
Variable
(
x
),
um
inus
(
Variable
(
x
))
UM
inus
(
Variable
(
x
))
))
))
absFun
.
typed
absFun
.
typed
}
}
/* Absolute value for Int (32 bit) type
*
* We use a recursive function here as the bv2int functionality provided
* through SMT solvers is waaaaay too slow. Recursivity requires the
* postcondition for verification efforts to succeed.
*
* def absInt(x: Int): BigInt = (if (x == 0) {
* BigInt(0)
* } else if (x > 0) {
* 1 + absInt(x - 1)
* } else {
* 1 + absInt(-(x + 1)) // avoids -Integer.MIN_VALUE overflow
* }) ensuring (_ >= 0)
*/
def
typedAbsIntFun
:
TypedFunDef
=
{
val
x
=
FreshIdentifier
(
"x"
,
Int32Type
,
alwaysShowUniqueID
=
true
)
val
absFun
=
new
FunDef
(
FreshIdentifier
(
"absInt"
,
alwaysShowUniqueID
=
true
),
Seq
(),
Seq
(
ValDef
(
x
)),
IntegerType
)
absFun
.
body
=
Some
(
IfExpr
(
Equals
(
Variable
(
x
),
IntLiteral
(
0
)),
InfiniteIntegerLiteral
(
0
),
IfExpr
(
GreaterThan
(
Variable
(
x
),
IntLiteral
(
0
)),
Plus
(
InfiniteIntegerLiteral
(
1
),
FunctionInvocation
(
absFun
.
typed
,
Seq
(
BVMinus
(
Variable
(
x
),
IntLiteral
(
1
))))),
Plus
(
InfiniteIntegerLiteral
(
1
),
FunctionInvocation
(
absFun
.
typed
,
Seq
(
BVUMinus
(
BVPlus
(
Variable
(
x
),
IntLiteral
(
1
))))))
)))
val
res
=
FreshIdentifier
(
"res"
,
IntegerType
,
alwaysShowUniqueID
=
true
)
absFun
.
postcondition
=
Some
(
Lambda
(
Seq
(
ValDef
(
res
)),
GreaterEquals
(
Variable
(
res
),
InfiniteIntegerLiteral
(
0
))))
absFun
.
typed
}
def
size
(
expr
:
Expr
)
:
Expr
=
{
def
size
(
expr
:
Expr
)
:
Expr
=
{
def
funDef
(
ct
:
ClassType
,
cases
:
ClassType
=>
Seq
[
MatchCase
])
:
FunDef
=
{
def
funDef
(
ct
:
ClassType
,
cases
:
ClassType
=>
Seq
[
MatchCase
])
:
FunDef
=
{
// we want to reuse generic size functions for sub-types
// we want to reuse generic size functions for sub-types
...
@@ -78,10 +106,8 @@ trait StructuralSize {
...
@@ -78,10 +106,8 @@ trait StructuralSize {
}).
foldLeft
[
Expr
](
InfiniteIntegerLiteral
(
0
))(
Plus
)
}).
foldLeft
[
Expr
](
InfiniteIntegerLiteral
(
0
))(
Plus
)
case
IntegerType
=>
case
IntegerType
=>
FunctionInvocation
(
typedAbsBigIntFun
,
Seq
(
expr
))
FunctionInvocation
(
typedAbsBigIntFun
,
Seq
(
expr
))
/*
case
Int32Type
=>
case
Int32Type
=>
FunctionInvocation
(
typedAbsIntFun
,
Seq
(
expr
))
FunctionInvocation
(
typedAbsIntFun
,
Seq
(
expr
))
*/
case
_
=>
InfiniteIntegerLiteral
(
0
)
case
_
=>
InfiniteIntegerLiteral
(
0
)
}
}
}
}
...
...
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