Ajout du code de pieuvre qui manipule les λ-expressions.}

This commit is contained in:
Mysaa 2022-05-06 01:29:00 +02:00
parent 542ae30dee
commit 4bfe9e9e6e
Signed by: Mysaa
GPG Key ID: 7054D5D6A90F084F

View File

@ -20,15 +20,16 @@ let rec string_of_lam (l: lam) : string = match l with
| LExf(l',t) -> "exf("^(string_of_lam l')^" : "^(string_of_ty t)^")" | LExf(l',t) -> "exf("^(string_of_lam l')^" : "^(string_of_ty t)^")"
;; ;;
let split (x:var) : string * int = let split, tsplit =
if string_match varRegex x 0 let splitter regex x =
then if string_match regex x 0
let xStr = matched_group 1 x in then
let xInt = matched_group 2 x in let xStr = matched_group 1 x in
(xStr, int_of_string xInt) let xInt = matched_group 2 x in
(xStr, int_of_string xInt)
else raise (IllegalVarNameException(x)) else raise (IllegalVarNameException(x))
;; in (splitter varRegex, splitter tvarRegex);;
(* Renvoie un nom non utilisé dans la formule l qui commence par x *) (* Renvoie un nom non utilisé dans la formule l qui commence par x *)
let findFreeName (l: lam) (x: var) = let findFreeName (l: lam) (x: var) =
@ -43,8 +44,20 @@ let findFreeName (l: lam) (x: var) =
in in
finder l; finder l;
xStr ^ (string_of_int (!maxi+1)) xStr ^ (string_of_int (!maxi+1))
;;
(* Renvoie un nom de type simple non utilisé dans le type t qui commence par x *)
let findTFreeName (t: ty) (x: tvar) =
let xStr = fst (tsplit x) in
let maxi = ref 0 in
let rec finder t = match t with
| TImpl(t1, t2) -> (finder t1;finder t2)
| TSimple(y) -> let (yS,yI) = split y in
if xStr=yS then maxi := max !maxi yI
| TFalse -> () (* Le faux ne réserve pas de variables *)
in
finder t;
xStr ^ (string_of_int (!maxi+1))
;; ;;
(* Renvoie l[s/x] *) (* Renvoie l[s/x] *)
@ -75,16 +88,50 @@ let rec alpha (l1: lam) (l2: lam) : bool =
;; ;;
(* Fait un pas de β-réduction, et renvoie None si on a une forme normale *) (* Fait un pas de β-réduction, et renvoie None si on a une forme normale *)
let betastep (l: lam) : lam option = let rec betastep (l: lam) : lam option = match l with
raise TODOException | LFun(v,t,l') -> betastep l'
| LApp(lf,lx) ->
begin
match (betastep lf) with
| Some lf' -> Some (LApp(lf',lx))
| None ->
match (betastep lx) with
| Some lx' -> Some (LApp(lf, lx'))
| None ->
match lf with
| LFun(x,_,corps) -> Some (replace corps x lx)
| _ -> None (* On ne peut pas β-réduire *)
end
| LVar(x) -> None
| LExf(l',_) -> betastep l'
;; ;;
(* Affiche les réductions du λ-terme l jusquà atteindre une forme normale, ou part en boucle infinie *) (* Affiche les réductions du λ-terme l jusquà atteindre une forme normale, ou part en boucle infinie *)
let reduce (l:lam) : unit = let rec reduce (l:lam) : unit =
raise TODOException print_string (string_of_lam l);
print_newline ();
match (betastep l) with
| Some l' -> reduce l'
| None -> ()
;; ;;
(* Vérifie que le λ-terme l sous l'environnement env a bien le type t *) (* Vérifie que le λ-terme l sous l'environnement env a bien le type t *)
let typecheck (env: gam) (l: lam) (t: ty) : bool = let rec typecheck (env: gam) (l: lam) (t: ty) : bool =
raise TODOException match (l,t) with
| (LFun(x,t,l'),TImpl(tx,ty)) -> typecheck ((x,tx)::env) l' ty
| (LFun(_,_,_),_) -> false (* Une fonction est forcément une ... fonction *)
| (LApp(lf,lx),t) ->
let t' = (raise TODOException; TFalse) in
(typecheck env lf (TImpl(t',t))) && (typecheck env lx t')
| (LVar(x),t) ->
begin
match env with
| (y,t')::env' -> if x=y then t=t'
else typecheck env' l t
| [] -> raise TODOException
end
| (LExf(l',t'),t) ->
if t=t'
then typecheck env l' t
else false (* Le ex falso a le mauvais type *)
;; ;;