September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,74 +1,76 @@
|
|||
#define system.
|
||||
#define system'routines.
|
||||
#define extensions.
|
||||
#define extensions'routines.
|
||||
import system'routines.
|
||||
import extensions.
|
||||
import extensions'routines.
|
||||
|
||||
#symbol joinable = (:aFormer:aLater)
|
||||
[ (aFormer@(aFormer length - 1)) == (aLater@0) ].
|
||||
joinable = (:aFormer:aLater)(aFormer[aFormer length - 1] == aLater[0]).
|
||||
|
||||
#symbol dispatcher =
|
||||
dispatcher =
|
||||
{
|
||||
eval : anArray &func2:aFunction
|
||||
eval object:anArray func2:aFunction
|
||||
[
|
||||
^ aFunction eval:(anArray@0):(anArray@1).
|
||||
^ aFunction eval(anArray[0],anArray[1]).
|
||||
]
|
||||
|
||||
eval : anArray &func3:aFunction
|
||||
eval object:anArray func3:aFunction
|
||||
[
|
||||
^ aFunction eval:(anArray@0):(anArray@1):(anArray@2).
|
||||
^ aFunction eval(anArray[0], anArray[1],anArray[2]).
|
||||
]
|
||||
|
||||
eval : anArray &func4:aFunction
|
||||
eval object:anArray func4:aFunction
|
||||
[
|
||||
^ aFunction eval:(anArray@0):(anArray@1):(anArray@2):(anArray@3).
|
||||
^ aFunction eval(anArray[0],anArray[1],anArray[2],anArray[3]).
|
||||
]
|
||||
|
||||
eval : anArray &func5:aFunction
|
||||
eval object:anArray func5:aFunction
|
||||
[
|
||||
^ aFunction eval:(anArray@0):(anArray@1):(anArray@2):(anArray@3):(anArray@4).
|
||||
^ aFunction eval(anArray[0],anArray[1],anArray[2],anArray[3],anArray[4]).
|
||||
]
|
||||
|
||||
}.
|
||||
|
||||
#class AmbValueCollection
|
||||
class AmbValueCollection
|
||||
{
|
||||
#field theCombinator.
|
||||
object theCombinator.
|
||||
|
||||
#constructor new &args:Arguments
|
||||
constructor new args:Arguments
|
||||
[
|
||||
theCombinator := SequentialEnumerator new &args:Arguments.
|
||||
theCombinator := SequentialEnumerator new args:Arguments.
|
||||
]
|
||||
|
||||
#method seek : aCondition
|
||||
seek : aCondition
|
||||
[
|
||||
theCombinator reset.
|
||||
|
||||
theCombinator seek &each: v
|
||||
[
|
||||
^ aCondition cast:%eval &to:dispatcher &with:v.
|
||||
].
|
||||
theCombinator seekEach(:v)(dispatcher eval(v,aCondition))
|
||||
]
|
||||
|
||||
#method do : aFunction
|
||||
do : aFunction
|
||||
[
|
||||
#var aResult := theCombinator get.
|
||||
(nil != aResult)
|
||||
? [ aFunction cast:%eval &to:dispatcher &with:aResult. ]
|
||||
! [ #throw InvalidArgumentException new. ].
|
||||
var aResult := theCombinator get.
|
||||
if (nil != aResult)
|
||||
[ dispatcher eval(aResult,aFunction) ];
|
||||
[ InvalidArgumentException new; raise ]
|
||||
]
|
||||
}
|
||||
|
||||
#symbol ambOperator =
|
||||
ambOperator =
|
||||
{
|
||||
for &args:Arguments
|
||||
= AmbValueCollection new &args:Arguments.
|
||||
generic for args:Arguments
|
||||
= AmbValueCollection new args:Arguments.
|
||||
}.
|
||||
|
||||
#symbol program =
|
||||
program =
|
||||
[
|
||||
ambOperator
|
||||
for &args:("the","that","a"):("frog", "elephant", "thing"):("walked", "treaded", "grows"):("slowly", "quickly")
|
||||
seek: (:a:b:c:d) [ (joinable:a:b) and:(joinable:b:c) and:(joinable:c:d) ]
|
||||
do: (:a:b:c:d) [ console writeLine:a:" ":b:" ":c:" ":d. ]
|
||||
| if &InvalidArgumentError: e [ console writeLine:"AMB is angry". ].
|
||||
try(ambOperator
|
||||
for(("the","that","a"),("frog", "elephant", "thing"),("walked", "treaded", "grows"),("slowly", "quickly"));
|
||||
seek(:a:b:c:d) ( joinable eval(a,b) && joinable eval(b,c) && joinable eval(c,d) );
|
||||
do(:a:b:c:d) [ console printLine(a," ",b," ",c," ",d) ])
|
||||
{
|
||||
on(Exception e)
|
||||
[
|
||||
console printLine:"AMB is angry"
|
||||
]
|
||||
}.
|
||||
|
||||
console readChar.
|
||||
].
|
||||
|
|
|
|||
41
Task/Amb/Kotlin/amb.kotlin
Normal file
41
Task/Amb/Kotlin/amb.kotlin
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// version 1.1.3
|
||||
|
||||
typealias Constraint<T> = (T, T) -> Boolean
|
||||
|
||||
fun <T> amb(lists: List<List<T>>, res: MutableList<T>, cons: Constraint<T>): Boolean {
|
||||
if (lists.isEmpty()) return true
|
||||
if (lists[0].isEmpty()) return false
|
||||
val z = res.size
|
||||
for ((i, el) in lists[0].withIndex()) {
|
||||
if (i == 0) res.add(el) else res[z] = el
|
||||
if (z > 0 && !cons(res[z - 1], res[z])) continue
|
||||
if (amb(lists.drop(1), res, cons)) return true
|
||||
}
|
||||
res.removeAt(z)
|
||||
return false
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val wordLists = listOf(
|
||||
listOf("the", "that", "a"),
|
||||
listOf("frog", "elephant", "thing"),
|
||||
listOf("walked", "treaded", "grows"),
|
||||
listOf("slowly", "quickly")
|
||||
)
|
||||
val res = mutableListOf<String>()
|
||||
val cons: Constraint<String> = { x, y -> x.last() == y.first() }
|
||||
if (amb(wordLists, res, cons))
|
||||
println(res)
|
||||
else
|
||||
println("No solution found")
|
||||
val numLists = listOf(
|
||||
listOf(1, 2, 3),
|
||||
listOf(7, 6, 4, 5)
|
||||
)
|
||||
val res2 = mutableListOf<Int>()
|
||||
val cons2: Constraint<Int> = { x, y -> x * y == 8 }
|
||||
if (amb(numLists, res2, cons2))
|
||||
println(res2)
|
||||
else
|
||||
println("No solution found")
|
||||
}
|
||||
|
|
@ -1,20 +1,22 @@
|
|||
/*REXX program demonstrates the Amd operator, choosing a word from each set.*/
|
||||
@.=; @.1 = "the that a"
|
||||
@.2 = "frog elephant thing"
|
||||
@.3 = "walked treaded grows"
|
||||
@.4 = "slowly quickly"
|
||||
do j=1 until @.j==''; end; @.0=j-1 /*define @.0 as the number of sets.*/
|
||||
call Amb 1 /*find all word combinations that works*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
Amb: procedure expose @.; parse arg # _; arg . u /*2nd parse uppercases U*/
|
||||
if #>@.0 then do; w=word(u,1) /*W: is a uppercased U*/
|
||||
do n=2 to words(u); ?=word(u,n)
|
||||
if left(?,1)\==right(w,1) then return; w=?
|
||||
end /*n*/
|
||||
say space(_)
|
||||
end
|
||||
do k=1 for words(@.#) /*process words in sets.*/
|
||||
call Amb #+1 _ word(@.#,k) /*generate combinations,*/
|
||||
end /*k*/ /* [↑] (recursively).*/
|
||||
return
|
||||
/*REXX program demonstrates the Amd operator, choosing a word from each set. */
|
||||
@.=; @.1 = "the that a"
|
||||
@.2 = "frog elephant thing"
|
||||
@.3 = "walked treaded grows"
|
||||
@.4 = "slowly quickly"
|
||||
call Amb 1 /*find all word combinations that works*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Amb: procedure expose @.; parse arg # x; arg . u /*2nd parse uppercases U*/
|
||||
do j=1 until @.j==''; end /*locate a null string. */
|
||||
t=j-1 /*define number of sets.*/
|
||||
if #>t then do; w=word(u,1) /*W: is a uppercased U*/
|
||||
do n=2 to words(u); ?=word(u, n)
|
||||
if left(?, 1) \== right(w, 1) then return; w=?
|
||||
end /*n*/
|
||||
say space(x)
|
||||
end
|
||||
|
||||
do k=1 for words(@.#) /*process words in sets.*/
|
||||
call Amb #+1 x word(@.#, k) /*generate combinations,*/
|
||||
end /*k*/ /* [↑] (recursively).*/
|
||||
return
|
||||
|
|
|
|||
1
Task/Amb/Zkl/amb-1.zkl
Normal file
1
Task/Amb/Zkl/amb-1.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
fcn joins(a,b){ a[-1]==b[0] } // the constraint
|
||||
6
Task/Amb/Zkl/amb-2.zkl
Normal file
6
Task/Amb/Zkl/amb-2.zkl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
amb(joins,
|
||||
T("the","that","a"),
|
||||
T("frog","elephant","thing"),
|
||||
T("walked","treaded","grows"),
|
||||
T("slowly","quickly")
|
||||
).println();
|
||||
6
Task/Amb/Zkl/amb-3.zkl
Normal file
6
Task/Amb/Zkl/amb-3.zkl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
a:=amb.future(joins,T("the","that","a"),T("frog","elephant","thing"));
|
||||
b:=amb.future(joins,T("walked","treaded","grows"),T("slowly","squacking"));
|
||||
c:=amb.future(joins,a,b); // a future of futures
|
||||
println(a,b,c);
|
||||
c=c.noop(); // trigger the landslide, referencing c forces a result for a,b,c
|
||||
println(a.noop(),b.noop(),c); // even though a has a result, it doesn't know it until we force it
|
||||
10
Task/Amb/Zkl/amb-4.zkl
Normal file
10
Task/Amb/Zkl/amb-4.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fcn amb(f,a,b,etc){
|
||||
fcn(sink,f,a,b,etc){
|
||||
abc:=vm.arglist[2,*]; // ((the,that),(frog,elephant))
|
||||
if(abc.len()<2) return(sink.write(abc[0][0])); // back out of recursion
|
||||
foreach a,b in (abc[0],abc[1]){ // Cartesian product
|
||||
if(f(a,b)) self.fcn(sink,f,T(String(a," ",b)),abc[2,*].xplode());
|
||||
}
|
||||
}(s:=List(),vm.pasteArgs());
|
||||
s
|
||||
}
|
||||
1
Task/Amb/Zkl/amb-5.zkl
Normal file
1
Task/Amb/Zkl/amb-5.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
fcn amb(f,a,b,c,etc){ Walker.cproduct(vm.pasteArgs(1)).filter1(f) }
|
||||
5
Task/Amb/Zkl/amb-6.zkl
Normal file
5
Task/Amb/Zkl/amb-6.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// [()] notation unpacks parameter list: f((1,2,3))-->a=1,b=2,c=3
|
||||
fcn f([(a,b,c,d)]){ joins(a,b) and joins(b,c) and joins(c,d) }
|
||||
amb(f, T("the","that","a"), T("frog","elephant","thing"),
|
||||
T("walked","treaded","grows"), T("slowly","quickly")
|
||||
).println();
|
||||
1
Task/Amb/Zkl/amb-7.zkl
Normal file
1
Task/Amb/Zkl/amb-7.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
amb(fcn([(x,y,z)]){ x*x + y*y == z*z },[1..],[1..10],[1..10]).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue