Skip to content

Commit c08394b

Browse files
committed
docs(fun_ty.mli): Refine the documentation of fun_ty
1 parent dd44150 commit c08394b

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

src/ppx-metaquot/fun_ty.mli

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,107 +27,123 @@
2727
Alternatively: [3 @: "word" @:!! false] *)
2828
type ('arrow, 'uarrow, 'ret) args
2929

30-
(** [last e], or equivalently [!! e], builds a one-element argument list *)
30+
(** [last e], or equivalently [!! e], builds a one-element argument list. *)
3131
val last :
3232
'a ->
3333
('a -> 'ret, 'a -> unit, 'ret) args
3434

3535
(** [arg a l], or equivalently [a @: l], adds [a] in front of the
36-
argument list [l] *)
36+
argument list [l]. *)
3737
val arg :
3838
'a ->
3939
('ar -> 'row, 'ar -> 'urow, 'ret) args ->
4040
('a -> 'ar -> 'row, 'a -> 'ar -> 'urow, 'ret) args
4141

42-
(** Helper notation for [last] *)
42+
(** Helper notation for [last]. *)
4343
val (!!) :
4444
'a ->
4545
('a -> 'ret, 'a -> unit, 'ret) args
4646

47-
(** Helper notation for [arg] *)
47+
(** Helper notation for [arg]. *)
4848
val (@:) :
4949
'a ->
5050
('ar -> 'row, 'ar -> 'urow, 'ret) args ->
5151
('a -> 'ar -> 'row, 'a -> 'ar -> 'urow, 'ret) args
5252

53-
(** [a @:!! l] is another notation for [a @: !! l] (with a space) *)
53+
(** [a @:!! l] is another notation for [a @: !! l] (with a space). *)
5454
val (@:!!) :
5555
'a -> 'b ->
5656
('a -> 'b -> 'ret, 'a -> 'b -> unit, 'ret) args
5757

58-
(** [apply f l] applies a n-ary function [f] to the arguments from [l] *)
58+
(** [apply f l] applies a n-ary function [f] to the arguments from [l]. *)
5959
val apply :
6060
('ar -> 'row) -> ('ar -> 'row, 'ar -> 'urow, 'ret) args ->
6161
'ret
6262

6363
(** GADT for function types.
6464
65-
Given an arrow type ['a -> 'row], the following construct provides
66-
a more precise representation of this function type than
67-
[[%ty: 'a -> 'row] : ('a -> 'row) Ty.ty]:
65+
This abstract type fulfills a similar aim as the ['a Ty.ty] type:
66+
provide a type-safe way to build terms representing an OCaml type
67+
(relying on [Parsetree.core_type]), the type of these terms being
68+
themselves parameterized by the type at stake, in order to
69+
constrain the type of other arguments involved in the considered
70+
(grader) expression.
6871
69-
[[%funty: 'a -> 'row] : (('a -> 'row) Ty.ty, 'a -> 'urow, 'ret) fun_ty]
72+
For ['a Ty.ty], this aim can be achived with the ppx expression
73+
[[%ty: int]], which builds an abstract term of type [int Ty.ty]
74+
(this term also gathering an appropriate encoding of the [int]
75+
type in terms of [Parsetree.core_type]).
7076
71-
In particular, the codomain type ['ret] is made explicit, so that
72-
if ['row = 'b -> 'c], we get ['urow = 'b -> unit] and ['ret = 'c].
77+
For implementing n-ary graders, this information is not sufficient
78+
in practice, notably as we need to make it explicit in the type
79+
annotation, what is the co-domain of the n-ary function at stake.
7380
74-
Usage: [arg_ty [%ty: int] @@ arg_ty [%ty: string] @@
75-
last_ty [%ty: bool] [%ty: unit]]
81+
This is achieved by the GADT
82+
[(('ar -> 'row) Ty.ty, 'ar -> 'urow, 'ret) fun_ty].
7683
77-
Alternatively: [[%funty: int -> string -> bool -> unit]] *)
84+
There are two ways to build terms of this type:
85+
86+
1. Use the two functions [arg_ty], [last_ty], and appropriate
87+
[[%ty: type]] expressions, for example:
88+
89+
[arg_ty [%ty: int] @@
90+
last_ty [%ty: string] [%ty: bool]];
91+
92+
2. Use directly the [[%funty: 'ar -> 'row]] construct, for example:
93+
94+
[[%funty: int -> string -> bool]].
95+
96+
For both cases in this example, we get a term of type:
97+
98+
[((int -> string -> bool) Ty.ty, int -> string -> unit, bool) fun_ty],
99+
100+
where the co-domain type [bool] is now explicit. *)
78101
type ('arrow, 'uarrow, 'ret) fun_ty
79102

80-
(** [last_ty [%ty: a] [%ty: r]] builds a function type for [a -> r] *)
103+
(** [last_ty [%ty: a] [%ty: r]] builds a function type for [a -> r]. *)
81104
val last_ty :
82105
'a Ty.ty ->
83106
'ret Ty.ty ->
84107
(('a -> 'ret) Ty.ty, 'a -> unit, 'ret) fun_ty
85108

86109
(** [arg_ty [%ty: a] [%funty: b ->...-> r]] builds a function type for
87-
[a -> b ->...-> r] *)
110+
[a -> b ->...-> r]. *)
88111
val arg_ty :
89112
'a Ty.ty ->
90113
(('ar -> 'row) Ty.ty, 'ar -> 'urow, 'ret) fun_ty ->
91114
(('a -> 'ar -> 'row) Ty.ty, ('a -> 'ar -> 'urow), 'ret) fun_ty
92115

93116
(** [ty_of_fun_ty funty] returns a term of type [('ar -> 'row) Ty.ty],
94-
assuming [funty : (('ar -> 'row) Ty.ty, _, _) fun_ty] *)
117+
assuming [funty : (('ar -> 'row) Ty.ty, _, _) fun_ty]. *)
95118
val ty_of_fun_ty :
96119
(('ar -> 'row) Ty.ty, 'ar -> 'urow, 'ret) fun_ty ->
97120
('ar -> 'row) Ty.ty
98121

99-
(** [get_ret_ty funty] returns a term of type ['ret Ty.ty], assuming
100-
[funty : (_ , _, 'ret) fun_ty] *)
122+
(** [get_ret_ty ty l] returns a term of type ['ret Ty.ty] such that if
123+
[ty : ('ar -> 'row) Ty.ty] and [l] contains n arguments, ['ar -> 'row]
124+
is the arrow type of an n-argument function with co-domain ['ret]. *)
101125
val get_ret_ty :
102-
('p -> 'a) Ty.ty -> ('p -> 'a, 'p -> 'c, 'ret) args -> 'ret Ty.ty
103-
126+
('ar -> 'row) Ty.ty -> ('ar -> 'row, 'ar -> 'urow, 'ret) args -> 'ret Ty.ty
104127

105-
(** Signature [S] is intended to be instantiated in [Test_lib] with:
106-
[module M = struct
107-
let typed_printer ty ppf v = Introspection.print_value ppf v ty
108-
let typed_sampler = Introspection.get_sampler
109-
end] *)
110128
module type S = sig
111-
val typed_printer :
112-
'a Ty.ty -> Format.formatter -> 'a -> unit
113-
val typed_sampler :
114-
'a Ty.ty -> unit -> 'a
129+
val typed_printer : 'a Ty.ty -> Format.formatter -> 'a -> unit
130+
val typed_sampler : 'a Ty.ty -> unit -> 'a
115131
end
116132

117-
(** [Make(M)] provides a generic printer and sampler for the arguments
118-
of n-ary functions specified using [args] and [fun_ty] GADTs *)
133+
(** [Make], used in [Test_lib], provides a generic printer and sampler
134+
for argument lists of n-ary functions, depending on their type. *)
119135
module Make : functor (M : S) -> sig
120136
val print :
121-
(('p -> 'a) Ty.ty, 'p -> 'c, 'r) fun_ty ->
122-
Format.formatter -> ('p -> 'a, 'p -> 'c, 'r) args -> unit
137+
(('ar -> 'row) Ty.ty, 'ar -> 'urow, 'ret) fun_ty ->
138+
Format.formatter -> ('ar -> 'row, 'ar -> 'urow, 'ret) args -> unit
123139
val get_sampler :
124-
(('p -> 'a) Ty.ty, 'p -> 'c, 'r) fun_ty ->
125-
unit -> ('p -> 'a, 'p -> 'c, 'r) args
140+
(('ar -> 'row) Ty.ty, 'ar -> 'urow, 'ret) fun_ty ->
141+
unit -> ('ar -> 'row, 'ar -> 'urow, 'ret) args
126142
end
127143

128144
(** [apply_args_1], [apply_args_2], [apply_args3], [apply_args_4] are
129145
variants of the [apply] function, assuming a fixed number of args;
130-
they have thus a more precise type and are used in [Test_lib] *)
146+
they have thus a more precise type and are used in [Test_lib]. *)
131147
val apply_args_1 :
132148
('a -> 'b) -> ('a -> 'c, 'a -> unit, 'c) args -> 'b
133149

0 commit comments

Comments
 (0)