21-aug-20101 type theory & practical for generic programming kaushik biswas*, dr. partha pratim...
TRANSCRIPT
21-Aug-2010 1
Type theory & practicalFor Generic Programming
Kaushik Biswas*, Dr. Partha Pratim Das, Kausik Datta
21-Aug-2010 2
Organization
• Genesis of Type• Glimpse of Haskell• Type inference (Haskell)• Type deduction and type checking (C++)------------------BREAK-----------------------• Type traits and type manipulation• Type pair, Type list and usage• Generic operations using types
21-Aug-2010 3
Genesis of Type: Russell’s Paradox
Set S = { x : x is a rectangle }
Set R = { x : x is a not a rectangle }
Russell’s query : Is R R ?
According to “naïve set theory” : TRUE
21-Aug-2010 4
Russel’s Paradox (contd.)
Set T = { x : x is set, which does not contain x }
1) T T T T , Contradiction
2) T T T T , Contradiction
Russel’s Paradox :: From naïve set theory, some set can be constructed, which leads to contradiction in set comprehension and predicate logic.
There was once a barber. Some say that he lived in Seville. Wherever he lived, all of the men in this town either shaved themselves or were shaved by the barber. And the barber only shaved the men who did not shave themselves.
Russel’s query: Who saved the barber?
21-Aug-2010 5
Avoiding Russell’s Paradox
Ernst Zermelo
Axiomatic Set Theory ( 7 axioms on set definition)
AXIOM III. Axiom of separation (Axiom der Aussonderung) "Whenever the propositional function –(x) is definite for all elements of a set M, M possesses a subset M' containing as elements precisely those elements x of M for which –(x) is true".
Bertrand Russell
Ramified hierarchy and Impredicative Principle
Napoleon was Corsican. => Predicative
A typical Englishman is one who possesses all the properties possessed by a majority of Englishman. => Impredicative.
First order properties => That do not refer to totality of properties
Second order properties => Refer to totality of first order properties
Third order …
21-Aug-2010 6
Simple Type Theory
• i is type of individuals
• ( ) is type of propositions
• if A1, A2, .., An are types then (A1, A2, ..,An) is the type of n-ary relations over objects of respective types A1, A2, .., An
Examples:-(i, i) => Binary relations over
individuals(( ), ( )) => Binary connectives((i)) => type of quantifiers over
individual
More general example:-
(A, B, (A), (B), ((A), (B)))
Formation of a type having objects, predicates, predicates of predicates.
Unfortunately, simple type theory is not predicative, though it avoids Russel’s paradox.
21-Aug-2010 7
Church’s Type Theory (-calculus)
• There are two basic types – i : type of individuals : type of propositions
• If A, B are types then AB, the type of functions from A to B is a type
Church introduced function as primitive objects in 1940 using -calculus
i (predicates (i) )(i) (predicates of
predicates ((i)) )i i (type of functions)(i i ) i (type of functional )
(Haskell) Curry notation
A1A2 … An B
= A1,A2, ..,An B
= B (A1,A2,…,An)
21-Aug-2010 8
Glimpse of Haskell
[1,2,3] => list
head [1,2,3] => 1
tail [1,2,3] => [2,3]
reverse [1,2,3] => [3,2,1]
[1] ++ [2] => [1,2]
(1, “rt”) => 2-tuple
fst (1, “rt”) => 1
snd(1,”rt”) => “rt”
let add a b = a + b
let sqr a = a * a
map sqr [1,2,3] => [1,4,9]
zipWith add [1,2,3] [4,5,6] => [ 5, 7, 9]
foldr add 0 [1,2,3] => 6
/export/home/software/ghc-6.12.3/bin/ghci (ssh mures)
21-Aug-2010 9
Seeing Types in Haskell
:t 1:t 1.2:t [1,2,3]:t “hi”:t (1, 2.1, “hi”)
:t add :t sqr:t reverse:t map:t zipWith:t head:t tail:t fst:t snd
1 :: (Num t) => t1.2 :: (Fractional t) => t[1,2,3] :: (Num t) => [t]"hi" :: [Char](1,2.1, “hi") :: (Num t, Fractional t1) => (t, t1,
[Char] )
add :: (Num a) => a -> a -> asqr :: (Num a) => a -> areverse :: [a] -> [a]map :: (a -> b) -> [a] -> [b]zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]head :: [a] -> atail :: [a] -> [a]fst :: (a, b) -> asnd :: (a, b) -> b
ListTuple
let add a b = a + b
let sqr a = a * a
Explain thisfoldr :: (a -> b -> b) -> b -> [a] -> b
21-Aug-2010 10
Type Inference
foo x y = (length x) + ylength :: [a] -> IntQuestions: :t foo = ? :t x =? :t y = ?
Assign every variable and expression a new type variable
foo : 1
x : 1
y : 2
(length x) + y : 2
length x : [ ] Int
Create constraint set
1 = 1 2 2
2 = Int
1 = [ ]
2 = Int
Solving by Hindley Milner algorithm
foo :: [a] Int Int
Type inference algorithm is NP-complete in general
21-Aug-2010 11
Type checking vs. Type inference
• Standard type checking (C++)
int f ( int x) { return x + 1; }
int g (int x) { return f(y+1) *2; }
– Examine body of each function. Use declared types of identifiers to check type agreement
• Type inferenceint f ( int x ) { return x + 1; }
int g ( int y ) { return f(y+1) *2; }
– Examine code without type information, Infer the most general types that could have been declared.
– ML and Haskell are designed to make type inference feasible.
– C#3.0, F#, Visual Basic.Net 9.0, also uses. Soon in C++0x.
21-Aug-2010 12
Type deduction in Generic Programming
Haskell Composition:t (.) (.) :: (b -> c) -> (a -> b) -> a -> c
Let construct a fn as follows:-fn : [1,2,3] -> [9,4,1]:t reverse reverse :: [a] -> [a]:t mapmap :: (a -> b) -> [a] -> [b]:t sqr sqr :: (Num a) => a -> a:t map sqrmap sqr :: (Num a) => [a] -> [a]:t ((map sqr) . reverse)((map sqr) . reverse) :: (Num a) => [a] -> [a]
((map sqr) . reverse) [1,2,3] [9,4,1]
- C++ compiler can not infer types like Haskell
- Type computation needs to be part of generic programming
template <typename F1, typename F2>struct compose { typename F1::c operator()( F2::a x) { F1 ftor1; F2 ftor2; return ftor1(ftor2(x)); }};Functor type F1 and F2 must have (b->c) and (a->b)
information.
To make it generic, it F1 and F2 must be encoded with their input/output types with
common type names.
21-Aug-2010 13
Workshop for Haskell
Problem: Let (x1,y1), (x2,y2), (x3,y3) are three co-ordinate of a triangle. Find area of the triangle using following formula.
= ½ { x1(y2-y3) + x2(y3-y1)+ x3(y1-y2)}
area :: (Float t) => [ (t,t) ] -> t Input [(1, 1.1), (5.5, 1), (3.1, 10.3)]
Generalize your implementation for n-polygon ?
= ½ { x1(y2-y4) + x2(y3-y1)+ x3(y4-y2)+x4(y1-y3)} (quadrilateral)
y1
y2
y4
y3
y3
y1
y2
y4
y2
y3
y1
y4
Use vector operations to compute
21-Aug-2010 14
Type traits
Trait :: a distinguishing characteristic or quality
Type trait :: a distinguishing characteristic or quality of a Type
From type theory, what could be type traits?
1. Propositions/predicates
2. Type of lower level hierarchies
Traits of “Lion” type has some common and some extended part based on observer type.
21-Aug-2010 15
Type trait implementation in C++
// Proposition/Predicatestemplate <typename T>struct is_object ;
template <> struct is_object <module_decl> { static const bool val=true;};
Access => is_object<X>::val
// Lower order typestemplate <typename T>struct return_type;
template <>struct return_type <sqr>{ typedef int type; };
Access => return_type<X>::type
// Keeping as part of the typestruct module_decl { static const bool is_object=true; typedef VeModule* type; typedef VeModule stype; static const char* name; enum { tid = 100};};
Access => module_decl :: is_object module_decl :: type module_decl :: stype module_decl :: name module_decl :: tid
21-Aug-2010 16
Type manipulation using type-traits
iterator <T> where T could beList
List_Node
List_Node :: next()
List_Node :: current()
Array
index
index ++
Array [index]
template<typename T>struct iterator { typename T:: index_type current; typename T:: type vobj; inline void operator ++ () { increment <typename T:: index_type > (current); } inline typename T :: stype operator * () { return (typename T :: stype) get_info<typename T:: index_type> ( current, vobj); } };
increment<T>() function could now farther be specialized on T :: index_type
21-Aug-2010 17
Type List
type list
set of types having similar notion
TL = [T1, T2, T3, ….., Tn]
Order is non important
There is an implicit [] null list at the end of the type_list
Usage:
- Types forming a group
- Field types of an object
Required Operations on type list
head (TL) = T1
tail (TL) = [T2, T3, ….., Tn]
length (TL) = n
remove (T2 , TL) = [T1, T3, ….., Tn]
index (T2 , TL) = 2
type_of(2, TL ) = T2
is_present (Tx, TL) = { true, false}
concat (TL1 , TL2) = TL1 ++ TL2
zip (TL1 , TL2) = [ (Tx , Ty) ]
where Tx TL1 and Ty TL2
21-Aug-2010 18
Type Tuple
Usage:
- Forming object and field relations
- Forming propositions based on types
Operations on tuple type:-
left (TPair ) = TLeft
right (TPair ) = TRight
type_of ( i , TU) = Ti
type tuple
n-ary relation of types
Tu = (T1, T2, T3, ….., Tn)
Order is important
(T1, T2, T3, ….., Tn)
= (T1, (T2, T3, ….., Tn) )
Binary relation is sufficient
TPair = (TLeft , TRight)
21-Aug-2010 19
Type list and pair (C++)
TYPE LISTtemplate <typename H, typename T>struct typeList{ typedef H head; typedef T tail;};
struct null_type {}; [int, long, double ] is defined as
typeList < int, typeList<long, typeList<double, null_type> > >
null_type is used for recursive definition creation using type_list
TYPE PAIR
template <typename L, typename R>
struct type_pair
{ typedef L left;
typedef R right;
};
( module_decl, ports ) is defined as
type_pair< module_decl , ports>
21-Aug-2010 20
Constructing generic type list
How do we form type_list <T1, T2, T3, T4, .., Tn > ?
Template <typename T1=null_type, typename T2=null_type, typename T3=null_type, typename T4=null_type>struct type_list { typedef typeList<T1, typeList<T2,
typeList<T3, typeList<T4, null_type> > > > local_type;
typedef remove_null <local_type>::type type;
};
Implementation of remove_null
template <typename T>struct remove_null;
template <class H, class T>struct remove_null<typeList<H,T> > { typedef typeList<H, typename
remove_null<T>::type> type; };
template <class T>struct remove_null<typeList<null_type ,T> >{ typedef null_type type;};
template <> struct remove_null<null_type>{ typedef null_type type; };
21-Aug-2010 21
Why important for Generic programming
• BNF syntaxes could be expressed in terms of type list and type tuple
– Rule := rule_1 | rule_2 | rule_3 | … | rule_n
• Implement above by type list
– Rule := rule_1 rule_2 rule_3 … rule_n
• Implement above by type tuple
• A higher order language based on types could be created for domain specific way
• An appropriate generative algorithm could be created based on this grammar to allow highly compressed expression
• Low level details of programming could be captured at generator level itself
21-Aug-2010 22
Non-deterministic Generic Structure
T1
Tx
Ty
NFA-Cloud
DFA
DFA
DFA
NFA
Get a node Tx from T1 We know how to work within binary expression, but not how we reach there and where we are going from there.
21-Aug-2010 23
Generating human expression
David Hilbert
-Could we form mathematics on solid foundation so that there is no ambiguity?
-Could we construct mechanical rules for proof?
Kurt Gödel
-You can ask questions that don't have answers within the system and never will.
-We will never have a complete symbolic generator.
Alan Turing
-Created abstract symbolic computation machine to model human thinking process
-Lot of deterministic things can be done even outside Halting problems
21-Aug-2010 24
Workshop Exercise
1) Create print_type function to print combination of type list and type pair in haskell form.
typedef type_list<T1, type_pair<T2, T4> , T3> ::type Tx;
print_type<Tx>();
Output: [T1, (T2, T4), T3]
2) Create “concat” , “index”, “type_of”” function on type_list
3) Create “map” function on type_list so that a specific named function could be called using single line for several types.
typedef type_list<T1, T2, T3, T4>::type Tx
map<Tx, function>();
21-Aug-2010 25
References
• http://plato.stanford.edu/entries/type-theory/
• http://www.haskell.org
• http://www.drdobbs.com/184403813
• http://www.cantrip.org/traits.html
21-Aug-2010 26
Thank you for your patience!