Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Control.Print.printDepth := 100;
  2.  
  3. datatype expression =  Constant of int |
  4.         Variable of string |
  5.         Operator of string * expression |
  6.         Pair of expression list |
  7.         List of expression list
  8.  
  9. datatype pattern = ConstantP of int
  10.     | VariableP of string
  11.     | OperatorP of string * pattern
  12.     | PairP of pattern list
  13.     | ListP of pattern list
  14.     | UnorderedListP of pattern list
  15.     | Wildcard
  16.  
  17. (*cross*)
  18. fun cross(tp) =
  19.     case tp of
  20.         (nil, xs) => nil
  21.         |(xs, nil) => nil
  22.         |(h::t, xs) => (List.map (fn x => (h, x) ) xs) @ cross(t, xs)
  23.  
  24. (*match*)
  25. fun match(ex: expression, pt: pattern) =
  26.     case pt of
  27.         Wildcard => SOME nil
  28.         | VariableP s => (case ex of
  29.                      Constant x => SOME[(s, Constant x)]
  30.                     | Variable y => SOME[(s, Variable y)]
  31.                     | Operator z => SOME[(s, Operator z)]
  32.                     | Pair w => SOME[(s, Pair w)]
  33.                     | List l => SOME[(s, List l)] )
  34.        
  35.         | ConstantP x => (case ex of
  36.                     Constant y => if(x = y) then SOME nil else NONE
  37.                     | _ => NONE)
  38.      
  39.         | ListP ps => ((
  40.             case ex of
  41.                 List xs => if(List.length(xs) <> List.length(ps)) then NONE else case (xs, ps) of
  42.                     (h::nil, g::nil) => match(h, g)
  43.                     | (h::t, g::r) => case match(h, g) of
  44.                         SOME v => SOME (v @ Option.valOf(match(List t, ListP r)))
  45.                         | NONE => NONE
  46.                     )handle Option => NONE)
  47.  
  48.         | PairP([a1, a2]) => (case ex of
  49.             Pair([b1,b2]) => (if(match(b2, a2) = NONE orelse match(b1, a1) = NONE) then NONE else SOME (Option.valOf(match(b1, a1)) @ Option.valOf(match(b2, a2)))))
  50.  
  51.         |OperatorP(a, x) => (case ex of
  52.             Operator(b, y) => if(a = b) then ( case(x,y) of (x,y) => match(y, x)) else NONE)
  53. (*eval*)
  54. exception InvalidVariable of string
  55. exception InvalidExpression  of expression
  56.  
  57. fun dobi(xs, var) =
  58.     case xs of
  59.         (a, b)::[] => if(a = var) then b else raise InvalidVariable a
  60.         |(a, b)::t => if(a = var) then b else dobi(t, var)
  61.  
  62. fun eval xs exp =
  63.     case exp of
  64.         Operator("+", a) => let
  65.             fun calc a =   case a of
  66.                 Constant a => a
  67.                 | Variable a => dobi(xs, a)
  68.                 | Pair [a1, a2] => calc(a1) + calc(a2)
  69.                 | Pair a => raise InvalidExpression (Pair a)
  70.                 | Operator(a, b) => eval xs (Operator(a,b))
  71.                 | List b => case b of
  72.                     nil => 0
  73.                     | b =>List.foldl (fn(x,y) => y + calc(x)) (calc(hd b)) (tl b)          
  74.         in
  75.             calc a
  76.         end
  77.         | Operator("-", a) => let
  78.             fun calc a =   case a of
  79.                 Constant a => a
  80.                 | Variable a => dobi(xs, a)
  81.                 | Pair [a1, a2] => calc(a1) - calc(a2)
  82.                 | Pair a => raise InvalidExpression (Pair a)
  83.                 | Operator(a, b) => eval xs (Operator(a,b))
  84.                 | List b => raise InvalidExpression (List b)
  85.         in
  86.             calc a
  87.         end
  88.          | Operator("*", a) => let
  89.             fun calc a =   case a of
  90.                 Constant a => a
  91.                 | Variable a => dobi(xs, a)
  92.                 | Pair [a1, a2] => calc(a1) * calc(a2)
  93.                 | Pair a => raise InvalidExpression (Pair a)
  94.                 | Operator(a, b) => eval xs (Operator(a,b))
  95.                 | List b => case b of
  96.                     nil => 1
  97.                     | b => List.foldl (fn(x,y) => y * calc(x)) (calc(hd b)) (tl b)
  98.         in
  99.             calc a
  100.         end
  101.         | Operator("/", a) => let
  102.             fun calc a =   case a of
  103.                 Constant a => a
  104.                 | Variable a => dobi(xs, a)
  105.                 | Pair [a1, a2] => if(calc(a2) = 0) then raise InvalidExpression (Pair [a1, a2]) else calc(a1) div calc(a2)
  106.                 | Pair a => raise InvalidExpression (Pair a)
  107.                 | Operator(a, b) => eval xs (Operator(a,b))
  108.                 | List b => raise InvalidExpression (List b)
  109.         in
  110.             calc a
  111.         end
  112.         | Operator("%", a) => let
  113.             fun calc a =   case a of
  114.                 Constant a => a
  115.                 | Variable a => dobi(xs, a)
  116.                 | Pair [a1, a2] => if(calc(a2) = 0) then raise InvalidExpression (Pair [a1, a2]) else calc(a1) mod calc(a2)
  117.                 | Pair a => raise InvalidExpression (Pair a)
  118.                 | Operator(a, b) => eval xs (Operator(a,b))
  119.                 | List b => raise InvalidExpression (List b)
  120.         in
  121.             calc a
  122.         end
  123.         | Operator(a, b) => raise InvalidExpression(Operator(a, b))
  124.    
  125. (* derivative *)
  126. fun derivative exp s = case exp of
  127.     Variable a => if(a = s) then Constant 1 else Constant 0
  128.     | Constant c => Constant 0
  129.     | Operator("+", Pair [a, b]) => Operator("+", Pair [derivative a s , derivative b s])
  130.     | Operator("-", Pair [a, b]) => Operator("-", Pair [derivative a s , derivative b s])
  131.     | Operator("/", Pair([a, b])) => Operator("/", Pair([ Operator("-", Pair([Operator("*", Pair([derivative a s, b])), Operator("*", Pair([a, derivative b s]))])), Operator("*", Pair([b, b]))]))
  132.     | Operator("*", Pair [a, b]) =>
  133.     case ([a,b]) of
  134.         ([Constant a, b]) => Operator("*", Pair([Constant a, derivative b s]))
  135.         | ([a, Constant b]) => Operator("*", Pair([derivative a s, Constant b]))
  136.         | ([a, b]) => Operator("+", Pair([Operator("*", Pair([derivative a s, b])), Operator("*", Pair([a, derivative b s]))]))
  137.  
  138. (*removeEmpty *)
  139.     fun obdelajkrat l = case (List.exists (fn x => if(x = Constant 0) then true else false) ((hd l)::(List.filter (fn x => if(x = Constant 1) then false else true) (tl l)))) of
  140.                 true => Constant 0
  141.                 | false => List ((hd l)::(List.filter (fn x => if(x = Constant 1) then false else true) (tl l)))
  142.  
  143.     fun removeEmpty exp = case exp of
  144.         Operator("+", b) => (case b of
  145.             List l => (case (List.length(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) l) )) of
  146.                 0 => Constant 0
  147.                 | 1 => hd(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) l))
  148.                 | _ => Operator("+", List (List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) l))))
  149.  
  150.             | Pair p => (case (List.length(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) p) )) of
  151.                 0 => Constant 0
  152.                 | 1 => hd(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) p))
  153.                 | _ => Operator("+", List (List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) p))) )
  154.             | Constant c => Constant c
  155.             | Variable v => Variable v)
  156.  
  157.         | Operator("-", b) => (case b of
  158.             List l => (case (List.length(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) l))) of
  159.                 0 => Constant 0
  160.                 | 1 => hd(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) l))
  161.                 | _ => Operator("-", List (List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) l))))
  162.  
  163.             | Pair p => (case (List.length(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) p))) of
  164.                 0 => Constant 0
  165.                 | 1 => hd(List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) p))
  166.                 | _ => Operator("-", List (List.filter (fn x => if(x = Constant 0) then false else true) (List.map (fn x => removeEmpty(x)) p))))
  167.             | Constant c => Constant c
  168.             | Variable v => Variable v)
  169.  
  170.         | Operator("/", b) => (case b of
  171.             List l => (case (List.length((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) l)))) of
  172.                  0 => hd l
  173.                 | 1 => hd(List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) l))
  174.                 | _ => Operator("/", List((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) l)))))
  175.  
  176.             | Pair p => (case (List.length((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) p)))) of
  177.                  0 => hd p
  178.                 | 1 => hd(List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) p))
  179.                 | _ => Operator("/", List((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) p)))))
  180.             | Constant c => Constant c
  181.             | Variable v => Variable v)
  182.  
  183.         | Operator("*", b) => (case b of
  184.             List l => (case (List.exists (fn x => if(x = Constant 0) then true else false) ((List.filter (fn x => if(x = Constant 1) then false else true) l))) of
  185.                 true => Constant 0
  186.                 | false => case (List.length((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) l)))) of
  187.                     0 => hd l
  188.                     | 1 => hd (List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) l))
  189.                     | 2 => Operator("*", List((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) l)))))
  190.  
  191.             | Pair p => (case (List.exists (fn x => if(x = Constant 0) then true else false) ((List.filter (fn x => if(x = Constant 1) then false else true) p))) of
  192.                 true => Constant 0
  193.                 | false => case (List.length((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) p)))) of
  194.                     0 => hd p
  195.                     | 1 => hd (List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) p))
  196.                     | 2 => Operator("*", List((List.filter (fn x => if(x = Constant 1) then false else true) (List.map (fn x => removeEmpty(x)) p)))))
  197.             | Constant c => Constant c
  198.             | Variable v => Variable v)
  199.  
  200.         | Constant c => Constant c
  201.         | Variable v => Variable v
  202.        
  203.      
  204.  
  205.  
  206.              
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.          
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. fun combinations a: 'a list list = a
  230. fun flatten exp = List [exp]
  231. fun joinSimilar exp = List [exp]
  232. fun divide(exp, exp1) = List [exp, exp1]
  233.  
  234.  
  235.  
  236. (*;;;; "type tests"  ;;;;
  237.  
  238. val test_cross_type: 'a list * 'b list -> ('a * 'b) list = cross
  239.  
  240. val test_match_type: expression * pattern -> (string * expression) list option = match
  241.  
  242. val test_eval_type: (string * int) list -> expression -> int = eval
  243.  
  244. val test_derivative_type: expression -> string -> expression = derivative
  245.  
  246. val test_removeEmpty_type: expression -> expression = removeEmpty
  247.  
  248. val test_combinations_type: 'a list list -> 'a list list = combinations
  249.  
  250. val test_flatten_type: expression -> expression = flatten
  251.  
  252. val test_joinSimilar_type: expression -> expression = joinSimilar
  253.  
  254. val test_divide_type: expression * expression -> expression = divide *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement