Metaprogramovanie / Systémy typov

Sergej Chodarev

Metaprogramovanie 8

Systémy typov

Sergej Chodarev

Načo sú nám typy?

Číslo

x strcmp ( x str1, x str2 )
x pow ( x base, x exponent )

int strcmp ( const char* str1, const char* str2 )
double pow ( double base, double exponent )

A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.

— Benjamin C. Pierce. Types and Programming Languages. 2002

Kontrola typov

Kontrola typov

Jazyky

Statická kontrola typov

Ako pomôcť typovej kontrole?

Špecifické typy namiesto všeobecných údajových štruktúr

Nevýhody

Komplikovanejší jazyk

Type soundness

A program fragment is said to have good behavior, or equivalently to be well behaved, if it does not cause any forbidden error to occur.
A program that passes the typechecker is said to be well typed.
Type soundness — well-typed programs are well behaved.

— Luca Cardelli: Type Systems

Teória typov

Na druhej strane

Java pred generickými typmi

List strings = new ArrayList();
strings.add("some string");
String s = strings.get(0);  // error!

Java pred generickými typmi

List strings = new ArrayList();
strings.add("some string");
String s = (String) strings.get(0);

Rozhodnutia

Dynamická kontrola typov

Systémy typov

Kovariancia a kontravariancia

Polia

Cat[] cats = new Cat[10];
Animal[] animals = cats;
animals[0] = new Duck();    // Exception!

Generické typy

List<Animal> animals = new ArrayList<Cat>();  // error!

Kovariancia

List<Cat>List<? extends Animal>

List<? extends Animal> animals = new ArrayList<Cat>();
…
Animal a = animals.get(0);
animals.add(new Duck());    // error!
animals.add(new Cat());	    // error!

Kontravariancia

List<Animal>List<? super Cat>

List<? super Cat> cats = new ArrayList<Animal>();
...
cats.add(new Cat());
Cat cat = cats.get(0);		// error!   
Object cat = cats.get(0);

Variancia

Typy a generovanie kódu

C++ Templates

template <typename A>
struct ListNode {
  A data;
  ListNode<A>* next;
};
template<typename T, int size = 100>
class Array {
  T array[size];
public:
  T& operator[](int index) {
    require(index >= 0 && index < size, "Index out of range");
    return array[index];
  }
  int length() const { return size; }
};
Array<int, 1000> my_array;

Typy a hodnoty

Dependent types

Haskell

data Vect a =
       Nil 
     | Cons a (Vect a)

append :: Vect a -> Vect a -> Vect a
append Nil         ys = ys
append (Cons x xs) ys = Cons x (append xs ys)

Idris

data Vect : Nat -> Type -> Type where
    Nil  : Vect 0 a
    Cons : (x : a) -> (xs : Vect n a) -> Vect (n + 1) a

append : Vect n a -> Vect m a -> Vect (n + m) a
append Nil         ys = ys
append (Cons x xs) ys = Cons x (append xs ys)

Záver

Článok

G. Bracha, M. Odersky, D. Stoutamire, and P. Wadler,
Making the future safe for the past,”
OOPSLA '98, doi: 10.1145/286936.286957

Secesná budova Hlavného nádraží
Secesná budova Hlavného nádraží (interier)