Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.75 KB | None | 0 0
  1.  
  2. type Term =
  3.     | Var of string
  4.     | Abs of string * Term
  5.     | App of Term * Term
  6.  
  7. let alphabet = ["a"; "b"; "c"; "d"; "e"; "f"; "g"; "k"; "l"; "m"; "w"; "q"; "s"; "l"]
  8.  
  9. let rec checkCollision x k =
  10.     match x with
  11.     | Var(s) when (s = k) -> true
  12.     | Abs(s, p) when (s = k) || checkCollision p k -> true  
  13.     | App(s, p) when (checkCollision s k) || (checkCollision p k) -> true      
  14.     | _ -> false
  15.  
  16. let getLetter k = List.head (List.filter (fun x -> not (checkCollision k x)) alphabet)  
  17.  
  18. let rename a b x =
  19.     if (a = b) then
  20.         getLetter x
  21.     else a
  22.  
  23. let rec alphaReduction check x y =
  24.     match x with
  25.     | Var(o) -> Var(rename o check y)
  26.     | Abs(s, p) -> Abs(rename s check y, alphaReduction check p y)
  27.     | App(s, p) -> App(alphaReduction check s y, alphaReduction check p y)
  28.  
  29. let rec betaAbs check k y =
  30.    match k with
  31.    | Var(t) when (t = check) -> y
  32.    | Var(t) -> Var(t)
  33.    | Abs(m, n) -> Abs(m, betaAbs check n y)
  34.    | App(m, n) -> App(betaAbs check m y, betaAbs check n y)
  35.  
  36. let rec betaReduction x =    
  37.     match x with  
  38.     | Var(t) -> Var(t)
  39.     | Abs(t, s) ->
  40.         match s with
  41.             | App(m, n) -> Abs(t, betaReduction (App(betaReduction m, betaReduction n)))
  42.             | Abs(m, n) -> Abs(t, betaReduction s)
  43.             | _ -> Abs(t, s)
  44.     | App(Var(y), s) ->
  45.         match s with
  46.         | App(m, n) -> App(Var(y), betaReduction (App(betaReduction m, betaReduction n)))
  47.         | _ -> App(Var(y), s)
  48.     | App(Abs(t, u), y) ->
  49.         match y with
  50.         |Var(o) when (checkCollision x o) -> betaReduction (betaAbs (rename t o x) (alphaReduction o u x) y)    
  51.         | _ ->  betaReduction (betaAbs t u y)
  52. | App(s, y) -> betaReduction (App(betaReduction s, betaReduction y))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement