Guest User

Untitled

a guest
Aug 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.36 KB | None | 0 0
  1. #light
  2.  
  3. // Programming Paradigms Project
  4.  
  5. //module DistributedExperimentUnification
  6.  
  7. open System
  8. open System.Threading
  9. open System.Collections.Generic
  10. open System.Windows.Forms          // Note that you'll need references for System.Windows.Forms and
  11. open System.Drawing                // System.Drawing  (See the "solution explorer", on the right in VS...)  ----->>>
  12.  
  13.  
  14.  
  15.  
  16. //////////////////////////////////////// Types for experiments, rules, etc.
  17.  
  18. type exp = A | B | Mix of exp * exp | Var of string
  19.  
  20.  
  21.  
  22.  
  23. /// The value (e, ee) represents that "e suffices instead of ee" or "e suff ee" for short - see the rule type.
  24. type sufficency = exp * exp  
  25.  
  26. /// The value (e, ee), [(e1,ee1) ... (eN,eeN)] represents the rule that: "E suff EE provided that for all i in 1..N, Ei suff EEi".  
  27. /// Here the E, EE, Ei, EEi are the same as e, ee, eI, eeI except with each variable (Var Vj) replaced by an experiment EEEj that
  28. /// contains no vars (the same EEEj each time Vj appears in the rule).  The rule holds for every such substitution for the vars.
  29. type rule = Rule of sufficency * (sufficency list)  
  30.  
  31. type ruleGen = unit -> rule      // A rule generator creates new variables for a rule each time it is called, to avoid clashes
  32.                                  // with variable names in other rules, or when the rule is used multiple times.
  33. type labRules = ruleGen list     // Each lab has a list of rules, represented as generators.
  34.  
  35.  
  36. // Types for identifying labs and clients
  37. type labID = int
  38. type clientID = int
  39.  
  40. /// The number of Bases and Mixes in an experiment
  41. let rec expSize = function A|B -> 1
  42.                          | Mix (x, y) -> 1+expSize x + expSize y
  43.                          | Var _ -> raise (Exception "expSize for a Var")       // This shouldn't happen
  44.  
  45.  
  46.  
  47.  
  48.  
  49. let contains comp arr = Array.exists (fun x -> (fst x = fst comp && comp <> x)) arr
  50.  
  51.  
  52. let rec unify exp1 exp2 =
  53.     match (exp1,exp2) with
  54.             | (exp.Var x, exp.Var y) when x <> y -> [|exp2,exp1|]
  55.             | (exp.Var a,b) | (b, exp.Var a) when Var(a) <> b -> [|Var(a),b|]
  56.             | (exp.Mix(x,xx), exp.Mix(y,yy)) -> let lists = Array.append (unify x y) (unify xx yy) |> Array.toList
  57.                                                 let mutable filtered = [||]
  58.                                                 for i in 1 .. lists.Length do
  59.                                                     let temp = lists.Item (i-1)
  60.                                                     if (contains temp (lists |> List.toArray) <> true) then
  61.                                                         filtered <- Array.append filtered [|temp|]
  62.                                                 filtered |> Map.ofArray |> Map.toArray
  63.             | _ -> [||]
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. let a = Mix(A,B)
  71. let b = Mix(Var("x"),Var("y"))
  72. let c = Mix(Mix(A,B),B)
  73. let d = Var("x")
  74.  
  75.  
  76.  
  77. // These are some handy functions for creating rule generators with different numbers of variables
  78.  
  79. let newVar =   let n = ref 0 in   fun v -> n:=!n+1;
  80.                                            Var (v + string !n)
  81.  
  82. let newVar2 v1 v2 = newVar v1, newVar v2
  83. let newVar3 (v1,v2,v3) = newVar v1, newVar v2, newVar v3
  84. let newVar4 (v1,v2,v3,v4) = newVar v1, newVar v2, newVar v3, newVar v4
  85.  
  86.  
  87.  
  88. let rule1 () = let x = newVar "x"
  89.                Rule ((x, x), [])
  90.  
  91. let rule3 () = let x, xx, y = newVar3 ("x", "xx", "y")
  92.                Rule ((Mix(x,xx), y),  [(xx,y)])
  93.  
  94. let getsuff x =
  95.     match x with  
  96.         | Rule(a,b) -> (fst a, snd a)
  97.  
  98. let getprereqs x =
  99.     match x with  
  100.         | Rule(a,b) -> b
  101.  
  102. let rulesA = [rule1]
  103.  
  104.  
  105. let getRuleset (rules : (unit->rule) list) =
  106.     let rulesforyou = [for i in 1 .. rules.Length -> rules.Item(i-1)()]
  107.     rulesforyou
  108.  
  109.  
  110. let submarine = unify a b
  111.  
  112.  
  113.  
  114. let rec applySubs expr sublist : exp=
  115.     let mutable newexpr = expr;
  116.     for i in 0 .. (Array.length sublist) - 1 do
  117.         let subfrom = fst (Array.get sublist i)
  118.         let subto = snd (Array.get sublist i)
  119.         match expr with
  120.             | Var(a) when Var(a) = subfrom -> newexpr <- subto
  121.             | Mix(a,b) -> newexpr <- Mix(applySubs a sublist,applySubs b sublist)
  122.             | _ -> ()
  123.     newexpr
  124.  
  125.  
  126. let pickone = Array.get submarine 0
  127.  
  128. let h = applySubs d submarine
  129.  
  130.  
  131. // Suffices checks whether exp1 suffices instead of exp2 according to rules.
  132. let rec suffices rules (exp1, exp2) =
  133.     let mutable istrue = false;
  134.     let ruleset = getRuleset rules
  135.     // for each rule
  136.     for i in 1 .. ruleset.Length do
  137.         let r = ruleset.Item(i-1)
  138.         let uniL = unify exp1 (fst (getsuff r))
  139.         let uniR = unify exp2 (snd (getsuff r))
  140.         // check if rule is compatiable
  141.         if (uniL <> [||] && uniR <> [||]) then
  142.             let preq = getprereqs r
  143.             let mutable isgood = true
  144.             // check each prereq (sufficient list)
  145.             for i in 0 .. preq.Length - 1 do
  146.                 let newexp1 = applySubs (fst (List.nth preq i)) uniL
  147.                 let newexp2 = applySubs (snd (List.nth preq i)) uniR
  148.                 if suffices rules (newexp1,newexp2) = true then
  149.                     istrue <- true
  150.     istrue
  151.  
  152.  
  153.  
  154. let testingsfasa = suffices rulesA (c,a)
  155.  
  156.  
  157.  
  158. (* so basically unify the input exp tuples with the string vars in the input rule
  159.     if they match, check all suffices in sufficient list
  160.     if they all true, return true, else false
  161.  
  162. pseudo code *)
Add Comment
Please, Sign In to add comment