You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. open import "prelude.ml"
  2. module S = import "data/set.ml"
  3. module M = import "data/map.ml"
  4. type expr =
  5. | Ref of string
  6. | App of expr * expr
  7. | Lam of string * expr
  8. | Case of expr * list (string * list string * expr)
  9. | If of expr * expr * expr
  10. | Lit of int
  11. | Let of list (string * expr) * expr
  12. let app = curry App
  13. let lam = curry Lam
  14. let rec free_vars = function
  15. | Ref v -> S.singleton v
  16. | App (f, x) -> S.union (free_vars f) (free_vars x)
  17. | Lam (v, x) -> v `S.delete` free_vars x
  18. | Case (e, bs) ->
  19. bs
  20. |> map (fun (_, a, e) -> free_vars e `S.difference` S.from_list a)
  21. |> foldl S.union S.empty
  22. |> S.union (free_vars e)
  23. | Let (vs, b) ->
  24. let bound = S.from_list (map (fun (x, _) -> x) vs)
  25. vs
  26. |> map (fun (_, e) -> free_vars e)
  27. |> foldl S.union S.empty
  28. |> S.union (free_vars b)
  29. |> flip S.difference bound
  30. | If (f, x, y) ->
  31. S.union (free_vars f) (S.union (free_vars x) (free_vars y))
  32. | Lit _ -> S.empty
  33. type hstype =
  34. | Tycon of string
  35. | Tyvar of string
  36. | Tyapp of hstype * hstype
  37. | Tyarr of hstype * hstype
  38. | Tytup of list hstype
  39. let rec free_cons = function
  40. | Tycon v -> S.singleton v
  41. | Tyvar _ -> S.empty
  42. | Tyapp (f, x) -> S.union (free_cons f) (free_cons x)
  43. | Tyarr (f, x) -> S.union (free_cons f) (free_cons x)
  44. | Tytup xs -> foldl (fun s x -> S.union s (free_cons x)) S.empty xs
  45. let rec arity = function
  46. | Tyarr (_, i) -> 1 + arity i
  47. | _ -> 0
  48. type constr = Constr of string * list hstype
  49. type call_conv = Lua | Prim
  50. type fdecl =
  51. Fimport of {
  52. cc : call_conv,
  53. fent : string,
  54. ftype : hstype,
  55. var : string
  56. }
  57. type decl =
  58. | Decl of string * list string * expr
  59. | Data of string * list string * list constr
  60. | Foreign of fdecl
  61. type prog <- list decl