Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 6.46 KB | None | 0 0
  1. #use "tp2.mli";;
  2.  
  3.  
  4. module Tp2 : TP2 = struct
  5.  
  6.     exception Non_Implante of string
  7.  
  8.     (* Principaux types du Tp ------------------------------------------------ *)
  9.     (* ----------------------------------------------------------------------- *)
  10.     type programme = transition list * etat
  11.     and  transition = etat * action * etat
  12.     and  action = Epsilon | Api of string
  13.     and  etat = int;;
  14.  
  15.     open List
  16.  
  17.     let pgm1 =
  18.       ([(1, Api "a", 2); (2, Epsilon, 6); (2, Epsilon, 6); (6, Api "e", 7);
  19.         (7, Epsilon, 7); (7, Api "exit", 10)],1);;
  20.    
  21.     let pgm2 =
  22.       ([(1, Api "a", 2); (2, Epsilon, 5); (5, Epsilon, 6); (6, Api "c", 7);
  23.         (7, Epsilon, 7); (1, Epsilon, 3); (3, Api "a", 4); (4, Epsilon, 4);
  24.         (4, Api "b", 8)],1);;
  25.    
  26.     let pgm3 =
  27.       ([(1, Epsilon, 2); (2, Api "a", 6); (6, Epsilon, 7); (7, Api "b", 10);
  28.           (10, Epsilon, 10); (6, Api "c", 8); (1, Epsilon, 4);
  29.           (4, Epsilon, 1)], 1);;
  30.    
  31.     let pgm4 =
  32.       ([(0, Api "a", 6); (0, Epsilon, 3); (6, Api "b", 10); (6, Api "c", 8);
  33.         (8, Epsilon, 8)],0);;
  34.  
  35.     let pgm30 = [ (7,[1;2]) ; (4, [3;4])];;
  36.     let pgm32 = [(1, Epsilon, 2) ; (2, Api "a", 6) ; (6, Epsilon, 7) ; (7, Api "b", 10) ; (10, Epsilon, 10) ;(6, Api "c", 8) ; (1, Epsilon, 4) ; (4, Epsilon, 1)] ;;
  37.  
  38.  
  39.     (* Fonctions du Tp à implanter ------------------------------------------- *)
  40.     let isActionAPI (action : action) = match action with
  41.     | Epsilon -> false
  42.     | Api(_) -> true;;
  43.  
  44.     let rec getTransition listeTransition etatRecherche listeAPI listeEpsi  =
  45.       match listeTransition with
  46.       | [] -> (listeAPI,listeEpsi)
  47.       | hd::tl -> match hd with
  48.       | (etat1,action,etat2) when etat1 = etatRecherche && isActionAPI(action) = true-> getTransition tl etatRecherche (hd::listeAPI) listeEpsi
  49.       | (etat1,action,etat2) when etat1 = etatRecherche && isActionAPI(action) = false -> getTransition tl etatRecherche listeAPI (hd::listeEpsi)
  50.       | (_,_,_) -> getTransition tl etatRecherche listeAPI listeEpsi;;
  51.      
  52.     (*Deuxième fonction*)
  53.  
  54.       let ( <+ ) x l = exists (fun y -> x = y) l;; (* x appartient à l? *)    
  55.       let ( <++ ) etat l = exists (fun y -> etat = fst(y)) l;;
  56.       let ( +: ) x l = if x <+ l then l else x::l;;  (* ajout x à l si x n'existe pas déjà dans l *)
  57.  
  58.       let rec getEpsiListeTransition listeTransition acc = match listeTransition with
  59.       | [] -> acc
  60.       | hd::tl -> match hd with
  61.           | (etat1,action,etat2) ->if  isActionAPI(action) = false then getEpsiListeTransition tl ((etat1,action,etat2)::acc) else getEpsiListeTransition tl (acc) ;;
  62.      
  63.       let rec getAPIListeTransition listeTransition acc = match listeTransition with
  64.           | [] -> acc
  65.           | hd::tl -> match hd with
  66.               | (etat1,action,etat2) ->if  isActionAPI(action) = true then getAPIListeTransition tl ((etat1,action,etat2)::acc) else getAPIListeTransition tl (acc) ;;
  67.  
  68.       let rec getSortedList listeEpsi listeAPI = match listeEpsi with
  69.           | [] -> listeAPI
  70.           | hd::tl ->getSortedList tl (hd::listeAPI);;
  71.  
  72.      
  73.       let rec getEtats1ListeTransition listeTransition acc = match listeTransition with
  74.       | [] -> acc
  75.       | hd::tl -> match hd with
  76.           | (etat1,action,etat2) -> getEtats1ListeTransition tl (etat1+:acc);;
  77.      
  78.       let rec getEtats2ListeTransition listeTransition acc = match listeTransition with
  79.           | [] -> acc
  80.           | hd::tl -> match hd with
  81.               | (etat1,action,etat2) -> getEtats2ListeTransition tl (etat2+:acc);;
  82.  
  83.       let rec getMissingEtats2 listeEtats1 listeEtats2 acc = match listeEtats2 with
  84.           | [] -> acc
  85.           | hd::tl -> match hd with
  86.               | (etat2) -> if(etat2 <+ listeEtats1) then getMissingEtats2 listeEtats1 tl acc else getMissingEtats2 listeEtats1 tl (etat2::acc) ;;
  87.  
  88.  
  89.       let rec getListeConvenable listeElemManquant listeTransition  =
  90.             match listeElemManquant with
  91.             | [] -> listeTransition
  92.             | hd::tl -> match hd with
  93.                 | (elem) ->  getListeConvenable tl ((elem,Api "fake",6969) :: listeTransition);;
  94.  
  95.        let actionEpsiImmediats transitions etat =
  96.               fold_left (fun l (e1,action,e2) -> if e1 = etat && isActionAPI(action) = false  then e2 +: l else l)
  97.                   [] transitions ;;
  98.        
  99.        let getEpsiSucc listeTransition etat =
  100.                   let rec parcours etat acc =
  101.                     let immediats = actionEpsiImmediats listeTransition etat in
  102.                     let etats = filter (fun x -> not (x <+ acc)) immediats in
  103.                     fold_left (fun l e -> parcours e l) (acc @ etats) etats
  104.                   in
  105.                   parcours etat [];;        
  106.  
  107.         let getEpsilons listeTransition =
  108.               let rec parcoursListe listeAjuste acc =
  109.                match listeAjuste with
  110.                | [] -> acc
  111.                | hd::tl -> match hd with
  112.                   | (etat1,action,etat2) when isActionAPI(action) = true -> if (etat1 <++ acc) = true then parcoursListe tl acc else parcoursListe tl ((etat1,[])::acc)
  113.                   | (etat1,action,etat2) when isActionAPI(action) = false -> if (etat1 <++ acc) = true then parcoursListe tl acc else parcoursListe tl ((etat1,(getEpsiSucc listeTransition etat1))::acc)
  114.               in parcoursListe listeTransition [];;
  115.              
  116.      
  117.     (*Troisième fonction*)
  118.     (* ----------------------------------------------------------------------- *)
  119.  
  120.     (* 5 points *)
  121.  
  122.     let transitionsImmediates pgm etat =
  123.       match pgm with
  124.       | (listeTransition,_)->  getTransition listeTransition etat [] [];;
  125.  
  126.     (* 30 points *)
  127.     let epsilonAtteignable (pgm: programme) =
  128.       match pgm with
  129.       | (listeTransition,_)->  getEpsilons (getListeConvenable(getMissingEtats2  (getEtats1ListeTransition listeTransition []) (getEtats2ListeTransition listeTransition []) []) (getSortedList (getEpsiListeTransition listeTransition []) (getAPIListeTransition listeTransition [])));;
  130.  
  131.     (* 30 points *)
  132.     let supprimeEpsilon pgm =
  133.         raise (Non_Implante "supprimeEpsilon a completer");;
  134.  
  135.     (* 20 points *)
  136.     let similaire pgm1 pgm2 =
  137.         raise (Non_Implante "similaire a completer");;
  138.  
  139.     (* 5 points *)
  140.     let bisimilaire pgm1 pgm2 =
  141.         raise (Non_Implante "bisimilaire a completer");;
  142.  
  143.     (* 10 points *)
  144.     let estSousPgm pgm1 pgm2 =
  145.         raise (Non_Implante "estSousPgm a completer");;
  146.  
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement