Академический Документы
Профессиональный Документы
Культура Документы
4. Types and Typeclasses. Please revise the handout for Types and Typeclasses, and the
corresponding lab sheet.
:t is used to check type
Eq is used for types that support equality testing.
Ord covers all the standard comparing functions such as >, <, >= and <=
Num is a numeric typeclass. Its members have the property of being able to act like numbers.
Advantages:
1. Avoids states, side effects and mutation of data – inputs will always produce the same
outputs.
2. Easier to write + read– tell the program what and not how.
3. Code is much easier to reuse, think of functions or prolog – same data functions build on
top of eachother, knowledge bases build on top of eachother.
Disadvantages:
1. Difficult to modify code precisely, have to work around the tools instead of working with
the tools – have to mitigate any imperfections and rely on underlying language
Re 1. Hint: always give the type information of a program. That is already the first mark.
Here an old exam question:
Type information:
count:: [Char] -> Int
Using recursion:
count [ ] = 0
count (x:xs) = if isDigit x
then 1 + count xs
else count xs
Using list comprehension:
count xs = length ([x| x <- xs, isDigit x])
Using higher order functions [was not in the question, just added for completeness]
count xs = length (filter isDigit xs)
Re 4. Types/Type classes:
Jan 2017, question 1 (e):
i) Type of: ([True, False], '1') is ([Bool], Char)
ii) most general type of swap function: (a,b) -> (b,a)
iii) most general type of g x y = x < y is: Ord a => a -> a -> Bool.
[The function g can compare two arguments of an arbitrary type, provided in the type we can
compare two elements, [i.e. of any type in Ord]].
HINTS PROLOG part 2
Unification of two terms, "do the following two terms unify". [In case they unify, we would also like
to see the variable instantiation(s) and make them unify, in case they do not unify, I recommend to
give justification. Reason: this way, I see whether you have understood, it says much more than just
"yes/no"
ALGORITHM OF UNIFICATION
1. If term1 and term2 are constants, then term1 and term2 unify if and only if they are the
same atom, or the same number.
2. If term1 is a variable and term2 is any type of term, then term1 and term2 unify, and term1 is
instantiated to term2 . Similarly, if term2 is a variable and term1 is any type of term, then
term1 and term2 unify, and term2 is instantiated to term1 . (So if they are both variables,
they’re both instantiated to each other, and we say that they share values.)
3. If term1 and term2 are complex terms, then they unify if and only if:
1. They have the same functor and arity, and
2. all their corresponding arguments unify, and
3. the variable instantiations are compatible. (For example, it is not possible to
instantiate variable X to mia when unifying one pair of arguments, and to instantiate
X to vincent when unifying another pair of arguments .)
4. Two terms unify if and only if it follows from the previous three clauses that they unify.
So it depends, there are three responses to the question answer given by the standard
unification algorithm (which is to say no), the response of older Prolog implementations
(they use up the available memory), and the answer given by sophisticated Prolog
implementations (which is to say yes, and return a finite representation of an infinite
term). In short, there is no ‘right’ answer to this question. What is important is that you
understand the difference between standard unification and Prolog unification, and know
how the Prolog implementation that you work with handles such examples. READ LINK
3. Modification of this question, "what is the result of the following query." See exam
questions, easy to check, just type the query into Prolog. Similary: the different between
different forms of equality, this was one of the late chapters.
Literally follow a tree or evaluate on paper to show working on the knowledge base.
Take time to read it carefully.
When we run
?- grandfather(G,chris).
a Prolog the execution tree will be constructed as follows:
?- grandfather(G, chris).
G=X Z=chris
Fail
4. Recursion and tail recursion. often a program can be turned into a tail recursive program by using
an addtional argument that stores intermediate results.
Base case and recursive case as always
Use counter in recursion in order to store data in the middle and then use the tail to be more
efficient ( look it up)
ground(0,T).
ground(N,T):-
arg(N,T,Arg),
ground(Arg),
N1 is N-1,
ground(N1,T).