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.

62 lines
1.4 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 arity = function
  37. | Tyarr (_, i) -> 1 + arity i
  38. | _ -> 0
  39. type constr = Constr of string * list hstype
  40. type call_conv = Lua | Prim
  41. type fdecl =
  42. Fimport of {
  43. cc : call_conv,
  44. fent : string,
  45. ftype : hstype,
  46. var : string
  47. }
  48. type decl =
  49. | Decl of string * list string * expr
  50. | Data of string * list string * list constr
  51. | Foreign of fdecl
  52. type prog <- list decl