Skip to content

Commit

Permalink
fix: Be more precise on the definition and lookup of samplers
Browse files Browse the repository at this point in the history
  • Loading branch information
AltGr committed Nov 3, 2023
1 parent c61a4d0 commit 7825a6b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/grader/grading.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ let get_grade
js = OCamlRes.(Res.find (Path.of_string "testing_dyn.js")
Embedded_grading_lib.root) };
handle_error (internal_error [%i"while preparing the tests"]) @@
Toploop_ext.use_string ~print_outcome:false ~ppf_answer {|open! Test_lib|};
handle_error (internal_error [%i"while preparing the tests"]) @@
Toploop_ext.use_string ~print_outcome:false ~ppf_answer {|open! Test_lib.Open_me|};
(* Registering the samplers that may be defined in [test.ml] requires
having their types and the definitions of the types they sample, hence
Expand Down
23 changes: 18 additions & 5 deletions src/grader/introspection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,31 @@ let register_sampler name f =
in
try sampler_path, Env.find_value sampler_path lookup_env
with Not_found ->
Env.find_value_by_name (Longident.Lident sampler_name)
lookup_env
let sampler_path =
Path.Pdot (Path.Pdot (Path.Pident (Ident.create_persistent "Test_lib"),
"Sampler_reg"),
sampler_name)
in
sampler_path, Env.find_value sampler_path lookup_env
(* Env.find_value_by_name (Longident.Lident sampler_name)
* lookup_env *)
with
| exception Not_found ->
Format.ksprintf failwith "Bad sampler registration (function %s not found).@."
sampler_name
| _sampler_path, sampler_desc ->
| sampler_path, sampler_desc ->
match
Env.find_type_by_name (Longident.Lident name) lookup_env
let ty_path = match sampler_path with
| Path.Pdot (pp, _) -> Path.Pdot (pp, name)
| _ -> raise Not_found
in
try ty_path, Env.find_type ty_path lookup_env
with Not_found ->
Env.find_type_by_name (Longident.Lident name) lookup_env
with
| exception Not_found ->
Format.eprintf "Warning: unrecognised sampler definition (type %s not found).@."
Format.eprintf
"Warning: unrecognised sampler definition (type %s not found).@."
name
| sampled_ty_path, sampled_ty_decl ->
let sampler_ty_expected =
Expand Down
3 changes: 3 additions & 0 deletions src/grader/introspection_intf.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module type INTROSPECTION = sig
val grab_stderr: unit -> unit
val release_stderr: unit -> string

(* The sampler type is actually [['x sampler ->]* t sampler] with ['x] all the
type variables of [t]. It is dynamically checked at runtime, based on the
cmi of the module that must be already loaded and opened. *)
val register_sampler: string -> ('a -> 'b) -> unit
val get_sampler: 'a Ty.ty -> (unit -> 'a)

Expand Down
15 changes: 15 additions & 0 deletions src/grader/test_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,21 @@ module Intro = Pre_test.Introspection
let ty = Typetexp.transl_type_scheme !Toploop.toplevel_env (Ty.obj [%ty: _ -> _ ]) in
Intro.install_printer path ty.Typedtree.ctyp_type fun_printer
end
module Sampler_reg = struct
include Sampler
let () = Intro.register_sampler "bool" sample_bool
let () = Intro.register_sampler "int" sample_int
let () = Intro.register_sampler "float" sample_float
let () = Intro.register_sampler "char" sample_char
let () = Intro.register_sampler "string" sample_string
let () = Intro.register_sampler "option" sample_option
let sample_array sample () = sample_array sample ()
let () = Intro.register_sampler "array" sample_array
let sample_list sample () = sample_list sample ()
let () = Intro.register_sampler "list" sample_list
type ('a, 'b) pair = 'a * 'b
let () = Intro.register_sampler "pair" sample_pair
end
let (@@@) f g = fun x -> f x @ g x
let (@@>) r1 f = if snd (Learnocaml_report.result r1) then r1 else f ()
Expand Down
15 changes: 15 additions & 0 deletions src/grader/test_lib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,21 @@
val printable_fun : string -> (_ -> _ as 'f) -> 'f
end

(** For internal use, needed for the default samplers registration *)
module Sampler_reg : sig
type 'a sampler = 'a Sampler.sampler
val sample_int : int sampler
val sample_float : float sampler
val sample_string : string sampler
val sample_char : char sampler
val sample_bool : bool sampler
val sample_list : 'a sampler -> 'a list sampler
val sample_array : 'a sampler -> 'a array sampler
val sample_option : 'a sampler -> 'a option sampler
type ('a, 'b) pair = 'a * 'b
val sample_pair : 'a sampler -> 'b sampler -> ('a, 'b) pair sampler
end

(** {1 Grading functions for references and variables } *)

(** Grading function for variables and references. *)
Expand Down

0 comments on commit 7825a6b

Please sign in to comment.