gelisam / klister

an implementation of stuck macros
BSD 3-Clause "New" or "Revised" License
129 stars 11 forks source link

lowercase naming convention affects variable naming convention #189

Open gelisam opened 1 year ago

gelisam commented 1 year ago

In Haskell, if I have a variable of type Foo, I like to name it foo:

data Foo = Foo Int

f :: Foo -> Int

g :: Foo -> Int
g foo
  = case foo of 
      Foo x -> x + f foo

But in Klister, the name foo is already taken by the data constructor, so I have to pick a different name:

(data (Foo)
  (foo Integer))

(define f (the (-> Foo Integer) ...))

(defun g (foo_)
  (case foo_
    [(foo x)
     (+ x (f foo_))]))

If we're going to be opinionated and recommend that Klister users use foo for their data constructor, I think we should also guide the user and recommend a default name for variables of type Foo.

gelisam commented 1 year ago

I propose to extend the prelude.kl language (but not the kernel) to support the following syntax in which a variable with the type Foo can be given the anonymous name (the Foo) (while still allowing (the Foo expr) to act as a type ascription).

(data (Foo)
  (foo Integer))

(define f (the (-> Foo Integer) ...))

(defun g ((the Foo))
  (case (the Foo)
    [(foo x)
     (+ x (f (the Foo)))]))
gelisam commented 1 year ago

A simpler alternative would be to recommend the naming convention the-foo for variables of type Foo, but my recommendation has the advantage that it can be extended into a syntax for specifying the type of the other arguments:

(defun f ((the Foo) (the Int n))
  (case (the Foo)
    [(foo x)
     (+ x n)]))

It might be worth adding more syntax sugar:

(defun f (:Foo n:Int)
  (case :Foo
    [(foo x)
     (+ x n)]))