Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun pretvori(pair: pattern) =
  2.     case pair of
  3.         VariableP a => Variable a
  4.         | ConstantP a => Constant a
  5.         | PairP [a,b] => Pair [pretvori(a), pretvori(b)]
  6.         | OperatorP(a, b) => Operator(a, (pretvori(b)))
  7.         | ListP a => List (List.map (fn x => pretvori(x)) a )
  8.  
  9. fun match(ex: expression, pt: pattern) =
  10.     case pt of (*najdemo prvi pattern*)
  11.         (*--------------------------------------WILDCARD--------------------------------------------///*)
  12.         Wildcard => SOME nil
  13.         (*--------------------------------------VARIABLE-------------------------------------------///*)
  14.         |VariableP s => let
  15.             fun ifExists(s) =
  16.                  case ex of
  17.                     Constant x => SOME[(s, Constant x)]
  18.                     | Variable y => SOME[(s, Variable y)]
  19.                     | Operator z => SOME[(s, Operator z)]
  20.                     | Pair w => SOME[(s, Pair w)]
  21.                     | List l => SOME[(s, List l)]
  22.         in
  23.             ifExists(s)
  24.         end
  25.         (*--------------------------------------CONSTANT-------------------------------------------///*)
  26.         | ConstantP x => let
  27.             fun ifExists(x) =
  28.                 case ex of
  29.                     Constant y => if(x = y) then SOME nil else NONE
  30.                     | _ => NONE
  31.         in
  32.             ifExists(x)
  33.         end
  34.         (*--------------------------------------PAIR----------------------------------------------*)
  35.         | PairP [a1, a2] => let
  36.             fun ifExists(a1, a2) =
  37.                 case ex of
  38.                     Pair [b1,b2] => if(b1 = pretvori(a1) andalso b2 = pretvori(a2)) then SOME [("merged", Pair [b1, b2])] else NONE
  39.                     | _ => NONE
  40.         in
  41.             ifExists(a1, a2)
  42.         end
  43.         (*--------------------------------------LIST----------------------------------------------*)
  44.         | ListP ps => let
  45.             fun ifExists(ps) =
  46.                 case ex of
  47.                     List xs => if(List xs = pretvori(ListP ps)) then SOME [("SFAS", List xs)] else NONE
  48.                     | _ => NONE
  49.         in
  50.             ifExists(ps)
  51.         end
  52.         (*--------------------------------------OPERATOR----------------------------------------------*)
  53.         | OperatorP(a, PairP([x1, x2])) => let
  54.             fun ifExists(a, x1, x2, ex) =
  55.                 case ex of (* Primerjamo pattern z expressiono*)
  56.                     Operator(b, Pair([y1, y2])) => if(a = b) then SOME[("x1", y1), ("x2", y2)] else NONE
  57.                     | _ => NONE
  58.         in
  59.             ifExists(a, x1, x2, ex)
  60.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement