Skip to content

Commit 4f21c45

Browse files
author
Matej Kosik
committed
CLEANUP: switching from "right-to-left" to "left-to-right" function composition operator.
Short story: This pull-request: (1) removes the definition of the "right-to-left" function composition operator (2) adds the definition of the "left-to-right" function composition operator (3) rewrites the code relying on "right-to-left" function composition to rely on "left-to-right" function composition operator instead. Long story: In mathematics, function composition is traditionally denoted with ∘ operator. Ocaml standard library does not provide analogous operator under any name. Batteries Included provides provides two alternatives: _ % _ and _ %> _ The first operator one corresponds to the classical ∘ operator routinely used in mathematics. I.e.: (f4 % f3 % f2 % f1) x ≜ (f4 ∘ f3 ∘ f2 ∘ f1) x We can call it "right-to-left" composition because: - the function we write as first (f4) will be called as last - and the function write as last (f1) will be called as first. The meaning of the second operator is this: (f1 %> f2 %> f3 %> f4) x ≜ (f4 ∘ f3 ∘ f2 ∘ f1) x We can call it "left-to-right" composition because: - the function we write as first (f1) will be called first - and the function we write as last (f4) will be called last That is, the functions are written in the same order in which we write and read them. I think that it makes sense to prefer the "left-to-right" variant because it enables us to write functions in the same order in which they will be actually called and it thus better fits our culture (we read/write from left to right).
1 parent 2ff6d31 commit 4f21c45

File tree

20 files changed

+51
-43
lines changed

20 files changed

+51
-43
lines changed

engine/evd.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ let evar_instance_array test_id info args =
246246
instrec filter (evar_context info) 0
247247

248248
let make_evar_instance_array info args =
249-
evar_instance_array (isVarId % NamedDecl.get_id) info args
249+
evar_instance_array (NamedDecl.get_id %> isVarId) info args
250250

251251
let instantiate_evar_array info c args =
252252
let inst = make_evar_instance_array info args in
@@ -684,7 +684,7 @@ let restrict evk filter ?candidates evd =
684684
evar_extra = Store.empty } in
685685
let evar_names = EvNames.reassign_name_defined evk evk' evd.evar_names in
686686
let ctxt = Filter.filter_list filter (evar_context evar_info) in
687-
let id_inst = Array.map_of_list (mkVar % NamedDecl.get_id) ctxt in
687+
let id_inst = Array.map_of_list (NamedDecl.get_id %> mkVar) ctxt in
688688
let body = mkEvar(evk',id_inst) in
689689
let (defn_evars, undf_evars) = define_aux evd.defn_evars evd.undf_evars evk body in
690690
{ evd with undf_evars = EvMap.add evk' evar_info' undf_evars;
@@ -1403,7 +1403,7 @@ let print_env_short env =
14031403
| RelDecl.LocalAssum (n,_) -> pr_name n
14041404
| RelDecl.LocalDef (n,b,_) -> str "(" ++ pr_name n ++ str " := " ++ print_constr b ++ str ")"
14051405
in
1406-
let pr_named_decl = pr_rel_decl % NamedDecl.to_rel_decl in
1406+
let pr_named_decl = NamedDecl.to_rel_decl %> pr_rel_decl in
14071407
let nc = List.rev (named_context env) in
14081408
let rc = List.rev (rel_context env) in
14091409
str "[" ++ pr_sequence pr_named_decl nc ++ str "]" ++ spc () ++

kernel/declareops.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ let subst_const_body sub cb =
147147
themselves. But would it really bring substantial gains ? *)
148148

149149
let hcons_rel_decl =
150-
RelDecl.map_type Term.hcons_types % RelDecl.map_value Term.hcons_constr % RelDecl.map_name Names.Name.hcons
150+
RelDecl.map_name Names.Name.hcons %> RelDecl.map_value Term.hcons_constr %> RelDecl.map_type Term.hcons_types
151151

152152
let hcons_rel_context l = List.smartmap hcons_rel_decl l
153153

kernel/names.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct
8989
| Anonymous -> true
9090
| Name _ -> false
9191

92-
let is_name = not % is_anonymous
92+
let is_name = is_anonymous %> not
9393

9494
let compare n1 n2 = match n1, n2 with
9595
| Anonymous, Anonymous -> 0

kernel/term_typing.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ let record_aux env s_ty s_bo suggested_expr =
262262
String.concat " "
263263
(CList.map_filter (fun decl ->
264264
let id = NamedDecl.get_id decl in
265-
if List.exists (Id.equal id % NamedDecl.get_id) in_ty then None
265+
if List.exists (NamedDecl.get_id %> Id.equal id) in_ty then None
266266
else Some (Id.to_string id))
267267
(keep_hyps env s_bo)) in
268268
Aux_file.record_in_aux "context_used" (v ^ ";" ^ suggested_expr)
@@ -285,7 +285,7 @@ let build_constant_declaration kn env (def,typ,proj,poly,univs,inline_code,ctx)
285285
let sort evn l =
286286
List.filter (fun decl ->
287287
let id = NamedDecl.get_id decl in
288-
List.exists (Names.Id.equal id % NamedDecl.get_id) l)
288+
List.exists (NamedDecl.get_id %> Names.Id.equal id) l)
289289
(named_context env) in
290290
(* We try to postpone the computation of used section variables *)
291291
let hyps, def =

