programming languages f28pl introductory lecture
play

Programming Languages F28PL,Introductory Lecture 1 st Semester - PowerPoint PPT Presentation

Programming Languages F28PL,Introductory Lecture 1 st Semester 2019/20 1 / 38 Programming Languages F28PL Languages OCaml (functional), weeks 1-4 Python (scripting), weeks 6-9 Prolog (logic), weeks 10-11 Lectures


  1. Programming Languages F28PL,Introductory Lecture 1 st Semester 2019/20 1 / 38

  2. Programming Languages F28PL ◮ Languages ◮ OCaml (functional), weeks 1-4 ◮ Python (scripting), weeks 6-9 ◮ Prolog (logic), weeks 10-11 ◮ Lectures ◮ Thursdays, 11:15, JW2 ◮ Thursday, 15:15, JW2 ◮ Fridays, 15:15, PG G.01 ◮ Labs ◮ Mondays, 11:15, EM 2.50 (not this week) 2 / 38

  3. F28PL Programming Languages course starts next lecture (Thursday morning). This introductory lecture: ◮ Property based tests ◮ Running OCaml code ◮ GitLab setup for this course ◮ Using git at command line 3 / 38

  4. Unit Testing ◮ In SD3 we used unit tests to check implementations assertEquals(12, add(10, 2), "10 + 2 is 12"); ◮ code under test: add method ◮ inputs: 10 and 2 ◮ expected result: 12 ◮ validates if code results in the expected output ◮ checks piece of code is correct More tests: assertEquals(0, add(0, 0), "0 + 0 is 0"); assertEquals(2, add(2, 0), "2 + 0 is 2"); ... ◮ Time consuming to write many tests ◮ How many unit tests until we have confidence in add(..) ? 4 / 38

  5. Declaring Properties of Code ◮ “Any number N added to 0 is N” ◮ “Any number N multiplied by 0 is 0” ◮ “Reversing a list then reversing it again returns the original list” ◮ “Multiplying an even number by 2 returns an even number” ◮ “After sorting an integer list the smallest value is the 1st element of the returned list” ◮ “After performing upheap on a binary tree the smallest value is the root of the tree” (priority queues from SD3) ◮ “After adding N unique words to a trie there are N words in the trie” (from SD3) QuickCheck is a framework that generates tests for these properties. Then runs the tests to find counterexamples. 5 / 38

  6. Property Testing with OCaml (* adding a list to an empty list returns empty list e.g. seqadd [1;2;3] [] = [] *) let seqadd_empty1 = QCheck . Test .make ~name:"seqadd_empty1" ~count:10000 QCheck .(make integer_sequence_gen) ( fun list1 -> seqadd list1 [] = []);; ◮ Generator: integer_sequence_gen generates integer lists ◮ Generator bound to the list1 variable ◮ Property: seqadd list1 [] = [] QuickCheck will generate lots of tests: seqadd [1;5;4;...] [] = [] seqadd [9;2;8;... [] = [] seqadd [8;8;3;... [] = [] 6 / 38

  7. Generators for test inputs OCaml qcheck library has generators: val small_nat : int arbitrary val int_range : int -> int -> int arbitrary val pos_float : float arbitrary val bool : bool arbitrary val char : char arbitrary val string_gen_of_size : int Gen .t -> char Gen .t -> string arbitrary val list : ' a arbitrary -> ' a list arbitrary val pair : ' a arbitrary -> ' b arbitrary -> ( ' a * ' b) arbitrary val oneof : ' a arbitrary list -> ' a arbitrary ... OCaml’s QuickCheck API: https://c-cube.github.io/qcheck/0.9/qcheck-core/QCheck/ https://c-cube.github.io/qcheck/0.9/qcheck-core/QCheck/Gen/ 7 / 38

  8. Generators for test inputs User defined OCaml generators: (* integer sequence generator of 100 elements *) let integer_sequence_gen = QCheck . Gen .(list_size (return 100) nat);; (* zeros sequence generator of 100 elements *) let zeros_gen = QCheck . Gen .(list_size (return 100) (return 0));; (* from the OCaml coursework *) type complex_number = CI of int * int ;; (* complex number generator *) let complex_number_gen = ( QCheck . Gen .map ( fun (x,y) -> CI (x,y)) QCheck . Gen .(pair small_nat small_nat));; 8 / 38

  9. Property Testing with OCaml (* adding 0 to each element in a sequence result in the same sequence, and hence the input sequence should be the same as the output sequence e.g. seqadd [1;2;3] [0;0;0] => [1;2;3] *) let seqadd_zeros = QCheck . Test .make ~name:"seqadd_zeros" ~count:1000 QCheck .(make ~print:show_sequence ( Gen .pair integer_sequence_gen zeros_gen)) ( fun (seq,zeros) -> seqadd seq zeros = seq);; 9 / 38

  10. Property Testing with OCaml ◮ seqmult multiplies every list element by a constant ◮ sum sums up all elements in a list Property: “for all integer lists, if we multiply every element by 0 then sum the resulting list is zero” . let seqmult_zeros = let sum = List .fold_left (+) 0 in QCheck . Test .make ~name:"seqmult_zeros" ~count:1000 QCheck .(make ~print:show_sequence ( Gen .pair integer_sequence_gen zeros_gen)) ( fun (seq,zeros) -> sum (seqmult seq zeros) = 0);; 10 / 38

  11. Property Testing with OCaml Property: “For all lists of generated integer lists, bubble sort and quick sort functions output the same list” let bubble_quick_equiv = QCheck . Test .make ~name:"bubble_quick_equiv" ~count:100 QCheck .(make ( Gen .list Gen .small_nat)) ( fun xs -> bubble_sort xs = quick_sort xs);; ◮ xs comes from (Gen.list Gen.small_nat) ◮ 100 tests generated ◮ Property: bubble_sort xs = quick_sort xs 11 / 38

  12. Property Testing and Functional Programming ◮ Unit testing common for imperative and object oriented languages ◮ Property based testing almost exclusive to functional languages ◮ Why? In (pure) functional languages, the output of a function is exclusively determined by its input values. Quick Check property testing frameworks generate and run 100s/1000s/millions of tests. Imperative/OO languages have global/hidden state: calling the same function/method twice might return a different result. A unit test is ran only once. 12 / 38

  13. Running OCaml Interpreter (ocaml): Read–eval–print loop (REPL) ◮ run functions with different inputs ◮ ask what type values/functions have ◮ slower performance > ocaml OCaml version 4.05.0 # #use "ocaml_part1.ml" ;; 13 / 38

  14. Running OCaml Compiler (ocamlc): compile to native code ◮ can’t test functions interactively ◮ code is optimised ◮ fast performance OCaml file has a main function: let main = print_int (add 2 3) ;; Compile it: > ocamlc -o part1 ocaml_part1.m > ./part1 5 14 / 38

  15. ◮ Text editors ◮ Emacs ◮ Tuareg mode: https://github.com/ocaml/tuareg ◮ Vim ◮ Merlin: https://github.com/ocaml/merlin ◮ gedit ◮ VSCode ◮ vscode-reasonml: https: //github.com/reasonml-editor/vscode-reasonml ◮ Compilers ◮ ocamlc ◮ Interpreters ◮ ocaml ◮ utop ◮ improved ocaml : line editing, history, real-time and context sensitive completion, colors. ◮ opam install dune ◮ https://github.com/ocaml-community/utop 15 / 38

  16. Structure of Ocaml projects ◮ lib directory for implementation ◮ test directory for tests - lib |-> SequenceArithmetic.ml -> dune - test |-> SequenceArithmeticTests.ml -> dune - dune-project https://gitlab-student.macs.hw.ac.uk/f28pl-2019-20/ f28pl-2019-20-ocaml-coursework/tree/master/ sequence-arithmetic 16 / 38

  17. Test files ◮ Ocaml coursework has unit tests and property based tests Unit tests: (* test seqadd on two non-empty lists *) let seqadd_test2 _test_ctxt = assert_equal [7;5;6] (seqadd [2;3;5] [5;2;1]);; Property tests: (* adding an integer list to list of 0's should return the list e.g. seqadd [1;2;3] [0;0;0] => [1;2;3] *) let seqadd_zeros = QCheck . Test .make ~name:"seqadd_zeros" ~count:1000 QCheck .(make ~print:show_sequence ( Gen .pair integer_sequence_gen zeros_gen)) ( fun (seq,zeros) -> seqadd seq zeros = seq);; 17 / 38

  18. Running OCaml Tests > cd sequence-arithmetic/ > dune clean > dune runtest --profile release ... Ran: 12 tests in: 0.11 seconds. FAILED: Cases: 12 Tried: 12 Errors: 5 Failures: 7 Skip: 0 Todo: 0 Timeouts: 0. Test driven development.. the goal: Ran: 12 tests in: 2.28 seconds. OK 18 / 38

  19. GitLab for F28PL https://gitlab-student.macs.hw.ac.uk/f28pl-2019-20 19 / 38

  20. Versioning metadata ◮ Who made changes? ◮ e.g. Rob Stewart � R.Stewart@hw.ac.uk � ◮ What files did they change? ◮ e.g. lib/arithmetic.ml ◮ Where in the file did they make the change? ◮ e.g. line 287 of lib/server.ml ◮ When did they make the change? ◮ e.g. 11:04, Friday 19th October 2018. ◮ Why did they make a change? ◮ e.g. to fix single-sign-on login ◮ Who is affected by the change? ◮ e.g. Paul to add login property based tests 20 / 38

  21. Visualising git History ◮ GitLab website (click “N commmits” for history) ◮ git gui ( gitk command) 21 / 38

  22. Fork repository 22 / 38

  23. Clone your fork 23 / 38

  24. 24 / 38

  25. 25 / 38

  26. Check status (Note: these slides are adapted from 3 rd year course.) $ git status On branch master nothing to commit, working directory clean 26 / 38

  27. $ vim README $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track) 27 / 38

  28. Track new files $ git add README $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README 28 / 38

  29. Staging modified files $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: benchmarks.rb 29 / 38

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend