Upcoming SlideShare
×

# Brunhild

1,523 views

Published on

A project for a new language based on gp

2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,523
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
21
0
Likes
2
Embeds 0
No embeds

No notes for slide

• ### Brunhild

1. 1. Brunhild —— GP
2. 2. Brunhild ✦ • V Valkyrie ✦ • • GP • GP •
3. 3. V ✦ ✦ C ✦ Pascal ✦ ✦ • GP concept • concept type concept-type concept-based overloading • OOP   ✦ ✦
4. 4. ✦ • • ✦ cast<::>() • int* b = &c; • char* a = cast<:char*:>(b); ✦ cast<::>() • • ✦ “=”
5. 5. Concept ✦ concept ✦ concept ✦ concept ✦ concept ✦ concept concept map
6. 6. ... ✦ V • STL ✤ count() sort() swap() ✤ vector list map • ✤ rect ellipse triangle ✤
7. 7. STL concept ✦ concept concept Containers<: T :> : DefaultConstruct<:T:>, CopyConstruct<:T:>, Destruct<:T:>, Comparable<:T,T:>, Swappable<:T:>, Assignable<:T,T:> reﬁne list { type T::value_type; type T::reference; type T::deference_type; type T::size_type; Associate Type type T::iterator; type T::const_iterator; ... require UIntegers<:T::size_type:> && sizeof(size_type) > 4; require Integers<:T::deference_type:>; require Iterators<:T::iterator:>; ... function T::size_type T::size(); function bool T::empty(); function T::const_iterator T::begin(); function T::const_iterator T::end(); ... function void swap(T&, T&); }
8. 8. STL concept ✦ reﬁne list require concept • DefaultConstruct, CopyConstruct, Destruct • UIntegers Integers • Comparable == != > < >= <= concept Comparable<:T, U:> { operator bool == (T&, U&); operator bool >(T&, U&); ... }
9. 9. STL concept • Swappable swap() concept Swappable<:T:> { function void T::swap(T&); } • Assignable “=” concept Comparable<:T, U:> { operator bool T::= (T&, U&); }
10. 10. STL ✦ count typeof(c)::difference_type count(Containers& c, typeof(c)::value_type& val) require Equable<:typeof(val), typeof(val):> { using ContT = typeof(c); using count_type = ContT::difference_type; count_type nelem = 0; for (ContT::iterator i = c.begin(); i != c.end(); ++i) { if (*i == val) ++nelem; } return nelem; } ✦ Equable “==” concept Equable<:T, U:> { operator bool ==(T&, U&); }
11. 11. STL ✦ sort void sort(Containers& c) require LessThan<:typeof(c)::value_type, typeof(c)::value_type> { ... } ✦ LessThan “<” concept LessThan<:T, U:> { operator bool <(T&, U&); } ✦ Container sort
12. 12. STL ✦ ✦ concept concept RandomAccessContainers<:T:> : Containers<:T:> { ... require RandomAccessIterators<:T::iterator:>; require ConstRandomAccessIterators<:T::const_iterator:>; ... operator T::reference [](T::difference_type index); ... } ✦ sort() void sort(RandomAccessContainers& c) require LessThan<:typeof(c)::value_type, typeof(c)::value_type:> { ... }
13. 13. STL ✦ RadomAccessContainers concept——Containers sort(Containers&) vector<int> vect1; list<int> list1; ... sort(vect1); // sort(RadomAccessContainers) sort(list1); // RadomAccessContainers sort(Containers&) ✦ concept-based overloading • concept concept • concept reﬁne concept reﬁned concept base concept • concept concept concept
14. 14. STL ✦ list sort() sort() sort() function void sort(Containers& c) require LessThan<:typeof(c)::value_type, typeof(c)::value_type:> && Sortable<:c:> { c.sort(); } ✦ sort() RadomAccessContainers Sortable function void sort(RandomAccessContainers& c) require LessThan<:typeof(c)::value_type, typeof(c)::value_type:> && !Sortable<:c:> { c.sort(); } ✦ !Sortable<::> sort() sort() Sortable
15. 15. STL ✦ swap() function void swap(CopyConstruct& lhd, any& rhd) require BiAssignable<:lhd, rhd:> { typeof(lhd) temp(lhd); lhd = rhd; rhd = temp; } ✦ BiAssignable concept BiAssignable<:T, U:> : Assignable<:T, U:> && Assignable<:U, T:> {} ✦ swap() Swappable swap() function void swap(Swappable& lhd, typeof(lhd) rhd) { lhd.swap(rhd); }
16. 16. STL ✦ CopyConstruct BiAssignable Swappable swap() Swappable function void swap(CopyConstruct& lhd, any& rhd) require BiAssignable<:lhd, rhd:> && !Swappable<:lhd:> { typeof(lhd) temp(lhd); lhd = rhd; rhd = temp; }
17. 17. STL ✦ STL concept concept AssociateContainers<:T:> : Containers<:T:> { type key_type; type mapped_value; ... operator mapped_value& [](key_type&); ... } ✦ sort() • function void sort(AssociateContainers& c) { } • function void sort(AssociateContainers& c)
18. 18. STL ✦ • vector type basic_vector<:Copyable T, Allocators AllocT:> require CopyConstruct<:T:> && AllocT::value_type == T; { ... public: using value_type = T; using size_type = size_t; ... type iterator { ... }; ... basic_vector(AllocT& alloc) { ... } using basic_vector() = basic_vector(AllocT()); basic_vector(basic_vector& vec) { ... } ... function size_type size() { return this.m_size; } ... operator T& [](size_type n) { ... }; ... } using vector<:T:> = basic_vector<:T, default_alloc<:T:>:>;
19. 19. STL • list type list<:Copyable T, Allocators AllocT:> { ... public: require CopyConstruct<:T:>; require AllocT::value_type == T; ... using value_type = T; using size_type = size_t; ... type iterator { ... }; ... list(AllocT& alloc) { ... } using list() = list(AllocT()); list(list& vec) { ... } ... function size_type size() { ... } ... } using list<:T:> = list<:T, default_alloc<:T:>:>;
20. 20. STL • map type map<:any Key, Copyable T, Allocators AllocT:> require CopyConstruct<:Key:> && LessThan<:T:> && CopyConstruct<:T:> && AllocT::value_type == T; { ... public: using value_type = pair<:Key, T:>; using size_type = size_t; ... type iterator { ... }; ... public: map(AllocT& alloc) { ... } using map() = map(AllocT()); list(list& vec) { ... } ... function size_type size() { ... } ... operator T& [](Key& key) { ... } } using map<:Key, T:> = map<:Key, T, default_alloc<:T:>:>;
21. 21. STL concept ✦ Concept V concept_map concept_map<:T, AllocT:> RadomAccessContainers<:basic_vector<:T, AllocT:>:> { } concept_map<:T, AllocT:> Containers<:list<:T, AllocT:>:> { } concept_map<:Key, T, AllocT:> AssociateContainers<:map<:Key, T, AllocT:>:> { } ✦ concept_map • map concept map base concept • concept auto concept
22. 22. STL ✦ vector<:int:> vec_int = [3, 24, 6, 7, 3, 9, 24, 2, 2, 17]; Integers count_24 = count(vec_int, 24); sort(vec_int); list<:float:> list_flot = [13.5, 17.7, 33, 13.5, 24.3]; list<:float:>::difference_type count_135 = count(list_flot, 13.5); sort(list_flot); map<:string, flot:> map_marks = [{“A”, 13.5}, {“B”, 22}, {“C”, 2.2}]; Integers count_A_135 = count(vec_int, {“C”, 2.2}); sort(map_marks); ...
23. 23. STL Concept Adapt ✦ concept Concept Adapt concept type X { ... public: void exchange(X& val) { ... } // swap() } concept_map Swappable<:X:> { function void X::swap(X& val) { that.exchange(val); } } X a, b = {“start”, 123}; swap(a, b);
24. 24. STL ✦ V C concept concept_map<:T, n:> RandomAccessContainers<:T[n]:> { using T[n]::value_type = T; using T[n]::size_type = size_t; ... function size_t size() { return n; } ... operator T& [](T[n]:size_type index) { return that[index]; } ... void push_back(T& val); // // } ✦ int[10] array_int = [3, 24, 6, 7, 3, 9, 24, 2, 2, 17]; Integers count_24 = count(array_int, 24); sort(array_int);
25. 25. ✦ ✦ V runtime concept ✦
26. 26. concept ✦ runtime concept concept concept Shape { function void draw(Devices device); } ✦ concept function void draw_it(Devices d, Shape& s) { s.draw(d); }
27. 27. ✦ type rect { public: function rect(point& left_top, point& right_bottom) { ... } ... function void draw(Devices device) { ... } } type ellipse { public: function eclipse(point& left_top, point& right_bottom) { ... } ... function void draw(Devices device) { ... } } type triangle { public: function triangle(point& p1, point& p2, point& p3) { ... } ... function void draw(Devices device) { ... } }
28. 28. ✦ concept concept_map Shapes<:rect:> { } concept_map Shapes<:ellipse:> { } concept_map Shapes<:triangle:> { }
29. 29. ✦ rect rect1 = {{10, 20}, {100, 200}}; ellipse ecli1 = ellipse(point(50, 50), point(100, 100)); triangle tri1(point(100, 50), {30, 50}, {30, 100}); draw_it(rect1); draw_it(ecli1); draw_it(tri1);
30. 30. ✦ CreateShape() function Shapes* CreateShape(string& cmd) { shape_cmd = parse_cmd(cmd); Shapes* res_shape = null; if (shape_cmd.name == “rect”) { res_shape = new rect(shape_cmd.param); } else if (shape_cmd.name == “ellipse”) { res_shape = new ellipse(shape_cmd.param); } else if (shape_cmd.name == “triangle”) { res_shape = new triangle(shape_cmd.param); } } ✦ runtime concept
31. 31. ✦ vector<:Shapes*:> theShapes; theShapes.push_back(new rect({20,20}, {30,50}); theShapes.push_back(new ellipse({20,20}, {30,50}); theShapes.push_back(new triangle({70,20}, {80,50}, {60,100}); for (vector<:Shapes:>::size_type i = 0; i < theShapes.size(); ++i) { theShapes[i]->draw(screen); } ✦ runtime concept
32. 32. Runtime Concept ✦ Runtime concept • static concept concept • concept runtime concept • concept concept runtime concept • • runtime concept runtime concept runtime concept Shapes* shape1 = CreateShapes(get_input()); shape1->draw(screen); • static concept runtime concept
33. 33. OOP ✦ runtime concept V OOP Sub-typing Polymerphic • concept concept • concept Interface concept • runtime concept static runtime concept static inline • concept runtime concept
34. 34. Concept & type ✦ concept type • concept • any • concept any • concept reﬁne concept • concept ✦ concept type • •
35. 35. Concept & type Any B A C D Concept
36. 36. Concept & type Any B A C D Concept
37. 37. V ✦ ✦ GP/concept ✦ GP ✦ ✦ concept concept-based overloading/specializing ✦ concept concept concept
38. 38. ✦ • • Coroutine • Reactor ✦ Functional Programming • Lambda/Clousure • V GP FP ✦ OOP • •
39. 39. V ✦ GC • • GC ✦ • • V GP/Concept duck typing ✦ VM • • • V VM V
40. 40. ✦ V ✦ ✦ concept concept ✦ concept / ✦ concept_map concept ✦ concept adapter concept ✦ runtime concept ✦ concept
41. 41. ✦ http://en.wikipedia.org/wiki/Generic_programming ✦ http://en.wikipedia.org/wiki/Type_polymorphism ✦ http://www.generic-programming.org/ ✦ http://www.research.att.com/~bs/oopsla06.pdf ✦ http://www.research.att.com/~bs/oops08.pdf ✦ http://www.research.att.com/~bs/popl06.pdf
42. 42. ✦ mail: mohuafeng@sdna.com ✦ gtalk: longshanksmo@gmail.com ✦ twitter: longshanksmo ✦ blog: https://blog.csdn.net/longshanks