Download presentation

Presentation is loading. Please wait.

Published byAlexis Ayers Modified over 3 years ago

1
21-Aug Type theory & practical For Generic Programming Kaushik Biswas*, Dr. Partha Pratim Das, Kausik Datta

2
21-Aug 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

3
21-Aug Genesis of Type: Russells Paradox Set S = { x : x is a rectangle } Set R = { x : x is a not a rectangle } Russells query : Is R R ? According to naïve set theory : TRUE

4
21-Aug Russels 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 Russels 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. Russels query: Who saved the barber?

5
21-Aug Avoiding Russells 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 …

6
21-Aug Simple Type Theory i is type of individuals ( ) is type of propositions if A 1, A 2,.., A n are types then (A 1, A 2,..,A n ) is the type of n- ary relations over objects of respective types A 1, A 2,.., A n 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 Russels paradox.

7
21-Aug Churchs Type Theory ( -calculus) There are two basic types –i : type of individuals – : type of propositions If A, B are types then A B, 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 A 1 A 2 … A n B = A 1,A 2,..,A n B = B (A 1,A 2,…,A n )

8
21-Aug 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 /bin/ghci (ssh mures)

9
21-Aug 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) => t 1.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 -> a sqr :: (Num a) => a -> a reverse :: [a] -> [a] map :: (a -> b) -> [a] -> [b] zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] head :: [a] -> a tail :: [a] -> [a] fst :: (a, b) -> a snd :: (a, b) -> b List Tuple let add a b = a + b let sqr a = a * a Explain this foldr :: (a -> b -> b) -> b -> [a] -> b

10
21-Aug Type Inference foo x y = (length x) + y length :: [a] -> Int Questions: :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 = = Int 1 = [ ] 2 = Int Solving by Hindley Milner algorithm foo :: [a] Int Int Type inference algorithm is NP-complete in general

11
21-Aug 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 inference int 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.

12
21-Aug 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 map map :: (a -> b) -> [a] -> [b] :t sqr sqr :: (Num a) => a -> a :t map sqr map 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 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.

13
21-Aug Workshop for Haskell Problem: Let (x 1,y 1 ), (x 2,y 2 ), (x 3,y 3 ) are three co-ordinate of a triangle. Find area of the triangle using following formula. = ½ { x 1 (y 2 -y 3 ) + x 2 (y 3 -y 1 )+ x 3 (y 1 -y 2 )} area :: (Float t) => [ (t,t) ] -> t Input [(1, 1.1), (5.5, 1), (3.1, 10.3)] Generalize your implementation for n- polygon ? = ½ { x 1 (y 2 -y 4 ) + x 2 (y 3 -y 1 )+ x 3 (y 4 - y 2 )+x 4 (y 1 -y 3 )} (quadrilateral) y1 y2y3 y4 y2 y1y4 y3 y2 y3y4 y1 Use vector operations to compute

14
21-Aug 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.

15
21-Aug Type trait implementation in C++ // Proposition/Predicates template struct is_object ; template <> struct is_object { static const bool val=true; }; Access => is_object ::val // Lower order types template struct return_type; template <> struct return_type { typedef int type; }; Access => return_type ::type // Keeping as part of the type struct 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

16
21-Aug Type manipulation using type-traits iterator where T could be List List_Node List_Node :: next() List_Node :: current() Array index index ++ Array [index] template struct iterator { typename T:: index_type current; typename T:: type vobj; inline void operator ++ () { increment (current); } inline typename T :: stype operator * () { return (typename T :: stype) get_info ( current, vobj); } }; increment () function could now farther be specialized on T :: index_type

17
21-Aug Type List type list set of types having similar notion T L = [T 1, T 2, T 3, ….., T n ] 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 (T L ) = T 1 tail (T L ) = [T 2, T 3, ….., T n ] length (T L ) = n remove (T 2, T L ) = [T 1, T 3, ….., T n ] index (T 2, T L ) = 2 type_of(2, T L ) = T 2 is_present (T x, T L ) = { true, false} concat (T L1, T L2 ) = T L1 ++ T L2 zip (T L1, T L2 ) = [ (T x, T y ) ] where T x T L1 and T y T L2

18
21-Aug Type Tuple Usage: - Forming object and field relations - Forming propositions based on types Operations on tuple type:- left (T Pair ) = T Left right (T Pair ) = T Right type_of ( i, T U ) = T i type tuple n-ary relation of types T u = (T 1, T 2, T 3, ….., T n ) Order is important (T 1, T 2, T 3, ….., T n ) = (T 1, (T 2, T 3, ….., T n ) ) Binary relation is sufficient T Pair = (T Left, T Right )

19
21-Aug Type list and pair (C++) TYPE LIST template struct typeList { typedef H head; typedef T tail; }; struct null_type {}; [int, long, double ] is defined as typeList < int, typeList

20
21-Aug Constructing generic type list How do we form type_list ? Template

21
21-Aug 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

22
21-Aug Non-deterministic Generic Structure T1T1 TxTx TyTy NFA- Cloud DFA NFA Get a node Tx from T 1 We know how to work within binary expression, but not how we reach there and where we are going from there.

23
21-Aug 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

24
21-Aug Workshop Exercise 1)Create print_type function to print combination of type list and type pair in haskell form. typedef type_list, T3> ::type Tx; print_type (); 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 ::type Tx map ();

25
21-Aug References

26
21-Aug Thank you for your patience!

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google