Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. % Note: There are some problems with this type system... :P
  2.  
  3. :- discontiguous(type/2).
  4. :- discontiguous(convert/2).
  5. :- style_check(-singleton).
  6.  
  7. /* implict conversions */
  8. subtype(X, T2) :- type(X, T1), distinct(convert(T1, T2)).
  9. convert(T, T).
  10.  
  11. /* function */
  12. type(apply(F, X), TY) :- subtype(F, TX -> TY), subtype(X, TX). % application
  13. convert(A1 -> B1, A2 -> B2) :- convert(A2, A1), convert(B1, B2).
  14. %type(lambda(X, Y), T -> V) :- var(X), subtype(Y, V), distinct(type(X, T)). Doesn't work :<
  15.  
  16. /* basic combinators */
  17. type(id, T -> T).
  18. type(const, T -> V -> T).
  19. type(fatal_error, string -> T). % can be used anywhere since crashes
  20.  
  21. /* bool */
  22. type(true, bool).
  23. type(false, bool).
  24. type(not(X), bool) :- subtype(X, bool).
  25. type(and(X, Y), bool) :- subtype(X, bool), subtype(Y, bool).
  26. type(or(X, Y), bool) :- subtype(X, bool), subtype(Y, bool).
  27. type(xor(X, Y), bool) :- subtype(X, bool), subtype(Y, bool).
  28. type(if_then_else(X, Y, N), T) :- subtype(X, bool), subtype(Y, T), subtype(N, T).
  29.  
  30. /* int */
  31. type(X, int) :- distinct(integer(X)). % built-in integer literal
  32. type(X + Y, int) :- subtype(X, int), subtype(Y, int).
  33. type(X - Y, int) :- subtype(X, int), subtype(Y, int).
  34. type(X * Y, int) :- subtype(X, int), subtype(Y, int).
  35. type(X / Y, int) :- subtype(X, int), subtype(Y, int).
  36. type(square, int -> int).
  37.  
  38. /* big_int */
  39. type(X, big_int) :- distinct(integer(X)). % built-in integer literal
  40. type(X + Y, big_int) :- subtype(X, big_int), subtype(Y, big_int).
  41. type(X - Y, big_int) :- subtype(X, big_int), subtype(Y, big_int).
  42. type(X * Y, big_int) :- subtype(X, big_int), subtype(Y, big_int).
  43. type(X / Y, big_int) :- subtype(X, big_int), subtype(Y, big_int).
  44. convert(int, big_int).
  45.  
  46. /* float */
  47. type(X, float) :- distinct(float(X)).
  48. type(X, float) :- distinct(integer(X)).
  49. type(X + Y, float) :- subtype(X, float), subtype(Y, float).
  50. type(X - Y, float) :- subtype(X, float), subtype(Y, float).
  51. type(X * Y, float) :- subtype(X, float), subtype(Y, float).
  52. type(X / Y, float) :- subtype(X, float), subtype(Y, float).
  53.  
  54. /* list */
  55. type([X|XS], apply(list, T)) :- subtype(X, T), subtype(XS, apply(list, T)).
  56. type([], apply(list, _)).
  57. type(X + Y, apply(list, T)) :- type(X, apply(list, T)), type(Y, apply(list, T)).
  58. type(cons(X, Y), apply(list, T)) :- type(X, T), type(Y, apply(list, T)).
  59.  
  60. /* string */
  61. type(X, string) :- distinct(string(X)). % built-in string literal
  62. type(reverse(X), string) :- subtype(X, string).
  63. type(char_at(X, I), char) :- subtype(X, string), subtype(I, int).
  64. convert(string, apply(list, char)).
  65. convert(apply(list, char), string).
  66.  
  67. /* optional */
  68. type(apply(some, X), apply(optional, T)) :- type(X, T).
  69. type(none, apply(optional, _)).
  70.  
  71. /* functor */
  72. conform(list, functor).
  73. conform(optional, functor).
  74. type(map, (T1 -> T2) -> apply(F1, T1) -> apply(F2, T2)) :- conform(F1, functor), conform(F2, functor).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement