Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. So I want to make a strong static typed Lisp that is as espressive and as concise as a dynamic typed Lisp.
  2.  
  3. I want do this by having the type checker be able to infer types as much as possible.
  4.  
  5. ```
  6. (defn identity [x] x)
  7. -> namespace/identity[A](A): A
  8.  
  9. (defn fun [f col] (map f col))
  10. -> namespace/fun[A, B](A -> B, Functor<A>): Functor[B]
  11. ```
  12.  
  13. In this example, the compiler should be able to understand the types implicitly from the body of the function.
  14.  
  15. Something like this wouldn't compile though
  16.  
  17. ```
  18. ((fn [f col] (map f col)) identity 1)
  19. -> TypeClassNotFoundError. Could not find instance of Functor for Int
  20. other information like line number and what symbol caused the type error
  21.  
  22. (() identity)
  23. -> NotAFunctionError. Unit is not a Function
  24. line number and symbol
  25. ```
  26.  
  27. Also unlike Unit, Map and Vector are functions. (A) -> B and (Int) -> A respectively.
  28.  
  29. ```
  30. (def m {:a 1 :b 2})
  31. (dev v [:a :b])
  32.  
  33. (m :a)
  34. -> 1
  35.  
  36. (m 12)
  37. -> Nil
  38.  
  39. (v 1)
  40. -> :b
  41.  
  42. (v 2)
  43. -> Nil
  44. ```
  45.  
  46. Accessing an index that does not exist returns Nil not throw an exception.
  47.  
  48. It's also possible to annotate all of the types
  49.  
  50. ```
  51. (defn fun [f: (Int) -> Int col: Vector[Int]] [Vector[Int]] (map f col))
  52. ```
  53.  
  54. To make this possible, the type checker should be able to understand typeclasses.
  55.  
  56. ```
  57. (def-typeclass Functor[A[_]] (pure [B] [A[B]]) (map [(B) -> C A[B]] [A[C]]))
  58.  
  59. (def-type Maybe (Just value) Nil)
  60. (def-functor Maybe
  61. (pure [value] (Just value))
  62. (map [f this]
  63. (when-not nil? (Just value))))
  64. ```
  65.  
  66. So im trying to think how I actually want to define types. I like the idea of a super type followed by subtypes and their parameters.
  67.  
  68. ```
  69. (def-type Maybe (Just value) Nil)
  70. (def-type Tree (Branch left: Tree right: Tree) Nil)
  71. (def-type List (Cons head tail: List) Nil)
  72. ```
  73.  
  74. but how do I differentiate that between fields.
  75.  
  76. ```
  77. (def-type TypeA some-generic some-int: Int)
  78. ```
  79.  
  80. maybe I enclose fields in a vector
  81.  
  82. ```
  83. (def-type Maybe (Just [value]) Nil)
  84. (def-type Tree (Branch [left: Tree right: Tree]) Nil)
  85. (def-type List (Cons [head tail: List]) Nil)
  86. (def-type TypeA [some-generic some-int: Int])
  87. ```
  88.  
  89. Do I want to handle inheritence?
  90.  
  91. How do I handle sub types that use that provide explicit type parameters to the super type?
  92.  
  93. ```
  94. (def-type Xor [A B] (Left [A Nothing] [left: A]) (Right [Nothing B] [right: B]))
  95. ```
  96.  
  97. How do I handle inheritence that overrides or provides a default value to a super type
  98.  
  99. ```
  100. (def-type Animal [name: String] (Ferret [name = "Ferret"]))
  101. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement