Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.51 KB | None | 0 0
  1. module Term
  2.  
  3. type Term =
  4. // Constants
  5. | False
  6. | True
  7.  
  8. // Variables
  9. | Var of int
  10.  
  11. // Quantifiers
  12. | All of Term * Term
  13.  
  14. // Predicates
  15. | Eqv of Term * Term
  16. | Not of Term
  17. | Or of Term list
  18.  
  19. // Comparison
  20. | Equal of Term * Term
  21.  
  22. type ShapeInfo = Shape of Term
  23.  
  24. let (|Leaf|Node|) a =
  25.     match a with
  26.     | False
  27.     | True ->
  28.         Leaf(Shape a)
  29.     | Var n ->
  30.         Leaf(Shape a)
  31.     | Not x ->
  32.         Node(Shape a, [x])
  33.     | All(x, y)
  34.     | Equal(x, y)
  35.     | Eqv(x, y) ->
  36.         Node(Shape a, [x; y])
  37.     | Or xs ->
  38.         Node(Shape a, xs)
  39.  
  40. let FromLeaf(Shape a) =
  41.     a
  42.  
  43. let FromNode(Shape a, xs) =
  44.     match a, xs with
  45.     | All(_, _), [x; y] ->
  46.         All(x, y)
  47.  
  48.     | Eqv(_, _), [x; y] ->
  49.         Eqv(x, y)
  50.     | Not _, [x] ->
  51.         Not x
  52.     | Or _, xs ->
  53.         Or xs
  54.  
  55.     | Equal(_, _), [x; y] ->
  56.         Equal(x, y)
  57.  
  58.     | _ ->
  59.         failwith "Internal error"
  60.  
  61. let rec eval model a =
  62.     match a with
  63.     | Equal(x, y) when x = y ->
  64.         True
  65.     | Not False ->
  66.         True
  67.     | Not True ->
  68.         False
  69.     | Or xs ->
  70.         let xs' = List.map(eval model) xs
  71.            |> List.filter(fun x -> x <> False)
  72.        if List.exists(fun x -> x = True) xs' then
  73.             True
  74.         else
  75.             match xs' with
  76.            | [] ->
  77.                False
  78.            | [x] ->
  79.                x
  80.            | _ ->
  81.                Or xs'
  82.  
  83.     | Node(s, xs) ->
  84.         FromNode(s, List.map(eval model) xs)
  85.     | Leaf _ ->
  86.         a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement