tags:

views:

36

answers:

1

I have the following Inductive type defined in Coq.

Inductive natlist : Type :=
| nil : natlist
| cons : nat -> natlist -> natlist.

Notation "x :: l" := (cons x l) (at level 60, right associativity). 
Notation "[ ]" := nil.
Notation "[ x , .. , y ]" := (cons x .. (cons y  nil) ..).

The natlist is basically a list of natural numbers (similar to lists in Python). I am trying to find the union of two natlist using the definition below.

Definition union_of_lists : natlist -> natlist -> natlist

i.e Eval simpl in (union_of_lists [1,2,3] [1,4,1]) should return [1,2,3,1,4,1]

I have the following doubts.

  • Since there are no arguments to this definition, how do I actually get the inputs and handle them?
  • What does the definition union_of_lists return exactly? Is it just a natlist?

Any help or hints are highly appreciated.

+1  A: 

Guys, I found the answer myself :) What I did was, I wrote a separate Fixpoint function 'append' and then assigned it to the definition of union_of_lists.

Fixpoint append(l1 l2 : natlist) : natlist := match l1 with | nil => l2 | (h :: t) => h :: app t l2 end.

and then

Definition union_of_lists : natlist -> natlist -> natlist := append.

Eval simpl in (append [1,2,3] [1,2,3]) returned [1,2,3,1,2,3]

The definition 'union_of_lists' returns a function which takes natlist as an argument and returns another function of type natlist -> natlist (i.e function taking a natlist argument and returning a natlist).

This definition of union_of_lists resembles functions in Functional Programming which can either return a function or a value.

dineshsriram
you could also directly have done : Fixpoint union_of_lists (l1 l2:natlist) ...., and the type would have been the same (just enter "Check append." to be sure). Or use the append List in the standard library (see http://coq.inria.fr/stdlib/Coq.Lists.List.html)
Vinz
Yes. You are right. The types of append and union_of_lists are the same. My intention was to use the definition instead of a fixpoint. That's why I have to do that in a round about manner. Thanks for the suggestion to use standard library. I will check that out.
dineshsriram