-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrornat.v
More file actions
59 lines (48 loc) · 1.46 KB
/
errornat.v
File metadata and controls
59 lines (48 loc) · 1.46 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
Require Import PeanoNat.
Local Open Scope nat_scope.
Inductive ErrorNat :=
|Error : ErrorNat
|Num: nat -> ErrorNat.
Inductive Exp :=
| number : ErrorNat -> Exp
| plus : Exp -> Exp -> Exp
| minus : Exp -> Exp -> Exp
| mult : Exp -> Exp -> Exp
| div : Exp -> Exp -> Exp.
Coercion number : ErrorNat >-> Exp.
Coercion Num : nat >-> ErrorNat.
Fixpoint plusErr (n1 : ErrorNat) (n2 : ErrorNat) : ErrorNat :=
match n1,n2 with
| Error, _ => Error
| _ , Error => Error
| Num n1',Num n2' => Num (n1' + n2')
end.
Fixpoint minusErr (n1 : ErrorNat) (n2 : ErrorNat) : ErrorNat :=
match n1,n2 with
| Error, _ => Error
| _ , Error => Error
| Num n1',Num n2' => if n1' <? n2' then Error else Num (n1' - n2')
end.
Fixpoint multiplyErr (n1 : ErrorNat) (n2 : ErrorNat) : ErrorNat :=
match n1,n2 with
| Error, _ => Error
| _ , Error => Error
| Num n1',Num n2' => Num (n1' * n2')
end.
Fixpoint divisionErr (n1 : ErrorNat) (n2 : ErrorNat) : ErrorNat :=
match n1,n2 with
| Error, _ => Error
| _ , Error => Error
| Num n1',Num n2' => if n2' =? 0 then Error else Num (n1' / n2')
end.
Fixpoint eval (exp: Exp) : ErrorNat :=
match exp with
|number n => n
|plus exp1 exp2 => plusErr (eval exp1)(eval exp2)
|minus exp1 exp2 => minusErr (eval exp1)(eval exp2)
|mult exp1 exp2 => multiplyErr(eval exp1)(eval exp2)
|div exp1 exp2 => divisionErr(eval exp1)(eval exp2)
end.
Compute (eval(div 7 2)).
Compute (eval (plus 12 3)).
Compute (eval (mult 12 0)).