|
| 1 | +# Step 8: Reusing the grader code |
| 2 | + |
| 3 | +This step explains how to separate the grader code, and eventually reuse it in |
| 4 | +other exercises. |
| 5 | + |
| 6 | +During the grading, the file **test.ml** is evaluated in an environment that |
| 7 | +contains notably: |
| 8 | +- **prelude.ml** and **prepare.ml** ; |
| 9 | +- the student code isolated in a module `Code` ; |
| 10 | +- **solution.ml** in a module `Solution` ; |
| 11 | +- the grading modules **Introspection**, **Report** and **Test_lib**. |
| 12 | + |
| 13 | +### Separating the grader code |
| 14 | + |
| 15 | +It is possible to extend this environment by declaring some other user-defined |
| 16 | +modules in an optional file **depend.txt**, located in the exercise directory. |
| 17 | + |
| 18 | +Each declaration in **depend.txt** is a single line containing the relative path |
| 19 | +of an *.ml* or *.mli* file. The order of the *.ml* declarations specifies the |
| 20 | +order in which each module is loaded in the grading environment. |
| 21 | + |
| 22 | +By default each dependency *foo.ml* is isolated in a module *Foo*, which can be |
| 23 | +constrained by the content of an optional signature file *foo.mli*. Furthermore, |
| 24 | +an annotation `[@@@included]` can be used at the beginning of a file *foo.ml* to |
| 25 | +denote that all the bindings of *foo.ml* are evaluated in the toplevel |
| 26 | +environment (and not in a module *Foo*). |
| 27 | + |
| 28 | +Dependencies that are not defined at the root of the exercise repository are |
| 29 | +ignored by the build system: therefore, if you modify them, do not forget to |
| 30 | +refresh the timestamp of `test.ml` (using `touch` for instance). |
| 31 | + |
| 32 | +### A complete example |
| 33 | + |
| 34 | +Let's write an exercise dedicated to *Peano numbers*. Here is the structure of |
| 35 | +the exercise: |
| 36 | + |
| 37 | +``` |
| 38 | +. |
| 39 | +├── exercises |
| 40 | +│ ├── index.json |
| 41 | +│ └── lib |
| 42 | +│ │ ├── check.ml |
| 43 | +│ │ └── check.mli |
| 44 | +│ ├── peano |
| 45 | +│ │ ├── depend.txt |
| 46 | +│ │ ├── descr.md |
| 47 | +│ │ ├── meta.json |
| 48 | +│ │ ├── prelude.ml |
| 49 | +│ │ ├── prepare.ml |
| 50 | +│ │ ├── solution.ml |
| 51 | +│ │ ├── template.ml |
| 52 | +│ │ ├── test.ml |
| 53 | +│ │ └── tests |
| 54 | +│ │ ├── samples.ml |
| 55 | +│ │ ├── add.ml |
| 56 | +│ │ └── odd_even.ml |
| 57 | +│ ├── an-other-exercise |
| 58 | +│ │ ├── depend.txt |
| 59 | +│ │ │ ... |
| 60 | +``` |
| 61 | +The exercise **peano** follows the classical format : **prelude.ml**, |
| 62 | +**prepare.ml**, **solution.ml**, **template.ml** and **test.ml**. |
| 63 | +It also includes several dependencies (**check.ml**, **samples.ml**, **add.ml** |
| 64 | +and **odd_even.ml**) which are declared as follows in **depend.txt**: |
| 65 | + |
| 66 | +```txt |
| 67 | +../lib/check.mli |
| 68 | +../lib/check.ml # a comment |
| 69 | +
|
| 70 | +tests/samples.ml |
| 71 | +tests/add.ml |
| 72 | +tests/odd_even.ml |
| 73 | +``` |
| 74 | + |
| 75 | +Here is in details the source code of the exercise : |
| 76 | + |
| 77 | +- **descr.md** |
| 78 | + |
| 79 | +> * implement the function `add : peano -> peano -> peano` ; |
| 80 | +> * implement the functions `odd : peano -> bool` and `even : peano -> bool`. |
| 81 | +
|
| 82 | +- **prelude.ml** |
| 83 | +```ocaml |
| 84 | +type peano = Z | S of peano |
| 85 | +``` |
| 86 | + |
| 87 | +- **solution.ml** |
| 88 | +```ocaml |
| 89 | +let rec add n = function |
| 90 | +| Z -> n |
| 91 | +| S m -> S (add n m) |
| 92 | +
|
| 93 | +let rec odd = function |
| 94 | +| Z -> false |
| 95 | +| S n -> even n |
| 96 | +and even = function |
| 97 | +| Z -> true |
| 98 | +| S n -> odd n |
| 99 | +``` |
| 100 | + |
| 101 | +- **test.ml** |
| 102 | +```ocaml |
| 103 | +let () = |
| 104 | +Check.safe_set_result [ Add.test ; Odd_even.test ] |
| 105 | +``` |
| 106 | + |
| 107 | +Note that **test.ml** is very compact because it simply combines functions |
| 108 | +defined in separated files. |
| 109 | + |
| 110 | +- **../lib/check.ml**: |
| 111 | +```ocaml |
| 112 | +open Test_lib |
| 113 | +open Report |
| 114 | +
|
| 115 | +let safe_set_result tests = |
| 116 | +set_result @@ |
| 117 | +ast_sanity_check code_ast @@ fun () -> |
| 118 | +List.mapi (fun i test -> |
| 119 | +Section ([ Text ("Question " ^ string_of_int i ^ ":") ], |
| 120 | +test ())) tests |
| 121 | +``` |
| 122 | +- **../lib/check.mli**: |
| 123 | +```ocaml |
| 124 | +val safe_set_result : (unit -> Report.t) list -> unit |
| 125 | +``` |
| 126 | + |
| 127 | +- **tests/add.ml**: |
| 128 | +```ocaml |
| 129 | +let test () = |
| 130 | +Test_lib.test_function_2_against_solution |
| 131 | +[%ty : peano -> peano -> peano ] "add" |
| 132 | +[ (Z, Z) ; (S(Z), S(S(Z))) ] |
| 133 | +``` |
| 134 | +- **tests/odd_even.ml** : |
| 135 | +```ocaml |
| 136 | +let test () = |
| 137 | +Test_lib.test_function_1_against_solution |
| 138 | +[%ty : peano -> bool ] "odd" |
| 139 | +[ Z ; S(Z) ; S(S(Z)) ] |
| 140 | +@ |
| 141 | +Test_lib.test_function_1_against_solution |
| 142 | +[%ty : peano -> bool ] "even" |
| 143 | +[ Z ; S(Z) ; S(S(Z)) ] |
| 144 | +``` |
| 145 | +Remember that **Test_lib** internally requires a user-defined sampler |
| 146 | +`sample_peano : unit -> peano` to generate value of type `peano`. This sampler |
| 147 | +has to be present in the toplevel environment -- and not in a module -- in order |
| 148 | +to be found by the introspection primitives during grading. Therefore, |
| 149 | +we define this sampler in a file starting with the annotation `[@@@included]`. |
| 150 | +- **tests/samples.ml**: |
| 151 | +```ocaml |
| 152 | +[@@@included] |
| 153 | +
|
| 154 | +let sample_peano () = |
| 155 | +let rec aux = function |
| 156 | +| 0 -> Z |
| 157 | +| n -> S (aux (n-1)) |
| 158 | +in aux (Random.int 42) |
| 159 | +``` |
| 160 | + |
| 161 | +Finally, the content of **test.ml** will be evaluated in the following |
| 162 | +environment: |
| 163 | + |
| 164 | +```ocaml |
| 165 | +val print_html : 'a -> 'b |
| 166 | +type peano = Z | S of peano |
| 167 | +module Code : |
| 168 | +sig |
| 169 | +val add : peano -> peano -> peano |
| 170 | +val odd : peano -> bool |
| 171 | +val even : peano -> bool |
| 172 | +end |
| 173 | +module Solution : |
| 174 | +sig |
| 175 | +val add : peano -> peano -> peano |
| 176 | +val odd : peano -> bool |
| 177 | +val even : peano -> bool |
| 178 | +end |
| 179 | +module Test_lib : Test_lib.S |
| 180 | +module Report = Learnocaml_report |
| 181 | +module Check : sig val check_all : (unit -> Report.t) list -> unit end |
| 182 | +val sample_peano : unit -> peano |
| 183 | +module Add : sig val test : unit -> Report.t end |
| 184 | +module Odd_even : sig val test : unit -> Report.t end |
| 185 | +``` |
| 186 | + |
| 187 | +In the end, this feature can provide an increased comfort for writing large a |
| 188 | +utomated graders and for reusing them in other exercises. |
| 189 | + |
| 190 | + |
0 commit comments