Acciones

Relación 1

De Razonamiento automático (2018-19)

chapter {* R1: Programación funcional en Isabelle *}

theory R1_Programacion_funcional_en_Isabelle_alu
imports Main 
begin

text {* ----------------------------------------------------------------
  Ejercicio 1. Definir, por recursión, la función
     longitud :: 'a list ⇒ nat
  tal que (longitud xs) es la longitud de la listas xs. Por ejemplo,
     longitud [a,b,c] = 3
  ------------------------------------------------------------------- *}

(* manperjim cammonagu pabalagon raffergon2 aribatval juacanrod josgomrom4
   marfruman1 gleherlop benber alfmarcua enrparlav chrgencar antramhur
   pabbergue *)  
fun longitud :: "'a list ⇒ nat" where
  "longitud [] = 0 "
| "longitud (x#xs) = 1 + longitud xs "

value "longitud [a,b,c] = 3"

(* hugrubsan giafus1 *)
fun longitud2 :: "'a list ⇒ nat" where
  "longitud2 [] = 0 "
| "longitud2 xs = 1 + longitud2 (tl xs) "

value "longitud2 [a,b,c] = 3"

text {* --------------------------------------------------------------- 
  Ejercicio 2. Definir la función
     fun intercambia :: 'a × 'b ⇒ 'b × 'a
  tal que (intercambia p) es el par obtenido intercambiando las
  componentes del par p. Por ejemplo,
     intercambia (u,v) = (v,u)
  ------------------------------------------------------------------ *}

(* manperjim pabalagon cammonagu raffergon2 aribatval juacanrod marfruman1
   gleherlop benber hugrubsan alfmarcua enrparalv giafus1 chrgencar
   antramhur alikan pabbergue *)  
fun intercambia :: "'a × 'b ⇒ 'b × 'a" where
  "intercambia (x,y) = (y, x)"

value "intercambia (u,v) = (v,u)"

(* josgomrom4 *)
fun intercambia2 :: "'a × 'b ⇒ 'b × 'a" where
  "intercambia2 xs = (snd xs, fst xs) "

value "intercambia2 (u,v) = (v,u)"

text {* ---------------------------------------------------------------
  Ejercicio 3. Definir, por recursión, la función
     inversa :: 'a list ⇒ 'a list
  tal que (inversa xs) es la lista obtenida invirtiendo el orden de los
  elementos de xs. Por ejemplo,
     inversa [a,d,c] = [c,d,a]
  ------------------------------------------------------------------ *}

(* pabalagon *)
fun aux :: "'a list ⇒ 'a list ⇒ 'a list" where
  "aux [] a     = a" 
| "aux (x#xs) a = aux xs (x#a)"

fun inversa :: "'a list ⇒ 'a list" where
  "inversa xs = aux xs []"

value "inversa [a,d,c] = [c,d,a]"

(* manperjim pabalagon raffergon2 cammonagu josgomrom4 marfruman1
   gleherlop alfmarcua enrparalv chrgencar antramhur alikan pabbergue *) 
fun inversa2 :: "'a list ⇒ 'a list" where
  "inversa2 []     = []" 
| "inversa2 (x#xs) = inversa2 xs @ [x]"

value "inversa2 [a,d,c] = [c,d,a]"

(* juacanrod hugrubsan giafus1 *)
fun inversa3 :: "'a list ⇒ 'a list" where
  "inversa3 []   = []"  
| "inversa3 (xs) = inversa3(tl xs) @ [ hd (xs)]"

value "inversa3 [a,d,c] = [c,d,a]"

(* aribatval *)
fun inversa4 :: "'a list ⇒ 'a list" where
  "inversa4 [] = []" 
| "inversa4 xs = last xs # (inversa4 (butlast xs))"

value "inversa4 [a,d,c] = [c,d,a]"

(* benber *)
fun inversa5aux :: "'a list ⇒ 'a ⇒ 'a list" where
  "inversa5aux [] y = [y]"
| "inversa5aux (x#xs) y = x#(inversa5aux xs y)"

fun inversa5 :: "'a list ⇒ 'a list" where
  "inversa5 [] = []"
| "inversa5 (x#xs) = inversa5aux (inversa5 xs) x"

value "inversa5 [a,d,c] = [c,d,a]"

