Guest User

Untitled

a guest
Jul 4th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 8.09 KB | None | 0 0
  1. (***************************************************************************)
  2. (* TP Hiver 2012 - IFT-3000                                                *)
  3. (*                                                                         *)
  4. (* Transformation automatique de programmes.                               *)
  5. (***************************************************************************)
  6. (* NOM: TROTTIER-HEBERT             PRÉNOM: JOEL                           *)
  7. (* MATRICULE: 910 126 534           PROGRAMME: IFT                         *)
  8. (***************************************************************************)
  9. (* Les fonctions à compléter sont indiquées par *****                      *)
  10. (***************************************************************************)
  11.  
  12. #use "tp1.mli";;
  13.  
  14. (***************************************************************************)
  15. (* Implantation                                                            *)
  16. (***************************************************************************)
  17.  
  18. module Tp1 : TP1 = struct
  19.  
  20.   open List
  21.  
  22.   (* *****************************************************************)
  23.   (* Déclarations de types                                           *)
  24.   (* *****************************************************************)
  25.  
  26.   type id = string
  27.    
  28.   type exp = EmptyExp | Int of int | Bool of bool | Var of id
  29.            | Not of exp | Par of exp | BinOp of binop * exp * exp
  30.        | RelBinOp of relbinop * exp * exp
  31.      
  32.   and binop = Plus | Minus | Times | Div
  33.   and relbinop = Eq | Less | Great | Leq | Geq | Diff | And | Or
  34.    
  35.   type io = IS | OS | Temp
  36.    
  37.   type statement = {name : id; exp : exp; cond : exp; io : io}
  38.   type program = statement list
  39.    
  40.   type value = Null | I of int | B of bool
  41.   type env = (id * value) list
  42.    
  43.    
  44.   (* *****************************************************************)
  45.   (* Listes utiles                                                   *)
  46.   (* *****************************************************************)
  47.    
  48.   let binOpStrList1 = [(Plus, "+");(Times, "*");(Minus, "-");(Div, "/")]
  49.        
  50.   let binOpStrList2 = [(Eq, "="); (Less, "<"); (Great, ">");
  51.                (Leq, "<="); (Geq, ">="); (Diff, "<>");
  52.                (And, "and"); (Or, "or")]
  53.      
  54.   let binOpList = [(Plus, ( + )); (Times, ( * )); (Minus, ( - ));
  55.            (Div, ( / ))]
  56.      
  57.   let relBinOpList1 = [(Eq, ( = )); (Less, ( < )); (Great, ( > ));
  58.                (Leq, ( <= ) ); (Geq, ( >= ) ); (Diff, ( <> ))]
  59.      
  60.   let relBinOpList2 = [(And, ( && )); (Or, ( || ))]
  61.      
  62.   (* *****************************************************************)
  63.   (* FONCTION UTILES (on peut en ajouter au besoin ...)              *)
  64.   (* *****************************************************************)
  65.      
  66.   (* Autres fonctions utiles                                         *)
  67.   let isMember e l = exists (fun x -> x = e) l
  68.      
  69.   let union l1 l2 =
  70.     fold_right (fun x l -> if (isMember x l1) then l else x::l) l2 l1  
  71.      
  72.   let diff l1 l2 =
  73.     fold_right (fun x l -> if (isMember x l2) then l else x::l) l1 []
  74.      
  75.   let join l1 l2 =  
  76.     fold_right (fun x l -> if (isMember x l2) then x::l else l) l1 []
  77.      
  78.   let equal l1 l2 = (diff l1 l2) = [] && (diff l2 l1) = []
  79.      
  80.   let inSet l1 l2 = for_all (fun x -> isMember x l2) l1
  81.            
  82.   let rec updateEnv env ((x,_) as p) = p::(remove_assoc x env)
  83.                              
  84.   let fixPoint f eq x0 =
  85.     let rec fixFunc x =
  86.       let x' = f x in
  87.       if (eq x x') then x' else fixFunc x'
  88.     in
  89.     fixFunc x0
  90.  
  91.   let (|>) f x = x f
  92.  
  93.   let unionMany l =
  94.     let rec aux l' = match l' with
  95.     | [] -> []
  96.     | x::r -> union x (aux r)
  97.     in
  98.     aux l
  99.  
  100.   let choose l =
  101.     let rec f m = begin match m with
  102.     | [] -> []
  103.     | x::r ->
  104.     begin match x with
  105.     | Some x -> x :: (f r)
  106.     | _ -> f r
  107.     end
  108.     end
  109.     in
  110.   f l
  111.      
  112.   (* *****************************************************************)
  113.   (* FONCTION DU TP1-H2012 (certaines (*****) à compléter)           *)
  114.   (* *****************************************************************)
  115.      
  116.    let rec pgmToStr p =
  117.      fold_left (fun result s -> result ^ (statToStr s) ^ "\n") "" p
  118.        
  119.    and statToStr {name=n; exp=e; cond=c; io=io} =
  120.      let ioToStr io = match io with
  121.      | IS -> "IS"
  122.      | OS -> "OS"
  123.      | Temp -> ""
  124.      in
  125.      "(" ^ n ^ "; " ^ (expToStr e) ^ "; " ^ (expToStr c) ^ "; " ^
  126.      (ioToStr io) ^ ")"
  127.              
  128.    and expToStr e = match e with
  129.    | EmptyExp -> ""
  130.    | Int i -> string_of_int i
  131.    | Bool b -> string_of_bool b
  132.    | Var x -> x
  133.    | Not c -> "not" ^ (expToStr c)
  134.    | Par e -> "(" ^ (expToStr e) ^ ")"
  135.    | BinOp(op1,e1,e2) -> (expToStr e1) ^" "^ (assoc op1 binOpStrList1) ^" "^ (expToStr e2)
  136.    | RelBinOp(op1,e1,e2) -> (expToStr e1) ^" "^ (assoc op1 binOpStrList2) ^" "^ (expToStr e2)
  137.  
  138.   let rec useExp e = match e with
  139.   | Var x -> [x]
  140.   | Par e -> useExp e
  141.   | BinOp(_,e1,e2) -> union (useExp e1) (useExp e2)
  142.   | RelBinOp(_,e1,e2) -> union (useExp e1) (useExp e2)
  143.   | _ -> []
  144.                                              
  145.   let useStat {exp = e; cond = c} = union (useExp e) (useExp c)
  146.      
  147.   let defStat {name = n} = n
  148.  
  149. (*****)
  150.   let inStat p = filter (fun x -> x.io = IS) p
  151.    
  152. (*****)
  153.   let outStat p = filter (fun x -> x.io = OS) p
  154.    
  155. (*****)
  156.   let usePgm p =  
  157.     let rec aux p' =
  158.       match p' with
  159.       | [] -> []
  160.       | x::r -> union (useStat x) (aux r)
  161.     in aux p
  162.  
  163. (*****)
  164.   let defPgm p = map defStat p
  165.          
  166. (*****)
  167.   let getStat p x = find (fun ({name = n} : statement) -> n = x) p
  168.    
  169.   let getStat2 p x =
  170.     try
  171.      Some (getStat p x)
  172.     with
  173.     | not_found -> None
  174.    
  175.    
  176. (*****)
  177. let useDirectIndirect p s =  
  178.   let x0 = useStat s in  
  179.   let f ids = union ids (usePgm (choose (map (getStat2 p) ids))) in  
  180.   fixPoint f equal x0
  181.  
  182. (*****)
  183.   let cyclicStat p s = isMember (defStat s) (useDirectIndirect p s)
  184.    
  185. (*****)
  186.   let cyclicPgm p = exists (cyclicStat p) p
  187.    
  188. (*****)
  189. let incompleteStat pgm =
  190.   let estDeclare var = exists (fun stat -> stat.name = var) pgm in
  191.   let checkVars vars = isMember false (map estDeclare vars) and
  192.       resultat = [] and
  193.       couples = map (fun stat -> stat, (useDirectIndirect pgm) stat) pgm
  194.   in
  195.   let rec aux liste = match liste with
  196.   | [] -> []
  197.   | x::r -> union (
  198.       if checkVars (snd x) = true
  199.       then union [(fst x).name] resultat
  200.   else union [] resultat) (aux r)
  201.  in aux couples |> map (getStat pgm)
  202.  
  203. (*****)
  204.  
  205. let superfluousStat =
  206.   let aux = map defStat in
  207.   fun pgm ->
  208.     let used = map (useDirectIndirect pgm) (outStat pgm) |> unionMany in
  209.     let all, out = aux pgm , aux (outStat pgm) in
  210.     (diff (diff all out) used) |> map (getStat pgm)
  211.  
  212.      
  213.   let sortp p =
  214.     let lower s1 s2 = if mem (defStat s1) (useDirectIndirect p s2) then -1 else 1
  215.     in
  216.     sort lower p
  217.      
  218. (*****)
  219.   let slice p n =
  220.     union [(getStat p n)] (useDirectIndirect p (getStat p n) |> map (getStat p))
  221.        
  222. (*****)
  223.   let outputSlices p =
  224.     outStat p
  225.   |> map defStat
  226.   |> map (fun id -> id, slice p id)
  227.      
  228. (*****)
  229.   let evalPgm env p =
  230.     let rec evalStat env s =
  231.       match s with
  232.       | {io = IS} -> env
  233.       | {name = n; exp = e; cond = c; io = _} ->
  234.     match evalExp env c with
  235.     | B true -> (n, evalExp env e) :: env
  236.     | _ -> (n, Null) :: env
  237.     and evalExp env e =
  238.       try
  239.     match e with
  240.     | Int i -> I i
  241.     | Bool b -> B b
  242.     | Par e -> evalExp env e
  243.     | Not c -> (match evalExp env c with
  244.       | B b -> B (not b)
  245.       | _ -> Null)
  246.     | RelBinOp (op, e1, e2) ->
  247.             (match op, evalExp env e1, evalExp env e2 with
  248.             | (And | Or), B b1, B b2 -> B (assoc op relBinOpList2 b1 b2)
  249.             | _, I i1, I i2 -> B (assoc op relBinOpList1 i1 i2)
  250.             | _ -> Null)
  251.     | BinOp (op, e1, e2) ->
  252.         (match evalExp env e1, evalExp env e2 with
  253.         |  I i1,  I i2 -> I (assoc op binOpList i1 i2)
  254.         | _ -> Null)
  255.     | Var v -> List.assoc v env
  256.     | EmptyExp -> failwith "ne devrait pas arriver."
  257.       with
  258.       | _ -> Null
  259.     in
  260.     List.fold_left evalStat env (sortp p)      
  261.  
  262. end;;
Add Comment
Please, Sign In to add comment