Advertisement
Hinski2

Untitled

May 26th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. (* The standard library of FUN *)
  2.  
  3. let stdlib =
  4. [ Ast.PVar "abs", "fun x -> if x < 0 then 0 - x else x"
  5. ; Ast.PVar "mod", "fun x -> fun y -> x - (x / y) * y"
  6. ; Ast.PVar "fix", "fun f -> (fun x -> fun y -> f (x x) y)
  7. (fun x -> fun y -> f (x x) y)"
  8. ; Ast.PVar "map", "fix (fun map -> fun f -> fun xs ->
  9. match xs with
  10. | Nil() -> Nil()
  11. | Cons(h, t) -> Cons(f h, map f t)
  12. end)"
  13. ; Ast.PVar "append", "fix (fun append -> fun xs -> fun ys ->
  14. match xs with
  15. | Nil() -> ys
  16. | Cons(h, t) -> Cons(h, append t ys)
  17. end)"
  18. ; Ast.PVar "insert", "fix (fun insert -> fun bst -> fun x ->
  19. match bst with
  20. | Leaf() -> Node(x, (Leaf(), Leaf()))
  21. | Node(v, (left, right)) ->
  22. if x < v then Node(v, (insert left x, right))
  23. else if x > v then Node(v, (left, insert right x))
  24. else Node(v, (left, right))
  25. end)"
  26. ; Ast.PVar "flatten", "fix (fun flatten -> fun bst ->
  27. match bst with
  28. | Leaf() -> Nil()
  29. | Node(v, (left, right)) -> append (append (flatten left) (Cons(v, Nil()))) (flatten right)
  30. end)"
  31. ; Ast.PVar "sort", "fun xs ->
  32. let bst_of_xs = fix(fun bst_of_xs -> fun xs -> fun bst ->
  33. match xs with
  34. | Nil() -> bst
  35. | Cons(h, t) -> bst_of_xs (t) (insert bst h)
  36. end) in
  37. flatten (bst_of_xs (xs) (Leaf()))"
  38. ; Ast.PVar "fold_left", "fix(fun fold_left -> fun f -> fun acc -> fun xs ->
  39. match xs with
  40. | Nil() -> acc
  41. | Cons(h, t) -> fold_left (f) (f acc h) (t)
  42. end)"
  43. ]
  44.  
  45. let parse_string str =
  46. Parser.prog Lexer.token (Lexing.from_string str)
  47.  
  48. let include_def (pat, str) prog =
  49. Ast.Let(pat, parse_string str, prog)
  50.  
  51. let include_stdlib prog =
  52. List.fold_right include_def stdlib prog
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement