Debian Codegraph @

test-suite/output/Extraction_matchs_2413.v

raw
1 (** Extraction : tests of optimizations of pattern matching *)
2
3 Require Corelib.extraction.Extraction.
4
5 (** First, a few basic tests *)
6
7 Definition test1 b :=
8 match b with
9 | true => true
10 | false => false
11 end.
12
13 Extraction test1. (** should be seen as the identity *)
14
15 Definition test2 b :=
16 match b with
17 | true => false
18 | false => false
19 end.
20
21 Extraction test2. (** should be seen a the always-false constant function *)
22
23 Inductive hole (A:Set) : Set := Hole | Hole2.
24
25 Definition wrong_id (A B : Set) (x:hole A) : hole B :=
26 match x with
27 | Hole _ => @Hole _
28 | Hole2 _ => @Hole2 _
29 end.
30
31 Extraction wrong_id. (** should _not_ be optimized as an identity *)
32
33 Definition test3 (A:Type)(o : option A) :=
34 match o with
35 | Some x => Some x
36 | None => None
37 end.
38
39 Extraction test3. (** Even with type parameters, should be seen as identity *)
40
41 Inductive indu : Type := A : nat -> indu | B | C.
42
43 Definition test4 n :=
44 match n with
45 | A m => A (S m)
46 | B => B
47 | C => C
48 end.
49
50 Extraction test4. (** should merge branchs B C into a x->x *)
51
52 Definition test5 n :=
53 match n with
54 | A m => A (S m)
55 | B => B
56 | C => B
57 end.
58
59 Extraction test5. (** should merge branches B C into _->B *)
60
61 Inductive indu' : Type := A' : nat -> indu' | B' | C' | D' | E' | F'.
62
63 Definition test6 n :=
64 match n with
65 | A' m => A' (S m)
66 | B' => C'
67 | C' => C'
68 | D' => C'
69 | E' => B'
70 | F' => B'
71 end.
72
73 Extraction test6. (** should merge some branches into a _->C' *)
74
75 (** NB : In Coq, "| a => a" corresponds to n, hence some "| _ -> n" are
76 extracted *)
77
78 Definition test7 n :=
79 match n with
80 | A m => Some m
81 | B => None
82 | C => None
83 end.
84
85 Extraction test7. (** should merge branches B,C into a _->None *)
86
87 (** Script from bug #2413 *)
88
89 Set Implicit Arguments.
90
91 Section S.
92
93 Definition message := nat.
94 Definition word := nat.
95 Definition mode := nat.
96 Definition opcode := nat.
97
98 Variable condition : word -> option opcode.
99
100 Section decoder_result.
101
102 Variable inst : Type.
103
104 Inductive decoder_result : Type :=
105 | DecUndefined : decoder_result
106 | DecUnpredictable : decoder_result
107 | DecInst : inst -> decoder_result
108 | DecError : message -> decoder_result.
109
110 End decoder_result.
111
112 Definition decode_cond_mode (mode : Type) (f : word -> decoder_result mode)
113 (w : word) (inst : Type) (g : mode -> opcode -> inst) :
114 decoder_result inst :=
115 match condition w with
116 | Some oc =>
117 match f w with
118 | DecInst i => DecInst (g i oc)
119 | DecError _ m => @DecError inst m
120 | DecUndefined _ => @DecUndefined inst
121 | DecUnpredictable _ => @DecUnpredictable inst
122 end
123 | None => @DecUndefined inst
124 end.
125
126 End S.
127
128 Extraction decode_cond_mode.
129 (** inner match should not be factorized with a partial x->x (different type) *)
130