I'm writing a partial-evaluator for Prolog queries. I tried to expand a query using expand_goal/2, but it simply unifies the Input with the Output in this case:
:- initialization(main).
main :-
Input=is_between(1,A,3),expand_goal(Input,Output),writeln(Output).
is_between(A,B,C) :-
B>A,B<C.
I also tried using term_expansion/2, but this causes the program to fail:
:- initialization(main).
main :-
Input=is_between(1,A,3),term_expansion(Input,Output),writeln(Output).
is_between(A,B,C) :-
B>A,B<C.
Does SWI-Prolog have a built-in predicate that can perform macro-expansion of queries at runtime, as I tried to do here?
It is possible to expand a Prolog clause using the built-in clause/2 predicate. This predicate expands the clause like a hygienic macro:
:- initialization(main).
main :- clause(is_between(1,2,3),B),writeln(B).
is_between(A,B,C) :- A<B,C>B.
This example prints 1<2,3>2.
It is possible to exand multiple clauses using the findall/3 predicate:
:- initialization(main).
main :- find_all_clauses(is_between(1,2,3),B),writeln(B).
find_all_clauses(Predicate,Output) :-
findall(Predicate1,clause(Predicate,Predicate1),Output1),
list_to_disjunction(Output1,Output).
list_to_disjunction([A],A).
list_to_disjunction([A|B],(A;B1)) :- list_to_disjunction(B,B1).
is_between(A,B,C) :- A<B,C>B.
is_between(A,B,C) :- B>A,B<C.
This example prints 1<2,3>2;2>1,2<3.
I also wrote another partial evaluator that expands the goals recursively. There are a few other open-source libraries for partial evaluation in Prolog, such as ecce and logen.
Related
I am new to Clingo. so, I am trying to find out how it works. I have two questions regarding Clingo and its python API.
first, I have nested tuple such as tuple((1,(2,(3,(4,5))))). the length of the nested tuple may vary and the sequence is random. I want to check if a number for example n exists in the tuple if not do something. I am trying to check it with python API something like the code below obviously my code does not work.
#script (python)
import clingo
def is_in_tuple(x,n):
if n in x.arguments:
return True
else:
return False
#end.
tuple((1,(2,(3,(4,5))))).
h(X) :- tuple(X), #is_in_tuple(X,2).
#show h/1.
my other question is about appending to a simple tuple in python API for example we have tuple((1,2,3,4)) in Clingo and I want to append 0 to its beginning so the answer must look like ((0,1,2,3,4)). I want to use something like the code below. but the code will return the first element and 0. is it even possible to change the tuple like this?
#script (python)
import clingo
def append(x,n):
return [n, *x.arguments]
#end.
tuple(c(1,2,3,4,5)).
h(#append(X,0)) :- tuple(X).
#show h/1.
I am working on a scalac plugin in which I am traversing the AST. Right now I am using a for each loop over the unit.body, which I get from the passed Global value. The issue I am facing is that due to the recursive nature of said for each traversal, I visit each Tree and its children, though I do not want to traverse the latter since I am using pattern matching to match for function calls.
So for example if I would traverse the statement:
x.do(arg1)(arg2)(arg3)
I will get the following things in my traversal:
1. x.do(arg1)(arg2)(arg3)
2. x.do(arg1)(arg2)
3. x.do(arg1)
4. x.do
5. x
6. do
7. arg1
8. arg2
9. arg3
Here I indented the Tree objects in order of traversal. Now if I would use the Apply case class to match with each of these, I would get a match out of 1,2, and 3, while I really only want 1. I thought about using a context sensitive solution (like check against the previous traversed Tree) but it is not consistent enough for my purposes. I am also not looking for specific methods, which would make matching easier, but for methods in general, but I cannot allow for these children to be counted.
Is there a better pattern I can match function calls with when writing a scalac plugin, or a better way to do the traversal, so that I do not run into this recursion issue?
I found the answer myself:
Instead of using the intuitive
for(tree <- treeObject), where treeObject : Tree
You can just use the children object in each Tree object and do the following
def func(treeObject : Tree) : Something = {
for(tree <- treeObject.children) {
if(/*your condition for further traversal*/)
func(treeObject : Tree) //RECURSE
else
someValue //do whatever and RETURN
}
}
With this approach you can set a stopping condition for traversal. So let's say you only want to look at each method invocation. A method invocation can be matched for using the Apply case class. Now you can just check whether or not your current tree matches with that pattern. If it does you do your work on it and return. If it is not traverse the Tree further by using recursion, to get possible method invocations further "down".
Please help to find out how Scalding DSL translates into regular Scala code.
https://github.com/twitter/scalding/wiki/Fields-based-API-Reference#sortBy
For example:
val fasterBirds = birds.map('speed -> 'doubledSpeed) { speed : Int => speed * 2 }
Questions:
What conventions I need to follow to add my own functions to Scalding map,reduce, groupBy,sort and `scanLeft?
How Scalding translates expressions on fields like `'inpFld -> 'outFld to Scala code?
What data structures/functions Scalding translator creates? Where to find them in Scalding source code?
Thanks!
That IS regular Scala code. One strength of Scala lies in its extensibility. The syntax allows the programmer to extend the syntax of programs to create domain-specific languages. This is especially helpful when using underlying libraries.
The domain-specific language of Scala doesn't translate so much as allow you to defer application of code until the appropriate time. The tick character (') means that the following set of characters is a symbol, built-in datatype. The -> operator is syntactic sugar that can be expressed in the same way that a comma is, but visually, it imparts the concept of "translation" or "from this to that".
The domain-specific language you are looking at doesn't create structures, although it looks like it does create a functor. In this case it is a seen by the Java Virtual Machine as a Function1[Type,Type] instance which has an apply method that takes its argument and returns a result which is calculated by the provided code.
I'm trying to use views.html.helper.select (documentation here). I don't know scala, so i'm using java. I need to pass object of type Seq[(String)(String)] to the template right? Something like:
#(fooForm:Form[Foo])(optionValues:Seq[(String)(String)])
#import helper._
#form(routes.foo){
#select(field=myForm("selectField"),options=optionValues)
}
I don't know how to create Seq[(String)(String)] in java. I need to fill this collection with pairs (id,title) from my enum class.
Can somebody show me some expample how to use the select helper?
I found this thread on users group, but Kevin's answer didn't helped me a lot.
The right type is: Seq[(String, String)]. It means a sequence of pairs of String. In Scala there is a way to define pairs using the arrow: a->b == (a, b). So you could write e.g.:
#select(field = myForm("selectField"), options = Seq("foo"->"Foo", "bar"->"Bar"))
But there is another helper, as shown in the documentation, to build the sequence of select options: options, so you can rewrite the above code as:
#select(myForm("selectField"), options("foo"->"Foo", "bar"->"Bar"))
In the case your options values are the same as their label, you can even shorten the code to:
#select(myForm("selectField"), options(List("Foo", "Bar")))
(note: in Play 2.0.4 options(List("Foo", "Bar")) doesn't compile, so you can try this options(Seq("Foo", "Bar")))
To fill the options from Java code, the more convenient way is to use either the overloaded options function taking a java.util.List<String> as parameter (in this cases options values will be the same as their label) or the overloaded function taking a java.util.Map<String, String>.
I have defined a prolog file with the following code:
divisible(X, Y) :-
X mod Y =:= 0.
divisibleBy(X, Y) :-
divisible(X, Y).
op(35,xfx,divisibleBy).
Prolog is complaining that
'$record_clause'/2: No permission to modify static_procedure `op/3'
What am I doing wrong? I want to define an divisibleBy operator that will allow me to write code like the following:
4 divisibleBy 2
Thanks.
Use
:- op(35,xfx,divisibleBy).
:- tells the Prolog interpreter to evaluate the next term while loading the file, i.e. make a predicate call, instead of treating it as a definition (in this case a redefinition of op/3).
The answer given by #larsmans is spot-on regarding your original problem.
However, you should reconsider if you should define a new operator.
In general, I would strongly advise against defining new operators for the following reasons:
The gain in readability is often overrated.
It may easily introduce new problems in places you wouldn't normally expect buggy.
It doesn't "scale" well: a small number of operators can make code on presentation slides super-concise, but what if you add more discriminate union cases over time? More operators?