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
27d1ebc2
Commit
27d1ebc2
authored
15 years ago
by
Philippe Suter
Browse files
Options
Downloads
Patches
Plain Diff
heroïc refactoring of pattern-matching extraction
parent
2011bc04
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/purescala/Analysis.scala
+71
-43
71 additions, 43 deletions
src/purescala/Analysis.scala
with
71 additions
and
43 deletions
src/purescala/Analysis.scala
+
71
−
43
View file @
27d1ebc2
...
@@ -196,8 +196,32 @@ object Analysis {
...
@@ -196,8 +196,32 @@ object Analysis {
e
.
isInstanceOf
[
MatchExpr
]
e
.
isInstanceOf
[
MatchExpr
]
}
}
def
rewritePM
(
e
:
Expr
)
:
Expr
=
{
def
rewritePM
(
e
:
Expr
)
:
Expr
=
e
.
asInstanceOf
[
MatchExpr
]
match
{
val
MatchExpr
(
scrutinee
,
cases
)
=
e
.
asInstanceOf
[
MatchExpr
]
case
SimplePatternMatching
(
scrutinee
,
classType
,
casesInfo
)
=>
{
val
newVar
=
Variable
(
FreshIdentifier
(
"pm"
,
true
)).
setType
(
e
.
getType
)
val
scrutAsLetID
=
FreshIdentifier
(
"scrut"
,
true
).
setType
(
scrutinee
.
getType
)
val
lle
:
List
[(
Variable
,
List
[
Expr
])]
=
casesInfo
.
map
(
cseInfo
=>
{
val
(
ccd
,
newPID
,
argIDs
,
rhs
)
=
cseInfo
val
newPVar
=
Variable
(
newPID
)
val
argVars
=
argIDs
.
map
(
Variable
(
_
))
val
(
rewrittenRHS
,
moreExtras
)
=
rewriteSimplePatternMatching
(
rhs
)
(
newPVar
,
List
(
Equals
(
newPVar
,
CaseClass
(
ccd
,
argVars
)),
Implies
(
Equals
(
Variable
(
scrutAsLetID
),
newPVar
),
Equals
(
newVar
,
rewrittenRHS
)))
:::
moreExtras
.
toList
)
}).
toList
val
(
newPVars
,
newExtras
)
=
lle
.
unzip
extras
=
Let
(
scrutAsLetID
,
scrutinee
,
And
(
Or
(
newPVars
.
map
(
Equals
(
Variable
(
scrutAsLetID
),
_
))),
And
(
newExtras
.
flatten
)))
::
extras
newVar
}
case
_
=>
e
}
val
cleanerTree
=
searchAndApply
(
isPMExpr
,
rewritePM
,
expr
)
(
cleanerTree
,
extras
.
reverse
)
}
object
SimplePatternMatching
{
// (scrutinee, classtypedef, list((caseclassdef, variable, list(variable), rhs)))
def
unapply
(
e
:
MatchExpr
)
:
Option
[(
Expr
,
ClassType
,
Seq
[(
CaseClassDef
,
Identifier
,
Seq
[
Identifier
]
,
Expr
)])]
=
{
val
MatchExpr
(
scrutinee
,
cases
)
=
e
val
sType
=
scrutinee
.
getType
val
sType
=
scrutinee
.
getType
if
(
sType
.
isInstanceOf
[
AbstractClassType
])
{
if
(
sType
.
isInstanceOf
[
AbstractClassType
])
{
...
@@ -205,61 +229,65 @@ object Analysis {
...
@@ -205,61 +229,65 @@ object Analysis {
if
(
cases
.
size
==
cCD
.
knownChildren
.
size
&&
cases
.
forall
(!
_
.
hasGuard
))
{
if
(
cases
.
size
==
cCD
.
knownChildren
.
size
&&
cases
.
forall
(!
_
.
hasGuard
))
{
var
seen
=
Set
.
empty
[
ClassTypeDef
]
var
seen
=
Set
.
empty
[
ClassTypeDef
]
val
newVar
=
Variable
(
FreshIdentifier
(
"pm"
,
true
)).
setType
(
e
.
getType
)
var
lle
:
List
[(
CaseClassDef
,
Identifier
,
List
[
Identifier
]
,
Expr
)]
=
Nil
val
scrutAsLetID
=
FreshIdentifier
(
"scrut"
,
true
).
setType
(
scrutinee
.
getType
)
for
(
cse
<-
cases
)
{
cse
match
{
val
lle
:
List
[(
Variable
,
List
[
Expr
])]
=
cases
.
map
(
cse
=>
cse
match
{
case
SimpleCase
(
CaseClassPattern
(
binder
,
ccd
,
subPats
),
rhs
)
if
subPats
.
forall
(
_
.
isInstanceOf
[
WildcardPattern
])
=>
{
case
SimpleCase
(
CaseClassPattern
(
binder
,
ccd
,
subPats
),
rhs
)
if
subPats
.
forall
(
_
.
isInstanceOf
[
WildcardPattern
])
=>
{
seen
=
seen
+
ccd
seen
=
seen
+
ccd
val
newPVar
=
if
(
binder
.
isDefined
)
{
val
patID
:
Identifier
=
if
(
binder
.
isDefined
)
{
Variable
(
binder
.
get
)
binder
.
get
}
else
{
}
else
{
Variable
(
FreshIdentifier
(
"cse"
,
true
)).
setType
(
CaseClassType
(
ccd
))
FreshIdentifier
(
"cse"
,
true
).
setType
(
CaseClassType
(
ccd
))
}
val
argIDs
:
List
[
Identifier
]
=
(
ccd
.
fields
zip
subPats
.
map
(
_
.
asInstanceOf
[
WildcardPattern
])).
map
(
p
=>
if
(
p
.
_2
.
binder
.
isDefined
)
{
p
.
_2
.
binder
.
get
}
else
{
FreshIdentifier
(
"pat"
,
true
).
setType
(
p
.
_1
.
tpe
)
}).
toList
lle
=
(
ccd
,
patID
,
argIDs
,
rhs
)
::
lle
}
}
val
argVars
=
(
ccd
.
fields
zip
subPats
.
map
(
_
.
asInstanceOf
[
WildcardPattern
])).
map
(
p
=>
if
(
p
.
_2
.
binder
.
isDefined
)
{
case
_
=>
;
Variable
(
p
.
_2
.
binder
.
get
)
}
else
{
Variable
(
FreshIdentifier
(
"pat"
,
true
)).
setType
(
p
.
_1
.
tpe
)
})
val
(
rewrittenRHS
,
moreExtras
)
=
rewriteSimplePatternMatching
(
rhs
)
(
newPVar
,
List
(
Equals
(
newPVar
,
CaseClass
(
ccd
,
argVars
)),
Implies
(
Equals
(
Variable
(
scrutAsLetID
),
newPVar
),
Equals
(
newVar
,
rewrittenRHS
)))
:::
moreExtras
.
toList
)
}
}
case
_
=>
(
null
,
Nil
)
}
}).
toList
lle
=
lle
.
reverse
if
(
seen
.
size
==
cases
.
size
)
{
if
(
seen
.
size
==
cases
.
size
)
{
val
(
newPVars
,
newExtras
)
=
lle
.
unzip
Some
((
scrutinee
,
sType
.
asInstanceOf
[
AbstractClassType
],
lle
))
extras
=
Let
(
scrutAsLetID
,
scrutinee
,
And
(
Or
(
newPVars
.
map
(
Equals
(
Variable
(
scrutAsLetID
),
_
))),
And
(
newExtras
.
flatten
)))
::
extras
newVar
}
else
{
}
else
{
e
Non
e
}
}
}
else
{
}
else
{
e
Non
e
}
}
}
else
{
}
else
{
val
cCD
=
sType
.
asInstanceOf
[
CaseClassType
].
classDef
val
cCD
=
sType
.
asInstanceOf
[
CaseClassType
].
classDef
if
(
cases
.
size
==
1
&&
!
cases
(
0
).
hasGuard
)
{
if
(
cases
.
size
==
1
&&
!
cases
(
0
).
hasGuard
)
{
e
val
SimpleCase
(
pat
,
rhs
)
=
cases
(
0
).
asInstanceOf
[
SimpleCase
]
pat
match
{
case
CaseClassPattern
(
binder
,
ccd
,
subPats
)
if
(
ccd
==
cCD
&&
subPats
.
forall
(
_
.
isInstanceOf
[
WildcardPattern
]))
=>
{
val
patID
:
Identifier
=
if
(
binder
.
isDefined
)
{
binder
.
get
}
else
{
FreshIdentifier
(
"cse"
,
true
).
setType
(
CaseClassType
(
ccd
))
}
val
argIDs
:
List
[
Identifier
]
=
(
ccd
.
fields
zip
subPats
.
map
(
_
.
asInstanceOf
[
WildcardPattern
])).
map
(
p
=>
if
(
p
.
_2
.
binder
.
isDefined
)
{
p
.
_2
.
binder
.
get
}
else
{
FreshIdentifier
(
"pat"
,
true
).
setType
(
p
.
_1
.
tpe
)
}).
toList
Some
((
scrutinee
,
CaseClassType
(
cCD
),
List
((
cCD
,
patID
,
argIDs
,
rhs
))))
}
case
_
=>
None
}
}
else
{
}
else
{
e
Non
e
}
}
}
}
}
}
val
cleanerTree
=
searchAndApply
(
isPMExpr
,
rewritePM
,
expr
)
// println("******************")
// println("rewrote: " + expr)
// println(" *** to ***")
// println(cleanerTree)
// println(" ** with side conds ** ")
// println(extras.reverse)
// println("******************")
(
cleanerTree
,
extras
.
reverse
)
// val theExtras = extras.reverse
// val onExtras: Seq[(Expr,Seq[Expr])] = theExtras.map(rewriteSimplePatternMatching(_))
// // the "moreExtras" should be cleaned up due to the recursive call..
// val (rewrittenExtras, moreExtras) = onExtras.unzip
// (cleanerTree, rewrittenExtras ++ moreExtras.flatten)
}
}
}
}
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