-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.ml
68 lines (60 loc) · 2.4 KB
/
type.ml
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
type typ = Bool | Int | Rat | Undefined | Pointeur of typ | TypeNomme of string | Enregistrement of (typ * string) list
let rec string_of_type t =
match t with
| Bool -> "Bool"
| Int -> "Int"
| Rat -> "Rat"
| Undefined -> "Undefined"
| Pointeur(typ) -> "Pointeur("^(string_of_type typ)^")"
| TypeNomme(nom) -> "Type nommées de "^nom
| Enregistrement([]) -> ""
| Enregistrement((typee,champ)::q) -> "Champ: "^(string_of_type typee)^" "^champ^(string_of_type (Enregistrement(q)))
let rec est_compatible t1 t2 =
match t1, t2 with
| Bool, Bool -> true
| Int, Int -> true
| Rat, Rat -> true
| Pointeur(_),Pointeur(Undefined) -> true
| Pointeur(Undefined),Pointeur(_) -> true
| Pointeur(type_t1), Pointeur(type_t2) -> est_compatible type_t1 type_t2
| _ -> false
let%test _ = est_compatible Bool Bool
let%test _ = est_compatible Int Int
let%test _ = est_compatible Rat Rat
let%test _ = not (est_compatible Int Bool)
let%test _ = not (est_compatible Bool Int)
let%test _ = not (est_compatible Int Rat)
let%test _ = not (est_compatible Rat Int)
let%test _ = not (est_compatible Bool Rat)
let%test _ = not (est_compatible Rat Bool)
let%test _ = not (est_compatible Undefined Int)
let%test _ = not (est_compatible Int Undefined)
let%test _ = not (est_compatible Rat Undefined)
let%test _ = not (est_compatible Bool Undefined)
let%test _ = not (est_compatible Undefined Int)
let%test _ = not (est_compatible Undefined Rat)
let%test _ = not (est_compatible Undefined Bool)
let est_compatible_list lt1 lt2 =
try
List.for_all2 est_compatible lt1 lt2
with Invalid_argument _ -> false
let%test _ = est_compatible_list [] []
let%test _ = est_compatible_list [Int ; Rat] [Int ; Rat]
let%test _ = est_compatible_list [Bool ; Rat ; Bool] [Bool ; Rat ; Bool]
let%test _ = not (est_compatible_list [Int] [Int ; Rat])
let%test _ = not (est_compatible_list [Int] [Rat ; Int])
let%test _ = not (est_compatible_list [Int ; Rat] [Rat ; Int])
let%test _ = not (est_compatible_list [Bool ; Rat ; Bool] [Bool ; Rat ; Bool ; Int])
let rec getTaille t =
match t with
| Int -> 1
| Bool -> 1
| Rat -> 2
| Undefined -> 0
| Pointeur(_) -> 1
| TypeNomme(_) -> failwith "internal error, pas d'apppel à ce cas"
| Enregistrement([]) -> 0
| Enregistrement((typee,_)::q) -> (getTaille typee) + getTaille(Enregistrement(q))
let%test _ = getTaille Int = 1
let%test _ = getTaille Bool = 1
let%test _ = getTaille Rat = 2