{-# OPTIONS --cubical-compatible #-}
module Padova2025.ProgrammingBasics.Vectors where
Vectors#
open import Padova2025.ProgrammingBasics.Naturals.Base
open import Padova2025.ProgrammingBasics.Naturals.Arithmetic
infixr 5 _β·_
data Vector (A : Set) : β β Set where
[] : Vector A zero
_β·_ : {n : β} β A β Vector A n β Vector A (succ n)
Exercise: Length#
Define a function which computes the length of a given vector. There are two possible implementations, one which runs in constant time and one which runs in linear time.
lengthVlengthV : {n : β} {A : Set} β Vector A n β β
lengthV = {!!}
Reference solution
lengthV : {n : β} {A : Set} β Vector A n β β
lengthV {n} xs = n
-- This is the O(1) implementation. A slower O(n) implementation
-- reconstructs the size from the vector itself, disregarding
-- the type information:
-- lengthV' [] = zero
-- lengthV' (x β· xs) = succ (lengthV xs)
Exercise: Replication#
Define a function replicateV which reads a number n and an element x as
input and outputs a vector containing n copies of x.
replicateVreplicateV : {A : Set} β (n : β) β A β Vector A n
replicateV = {!!}
Exercise: Map#
Define the map function for vectors.
For instance, map f (x β· y β· z β· []) should reduce to f x β· f y β· f z β· [].
mapVmapV : {n : β} {A B : Set} β (A β B) β Vector A n β Vector B n
mapV = {!!}
Exercise: zipWith#
For instance, zipWithV f (x β· y β· []) (a β· b β· []) should evaluate to f x a β· f y b β· [].
zipWithVzipWithV : {A B C : Set} {n : β} β (A β B β C) β Vector A n β Vector B n β Vector C n
zipWithV = {!!}
Exercise: drop#
For instance, dropV one (a β· b β· c β· []) should evaluate to b β· c β· [].
dropVdropV : {A : Set} {n : β} (k : β) β Vector A (k + n) β Vector A n
dropV = {!!}
Exercise: take#
For instance, takeV one (a β· b β· c β· []) should evaluate to a β· [].
takeVtakeV : {A : Set} {n : β} (k : β) β Vector A (k + n) β Vector A k
takeV = {!!}
Exercise: Concatenation#
For instance, (a β· b β· []) ++V (c β· d β· []) should evaluate to a β· b β· c β· d β· [].
_++V__++V_ : {A : Set} {n m : β} β Vector A n β Vector A m β Vector A (n + m)
_++V_ = {!!}
Exercise: Snocking#
Write a function which appends a new element at the end.
For instance, snocV (a β· b β· []) c should evaluate to a β· b β· c β· [].
snocVsnocV : {A : Set} {n : β} β Vector A n β A β Vector A (succ n)
snocV = {!!}
Exercise: Reversal#
For instance, reverseV (a β· b β· c β· []) should evaluate to c β· b β· a β· [].
reverseVreverseV : {A : Set} {n : β} β Vector A n β Vector A n
reverseV = {!!}
Exercise: Flattening vectors of vectors#
For instance, concatV ((a β· b β· []) β· (c β· d β· []) β· []) should evaluate
to a β· b β· c β· d β· [].
concatVconcatV : {!!}
concatV = {!!}
Note that this exercise nontrivially depends on the somewhat arbitrary choices
in implementing addition and multiplication of natural numbers: Should the case
split be on the first or on the second argument? Should succ a Β· b be defined
as b + a Β· b or as a Β· b + b? Et cetera. This state of affairs is
unsatisfying and there are a couple of proposals attacking this problem. One of
these is objective type theory by Benno van
den Bergh and Martijn den Besten.