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.

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