├── LICENSE ├── README.md ├── handouts ├── ceng242-abstraction.pdf ├── ceng242-binding.pdf ├── ceng242-controlflow.pdf ├── ceng242-encapsulation.pdf ├── ceng242-highorderfunctions.pdf ├── ceng242-introduction.pdf ├── ceng242-lp-paradigm.pdf ├── ceng242-oo-objects.pdf ├── ceng242-oo-opolymorphism.pdf ├── ceng242-oo-orelations.pdf ├── ceng242-storage.pdf ├── ceng242-syntax-parsing.pdf ├── ceng242-typesystems.pdf └── ceng242-valuesandtypes.pdf ├── slides ├── ceng242-sl-abstraction.pdf ├── ceng242-sl-binding.pdf ├── ceng242-sl-controlflow.pdf ├── ceng242-sl-encapsulation.pdf ├── ceng242-sl-highorderfunctions.pdf ├── ceng242-sl-introduction.pdf ├── ceng242-sl-lp-paradigm.pdf ├── ceng242-sl-oo-objects.pdf ├── ceng242-sl-oo-opolymorphism.pdf ├── ceng242-sl-oo-orelations.pdf ├── ceng242-sl-storage.pdf ├── ceng242-sl-syntax-parsing.pdf ├── ceng242-sl-typesystems.pdf └── ceng242-sl-valuesandtypes.pdf └── sources ├── Makefile ├── abstraction.tex ├── beamerthememetuceng.sty ├── binding.tex ├── by-sa.pdf ├── by-sa.svg ├── by-sa_backup.svg ├── controlflow.tex ├── diagram-light.pdf ├── encapsulation.tex ├── haskell-class-tree-eps-converted-to.pdf ├── haskell-class-tree.eps ├── highorderfunctions.tex ├── histtalen2-eps-converted-to.pdf ├── histtalen2.eps ├── iceberg-eps-converted-to.pdf ├── iceberg.eps ├── introduction.tex ├── lp-paradigm.tex ├── metu-logo-name.pdf ├── metu-logo-name.svg ├── metu-official-logo.pdf ├── oo-objects.tex ├── oo-opolymorphism.tex ├── oo-orelations.tex ├── storage.tex ├── syntax-parsing.tex ├── typesystems.tex └── valuesandtypes.tex /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pllecturenotes 2 | Programming Language Concepts Course (CEng 242) Lecture notes 3 | 4 | 5 | -------------------------------------------------------------------------------- /handouts/ceng242-abstraction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-abstraction.pdf -------------------------------------------------------------------------------- /handouts/ceng242-binding.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-binding.pdf -------------------------------------------------------------------------------- /handouts/ceng242-controlflow.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-controlflow.pdf -------------------------------------------------------------------------------- /handouts/ceng242-encapsulation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-encapsulation.pdf -------------------------------------------------------------------------------- /handouts/ceng242-highorderfunctions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-highorderfunctions.pdf -------------------------------------------------------------------------------- /handouts/ceng242-introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-introduction.pdf -------------------------------------------------------------------------------- /handouts/ceng242-lp-paradigm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-lp-paradigm.pdf -------------------------------------------------------------------------------- /handouts/ceng242-oo-objects.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-oo-objects.pdf -------------------------------------------------------------------------------- /handouts/ceng242-oo-opolymorphism.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-oo-opolymorphism.pdf -------------------------------------------------------------------------------- /handouts/ceng242-oo-orelations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-oo-orelations.pdf -------------------------------------------------------------------------------- /handouts/ceng242-storage.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-storage.pdf -------------------------------------------------------------------------------- /handouts/ceng242-syntax-parsing.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-syntax-parsing.pdf -------------------------------------------------------------------------------- /handouts/ceng242-typesystems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-typesystems.pdf -------------------------------------------------------------------------------- /handouts/ceng242-valuesandtypes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/handouts/ceng242-valuesandtypes.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-abstraction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-abstraction.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-binding.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-binding.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-controlflow.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-controlflow.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-encapsulation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-encapsulation.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-highorderfunctions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-highorderfunctions.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-introduction.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-lp-paradigm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-lp-paradigm.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-oo-objects.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-oo-objects.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-oo-opolymorphism.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-oo-opolymorphism.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-oo-orelations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-oo-orelations.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-storage.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-storage.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-syntax-parsing.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-syntax-parsing.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-typesystems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-typesystems.pdf -------------------------------------------------------------------------------- /slides/ceng242-sl-valuesandtypes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/slides/ceng242-sl-valuesandtypes.pdf -------------------------------------------------------------------------------- /sources/Makefile: -------------------------------------------------------------------------------- 1 | SHELL=/bin/bash 2 | 3 | files = $(wildcard *.tex) 4 | 5 | pdfs = $(files:%tex=%pdf) 6 | 7 | handouts = $(pdfs:%=../handouts/ceng242-%) 8 | 9 | slides = $(pdfs:%=../slides/ceng242-sl-%) 10 | 11 | 12 | all: $(slides) $(handouts) 13 | 14 | #install: all 15 | # rsync -av $(slides) ../slides/ ;\ 16 | # rsync -av $(handouts) ../handouts/ 17 | 18 | ../handouts/ceng242-%.pdf : %.tex 19 | -pdflatex -jobname=$(@:../handouts/%.pdf=%) '\documentclass[handout,compress,xcolor=table]{beamer}\input{$<}' && \ 20 | pdflatex -jobname=$(@:../handouts/%.pdf=%) '\documentclass[handout,compress,xcolor=table]{beamer}\input{$<}' && \ 21 | mv $(@:../handouts/%=%) $(@) && \ 22 | rm $(@:../handouts/%.pdf=%).{aux,log,nav,out,snm,vrb,toc} 23 | 24 | ../slides/ceng242-sl-%.pdf : %.tex 25 | -pdflatex -jobname=$(@:../slides/%.pdf=%) '\documentclass[slides,compress,xcolor=table]{beamer}\input{$<}' && \ 26 | pdflatex -jobname=$(@:../slides/%.pdf=%) '\documentclass[slides,compress,xcolor=table]{beamer}\input{$<}' && \ 27 | mv $(@:../slides/%=%) $(@) && \ 28 | rm $(@:../slides/%.pdf=%).{aux,log,nav,out,snm,vrb,toc} 29 | 30 | clean: auxclean 31 | #-rm $(slides) $(handouts) 32 | 33 | auxclean: 34 | -rm *.aux *.log *.nav *.out *.snm *.vrb *.toc 35 | 36 | -------------------------------------------------------------------------------- /sources/beamerthememetuceng.sty: -------------------------------------------------------------------------------- 1 | \definecolor{metucolor}{RGB}{227,24,55} 2 | \definecolor{metugray}{rgb}{0.44140625, 0.4375, 0.44921875} 3 | \newcommand*{\metufont}{\usefont{T1}{bourgs}{b}{n}} 4 | 5 | \mode 6 | 7 | %% Themes 8 | % Outer themes 9 | \useoutertheme{shadow} 10 | % Color themes 11 | \usecolortheme{orchid} 12 | \usecolortheme{whale} 13 | % Rounded boxes and shadows 14 | \useinnertheme[shadow=true]{rounded} 15 | % Solid \item symbols 16 | \useinnertheme{rectangles} 17 | 18 | \usepackage{tikz} 19 | \usetikzlibrary{positioning} 20 | 21 | %% define metu logo 22 | \tikzset{pics/metulogo/.style args={#1}{code={ 23 | \path (#1,#1) node (cA) {}; 24 | \path (1.3819660112501051*#1,#1) node (cB) {}; 25 | \path [line width=#1/40,draw=white,fill=white] (cA) circle [radius=#1*1.025]; 26 | \path [line width=#1/40,draw=white,fill=white] (cB) circle [radius=#1*1.025]; 27 | \path [line width=#1/40,draw,fill] (cA) circle [radius=#1]; 28 | \path [line width=#1/40,draw,fill=white] (cB) circle [radius=#1]; 29 | \draw [fill] (#1*1.19098300562505238,0.01840665672467956*#1) arc [radius=#1,start angle=-78.9898431656865, end angle=78.9898431656865]; 30 | \draw [fill=white] (#1*1.19098300562505238,1.9815933432753204*#1) arc [radius=#1,start angle=180-78.9898431656865, end angle=180+78.9898431656865]; 31 | }}} 32 | 33 | 34 | %% Custom colors 35 | %\setbeamerfont{block title}{size={}} 36 | \definecolor{metustructurecolor}{RGB}{10,70,150} 37 | \setbeamercolor{structure}{fg=metustructurecolor} 38 | \setbeamercolor{item}{fg=red!40!black} 39 | \def\insertmetutitle{\includegraphics[height=8mm]{metu-logo-name}} 40 | \def\insertlicense{\tikz [remember picture,overlay] \node [above=0pt of current page.south west,anchor=south west] {\href{https://creativecommons.org/licenses/by-sa/4.0/legalcode}{\includegraphics[height=4mm]{by-sa}}};} 41 | 42 | 43 | % Hide navigation symbols 44 | %\setbeamertemplate{navigation symbols}{} 45 | 46 | %% Title block 47 | \definecolor{beamer@darkred}{RGB}{90,0,0} 48 | \setbeamercolor*{title}{use=structure,fg=white,bg=beamer@darkred} 49 | \setbeamercolor{frametitle}{fg=white} 50 | 51 | 52 | 53 | \setbeamertemplate{headline}{% 54 | \leavevmode% 55 | \hbox{% 56 | \begin{beamercolorbox}[wd=\paperwidth,ht=2.5ex,dp=1.125ex]{palette secondary}% 57 | \insertsectionnavigationhorizontal{\paperwidth}{}{\hskip0pt plus1filll} 58 | \end{beamercolorbox}% 59 | } 60 | } 61 | 62 | 63 | 64 | %% Bottom infolines 65 | \setbeamercolor{footline}{bg=metustructurecolor,fg=white!70!black} 66 | \setbeamertemplate{footline} 67 | { 68 | \leavevmode% 69 | \begin{beamercolorbox}[ht=2.5ex,dp=1.125ex,% 70 | leftskip=.3cm,rightskip=.3cm plus1fil]{footline} 71 | \hspace*{5pt}% 72 | \raisebox{-.26\height}{\tikz[metucolor]\pic{metulogo={1.3mm}};} %\includegraphics[height=1.2em]{metu-official-logo}} 73 | {\metufont METU Computer Engineering} \hspace*{3em} \insertshorttitle\hspace{1pt}/\hspace{1pt}\insertshortsubtitle \hfill \insertframenumber\hspace{1pt}/\hspace{1pt}\inserttotalframenumber\hspace*{4ex} 74 | \end{beamercolorbox} 75 | \vskip0pt% 76 | } 77 | 78 | \mode 79 | 80 | -------------------------------------------------------------------------------- /sources/binding.tex: -------------------------------------------------------------------------------- 1 | %\documentclass[compress,dvips,xcolor=table]{beamer} 2 | \usepackage{etex} 3 | %\documentclass{article} 4 | %\usepackage{beamerarticle} 5 | %\usepackage{pstricks,pst-node} % PSTricks package 6 | %\usepackage[turkish]{babel} 7 | \usepackage[utf8]{inputenc} 8 | \usepackage{listings} 9 | \usepackage{multicol} 10 | %\includeonlyframes{current} 11 | 12 | \def\circtxt#1{$\mathalpha \bigcirc \mkern-13mu \mathtt #1$} 13 | 14 | \mode
15 | { 16 | \usepackage{fullpage} 17 | \usepackage{pgf} 18 | \usepackage{hyperref} 19 | } 20 | 21 | \mode 22 | { 23 | \usetheme{metuceng} 24 | 25 | % \setbeamercovered{transparent} 26 | } 27 | 28 | 29 | \title{Programming Language Concepts/Binding and Scope} 30 | \author{Onur Tolga Şehitoğlu} 31 | \institute[ODTÜ]{Bilgisayar Mühendisliği} 32 | \subject{Binding and Scope} 33 | \date{} 34 | 35 | \begin{document} 36 | \lstset{language=C, 37 | basicstyle=\scriptsize\ttfamily, 38 | keywordstyle=\color{blue!50!black}\bfseries, 39 | identifierstyle=\color{blue!60!green}\sffamily, 40 | stringstyle=\color{red!70!green}\ttfamily, 41 | commentstyle=\color{blue!30!white}\itshape, 42 | showstringspaces=true} 43 | \setbeamercolor{hexample}{bg=green!5!white,fg=black}% 44 | \setbeamercolor{cexample}{bg=blue!5!white,fg=black}% 45 | \setbeamercolor{pexample}{bg=orange!5!white,fg=black}% 46 | \setbeamercolor{oexample}{bg=violet!5!white,fg=black}% 47 | 48 | \frame[plain]{\maketitle} 49 | \begin{frame} 50 | \frametitle{Outline} 51 | \begin{multicols}{2} 52 | \tableofcontents 53 | \end{multicols} 54 | \end{frame} 55 | \section{Binding} 56 | \begin{frame} 57 | \frametitle{Binding} 58 | \begin{itemize}[<+->] 59 | \item Most important feature of high level languages: programmers able to 60 | give names to program entities (variable, constant, function, type, ...). 61 | These names are called \structure{identifiers}. 62 | \item definition of an identifier $\leftrightarrows$ used position of an 63 | identifier. Formally: 64 | binding occurrence $\leftrightarrows$ applied occurrence. 65 | \item Identifiers are declared once, used $n$ times. 66 | \item Language should map which corresponds to which. 67 | \item \structure{Binding:} Finding the corresponding binding occurrence 68 | (definition/declaration) for an applied occurrence (usage) of an identifier. 69 | \end{itemize} 70 | \end{frame} 71 | 72 | \defverbatim[colored]\codescopefailC{ 73 | \begin{lstlisting}[language={C},escapechar=\#] 74 | double f,y; 75 | int f() { #\alert{$\times$ error!}# 76 | ... 77 | } 78 | double y; #\alert{$\times$ error!}# 79 | \end{lstlisting}} 80 | \defverbatim[colored]\codescopeokC{ 81 | \begin{lstlisting}[language={C},escapechar=\#] 82 | double y; 83 | int f() { 84 | double f; #\color{green!60!black}{$\surd$ OK}# 85 | int y ; #\color{green!60!black}{$\surd$ OK.}# 86 | } 87 | \end{lstlisting}} 88 | 89 | \begin{frame} 90 | for binding: 91 | \begin{enumerate}[<+->] 92 | \item \structure{Scope of identifiers} should be known. What is the block 93 | structure? Which blocks the identifier is available. 94 | \item What will happen if we use same identifier name again\\ 95 | ``C forbids reuse of same identifier name in the same scope. Same name can 96 | be used in different nested blocks. The identifier inside 97 | \structure{hides} the outside identifier''. 98 | \begin{columns} 99 | \begin{column}{.5\linewidth} 100 | \begin{beamercolorbox}{cexample} 101 | \codescopefailC 102 | \end{beamercolorbox} 103 | \end{column} 104 | \begin{column}{.5\linewidth} 105 | \begin{beamercolorbox}{cexample} 106 | \codescopeokC 107 | \end{beamercolorbox} 108 | \end{column} 109 | \end{columns} 110 | \end{enumerate} 111 | \end{frame} 112 | 113 | \defverbatim[colored]\codeenvC{ 114 | \begin{lstlisting}[language={C},escapechar=\#] 115 | struct Person { ... } x; 116 | int f(int a) { 117 | double y; 118 | int x; 119 | ... #\circtxt{1}# 120 | } 121 | int main() { 122 | double a; 123 | ... #\circtxt{2}# 124 | } 125 | \end{lstlisting}} 126 | \section{Environment} 127 | \begin{frame} 128 | \frametitle{Environment} 129 | \begin{itemize}[<+->] 130 | \item \structure{Environment}: The set of binding occurrences that are accessible at a 131 | point in the program. 132 | \item Example: \\ 133 | \begin{columns} 134 | 135 | \begin{column}{.45\linewidth} 136 | \begin{beamercolorbox}{cexample} 137 | \codeenvC 138 | \end{beamercolorbox} 139 | \end{column} 140 | 141 | \begin{column}{.55\linewidth}\small 142 | \noindent 143 | O(\circtxt{1})=\{struct Person $\mapsto$ type,\\ 144 | x $\mapsto$ int, f $\mapsto$ func, a $\mapsto$ int, \\ 145 | y $\mapsto$ double\}\\[2em] 146 | O(\circtxt{2})=\{struct Person $\mapsto$ type,\\ 147 | x $\mapsto$ struct Person, f $\mapsto$ func, \\ 148 | a $\mapsto$ double, main $\mapsto$ func\}\\ 149 | \end{column} 150 | \end{columns} 151 | 152 | \end{itemize} 153 | \end{frame} 154 | 155 | \section{Block Structure} 156 | \begin{frame} 157 | \frametitle{Block Structure} 158 | \begin{itemize}[<+->] 159 | \item Program blocks define the scope of the identifiers declared inside. (boundary of the 160 | definition validity) For variables, they also define the lifetime. 161 | \item Languages may have different block structures: 162 | \begin{itemize} 163 | \item[\bf C] function definitions and command blocks (\{ ... \}) define local scopes. Also each 164 | source code define a block. 165 | \item[\bf Java] Class definitions, class member function definitions, block commands define 166 | local scopes. Nested function definitions and namespaces possible. 167 | \item[\bf Haskell] `\texttt{let {\em definitions\/} in {\em expression\/}}' defines a block 168 | expression. Also `\texttt{ {\em expression \/} where {\em definitions\/}}' defines a block 169 | expression. (the definitions have a local scope and not accessible outside of the expression) 170 | \end{itemize} 171 | \item Block structure of the language is defined by the organization of the blocks. 172 | \end{itemize} 173 | \end{frame} 174 | 175 | \subsection{Monolithic block structure} 176 | \begin{frame} 177 | \frametitle{Monolithic block structure} 178 | \begin{itemize} 179 | \item Whole program is a block. All identifiers have global scope starting from the 180 | definition. 181 | \item \structure{Cobol} is a monolithic block structure language.\\ 182 | \texttt{\begin{tabular}{|>{\columncolor{blue!15!white}}p{\linewidth}|}\hline 183 | int x;\\ 184 | int y;\\ 185 | ....\\ 186 | ....\\ \hline 187 | \end{tabular}} 188 | \item In a long program with many identifiers, they share the same scope and they need to be 189 | distinct. 190 | \end{itemize} 191 | 192 | \end{frame} 193 | 194 | \subsection{Flat block structure} 195 | \begin{frame} 196 | \frametitle{Flat block structure} 197 | \begin{itemize} 198 | \item Program contains the global scope and only a single level local scope of function 199 | definitions. No further nesting is possible. 200 | \item \structure{Fortran} and partially \structure{C} has flat block structure.\\ 201 | {\tiny \tt 202 | \begin{tabular}{|>{\columncolor{blue!15!white}}p{\linewidth}|} \hline 203 | int x;\\ 204 | int y;\\ 205 | int f() \\ 206 | \ \begin{tabular}{|>{\columncolor{blue!10!white}}p{.8\linewidth}|} \hline 207 | \{\ int a;\\ 208 | \ \ double b;\\ 209 | ... \\ 210 | \} \\ \hline 211 | \end{tabular} \\[1em] 212 | int g() \\ 213 | \ \begin{tabular}{|>{\columncolor{blue!10!white}}p{.8\linewidth}|} \hline 214 | \{ \ int a;\\ 215 | \ \ double b;\\ 216 | ... \\ 217 | \}\\ \hline 218 | \end{tabular}\\ 219 | ....\\ \hline 220 | \end{tabular}} 221 | \end{itemize} 222 | \end{frame} 223 | 224 | \subsection{Nested block structure} 225 | \begin{frame} 226 | \frametitle{Nested block structure} 227 | \begin{itemize} 228 | \item Multiple blocks with nested local scopes can be defined. 229 | \item \structure{Pascal} and \structure{Java} have nested block structure.\\ 230 | {\tiny \tt 231 | \begin{tabular}{|>{\columncolor{blue!15!white}}p{\linewidth}|} \hline 232 | int x;\\ 233 | int f() \\ 234 | \ \begin{tabular}{|>{\columncolor{blue!10!white}}p{.8\linewidth}|} \hline 235 | \{\ int a;\\ 236 | double g() \\ 237 | \ \begin{tabular}{|>{\columncolor{blue!5!white}}p{.8\linewidth}|} \hline 238 | \{\ int x;\\ 239 | ... \\ 240 | \} \\ \hline 241 | \end{tabular} \\[1em] 242 | ... \\ 243 | \} \\ \hline 244 | \end{tabular} \\[1em] 245 | int g() \\ 246 | \ \begin{tabular}{|>{\columncolor{blue!10!white}}p{.8\linewidth}|} \hline 247 | \{ \ int h()\\ 248 | \begin{tabular}{|>{\columncolor{blue!5!white}}p{.8\linewidth}|} \hline 249 | \{\ int x;\\ 250 | ... \\ 251 | \} \\ \hline 252 | \end{tabular} \\[1em] 253 | ... \\ 254 | \}\\ \hline 255 | \end{tabular}\\ 256 | ....\\ \hline 257 | \end{tabular}} 258 | \item C block commands can be nested. 259 | \item GCC extensions (\structure{Not C99 standard!}) to C allow nested function definitions. 260 | \end{itemize} 261 | \end{frame} 262 | 263 | 264 | \defverbatim[colored]\codehidingC{ 265 | \begin{lstlisting}[language={C},escapechar=\#] 266 | int x,y; 267 | int f(double x) { 268 | ... // parameter x hides global x in f() 269 | } 270 | int g(double a) { 271 | int y; // local y hides global y in g() 272 | double f; // local f hides global f() in g() 273 | ... 274 | } 275 | int main() { 276 | int y; // local y hides global y in main() 277 | } 278 | \end{lstlisting}} 279 | \section{Hiding} 280 | \begin{frame} 281 | \frametitle{Hiding} 282 | \begin{itemize} 283 | \item Identifiers defined in the inner local block hides the outer block identifiers with the 284 | same name during their scope. They cannot be accessed within the inner block. 285 | \begin{beamercolorbox}{cexample} 286 | \codehidingC 287 | \end{beamercolorbox} 288 | \end{itemize} 289 | \end{frame} 290 | 291 | \section{Static vs Dynamic Scope/Binding} 292 | \begin{frame} 293 | \frametitle{Static vs Dynamic Scope/Binding} 294 | The binding and scope resolution is done at compile time or run time? Two options: 295 | \begin{enumerate} 296 | \item Static binding, static scope 297 | \item Dynamic binding, dynamic scope 298 | \end{enumerate} 299 | \begin{itemize} 300 | \item First defines scope and binding based on the lexical structure of the program and 301 | binding is done at compile time. 302 | \item Second activates the definitions in a block during the execution of the block. The 303 | environment changes dynamically at run time as functions are called and returned. 304 | \end{itemize} 305 | \end{frame} 306 | 307 | \defverbatim[colored]\codestaticbindC{ 308 | \begin{lstlisting}[language={C},escapechar=\#] 309 | int x=1,y=2; 310 | int f(int y) { 311 | y=x+y; 312 | return x+y; 313 | } 314 | int g(int a) { 315 | int x=3; 316 | y=x+x+a; x=x+y; y=f(x); 317 | return x; 318 | } 319 | int main() { 320 | int y=0; int a=10; 321 | x=a+y; y=x+a; a=f(a); a=g(a); 322 | return 0; 323 | } 324 | \end{lstlisting}} 325 | \defverbatim[colored]\codestaticbindCR{ 326 | \def\X{\color{green!60!black}\bf x} 327 | \def\gX{\color{red!60!black}\bf x} 328 | \def\Y{\color{green!60!black}\bf y} 329 | \def\mY{\color{red!60!black}\bf y} 330 | \def\fY{\color{yellow!60!black}\bf y} 331 | \def\gA{\color{yellow!60!black}\bf a} 332 | \def\mA{\color{red!60!black}\bf a} 333 | \begin{lstlisting}[language={C},escapechar=\#] 334 | int #\X#=1,#\Y#=2; 335 | int f(int #\fY#) { 336 | #\fY#=#\X#+#\fY#; /* #\X# global, #\fY# local */ 337 | return #\X#+#\fY#; 338 | } 339 | int g(int #\gA#) { 340 | int #\gX#=3; /* #\gX# local, #\Y# global */ 341 | #\Y#=#\gX#+#\gX#+#\gA#; #\gX#=#\gX#+#\Y#; #\Y#=f(#\gX#); 342 | return #\gX#; 343 | } 344 | int main() { 345 | int #\mY#=0; int #\mA#=10; /* #\X# global #\mY# local */ 346 | #\X#=#\mA#+#\mY#; #\mY#=#\X#+#\mA#; #\mA#=f(#\mA#); #\mA#=g(#\mA#); 347 | return 0; 348 | } 349 | \end{lstlisting}} 350 | \subsection{Static binding} 351 | \begin{frame} 352 | \frametitle{Static binding} 353 | \begin{itemize} 354 | \item Programs shape is significant. Environment is based on the position in the source 355 | (lexical scope) 356 | \item Most languages apply static binding (C, Haskell, Pascal, Java, ...) 357 | \only<2>{\begin{beamercolorbox}{cexample} 358 | \codestaticbindCR 359 | \end{beamercolorbox}} 360 | \only<1>{\begin{beamercolorbox}{cexample} 361 | \codestaticbindC 362 | \end{beamercolorbox}} 363 | \end{itemize} 364 | \end{frame} 365 | 366 | \defverbatim[colored]\codedynamicbindC{ 367 | \begin{lstlisting}[language={C},escapechar=\#,basicstyle=\tiny\ttfamily, 368 | numbers=left,numberstyle=\tiny,stepnumber=1] 369 | int x=1,y=2; 370 | int f(int y) { 371 | y=x+y; 372 | return x+y; 373 | } 374 | int g(int a) { 375 | int x=3; 376 | y=x+x+a; x=x+y; 377 | y=f(x); 378 | return x; 379 | } 380 | int main() { 381 | int y=0; int a=10; 382 | x=a+y; y=x+a; 383 | a=f(a); a=g(a); 384 | return 0; 385 | } 386 | \end{lstlisting}} 387 | \subsection{Dynamic binding} 388 | \begin{frame} 389 | \frametitle{Dynamic binding} 390 | \begin{itemize} 391 | \item Functions called update their declarations on the environment at \structure{run-time}. 392 | Delete them on return. Current stack of activated 393 | blocks is significant in binding. 394 | \item Lisp and some script languages apply dynamic binding. 395 | \begin{columns} 396 | \begin{column}{.35\linewidth} 397 | \begin{beamercolorbox}{cexample} 398 | \codedynamicbindC 399 | \end{beamercolorbox} 400 | \end{column} 401 | \begin{column}{.65\linewidth} 402 | \only<2->{ 403 | \def\T{\rule{0pt}{1em}\hspace*{1em}} 404 | \noindent\tiny\begin{tabular}{rll} 405 | & Trace & Environment (without functions)\\ \hline 406 | & initial & \{x:GL, y:GL \} \\ \rowcolor{blue!5} 407 | 12& call main & \{x:GL, y:main, a:main \} \\ \rowcolor{blue!15} 408 | 15&\T call f(10) & \{x:GL, y:f , a:main \} \\ \rowcolor{blue!15} 409 | 4&\T return f : 30 & back to environment before f \\ \rowcolor{blue!5} 410 | 15& in main & \{x:GL, y:main, a:main \} \\ \rowcolor{blue!10} 411 | 15&\T call g(30) & \{x:g, y:main, a:g \} \\ \rowcolor{blue!25} 412 | 9&\T\T call f(39) & \{x:g, y:f, a:g \} \\ \rowcolor{blue!25} 413 | 4&\T\T return f : 117 & back to environment before f\\ \rowcolor{blue!10} 414 | 9&\T in g & \{x:g, y:main, a:g \} \\ \rowcolor{blue!10} 415 | 10&\T return g : 39 & back to environment before g \\ \rowcolor{blue!5} 416 | 15& in main & \{x:GL, y:main, a:main\} \\ \rowcolor{blue!5} 417 | 16& return main & x:GL=10, y:GL=2, y:main=117, a:main=39 \\ 418 | \end{tabular} 419 | } 420 | \end{column} 421 | \end{columns} 422 | \end{itemize} 423 | \end{frame} 424 | 425 | \begin{frame}[fragile] 426 | \begin{itemize} 427 | \item It gets more complicated if nested functions are allowed: 428 | \begin{beamercolorbox}{hexample} 429 | \begin{lstlisting}[language=Haskell] 430 | let x = 5 431 | y = 10 432 | func x = x * x + other y 433 | other x = x + x 434 | in func x + let other x = x - 1 435 | y = 2 436 | in func x 437 | \end{lstlisting} 438 | \end{beamercolorbox} 439 | \end{itemize} 440 | \end{frame} 441 | 442 | \section{Binding Process} 443 | \begin{frame} 444 | \frametitle{Binding Process} 445 | \begin{itemize} 446 | \item Language processor keeps track of current environment in a data structure called \structure{Symbol Table} or \structure{Identifier Table} 447 | \item Symbol table maps identifier strings to their type and binding. 448 | \item Each new block introduces its declarations/bindings to the symbol table and on exit, they are cleared. 449 | \item Usually implemented as a Hash Table. 450 | \item For static binding, Symbol Table is a compile time data structure and maintained during different stages of compilation. 451 | \item For dynamic binding, symbol table is maintained at run time. 452 | \end{itemize} 453 | \end{frame} 454 | 455 | \section{Declarations} 456 | \begin{frame} 457 | \frametitle{Declarations} 458 | \begin{itemize} 459 | \item Definitions vs Declarations 460 | \item Sequential declarations 461 | \item Collateral declarations 462 | \item Recursive declarations 463 | \item Collateral recursive declarations 464 | \item Block commands 465 | \item Block expressions 466 | \end{itemize} 467 | \end{frame} 468 | 469 | \subsection{Definitions and Declarations} 470 | \begin{frame} 471 | \frametitle{Definitions and Declarations} 472 | \begin{itemize} 473 | \item \structure{Definition:} Creating a new name for an existing binding. 474 | \item \structure{Declaration:} Creating a completely new binding. 475 | \item in C: \texttt{struct Person} is a declaration. \texttt{typedef struct Person persontype} 476 | is a definition. 477 | \item in C++: \texttt{double x} is a declaration. \texttt{double \&y=x;} is a definition. 478 | \item creating a new entity or not. Usually the distinction is not clear and used 479 | interchangeably. 480 | \end{itemize} 481 | \end{frame} 482 | 483 | \subsection{Sequential Declarations} 484 | \begin{frame} 485 | \frametitle{Sequential Declarations} 486 | \begin{itemize} 487 | \item \texttt{D$_1$ ; D$_2$ ; ... ; D$_n$} 488 | \item Each declaration is available starting with the next line. \texttt{D$_1$} can be used in 489 | \texttt{D$_2$} an afterwards, \texttt{D$_2$} can be used in \texttt{D$_3$} and afterwards,... 490 | \item Declared identifier is not available in preceding declarations. 491 | \item Most programming languages provide only such declarations. 492 | \end{itemize} 493 | \end{frame} 494 | 495 | \subsection{Collateral Declarations} 496 | \begin{frame} 497 | \frametitle{Collateral Declarations} 498 | \begin{itemize} 499 | \item \texttt{Start; D$_1$ and D$_2$ and ... and T$_n$ ; End} 500 | \item Each declaration is evaluated in the environment preceding the declaration group. 501 | Declared identifiers are available only after all finish. 502 | \texttt{D$_1$},... \texttt{D$_n$} uses in the environment of \texttt{Start}. They are 503 | available in the environment of \texttt{End}. 504 | \item ML allows collateral declarations additionally. 505 | \end{itemize} 506 | \end{frame} 507 | 508 | \subsection{Recursive declarations} 509 | \begin{frame} 510 | \frametitle{Recursive declarations} 511 | \begin{itemize} 512 | \item Declaration:\texttt{Name = Body} 513 | \item The body of the declaration can access the declared identifier. Declaration is available 514 | in the body of itself. 515 | \item C functions and type declarations are recursive. Variable definitions are usually not 516 | recursive. ML allows programmer to choose among recursive and non-recursive 517 | function definitions. 518 | \end{itemize} 519 | \end{frame} 520 | 521 | \subsection{Recursive Collateral Declarations} 522 | \begin{frame} 523 | \frametitle{Recursive Collateral Declarations} 524 | \begin{itemize} 525 | \item All declarations can access the others regardless of their order. 526 | \item All Haskell declarations are recursive collateral (including variables) 527 | \item All declarations are mutually recursive. 528 | \item ML allows programmer to do such definitions. 529 | \item C++ class members are like this. 530 | \item in C a similar functionality can be achieved by prototype definitions. 531 | \end{itemize} 532 | \end{frame} 533 | 534 | \subsection{Block Expressions} 535 | \begin{frame}[fragile] 536 | \frametitle{Block Expressions} 537 | \begin{itemize} 538 | \item Allows an expression to be evaluated in a special local environment. Declarations done 539 | in the block is not available outside. 540 | \item in Haskell: \texttt{let D$_1$; D$_2$; ... ; D$_n$ in {\em Expression\/} } or 541 | \texttt{{\em Expression\/} where D$_1$; D$_2$; ... ; D$_n$} 542 | \begin{beamercolorbox}{hexample} 543 | \begin{lstlisting}[language={Haskell},escapechar=\#] 544 | x=5 545 | t=let xsquare=x*x 546 | factorial n = if n<2 then 1 else n*factorial (n-1) 547 | xfact = factorial x 548 | in (xsquare+1)*xfact/(xfact*xsquare+2) 549 | \end{lstlisting}\end{beamercolorbox} 550 | \end{itemize} 551 | \end{frame} 552 | 553 | \begin{frame}[fragile] 554 | \begin{itemize} 555 | \item Hiding works in block expressions as expected: 556 | \begin{beamercolorbox}{hexample} 557 | \begin{lstlisting}[language={Haskell},escapechar=\#] 558 | x=5 ; y=6 ; z = 3 559 | t=let x=1 560 | in let y=2 561 | in x+y+z 562 | {-- t is 1+2+3 here. local x and y hides the ones above --} 563 | \end{lstlisting} 564 | \end{beamercolorbox} 565 | \item GCC (only GCC) block expressions has the last expression in block as the value: 566 | \begin{beamercolorbox}{cexample} 567 | \begin{lstlisting}[language=C,escapechar=\#] 568 | double min ; 569 | ... 570 | min = ({ double tmp; 571 | if (b < a) then { 572 | tmp = a; a = b ; b = tmp; 573 | } 574 | a; // this is the value of the block 575 | }); 576 | \end{lstlisting}\end{beamercolorbox} 577 | \end{itemize} 578 | \end{frame} 579 | 580 | \defverbatim[colored]\codeblokcomC{ 581 | \begin{lstlisting}[language={C},escapechar=\#] 582 | int x=3, i=2; 583 | x += i; 584 | while (x>i) { 585 | int i=0; 586 | ... 587 | i++; 588 | } 589 | /* i is 2 again */ 590 | \end{lstlisting}} 591 | \subsection{Block Commands} 592 | \begin{frame} 593 | \frametitle{Block Commands} 594 | \begin{itemize} 595 | \item Similar to block expressions, declarations done inside a block command is available only 596 | during the block. Statements inside work in this environment. The declarations lost outside of 597 | the block. 598 | \item \ \\ 599 | \begin{beamercolorbox}{cexample} 600 | \codeblokcomC 601 | \end{beamercolorbox} 602 | \end{itemize} 603 | 604 | \end{frame} 605 | 606 | \defverbatim[colored]\codeblokdecH{ 607 | \begin{lstlisting}[language={Haskell},escapechar=\#] 608 | fifthpower x = (forthpowerx) * x where 609 | squarex = x*x 610 | forthpowerx = squarex*squarex 611 | \end{lstlisting}} 612 | \subsection{Block Declarations} 613 | \begin{frame} 614 | \frametitle{Block Declarations} 615 | \begin{itemize} 616 | \item A declaration is made in a local environment of declarations. Local declarations 617 | are not made available to the outer environment. 618 | \item in Haskell: \texttt{D$_{exp}$ where D$_1$; D$_2$; ... ; D$_n$}\\ 619 | Only \texttt{D$_{exp}$} is added to environment. Body of \texttt{D$_{exp}$} has all local 620 | declarations available in its environment. 621 | \begin{beamercolorbox}{hexample} 622 | \codeblokdecH 623 | \end{beamercolorbox} 624 | \end{itemize} 625 | \end{frame} 626 | 627 | \section{Summary} 628 | \begin{frame} 629 | \frametitle{Summary} 630 | \begin{itemize} 631 | \item Binding, scope, environment 632 | \item Block structure 633 | \item Hiding 634 | \item Static vs Dynamic binding 635 | \item Declarations 636 | \item Sequential, recursive, collateral 637 | \item Expression, command and declaration blocks 638 | \end{itemize} 639 | \end{frame} 640 | \end{document} 641 | -------------------------------------------------------------------------------- /sources/by-sa.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/sources/by-sa.pdf -------------------------------------------------------------------------------- /sources/by-sa.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 20 | 22 | 46 | 48 | 49 | 51 | image/svg+xml 52 | 54 | 55 | 56 | 57 | 61 | 67 | 73 | 76 | 86 | 89 | 94 | 95 | 96 | 101 | 104 | 109 | 114 | 115 | 118 | 123 | 128 | 129 | 132 | 142 | 145 | 150 | 155 | 156 | 157 | 159 | 165 | 167 | 172 | 178 | 179 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /sources/by-sa_backup.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 21 | 43 | 45 | 46 | 48 | image/svg+xml 49 | 51 | 52 | 53 | 54 | 58 | 64 | 69 | 70 | 73 | 74 | 83 | 84 | 87 | 90 | 91 | 92 | 93 | 94 | 95 | 98 | 99 | 102 | 106 | 107 | 111 | 112 | 113 | 114 | 117 | 121 | 122 | 126 | 127 | 128 | 129 | 132 | 133 | 142 | 143 | 146 | 149 | 150 | 153 | 154 | 155 | 156 | 157 | 158 | 160 | 170 | 171 | 173 | 176 | 177 | 186 | 187 | 188 | 189 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /sources/controlflow.tex: -------------------------------------------------------------------------------- 1 | %\documentclass[compress,xcolor=table]{beamer} 2 | \usepackage{etex} 3 | %\documentclass{article} 4 | %\usepackage{beamerarticle} 5 | %\usepackage{pstricks,pst-node} % PSTricks package 6 | \usepackage{tikz} 7 | \usetikzlibrary{patterns,positioning,fit,arrows,matrix,calc,shapes.geometric,shapes.multipart,decorations.pathreplacing} 8 | %\usepackage[turkish]{babel} 9 | \usepackage[utf8]{inputenc} 10 | \usepackage{listings} 11 | \usepackage{multicol} 12 | %\includeonlyframes{current} 13 | 14 | \mode
15 | { 16 | \usepackage{fullpage} 17 | \usepackage{pgf} 18 | \usepackage{hyperref} 19 | } 20 | 21 | \mode 22 | { 23 | \usetheme{metuceng} 24 | 25 | %\setbeamercovered{transparent} 26 | } 27 | 28 | 29 | \title{Programming Languages} 30 | \subtitle{Control Flow} 31 | \author{Onur Tolga Şehitoğlu} 32 | \institute[METU]{Computer Engineering} 33 | \subject{Control Flow} 34 | \date{} 35 | \titlegraphic{\insertmetutitle\insertlicense} 36 | 37 | 38 | \def\circtxt#1{$\mathalpha \bigcirc \mkern-13mu \mathtt #1$} 39 | \def\smiley{\textcircled{\scriptsize $\mkern3mu\ddot{\ } \mkern-15mu \smallsmile$}} 40 | 41 | \begin{document} 42 | 43 | \lstset{language=C, 44 | basicstyle=\scriptsize\ttfamily, 45 | keywordstyle=\color{blue!50!black}\bfseries, 46 | identifierstyle=\color{blue!60!green}\sffamily, 47 | stringstyle=\color{red!70!green}\ttfamily, 48 | commentstyle=\color{blue!30!white}\itshape, 49 | showstringspaces=true} 50 | \setbeamercolor{hexample}{bg=green!5!white,fg=black}% 51 | \setbeamercolor{cexample}{bg=blue!5!white,fg=black}% 52 | \setbeamercolor{pexample}{bg=orange!5!white,fg=black}% 53 | \setbeamercolor{oexample}{bg=violet!5!white,fg=black}% 54 | 55 | \frame[plain]{\maketitle} 56 | \begin{frame} 57 | \frametitle{Outline} 58 | \begin{multicols}{2} 59 | \small 60 | \tableofcontents 61 | \end{multicols} 62 | \end{frame} 63 | \section{Control Flow} 64 | \begin{frame} 65 | \frametitle{Control Flow} 66 | \begin{itemize} 67 | \item Usual control flow: a command followed by the other. Executed in sequence. 68 | \structure{single entrance - single exit} 69 | \item Commands to change control flow and transfer execution to another point: 70 | \structure{sequencers} 71 | \begin{itemize} 72 | \item Jumps 73 | \item Escapes 74 | \item Exceptions 75 | \end{itemize} 76 | \end{itemize} 77 | \end{frame} 78 | 79 | \newcommand{\R}[2]{\tikz [remember picture,overlay] \node (#1) {#2};} 80 | 81 | \defverbatim[colored]\codejump{ 82 | \begin{lstlisting}[language={C},escapechar=\#] 83 | #\R{l1}{}#L1: x++; 84 | if (x>10) goto L2#\R{gl2}{}#; 85 | j++; 86 | for (i=0;i1000) goto L3#\R{gl3}{}#; 89 | else goto L1#\R{gl1}{}#; 90 | } 91 | #\R{l3}{}#L3: printf("out\n"); 92 | \end{lstlisting}} 93 | \section{Jumps} 94 | \begin{frame} 95 | \frametitle{Jumps} 96 | \begin{itemize} 97 | \item Jumps transfer control to a point in the code. The destination is marked with 98 | \structure{labels} 99 | \item When jumps to arbitrary positions are possible...: 100 | \begin{beamercolorbox}{cexample} 101 | \codejump 102 | \end{beamercolorbox} 103 | \only<2->{\begin{tikzpicture} [thick,remember picture,overlay,rounded corners=3pt] 104 | \draw [green,->] (gl1) -- +(0,2.5) -| (l1); 105 | \draw [violet,->] (gl2) -- +(0,-0.3) -| (l2); 106 | \draw [red,->] (gl3) -- +(0,-1.5) -| (l3); 107 | \end{tikzpicture}} 108 | %\ncbar[linecolor=green,angleA=90,angleB=90,nodesep=2pt,linearc=.3,arm=15pt]{->}{gl1}{l1} 109 | %\ncbar[linecolor=magenta,angleA=90,angleB=90,nodesep=2pt,linearc=.3,arm=15pt]{->}{gl2}{l2} 110 | %\ncbar[linecolor=red,angleA=-90,angleB=-90,nodesep=2pt,linearc=.3,arm=15pt]{->}{gl3}{l3} } 111 | \item Called \structure{spaghetti coding} 112 | \end{itemize} 113 | \end{frame} 114 | 115 | \defverbatim[colored]\codejumpblock{ 116 | \begin{lstlisting}[language={C},escapechar=\#] 117 | L1: .... 118 | goto L2; #\color{violet}\circtxt{1}# 119 | .... 120 | for (i=0;i<10;i++) { 121 | int x=t; 122 | L2: .... 123 | goto L1; #\color{violet}\circtxt{2}# 124 | ... 125 | goto L2: #\color{violet}\circtxt{3}# 126 | } 127 | \end{lstlisting}} 128 | \begin{frame} 129 | \small 130 | \begin{itemize} 131 | \item Unrestricted jumps $\Rightarrow$ spaghetti coding. 132 | \item GCC extension allows storing labels in variables. Would you like to debug that code? \smiley 133 | \item Further problems. Which jumps have problems?: 134 | \begin{beamercolorbox}{cexample} 135 | \codejumpblock 136 | \end{beamercolorbox} 137 | \item Lifetime and values of local variables? Values of index variables? 138 | \item C: Labels are local to enclosing block. No jumps allowed into the block. 139 | Newer languages avoid jumps. 140 | \item Single entrance multiple exit is still desirable.$\rightarrow$ escapes 141 | \end{itemize} 142 | \end{frame} 143 | 144 | \section{Escapes} 145 | \begin{frame} 146 | \frametitle{Escapes} 147 | \begin{itemize} 148 | \item Restricted jumps to out of textually enclosing block(s) 149 | \item Depending on which enclosing block to jump out of: 150 | \begin{itemize} 151 | \item loop: \structure{break} sequencer. 152 | \item loops: \structure{exit} sequencer. 153 | \item function: \structure{return} sequencer. 154 | \item program: \structure{halt} sequencer. 155 | \end{itemize} 156 | \end{itemize} 157 | \end{frame} 158 | 159 | \defverbatim[colored]\codejavaescapes{ 160 | \begin{lstlisting}[language={Java},escapechar=\#] 161 | L1: for (i=0;i<10;i++) { 162 | for (j=i;j,green,thick] (stk1.two east) -- +(1em,0) |- (stk2.two west); 241 | \draw [->,red,thick] (stk2.two east) -- +(1em,0) node [fill=white, yshift=.9em] {$\times$} |- ($(stk3.one west) - (0,1.6em)$); 242 | \end{tikzpicture} 243 | } 244 | \item Are non-local jumps useful? One of the uses: unexpected error occuring inside of many levels of recursion. Jump to 245 | the outer-most related caller function. \structure{Exceptions} 246 | \end{itemize} 247 | \end{frame} 248 | 249 | \section{Exceptions} 250 | \begin{frame} 251 | \frametitle{Exceptions} 252 | \begin{itemize} 253 | \item Controlled jumps out of multiple levels of function calls to an outer control 254 | point (\texttt{handler} or \texttt{catch}) 255 | \item C does not have exceptions but non-local jumps possible via \texttt{setjmp()}, 256 | \texttt{longjmp()} library calls. 257 | \item C++ and Java: \texttt{try \{...\} catch(...) \{...\}} 258 | \item Each \texttt{try-catch} block introduces a non-local jump point. \texttt{try} block 259 | is executed and whenever a \texttt{throw {\em expr\/}} command is called in any functions 260 | called (even indirectly) inside \texttt{try} block execution jumps to the 261 | \texttt{catch()} part. 262 | \item \texttt{try-catch} blocks can be nested. Execution jumps to closes 263 | \texttt{catch} block with a matching type in the parameters with the thrown expression. 264 | \end{itemize} 265 | \end{frame} 266 | 267 | \defverbatim[colored]\codeerrorhandling{ 268 | \begin{lstlisting}[language={C},escapechar=\#] 269 | ... 270 | int searchopen(char *f) { ... 271 | /* if search fails #\alert{error occurs here}#*/ 272 | return -5;#\R{e1}{}# 273 | ...} 274 | int openparse(char *f) { ... 275 | if ((r = searchopen(f))<0)#\R{e2}{}# 276 | return r;#\R{e3}{}# 277 | else ... 278 | } 279 | int main() { ... 280 | if ((rv=openparse("file.txt"))<0) {#\R{e4}{}# 281 | /*#\alert{handle error here }#*/ 282 | ... 283 | } 284 | \end{lstlisting}} 285 | \begin{frame} 286 | \begin{itemize} 287 | \item Conventional error handling. Propagate errors with return values. 288 | \begin{beamercolorbox}{cexample} 289 | \codeerrorhandling 290 | \end{beamercolorbox} 291 | \begin{tikzpicture} [remember picture, overlay, thick] 292 | \draw [->,blue] (e1) -- +(4,0) |- (e2); 293 | \draw [->,blue] (e3) -- +(5,0) |- (e4); 294 | \end{tikzpicture} 295 | \end{itemize} 296 | \end{frame} 297 | 298 | \defverbatim[colored]\codeerrorexcep{ 299 | \begin{lstlisting}[language={C++},escapechar=\#] 300 | ... 301 | enum Exception { NOTFOUND, ..., PERMS}; 302 | void searchopen(char *f) { ... 303 | /* if open fails #\alert{error occurs here}#*/ 304 | throw PERMS;#\R{ee1}{}# 305 | ...} 306 | void openparse(char *f) { ... 307 | searchopen(f); 308 | ... 309 | } 310 | int main() { ... 311 | try {... 312 | openparse("file.txt"); 313 | ... 314 | } catch(Exception e) {#\R{ee4}{}# 315 | /*#\alert{handle error here }#*/ 316 | } 317 | ... 318 | } 319 | \end{lstlisting}} 320 | \begin{frame} 321 | \begin{itemize} 322 | \item Error handling with \texttt{try-catch}. (based on run-time stack) 323 | \begin{beamercolorbox}{cexample} 324 | \codeerrorexcep 325 | \end{beamercolorbox} 326 | \tikz [remember picture, overlay,blue] \draw [->,thick] (ee1) -- +(4,0) |- (ee4); 327 | \end{itemize} 328 | \end{frame} 329 | 330 | \defverbatim[colored]\codeexceptype{ 331 | \begin{lstlisting}[language={C++},escapechar=\#] 332 | int main() {... try { C1; f#\R{tf}{}#() ; C2 } catch (double#\R{cdoub}{}# a) {...}} 333 | 334 | 335 | void f#\R{ff}{}#() {...; try {...; g#\R{tg}{}#() ; ... } catch (int#\R{cint}{}# a) {...} } 336 | 337 | 338 | void g#\R{fg}{}#() {...; throw 4#\R{tint}{}#; ... ; throw 1.5#\R{tdoub}{}#; ...} 339 | \end{lstlisting}} 340 | \begin{frame} 341 | Nested exceptions are handled based on types. C++: 342 | \begin{beamercolorbox}{cexample} 343 | \codeexceptype 344 | \end{beamercolorbox} 345 | \begin{tikzpicture} [remember picture,overlay, thick] 346 | \draw [->,blue] (tf) -- +(0,-0.5) -| node [fill=blue!5!white,pos=0.2] {\tiny call} (ff); 347 | \draw [->,blue] (tg) -- +(0,-0.4) -| node [fill=blue!5!white,pos=0.2] {\tiny call} (fg); 348 | \draw [->,red] (tint) -- +(0,0.4) -| node [fill=blue!5!white,pos=0.2] {\tiny exception} (cint); 349 | \draw [->,red] (tdoub) -- +(0,1.5) -| node [fill=blue!5!white,pos=0.2] {\tiny exception} (cdoub); 350 | \end{tikzpicture} 351 | 352 | \noindent 353 | In case no handlers found a run time error generated. Program halts. 354 | \end{frame} 355 | 356 | \section{Co-routines} 357 | \begin{frame}[fragile] 358 | \frametitle{Co-routines} 359 | \begin{itemize} 360 | \item Sequential flow: local jumps, subroutine calls, exceptions 361 | \item Concurrent flow: multiple contexes (stack and instruction pointer). Execution switches between them. 362 | \item Multiple uses: \structure{callbacks}, \structure{generators} (iterators), \structure{threads}, \structure{fibers}, \structure{asynchronous}, \structure{event based}, or 363 | \structure{concurrent} programming\\ 364 | \item Non-local jumps to different environments guided coordinated programs or a global scheduling mechanism:\\ 365 | {\tiny \begin{tikzpicture} 366 | [stack/.style={rectangle split, rectangle split parts=4,draw, 367 | text width=10em}, 368 | scolor/.style={rectangle split part fill={white,blue!20!white,blue!20!white,blue!20!white}}] 369 | 370 | \node [stack,scolor,rectangle split parts=3] (stk2) { 371 | \nodepart{one} \rule{0pt}{4.7em} 372 | \nodepart{two} record of g() 373 | \nodepart{three} record of main() 374 | }; 375 | \node [stack,scolor,right=5em of stk2] (stk1) { 376 | \nodepart{one} \rule{0pt}{3em} 377 | \nodepart{two} record of free() 378 | \nodepart{three} record of traverse() 379 | \nodepart{four} record of gc() 380 | }; 381 | \node [stack,scolor, rectangle split parts=3,right=5em of stk1] (stk3) { 382 | \nodepart{one} \rule{0pt}{4.7em}\hspace*{2em} 383 | \nodepart{two} record of mouseevent() 384 | \nodepart{three} record of listener() 385 | }; 386 | \draw [->,blue!40!black,thick] (stk2.two east) -- +(1em,0) node [xshift=2em, yshift=.9em] {\fontsize{2}{2}\selectfont sched()} |- (stk1.two west); 387 | \draw [->,blue!40!black,thick] (stk1.two east) -- +(1em,0) node [xshift=2em, yshift=-.9em] {\fontsize{2}{2}\selectfont sched()} |- (stk3.two west); 388 | \draw [->,blue!40!black,thick] (stk3.two east) -- +(2em,0) -- +(2em,-4em) node [xshift=-20em, yshift=.5em] {\fontsize{2}{2}\selectfont sched()} -| ($(stk2.two west)+(-2em,0)$) -- (stk2.two west); 389 | \end{tikzpicture} 390 | } 391 | 392 | \end{itemize} 393 | \end{frame} 394 | \end{document} 395 | -------------------------------------------------------------------------------- /sources/diagram-light.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onursehitoglu/pllecturenotes/37e988e7c53e628ad4417723c31007d2ec6836d8/sources/diagram-light.pdf -------------------------------------------------------------------------------- /sources/encapsulation.tex: -------------------------------------------------------------------------------- 1 | %\documentclass[compress,dvips,xcolor=table]{beamer} 2 | \usepackage{etex} 3 | %\documentclass{article} 4 | %\usepackage{beamerarticle} 5 | \usepackage{tikz} 6 | \usetikzlibrary{positioning,arrows,matrix,calc,shapes.multipart} 7 | %\usepackage[turkish]{babel} 8 | \usepackage[utf8]{inputenc} 9 | \usepackage{listings} 10 | \usepackage{multicol} 11 | %\includeonlyframes{current} 12 | 13 | \def\circtxt#1{$\mathalpha \bigcirc \mkern-13mu \mathtt #1$} 14 | \def\colorfline#1{\noalign{\color{#1} \hrule height 1pt}} 15 | \def\colorline#1{\cr \noalign{\color{#1} \hrule height 1pt \vskip-3em}} 16 | 17 | \mode
18 | { 19 | \usepackage{fullpage} 20 | \usepackage{pgf} 21 | \usepackage{hyperref} 22 | } 23 | 24 | \mode 25 | { 26 | \usetheme{metuceng} 27 | 28 | % \setbeamercovered{transparent} 29 | } 30 | 31 | 32 | \title{Programming Language Concepts} 33 | \subtitle{Encapsulation} 34 | \author{Onur Tolga Şehitoğlu} 35 | \institute[ODTÜ]{Bilgisayar Mühendisliği} 36 | \subject{Encapsulation} 37 | \date{} 38 | \titlegraphic{\insertmetutitle\insertlicense} 39 | 40 | 41 | \begin{document} 42 | \lstset{language=C, 43 | basicstyle=\scriptsize\ttfamily, 44 | keywordstyle=\color{blue!50!black}\bfseries, 45 | identifierstyle=\color{blue!60!green}\sffamily, 46 | stringstyle=\color{red!70!green}\ttfamily, 47 | commentstyle=\color{blue!30!white}\itshape, 48 | showstringspaces=true} 49 | \setbeamercolor{hexample}{bg=green!5!white,fg=black}% 50 | \setbeamercolor{cexample}{bg=blue!5!white,fg=black}% 51 | \setbeamercolor{pexample}{bg=orange!5!white,fg=black}% 52 | \setbeamercolor{oexample}{bg=violet!5!white,fg=black}% 53 | 54 | \frame[plain]{\maketitle} 55 | \begin{frame} 56 | \frametitle{Outline} 57 | \begin{multicols}{2} 58 | \tableofcontents 59 | \end{multicols} 60 | \end{frame} 61 | 62 | \section{Encapsulation} 63 | \begin{frame} 64 | \frametitle{Encapsulation} 65 | Managing the complexity $\rightarrow$ 66 | Re-usable code and abstraction. Example:\\ 67 | \rowcolors[]{1}{blue!10}{blue!5} 68 | \begin{tabular}{lp{.6\linewidth}} 69 | 50 lines & no abstraction is essential, all in main() \\ 70 | 500 lines & function/procedure abstraction sufficient\\ 71 | 5,000 lines & function groups forming modules, modules are combined to form the application\\ 72 | 500,000 lines & heavy abstraction and modularization, all parts designed for reuse 73 | (libraries, components etc) 74 | \end{tabular} 75 | \end{frame} 76 | 77 | \begin{frame} 78 | \frametitle{Modularization and Encapsulation} 79 | \begin{itemize}[<+->] 80 | \item Building an independent and self complete set of function and variable declarations 81 | (\structure{Packaging}) 82 | \item Restricting access to this set only via a set of interface function and variables. 83 | (\structure{Hiding and Encapsulation}) \\[2em] 84 | \end{itemize} 85 | \begin{tikzpicture}[private/.style = {black,thick}, public/.style = {black,thick}, 86 | pubcall/.style = {transparent}, pubtext/.style = {transparent},pricall/.style={transparent}, 87 | pritext/.style={transparent} ] 88 | \only<2->{\tikzset{private/.style={red,thick}, public/.style = {green,thick}}} 89 | \only<3-3>{\tikzset{pubcall/.style={green,draw,thick},pubtext/.style={green,fill=white}, 90 | pricall/.style={red,draw,thick},pritext/.style={red,fill=white}}} 91 | \node [rectangle split, rectangle split parts=2, text width=9em,rectangle split draw splits=true] (box) { 92 | interface 93 | \nodepart{two} 94 | detail \\ 95 | \tiny ( functions, variables, algorithm) }; 96 | \node [right of=box,xshift=5cm] (other) {other application}; 97 | \draw (box.text split west) -- (box.text split east); 98 | \draw [private] (box.north west) -- (box.south west) -- (box.south east) -- (box.north east); 99 | \draw [public] (box.north west) -- (box.north east); 100 | \path [pubcall,->] (other) -- +(0,1) -| node [pubtext,pos=0.3] {$\surd$} (box.mid); 101 | \path [pricall,->,bend left=10] (other) to node [pritext] {$\times$} ($(box.two) +(2cm,-2mm)$); 102 | \end{tikzpicture} 103 | \end{frame} 104 | 105 | \begin{frame} 106 | \frametitle{Advantages of Encapsulation} 107 | \begin{itemize} 108 | \item High volume details reduced to interface definitions (\structure{Ease of 109 | development/maintenance}) 110 | \item Many different applications use the same module via the same interface (\structure{ 111 | Code re-usability}) 112 | \item Lego like development of code with building blocks (\structure{Ease of 113 | development/maintenance}) 114 | \item Even details change, applications do not change (as long as interface is kept same) (\structure{Ease of 115 | development/maintenance}) 116 | \item Module can be used in following projects (\structure{Code re-usability}) 117 | \end{itemize} 118 | 119 | \end{frame} 120 | 121 | \defverbatim[colored]\codenamespCpp{ 122 | \begin{lstlisting}[language={C++},escapechar=\#] 123 | namespace Trig { 124 | const double pi=3.14159265358979; 125 | double sin(double x) { ... } 126 | double cos(double x) { ... } 127 | double tan(double x) { ... } 128 | double atan(double x) { ... } 129 | ... 130 | }; 131 | \end{lstlisting}} 132 | \section{Packages} 133 | \frametitle{Packages} 134 | \begin{frame} 135 | \begin{itemize} 136 | \item A group of declarations put into a single body. 137 | \item C has indirect way of packaging per source file. Python defines modules per source file. 138 | \item C++\\ 139 | \begin{beamercolorbox}{cexample} 140 | \codenamespCpp 141 | \end{beamercolorbox} 142 | \item \texttt{\small Trig::sin(Trig::pi/2+x)+Trig::cos(x)} 143 | \item C++: (\structure{::}) Scope operator. 144 | \item Identifier overlap is avoided. \texttt{List::insert(...)} and \texttt{Tree::insert(...)} 145 | no name collisions. 146 | \end{itemize} 147 | \end{frame} 148 | 149 | \defverbatim[colored]\codehideH{ 150 | \begin{lstlisting}[language={Haskell},escapechar=\#] 151 | {-- only sin, pi and cos are accessible --} 152 | module Trig(#\color{red}{\tt sin,pi,cos}#) where 153 | taylorseries x = ... 154 | sin x = ... 155 | pi=3.14159265358979 156 | randomseed= ... 157 | cos x = ... 158 | errorcorrect x = ... 159 | \end{lstlisting}} 160 | \section{Hiding} 161 | \begin{frame} 162 | \frametitle{Hiding} 163 | \begin{itemize} 164 | \item A group of functions and variables hidden inside. The others are interface. 165 | Abstraction inside of a package:\\ 166 | {\scriptsize 167 | \begin{tabular}{|>{\tt}l|} \hline 168 | \color{gray}double taylorseries(double); \\ 169 | double sin(double x); \\ 170 | double pi=3.14159265358979; \\ 171 | \color{gray}double randomseed; \\ 172 | double cos(double x); \\ 173 | \color{gray}double errorcorrect(double x); \\ \hline 174 | \end{tabular}} 175 | \begin{beamercolorbox}{hexample} 176 | \codehideH 177 | \end{beamercolorbox} 178 | 179 | \end{itemize} 180 | \end{frame} 181 | 182 | \section{Abstract Data Types} 183 | \begin{frame} 184 | \frametitle{Abstract data types} 185 | \begin{itemize} 186 | \item Internals of the datatype is hidden and only interface functions provide the 187 | access. 188 | \item Example: rational numbers: 3/4 , 2/5, 19/3 \\ 189 | \texttt{data Rational = Rat (Integer,Integer) \\ 190 | x = Rat (3,4)\\ 191 | add (Rat(a,b)) (Rat(c,d)) = Rat (a*d+b*c,b*d)} \begin{enumerate} 192 | \item \alert{Invalid value?} \texttt{Rat (3,0)} 193 | \item \alert{Multiple representations of the same value?} \\ \texttt{Rat (2,4) = Rat (1,2) = Rat(3,6)} 194 | \end{enumerate} 195 | \item Solution: avoid arbitrary values by the user. 196 | \end{itemize} 197 | \end{frame} 198 | 199 | \defverbatim[colored]\codeabstypeH{ 200 | \begin{lstlisting}[language={Haskell},escapechar=\#] 201 | module Rational(#\color{red}{Rational,rat,add,subtract,multiply,divide}#) where 202 | data Rational = #\color{gray}{Rat}# (Integer,Integer) 203 | rat (x,y) = simplify (Rat(x,y)) 204 | add (Rat(a,b)) (Rat(c,d)) = rat (a*d+b*c,b*d) 205 | subtract(Rat(a,b)) (Rat(c,d)) = rat (a*d-b*c,b*d) 206 | multiply(Rat(a,b)) (Rat(c,d)) = rat (a*c,b*d) 207 | divide (Rat(a,b)) (Rat(c,d)) = rat (a*d,b*c) 208 | #\color{gray}{gcd}# x y = if (x==0) then y 209 | else if (y==0) then x 210 | else if (x