Looking for the Agda module that contains decidable equality for lists - equality

Given two lists xs and ys, I would like to obtain a value of Dec(xs ≡ ys).
Does any one know the name of the standard library module which contains such an operator?

It's in Data.List.Properties under the name ≡-dec

Related

How can I give an alias to a type in coq

Let's say I wanna to create a matrix of natural numbers in coq.
I have the built-in coq List, and to create a list of natural numbers, I just write list nat.
In order to create a 2-dimension list (i.e. a matrix), I need to write list (list nat).
My question is: instead of writing list (list nat), what should I do for coq to understand the word matrix exactly as if it was list (list nat)?
I tried Notation "matrix" := list (list nat), Notation "(matrix nat)" := (list (list nat)), etc., but nothing seems to work.
You can just write a definition: Definition matrix := list (list nat). The definition should just work for the most part (eg, you can still write Definition foo : matrix := [nil], with ListNotations).
If you don't want a definition (especially because in proofs you may have to explicitly unfold the definition for some tactics), then you can use a notation. The correct syntax for that is Notation matrix := (list (list nat)).

In lambda calculus, can variable be expression in general?

For better understanding of functional programming, I am reading the wiki page for lambda calculus here.
The definition says:
If x is a variable and M ∈ Λ, then (λx.M) ∈ Λ
Intuitively I thought variable are / represented by single-letter id's. But since here we deal with strict math definitions, I just want to double confirm this understanding: in general, can expression be classified as variable?
e.g. if x is a variable, is expression (x + x) a variable in lambda calculus? i.e. is it ok to write (λ(x+x).M) as an lambda calculus abstraction?
(Concern is in some context this is true. e.g. Here: An expression such as 4x^3 is a variable)
No, (x + x) is no variable (indeed it's not even a expression in naive lambda calculus).
I think you mix the terms variables and expressions somehow (or want some kind of pattern-matching?).
So let's follow the core-definition of lambda-calculus and expressions:
The definition itself is not that hard (indeed you linked it yourself with the wiki-page).
It's mentioned right from the start:
you have a set of variables V: (v_1, v_2, ...) (of course you can name them as you want - it's only important that you remmber that these are considered different symbols in your calculus)
the symbols λ, ., ( and )
This is it - thats all of the "Tokens" for this grammar/calculus.
Now there are a couple of rules how you can form Expressions from these:
each Variable is a expression
Abstraction: if E is a expression and x is a Variable then (λx.E) is a expression (here x and E are templates or Metavariables - you have to fill them with some real Expression to make this an Expression!)
Application: if A and B are expressions than (A B) is a expression.
So possible expressions are:
v_50
(λv_4.v_5)
((λv_4.v_5) v_50)
....
This is all when it comes to expressions.
You see: if you don't allow (x+x) as a symbol or name for a variable from the start it can never be a variable - indeed no expression is a variable even if there are some expressions consisting only of one said variable - if you called something expression it will never be a variable (again) ;)
PS: of course there are a couple of conventions to keep the parentheses a bit down - but for a start you don't need those.

Correct use of findall/3, especially the last result argument

I'm a beginner in Prolog and I am dealing with a problem that might seem stupid to you, but I really can't understand what I'm doing wrong! Ok, I have this file fruits.pl and inside that I have something like this:
fruit(apple,small,sweet).
fruit(lemon,small,nosweet).
fruit(melon,big,sweet).
I have already (inside that file made a coexist(X,Y) atom that checks if two fruits can be put together in a plate. It works fine! But now I can't create a suggest(X) that takes as a parameter a fruit and returns a list of fruits that can be put together in the same plate.
The thing is I was trying to make something like that
suggest(X) :- findall(Y,fruit(Y,_,_), List), coexist(X,Y).
What do you think? Every time I try to run this in swi prolog there is a warning 'singleton variable' and when I press
suggest(apple).
then it says false..
sorry for my english :/
Predicates in Prolog do not return anything. You have goals that are satisfied or not and you can interpret that as returning true or false.
Your predicate suggest(X) should contain another parameter that will be bound to the list of fruits that go together with X. An option would be: suggest(X, List) which describes the following relation: List represents all the fruits that go together with X. Then, you could ask:
?- suggest(apple, List).
List = [pear, cherry].
The goal findall(Y, ... , ...) uses the Y variable internally and Y is still unbound after the goal is satisfied. So, you should move coexist(X,Y) inside the second argument of findall/3 which is the goal that is satisfied in all possible ways. Th rule below works only if X is instantiated (suggest(+X, -List)).
suggest(X, List) :- findall(Y, (fruit(Y,_,_), coexist(X, Y)), List).
You can read this as follows: "List represents all fruits Y that coexist with X".
When you try to define a predicate in Prolog, first of all pretend that you have written that predicate already and start with imagining how you would use it. That is, what queries you would like to pose.
To me, it looks as if coexist/2 already describes what you want. BTW, may_coexist/2 might be a more descriptive name. Why do you want this in a separate list? And why using fruit/3 at all? But for the sake of the question let's assume that this makes sense. So essentially you would have now a relation fruit_compatible/2:
fruit_compatible(F, G) :-
fruit(F, _, _),
may_coexist(F, G),
fruit(G, _, _). % maybe you want to add this?
And now, let's assume you want this list too. So you would have a relation fruit_suggestions/2. How to use it?
?- fruit_suggestions(apple, L).
L = [cherry,pear].
or ... should it be rather L = [pear,cherry]? Or both?
?- fruit_suggestions(lemon, L).
L = [orange].
So every time I want a suggestion I have to think of a fruit. Always thinking: what fruit should it be? Fortunately there is a less demanding way in Prolog: Simply use a variable instead of the fruit! Now we should get all suggestions at once!
?- fruit_suggestions(F, L).
F = apple, L = [cherry, pear]
; F = lemon, L = [orange]
; F = cromulon, L = [embiggy, mushfruit].
So we need to implement it such that it will behave that way. findall/3 alone does not solve this. And implementing it manually is far from trivial. But there is setof/3 which handles variables in exactly that manner. Many of the tiny nitty-gritty design decisions have already been made, like that the list will be sorted ascendingly.
fruit_suggestions(F, L) :-
setof(G, fruit_compatible(F, G), L).
Edit: Due to the discussion below, here would be a solution that also permits empty lists. Note that this sounds trivial but it is not. To see this, consider the query:
?- fruit_suggestions(F, []).
What does it mean? What should F be? Also things that are no fruits at all? In that case we would have to produce solutions for everything. Like F = badger ; F = 42 ; .... Most probably this does not make much sense. What might be intended is those fruits that are incompatible with everything. To this end, we need to add a new rule:
fruit_suggestions(F, []) :-
setof(t,X^Y^fruit(F,X,Y),_),
\+ fruit_compatible(F, _).
fruit_suggestions(F, L) :-
setof(G, fruit_compatible(F, G), L).

Context sensitive grammar

Noam Chomsky - formal languages - type 1 - context sensitive grammar
Does AB->BA violate the rule? I assume it does.
A -> aAB does not violate condition?
aAB->ABc violates condition?
Using the wikipedia link provided, you can answer each question if you can map your production rules to the form:
iAr -> ibr, where A is a single non-terminal, i and r are (possibly empty) strings of terminals and non-terminals, and b is a non-empty string of terminals and non-terminals.
In other words, look at each of your rules, and try to make suitable choices for i, A, r, and b.
Before we look at your questions, let's look at some hypothetical examples:
Is CRC -> CRRRRRC a valid context-sensitive rule?
Yes. I can choose i=empty, A=C, r=RC, and b=CRRRR. Note, I could have made other choices that work, too.
Is xYz -> xWzv a valid context-sensitive rule?
No. There is no choice for i, A, and r that allow a match. If I chose i=x A=Y, r=z, and b=W, that trailing v screws things up.
Is xY -> xWzv a valid context-sensitive rule?
Yes. I can choose i=x, A=Y, r=empty, and b=Wzv.
This is the scheme you should use to answer your questions. Now, let's look at those:
AB -> BA: Assume you choose either A or B to be your single non-terminal. The choice fixes i and r (one will be empty, the other will be the non-terminal you didn't choose). Is there a string of the form ibr that can match based on how you fixed i and r? In other words, can you choose the string to replace b that maps to your rule?
A -> aAB. I hope the choice of your single non-terminal on the left is intuitively obvious. This choice will again fix i and r. Does the right map to a suitable ibr form where b is a nonempty string of terminals and nonterminals?
aAB -> ABc. Again, choose A or B to be your single non-terminal. This fixes i and r. Is there a choice that allows you to choose a suitable ibr?

What is the name of a [foo, bar] = ["foo", "bar"] feature?

I need to know a correct name for this cool feature that some languages provide.
FYI: In some languages it is possible to do a multiple assignments by assigning a structure of values to a structure of "variables". In the example in the question title it assigns "foo" to foo and "bar" to bar.
It's generally called destructuring bind in functional languages (which don't have assignments) and destructuring assignment in imperative languages.
Some languages provide subsets of that feature and then call it something different. For example, in Python it works with Tuples, Lists or Sequences and is called Tuple unpacking, List unpacking or Sequence unpacking, in Ruby, it works with Arrays (or objects that are convertible to an array) and is called parallel assignment.
Destructuring bind can get arbitrarily complex. E.g. this (imaginary) bind
[Integer(a), b, 2, c] = some_array
would assign the first element of some_array to a, the second element to b and the fourth element to c, but only if the first element is an Integer, the third element is equal to 2 and the length is 4. So, this even incorporates some conditional logic.
Destructuring bind is a subset of more general pattern matching, which is a standard feature of functional languages like Haskell, ML, OCaml, F#, Erlang and Scala. The difference is that destructuring bind only lets you take apart a structure and bind its components to variables, whereas pattern matching also matches on values inside those structures and lets you make decisions and in particular lets you run arbitrary code in the context of the bindings. (You can see the above imaginary bind as half-way in between destructuring bind and pattern matching.)
Here's the classical example of a reverse function in an imaginary language, written using pattern matching:
def reverse(l: List): List {
match l {
when [] { return [] }
when [first :: rest] { return (reverse(rest) :: first) }
}
}
In Python it is known as list or sequence unpacking: http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences
my_list = ["foo", "bar"]
foo, bar = my_list
It's called parallel assignment in Ruby and other languages.
Perl and PHP call it list assignment
Perl:
my ($foo, $bar, $baz) = (1, 2, 3);
PHP:
list($foo, $bar, $baz) = array(1, 2, 3);
If you view the right hand side as a tuple, one could view the assignment as a kind of Tuple Unpacking.
In Erlang it's ... well, it's not assignment, it's pattern matching (seeing as there is no assignment, as such, in Erlang).
$ erl
Erlang R14B (erts-5.8.1) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:true]
Eshell V5.8.1 (abort with ^G)
1> [H1, H2, H3| Rest] = [1,2,3,4,5].
[1,2,3,4,5]
2> H1.
1
3> H2.
2
4> H3.
3
5> Rest.
[4,5]
Why is it called "pattern matching"? Because it actually is matching patterns. Look:
6> [1,2,3,4,A] = [1,2,3,4,5].
[1,2,3,4,5]
7> A.
5
8> [1,2,3,4,A] = [1,2,3,4,6].
** exception error: no match of right hand side value [1,2,3,4,6]
In the first one we did what effectively amounts to an assertion that the list would start with [1,2,3,4] and that the fifth value could be anything at all, but please bind it into the unbound variable A. In the second one we did the same thing except that A is now bound so we're looking explicitly for the list [1,2,3,4,5] (because A is now 5).
Mozilla calls it destructuring assignment. In Python, it's sequence unpacking; tuple unpacking is a common special case.
In Clojure it would be called destructuring. Simple example:
(let [[foo bar] ["foo" "bar"]]
(println "I haz" foo "and" bar))
It's also often used in function definitions, e.g. the following destructures a single point argument into x and y components:
(defn distance-from-origin [[x y]]
(sqrt (+ (* x x) (* y y))))
You can also use the same technique to destructure nested data structures or key/value associative maps.