Question: def snoc : list A A list A | [ ] a : = [ a ] | ( a :: as ) b :

def snoc : list A A list A
|[] a :=[a]
|(a :: as) b := a :: (snoc as b)
def rev : list A list A
|[] :=[]
|(a :: as) := snoc (rev as) a
/-
Our implementation of rev is inefficient: it has O(n^2) complexity.
The definition below (called fastrev) is efficient, having only O(n) complexity.
It uses an auxilliary function revaux which accumulates the reversed
list in a 2nd variable.
-/
def revaux : list A list A list A
|[] bs := bs
|(a :: as) bs := revaux as (a :: bs)
def fastrev (l : list A) : list A
:= revaux l []
#reduce fastrev [1,2,3]
end revDefn
namespace ex07
open revDefn
/---- Do not add/change anything above this line ----/
/-
However we would like to show that fast rev and rev do the same thing.
You'll need to establish a lemma about revaux (hint: try writing
down a precise specification of what revaux does).
You may use the fact that lists with ++ form a monoid
(just copy the proofs from the lecture).
-/
open nat
def append : list A list A list A
|[] bs := bs
|(a :: as) bs := a :: (append as bs)
local notation (name := append) as ++ bs := append as bs
theorem rneutr : as : list A,
as ++ nil = as :=
begin
assume as,
induction as with a as' ih,
reflexivity,
dsimp [append],
rewrite ih,
end
lemma lem1 : (as bs : list A), revaux as bs =(rev as)++ bs :=
begin
assume as bs,
end
theorem fastrev_thm : as : list A , fastrev as = rev as :=
begin
sorry,
end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!