Subscribe Now
Trending News

Blog Post

How does find by example work in the Pharo Finder
News

How does find by example work in the Pharo Finder 

Veja o post original em português em How does find by example work in the Pharo Finder.

One of the things that looks like magic is the search for methods through examples. But we will see that there is no black magic in this if we take into account the fantastic reflection capacity of Pharo Smalltalk . The tool is available from Squeak , from which Pharo descends.

Let’s do an experiment with the Finder . We are going to provide a shuffled array and the same ordered array . See How to find methods based on examples in the Pharo Tutorial .

Using the Finder

Open the Finder.

Provide the arrays .

See the results.

Finder Script

Now we want to create a script to do the same, roughly.

We will avoid creating new classes and methods using block closures .

We will develop the script gradually. Initially we will check the partial results of the computation.

We start by obtaining the example parameters and the class hierarchy that can contain the methods sought.

The example is passed as an array . The first element is the recipient of the message. The last is the returned value. The intermediate values ​​are the arguments of the message.

The internal hierarchy closure recursively navigates up the class hierarchy.

In our case, 4 classes are obtained. We excluded the Object classes and superclasses from it.

Let’s get all the methods in the class hierarchy.

We use flatCollect : instead of collect : to get the flatten of the array of arrays . Each class would return an array with their methods within another array if we used the collect :.

We see that we have obtained many methods that are not of interest since our example is an array with two elements. From this array we get the receiver and the result of the method. There are no arguments. Soon the message is unary, with no argument. So let’s filter the methods further.

We will initially filter the number of arguments.

Now we only have unary methods on the list.

And finally we will add the filter for the methods that return the result of the example.

We take care to predict the possibility of an error in the method. In this case it is excluded from the list.

Below the script code :

findMethodByExample :=[ :example | | receiver arguments result class hierarchy |
	
	receiver :=example first.
	arguments :=example allButFirst allButLast.
	result :=example last.
	class :=receiver class.
	
	hierarchy :=[ :clazz | 
		clazz=Object ifTrue: [ {  } ] ifFalse: [  
			{ clazz }, (hierarchy value: clazz superclass)
		].
	].

	((hierarchy value: class) flatCollect: [ :aClass | aClass methods ]) 
		select: [ :method | 
			method numArgs=arguments size 
			and: [ 
				[((Message selector: method selector arguments: arguments) sendTo: receiver deepCopy)=result] 
				on: Error do: [ false ] ]
		].
	
].

findMethodByExample value: { #(1 2 3 4 5 6 7 8 9) shuffled. #(1 2 3 4 5 6 7 8 9) }.

Combining results

We will now try to combine the results of several simultaneous examples to reduce the size of the possibilities.

Let’s imagine that we are trying to get the name of the method to round a number.

Our example below shows several methods not related to rounding.

Our example is a little artificial but will serve to illustrate the idea.

We then try another example. And it gets better.

We then decided to combine the two examples and the result is narrower.

Combining results with the Finder

Can we do the same with the Finder? Perhaps…

Question How does find-by-example work in the Pharo Finder? on the Stack Overflow website suggests that we investigate the MethodFinder class.

There are two methods:

findMethodsByExampleInput:andExpectedResult: e
possibleSolutionsForInput:

The first invokes the second and has arguments compatible with what we need.

We repeated with MethodFinder the examples {2.4. 2} and {2.5. 3} .

We can naively try to get the combination of the examples and get nothing!

The reason for the failure is due to the fact that MethodFinder>> findMethodsByExampleInput: andExpectedResult: returns a collection of instances of the MethodFinderSend class .

We noticed that there is the Met hodFinderSend>> selector method that we can use to correct the problem.

Now we have obtained the list, with a single element for this case, of method selectors.

If you click on the list you will get more information about the methods that implement the message represented by the selector.

Read More

Related posts

© Copyright 2022, All Rights Reserved