lib/util.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@ let matrix_transpose mat =
8787

8888
let identity x = x
8989

90-
(** Function composition: the mathematical [∘] operator.
90+
(** Left-to-right function composition:
91+
92+
[f1 %> f2] is [fun x -> f2 (f1 x)].
9193
92-
So [g % f] is a synonym for [fun x -> g (f x)].
94+
[f1 %> f2 %> f3] is [fun x -> f3 (f2 (f1 x))].
9395
94-
Also because [%] is right-associative, [h % g % f] means [fun x -> h (g (f x))].
95-
*)
96-
let (%) f g x = f (g x)
96+
[f1 %> f2 %> f3 %> f4] is [fun x -> f4 (f3 (f2 (f1 x)))]
97+
98+
etc.
99+
*)
100+
let (%>) f g x = g (f x)
97101

98102
let const x _ = x
99103

lib/util.mli

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,17 @@ val matrix_transpose : 'a list list -> 'a list list
8484

8585
val identity : 'a -> 'a
8686

87-
(** Function composition: the mathematical [∘] operator.
87+
(** Left-to-right function composition:
88+
89+
[f1 %> f2] is [fun x -> f2 (f1 x)].
8890
89-
So [g % f] is a synonym for [fun x -> g (f x)].
91+
[f1 %> f2 %> f3] is [fun x -> f3 (f2 (f1 x))].
9092
91-
Also because [%] is right-associative, [h % g % f] means [fun x -> h (g (f x))].
93+
[f1 %> f2 %> f3 %> f4] is [fun x -> f4 (f3 (f2 (f1 x)))]
94+
95+
etc.
9296
*)
93-
val (%) : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b
97+
val ( %> ) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
9498

9599
val const : 'a -> 'b -> 'a
96100
val iterate : ('a -> 'a) -> int -> 'a -> 'a

library/impargs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ let impls_of_context ctx =
520520
| Implicit -> Some (NamedDecl.get_id decl, Manual, (true, true))
521521
| _ -> None
522522
in
523-
List.rev_map map (List.filter (is_local_assum % fst) ctx)
523+
List.rev_map map (List.filter (fst %> is_local_assum) ctx)
524524

525525
let adjust_side_condition p = function
526526
| LessArgsThan n -> LessArgsThan (n+p)

library/lib.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ let extract_hyps (secs,ohyps) =
445445
in aux (secs,ohyps)
446446

447447
let instance_from_variable_context =
448-
Array.of_list % List.map NamedDecl.get_id % List.filter is_local_assum % List.map fst
448+
List.map fst %> List.filter is_local_assum %> List.map NamedDecl.get_id %> Array.of_list
449449

450450
let named_of_variable_context =
451451
List.map fst

ltac/rewrite.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ let assert_replacing id newt tac =
15401540
let concl = Proofview.Goal.concl gl in
15411541
let env = Proofview.Goal.env gl in
15421542
let ctx = Environ.named_context env in
1543-
let after, before = List.split_when (Id.equal id % NamedDecl.get_id) ctx in
1543+
let after, before = List.split_when (NamedDecl.get_id %> Id.equal id) ctx in
15441544
let nc = match before with
15451545
| [] -> assert false
15461546
| d :: rem -> insert_dependent env (LocalAssum (NamedDecl.get_id d, newt)) [] after @ rem

plugins/funind/functional_principles_proofs.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,8 @@ let generalize_non_dep hyp g =
940940
((* observe_tac "thin" *) (thin to_revert))
941941
g
942942

943-
let id_of_decl = Nameops.out_name % RelDecl.get_name
944-
let var_of_decl = mkVar % id_of_decl
943+
let id_of_decl = RelDecl.get_name %> Nameops.out_name
944+
let var_of_decl = id_of_decl %> mkVar
945945
let revert idl =
946946
tclTHEN
947947
(Proofview.V82.of_tactic (generalize (List.map mkVar idl)))
@@ -1121,11 +1121,11 @@ let prove_princ_for_struct (evd:Evd.evar_map ref) interactive_proof fun_num fnam
11211121
)
11221122
in
11231123
observe (str "full_params := " ++
1124-
prlist_with_sep spc (Ppconstr.pr_id % Nameops.out_name % RelDecl.get_name)
1124+
prlist_with_sep spc (RelDecl.get_name %> Nameops.out_name %> Ppconstr.pr_id)
11251125
full_params
11261126
);
11271127
observe (str "princ_params := " ++
1128-
prlist_with_sep spc (Ppconstr.pr_id % Nameops.out_name % RelDecl.get_name)
1128+
prlist_with_sep spc (RelDecl.get_name %> Nameops.out_name %> Ppconstr.pr_id)
11291129
princ_params
11301130
);
11311131
observe (str "fbody_with_full_params := " ++
@@ -1279,7 +1279,7 @@ let prove_princ_for_struct (evd:Evd.evar_map ref) interactive_proof fun_num fnam
12791279
(do_replace evd
12801280
full_params
12811281
(fix_info.idx + List.length princ_params)
1282-
(args_id@(List.map (Nameops.out_name % RelDecl.get_name) princ_params))
1282+
(args_id@(List.map (RelDecl.get_name %> Nameops.out_name) princ_params))
12831283
(all_funs.(fix_info.num_in_block))
12841284
fix_info.num_in_block
12851285
all_funs
@@ -1558,7 +1558,7 @@ let prove_principle_for_gen
15581558
| _ -> assert false
15591559
in
15601560
(* observe (str "rec_arg_id := " ++ pr_lconstr (mkVar rec_arg_id)); *)
1561-
let subst_constrs = List.map (mkVar % Nameops.out_name % get_name) (pre_rec_arg@princ_info.params) in
1561+
let subst_constrs = List.map (get_name %> Nameops.out_name %> mkVar) (pre_rec_arg@princ_info.params) in
15621562
let relation = substl subst_constrs relation in
15631563
let input_type = substl subst_constrs rec_arg_type in
15641564
let wf_thm_id = Nameops.out_name (fresh_id (Name (Id.of_string "wf_R"))) in
@@ -1586,7 +1586,7 @@ let prove_principle_for_gen
15861586
)
15871587
g
15881588
in
1589-
let args_ids = List.map (Nameops.out_name % get_name) princ_info.args in
1589+
let args_ids = List.map (get_name %> Nameops.out_name) princ_info.args in
15901590
let lemma =
15911591
match !tcc_lemma_ref with
15921592
| None -> error "No tcc proof !!"
@@ -1633,7 +1633,7 @@ let prove_principle_for_gen
16331633
[
16341634
observe_tac "start_tac" start_tac;
16351635
h_intros
1636-
(List.rev_map (Nameops.out_name % get_name)
1636+
(List.rev_map (get_name %> Nameops.out_name)
16371637
(princ_info.args@princ_info.branches@princ_info.predicates@princ_info.params)
16381638
);
16391639
(* observe_tac "" *) Proofview.V82.of_tactic (assert_by
@@ -1671,7 +1671,7 @@ let prove_principle_for_gen
16711671
in
16721672
let acc_inv = lazy (mkApp(Lazy.force acc_inv, [|mkVar acc_rec_arg_id|])) in
16731673
let predicates_names =
1674-
List.map (Nameops.out_name % get_name) princ_info.predicates
1674+
List.map (get_name %> Nameops.out_name) princ_info.predicates
16751675
in
16761676
let pte_info =
16771677
{ proving_tac =
@@ -1687,7 +1687,7 @@ let prove_principle_for_gen
16871687
is_mes acc_inv fix_id
16881688

16891689
(!tcc_list@(List.map
1690-
(Nameops.out_name % get_name)
1690+
(get_name %> Nameops.out_name)
16911691
(princ_info.args@princ_info.params)
16921692
)@ ([acc_rec_arg_id])) eqs
16931693
)
@@ -1716,7 +1716,7 @@ let prove_principle_for_gen
17161716
(* observe_tac "instanciate_hyps_with_args" *)
17171717
(instanciate_hyps_with_args
17181718
make_proof
1719-
(List.map (Nameops.out_name % get_name) princ_info.branches)
1719+
(List.map (get_name %> Nameops.out_name) princ_info.branches)
17201720
(List.rev args_ids)
17211721
)
17221722
gl'

0 commit comments

Comments
 (0)