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
874127e1
Commit
874127e1
authored
13 years ago
by
Philippe Suter
Browse files
Options
Downloads
Patches
Plain Diff
playing with graphs
parent
9fa5979d
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
cp-demo/PaperExamples.scala
+70
-3
70 additions, 3 deletions
cp-demo/PaperExamples.scala
with
70 additions
and
3 deletions
cp-demo/PaperExamples.scala
+
70
−
3
View file @
874127e1
...
@@ -60,8 +60,10 @@ object PaperExamples extends App {
...
@@ -60,8 +60,10 @@ object PaperExamples extends App {
}).
reduceLeft
(
_
||
_
)).
reduceLeft
(
_
&&
_
).
find
}).
reduceLeft
(
_
||
_
)).
reduceLeft
(
_
&&
_
).
find
}
}
println
(
"satSolve(something sat)"
,
satSolve
(
List
(
List
(-
1
,
2
,
3
),
List
(
1
,
-
2
,
4
),
List
(-
3
,
-
4
))))
println
(
"satSolve(p)"
,
satSolve
(
Seq
(
Seq
(
1
,-
2
,-
3
),
Seq
(
2
,
3
,
4
),
Seq
(-
1
,-
4
))))
println
(
"satSolve(something unsat)"
,
satSolve
(
List
(
List
(
1
,
2
),
List
(
1
,
-
2
),
List
(-
1
,
2
),
List
(-
1
,
-
2
))))
println
(
"satSolve(Seq(Seq(1,2), Seq(-1), Seq(-2)))"
,
satSolve
(
Seq
(
Seq
(
1
,
2
),
Seq
(-
1
),
Seq
(-
2
))))
// println("satSolve(something sat)", satSolve(List(List(-1, 2, 3), List(1, -2, 4), List(-3, -4))))
// println("satSolve(something unsat)", satSolve(List(List(1, 2), List(1, -2), List(-1, 2), List(-1, -2))))
@spec
def
divides
(
i
:
Int
,
j
:
Int
)
:
Boolean
=
i
*
(
j
/
i
)
==
j
@spec
def
divides
(
i
:
Int
,
j
:
Int
)
:
Boolean
=
i
*
(
j
/
i
)
==
j
@spec
def
noneDivides
(
from
:
Int
,
j
:
Int
)
:
Boolean
=
{
@spec
def
noneDivides
(
from
:
Int
,
j
:
Int
)
:
Boolean
=
{
...
@@ -140,6 +142,71 @@ object PaperExamples extends App {
...
@@ -140,6 +142,71 @@ object PaperExamples extends App {
}
}
}
}
SendMoreMoney
.
run
SendMoreMoney
.
run
object
Graphs
{
@spec
sealed
abstract
class
List
@spec
case
class
Cons
(
head
:
Int
,
tail
:
List
)
extends
List
@spec
case
class
Nil
()
extends
List
@spec
sealed
abstract
class
OptInt
@spec
case
class
IntSome
(
value
:
Int
)
extends
OptInt
@spec
case
class
None
()
extends
OptInt
@spec
def
contains
(
l
:
List
,
e
:
Int
)
:
Boolean
=
l
match
{
case
Nil
()
=>
false
case
Cons
(
x
,
xs
)
=>
x
==
e
||
contains
(
xs
,
e
)
}
//type Graph = Map[Int,List]
@spec
def
hasEdge
(
g
:
Map
[
Int
,
List
],
src
:
Int
,
dst
:
Int
)
:
Boolean
=
{
if
(
g
.
isDefinedAt
(
src
))
contains
(
g
(
src
),
dst
)
else
false
}
@spec
def
validPath
(
g
:
Map
[
Int
,
List
],
path
:
List
)
:
Boolean
=
path
match
{
case
Nil
()
=>
true
case
Cons
(
x
,
Nil
())
=>
true
// case Cons(x, xs @ Cons(y, _)) if (x == y) => validPath(g, xs)
case
Cons
(
x
,
xs
@
Cons
(
y
,
_
))
=>
hasEdge
(
g
,
x
,
y
)
&&
validPath
(
g
,
xs
)
}
@spec
def
length
(
l
:
List
)
:
Int
=
(
l
match
{
case
Nil
()
=>
0
case
Cons
(
_
,
xs
)
=>
1
+
length
(
xs
)
})
ensuring
(
_
>=
0
)
@spec
def
head
(
l
:
List
)
:
OptInt
=
l
match
{
case
Nil
()
=>
None
()
case
Cons
(
x
,
_
)
=>
IntSome
(
x
)
}
@spec
def
tail
(
l
:
List
)
:
OptInt
=
l
match
{
case
Nil
()
=>
None
()
case
Cons
(
x
,
Nil
())
=>
IntSome
(
x
)
case
Cons
(
x
,
xs
)
=>
tail
(
xs
)
}
def
paths
(
g
:
Map
[
Int
,
List
],
src
:
Int
,
dst
:
Int
)
=
{
((
path
:
List
)
=>
head
(
path
)
==
IntSome
(
src
)
&&
tail
(
path
)
==
IntSome
(
dst
)
&&
validPath
(
g
,
path
)).
minimizing
(
length
(
_
:
List
)).
findAll
}
def
run
:
Unit
=
{
def
mkList
(
vals
:
Int*
)
:
List
=
vals
.
foldRight
[
List
](
Nil
())((
v
,
l
)
=>
Cons
(
v
,
l
))
val
graph
:
Map
[
Int
,
List
]
=
Map
(
1
->
mkList
(
2
,
3
),
2
->
mkList
(
3
),
3
->
mkList
(
4
),
4
->
mkList
(
1
))
// That crashes somehow...
//p("Paths from 1 to 4, in order of length", paths(graph, 1, 4).toList)
((
g
:
Map
[
Int
,
List
],
p
:
List
)
=>
validPath
(
g
,
p
)
&&
length
(
p
)
==
10
).
find
}
}
Graphs
.
run
}
}
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