text {* --------------------------------------------------------------- 
  Ejercicio 4. Definir la función
     repite :: nat ⇒ 'a ⇒ 'a list
  tal que (repite n x) es la lista formada por n copias del elemento
  x. Por ejemplo, 
     repite 3 a = [a,a,a]
  ------------------------------------------------------------------ *}

(* pabalag aribatval antramhur*)
fun repite :: "nat ⇒ 'a ⇒ 'a list" where
  "repite 0 x = []"
| "repite (Suc n) x = x # repite n x"

value "repite 3 a = [a,a,a]"

(* manperjim raffergon2 cammonagu josgomrom4 marfruman1 benber alfmarcua gleherlop giafus1 chrgencar pabbergue *)
fun repite2 :: "nat ⇒ 'a ⇒ 'a list" where
  "repite2 0 x = [] "
| "repite2 n x = x # repite2 (n-1) x "

value "repite2 3 a = [a,a,a]"

(* juacanrod hugrubsan*)
fun repite3 :: "nat ⇒ 'a ⇒ 'a list" where
  "repite3 0 a = []" 
| "repite3 n a = [a] @ repite3 (n-1) a" 
  
value "repite3 3 a = [a,a,a]"

(* enrparalv*)
fun repite4 :: "nat ⇒ 'a ⇒ 'a list" where
  "repite4 n x = (if n = 0 then [] else repite4 (n-1) x @ [x])"

value "repite4 3 a = [a,a,a]"

text {* --------------------------------------------------------------- 
  Ejercicio 5. Definir la función
     conc :: 'a list ⇒ 'a list ⇒ 'a list
  tal que (conc xs ys) es la concatención de las listas xs e ys. Por
  ejemplo, 
     conc [a,d] [b,d,a,c] = [a,d,b,d,a,c]
  ------------------------------------------------------------------ *}

(* manperjim pabalagon raffergon2 josgomrom4 aribatval gleherlop
   chrgencar benber *) 
fun conc :: "'a list ⇒ 'a list ⇒ 'a list" where
  "conc [] ys     = ys" |
  "conc (x#xs) ys = x # conc xs ys"

value "conc [a,d] [b,d,a,c] = [a,d,b,d,a,c]"

(* cammonagu marfruman1 pabbergue *)
fun conc2 :: "'a list ⇒ 'a list ⇒ 'a list" where
  "conc2 ys []     = ys" |
  "conc2 xs (y#ys) = xs @y # ys"

value "conc2 [a,d] [b,d,a,c] = [a,d,b,d,a,c]"

(* juacanrod *)
fun conc3 :: "'a list ⇒ 'a list ⇒ 'a list" where
  "conc3 [] ys = ys" |
  "conc3 xs ys = [hd (xs)] @ conc3 (tl (xs)) ys"

value "conc3 [a,d] [b,d,a,c] = [a,d,b,d,a,c]"

(* hugrubsan enrparalv giafus1 alikan*)
fun conc4 :: "'a list ⇒ 'a list ⇒ 'a list" where
  "conc4 xs ys = xs @ ys"

(* alfmarcua *)
fun conc5 :: "'a list ⇒ 'a list ⇒ 'a list" where
  "conc5 [] ys = ys"
| "conc5 xs ys = conc5 (butlast xs) ((last xs)#ys)"

text {* --------------------------------------------------------------- 
  Ejercicio 6. Definir la función
     coge :: nat ⇒ 'a list ⇒ 'a list
  tal que (coge n xs) es la lista de los n primeros elementos de xs. Por 
  ejemplo, 
     coge 2 [a,c,d,b,e] = [a,c]
  ------------------------------------------------------------------ *}

(* pabalagon raffergon2 *)
fun coge :: "nat ⇒ 'a list ⇒ 'a list" where
  "coge 0 xs           = []" |
  "coge n []           = []" |
  "coge (Suc n) (x#xs) = x # coge n xs"

value "coge 2 [a,c,d,b,e] = [a,c]"

(* manperjim cammonagu josgomrom4 marfruman1 benber alfmarcua chrgencar
   gleherlop giafus1 pabbergue *) 
fun coge2 :: "nat ⇒ 'a list ⇒ 'a list" where
  "coge2 0 xs = []" |
  "coge2 n [] = []" |
  "coge2 n (x#xs) = x # coge2 (n-1) xs"

