-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplifier.ml
More file actions
78 lines (70 loc) · 2.65 KB
/
simplifier.ml
File metadata and controls
78 lines (70 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
open Target
let rec size = function
| App (u,v) -> (size u) + (size v)
| _ -> 1
(** [simplify term] returns a [term'] which is equivalent to the argument
* [term] but may be simplified.and a boolean equals to true if term as be
* simplified. *)
(* La première stratégie testée consistait à traduire le term en une liste de
* sous-termes pour aplatir l'arbre avec les composition.
* Mais comme le code produit par le compilier contient déjà un grand nombre
* de simplification, les seuls cas de simplification nécessaires peuvent être
* traités de manière gloutonne avec le pattern-matching qui suit *)
let rec simplify = function
(* simplification de
* (apply o ( curry h /\ g )) => (h o (id /\ g))
*)
| App (
App (Compose (ok0, OkPair (OkArrow (okd, okb),_), _ ), Apply _),
App (App (
Fork _,
App (Curry _, h)),
g)) ->
let id = Identity ok0 in
let _ = if !Options.print_simplification then
Printf.printf "On simplifie un [apply ∘ (curry h Δ g)] -> [h ∘ (id Δ g)]\n"
in
let fork = Fork (ok0, ok0, okd) in
let compose = Compose (ok0, OkPair (ok0, okd), okb) in
App (
App (compose, h),
App (App (fork, id), g)
), true
(* simplification des identités à gauche & à droite *)
| App (App (Compose _, Identity _), u)
| App (App (Compose _, u), Identity _) ->
let _ = if !Options.print_simplification then
Printf.printf "On simplifie un [id ∘ f] -> [f]\n"
in
u, true
(* cas récursifs *)
| App (u, v) ->
let simp_u, modify_u = simplify u in
let simp_v, modify_v = simplify v in
App (simp_u, simp_v), modify_u || modify_v
(* Cas de base *)
| x -> x, false
(** [simplify_iter prog] calls [simplify] as long as it simplifies [prog] *)
let rec simplify_iter prog =
let prog, modify = simplify prog in
if modify then
simplify_iter prog
else
prog
(** [simplify_wrap] is a small wrapper for [simplify_*]. It also prints the
* size of the term before and after simplification *)
let simplify_wrap prog =
let _ = if !Options.print_simplification then
Printf.printf "Avant simplify: %d\n" (size prog) in
let prog = simplify_iter prog in
let _ = if !Options.print_simplification then
Printf.printf "Après simplify: %d\n" (size prog)
in
prog
(** [rewrite defs] applies category laws to remove [apply] and [curry]
from the compiled programs. *)
let rewrite : Target.program -> Target.program = fun defs ->
if !Options.simplify then
List.map (fun (bind, prog) -> bind, simplify_wrap prog) defs
else
defs