value "coge2 2 [a,c,d,b,e] = [a,c]"

(* juacanrod *)
fun coge3 :: "nat ⇒ 'a list ⇒ 'a list" where
  "coge3 0 xs = []" |
  "coge3 n xs = [hd (xs)] @ coge3 (n-1) (tl (xs))" 

value "coge3 2 [a,c,d,b,e] = [a,c]"

(* aribatval hugrubsan *)
fun coge4 :: "nat ⇒ 'a list ⇒ 'a list" where
 "coge4 0 xs = []" |
 "coge4 n [] = []" |
 "coge4 n xs = (hd xs) # coge4 (n-1) (tl xs)"

value "coge4 2 [a,c,d,b,e] = [a,c]"

(* enrparalv *)
fun coge5 :: "nat ⇒ 'a list ⇒ 'a list" where
  "coge5 n []      = []"
| "coge5 n (x#xs) = (if (n=0) then [] else [x] @ coge5 (n-1) xs )"

value "coge5 2 [a,c,d,b,e] = [a,c]"

(* alikan *)
fun coge6 :: "nat ⇒ 'a list ⇒ 'a list" where
  "coge6 n [] = []"|
  "coge6 n (x#xs) = (case n of 0 ⇒ [] | Suc n ⇒ x # coge6 n xs)"

value "coge6 2 [a,c,d,b,e] = [a,c]"

text {* --------------------------------------------------------------- 
  Ejercicio 7. Definir la función
     elimina :: nat ⇒ 'a list ⇒ 'a list
  tal que (elimina n xs) es la lista obtenida eliminando los n primeros
  elementos de xs. Por ejemplo, 
     elimina 2 [a,c,d,b,e] = [d,b,e]
  ------------------------------------------------------------------ *}

(* pabalagon *)
fun elimina :: "nat ⇒ 'a list ⇒ 'a list" where
  "elimina 0 xs           = []" |
  "elimina n []           = []" |
  "elimina (Suc n) (x#xs) = elimina n xs"

value "elimina 2 [a,c,d,b,e] = [d,b,e]"

(* manperjim raffergon2 cammonagu josgomrom4 marfruman1 benber alfmarcua enrparalv
   aribatval gleherlop chrgencar giafus1 pabbergue*) 
fun elimina2 :: "nat ⇒ 'a list ⇒ 'a list" where
  "elimina2 0 xs     = xs" |
  "elimina2 n []     = []" |
  "elimina2 n (x#xs) = elimina2 (n-1) xs"

value "elimina2 2 [a,c,d,b,e] = [d,b,e]"

(* juacanrod hugrubsan *)
fun elimina3 :: "nat ⇒ 'a list ⇒ 'a list" where
  "elimina3 0 xs = xs" |
  "elimina3 n xs = elimina3 (n-1) (tl xs)" 

value "elimina3 2 [a,c,d,b,e] = [d,b,e]"

(* alikan *)
fun elimina4 :: "nat ⇒ 'a list ⇒ 'a list" where
  "elimina4 n [] = []"|
  "elimina4 n (x#xs) = (case n of 0 ⇒ x#xs | Suc n ⇒ elimina4 n xs)"

value "elimina4 2 [a,c,d,b,e] = [d,b,e]"

text {* --------------------------------------------------------------- 
  Ejercicio 8. Definir la función
     esVacia :: 'a list ⇒ bool
  tal que (esVacia xs) se verifica si xs es la lista vacía. Por ejemplo,
     esVacia [a] = False
  ------------------------------------------------------------------ *}

(* manperjim pabalagon raffergon2 josgomrom4 marfruman1 benber hugrubsan
   alfmarcua enrparalv aribatval chrgencar giafus1 alikan pabbergue *) 
fun esVacia :: "'a list ⇒ bool" where
  "esVacia [] = True" |
  "esVacia xs = False"

value "esVacia [a] = False"
  
(* cammonagu juacanrod *)
fun esVacia2 :: "'a list ⇒ bool" where
  "esVacia2 xs = (longitud xs = 0)" 

value "esVacia2 [a] = False"

text {* --------------------------------------------------------------- 
  Ejercicio 9. Definir la función
     inversaAc :: 'a list ⇒ 'a list
  tal que (inversaAc xs) es a inversa de xs calculada usando
  acumuladores. Por ejemplo, 
     inversaAc [a,c,b,e] = [e,b,c,a]
  ------------------------------------------------------------------ *}

(* manperjim pabalagon cammonagu josgomrom4 marfruman1 benber alfmarcua
   enrparalv aribatval gleherlop chrgencar giafus1 alikan pabbergue *) 
fun inversaAcAux :: "'a list ⇒ 'a list ⇒ 'a list" where
  "inversaAcAux [] ys     = ys" |
  "inversaAcAux (x#xs) ys = inversaAcAux xs (x#ys)"

fun inversaAc :: "'a list ⇒ 'a list" where
  "inversaAc xs = inversaAcAux xs []"

value "inversaAc [a,c,b,e] = [e,b,c,a]"

(* juacanrod *)
fun inversaAcAux2 :: "'a list ⇒ 'a list ⇒ 'a list" where
  "inversaAcAux2 [] ys = ys" |
  "inversaAcAux2 xs ys = inversaAcAux2 (tl xs) ([hd xs]) @ ys"

value "inversaAcAux2 [a,b,c] []"

fun inversaAc2 :: "'a list ⇒ 'a list" where
  "inversaAc2 xs = inversaAcAux2 xs []"

value "inversaAc2 [a,c,b,e] = [e,b,c,a]"

(* hugrubsan *)
fun inversaAcAux3 :: "'a list ⇒ 'a list ⇒ 'a list" where
  "inversaAcAux3 [] ys = ys"
  |"inversaAcAux3 xs ys = inversaAcAux3 (tl xs) [(hd xs)] @ ys"

fun inversaAc3 :: "'a list ⇒ 'a list" where
  "inversaAc3 xs =  inversaAcAux3 (tl xs) [(hd xs)]"

value "inversaAc3 [a,c,b,e] = [e,b,c,a]"

text {* --------------------------------------------------------------- 
  Ejercicio 10. Definir la función
     sum :: nat list ⇒ nat
  tal que (sum xs) es la suma de los elementos de xs. Por ejemplo,
     sum [3,2,5] = 10
  ------------------------------------------------------------------ *}

(* manperjim pabalagon raffergon2 josgomrom4 marfruman1 benber alfmarcua
   enrparalv aribatval gleherlop chrgencar giafus1 pabbergue *) 
fun sum :: "nat list ⇒ nat" where
  "sum []     = 0" |
  "sum (x#xs) = x + sum xs"

value "sum [3,2,5] = 10"
  
(* cammonagu *)
fun sum2:: "nat list ⇒ nat" where
  "sum2 []     = 0" |
  "sum2 [x]    = x" |
  "sum2 (x#xs) = x + sum2 xs"

value "sum2 [3,2,5] = 10"

(* juacanrod hugrubsan *)
fun sum3 :: "nat list ⇒ nat" where
  "sum3 [] = 0" |
  "sum3 xs = (hd xs) + sum3 (tl xs)"

value "sum3 [3,2,5,1]"

text {* --------------------------------------------------------------- 
  Ejercicio 11. Definir la función
     map :: ('a ⇒ 'b) ⇒ 'a list ⇒ 'b list
  tal que (map f xs) es la lista obtenida aplicando la función f a los
  elementos de xs. Por ejemplo,
     map (λx. 2*x) [3,2,5] = [6,4,10]
  ------------------------------------------------------------------ *}

(* manperjim pabalagon raffergon2 cammonagu josgomrom4 marfruman1 benber
   alfmarcua aribatval gleherlop chrgencar giafus1 pabbergue *) 
fun map :: "('a ⇒ 'b) ⇒ 'a list ⇒ 'b list" where
  "map f []     = []" |
  "map f (x#xs) = f x # map f xs"

value "map (λn. Suc n) [2,3,4,5]"
  
(* juacanrod *)
fun map2 :: "('a ⇒ 'b) ⇒ 'a list ⇒ 'b list" where
  "map2 f []     = []" |
  "map2 f (x#xs) = [(f x)] @ map2 f xs"

value "map2 (λn. Suc n) [2,3,4,5]"

(* hugrubsan *)
fun map3 :: "('a ⇒ 'b) ⇒ 'a list ⇒ 'b list" where
  "map3 f [] = []" |
  "map3 f xs = f (hd xs) # map3 f (tl xs)"

value "map3 (λn. Suc n) [2,3,4,5]"

end