r/haskell • u/pomone08 • May 19 '22
question How do you work around reserved keywords?
Hello, everyone!
I am writing a type checker in Haskell, and I am frequently annoyed by the fact that I can't name a variable as type.
In Rust, you can append the prefix r# to an identifier (like r#type), but it feels so noisy. Glad I never had to use it in practice.
That made me curious: how do you guys work around this kind of problem in Haskell?
19
u/Noughtmare May 19 '22
I've used typ (when also using exp for expressions) and type' before.
4
u/overwritten-entry May 19 '22
I like
expfor expressions, buttypandtype'both seem misleading10
u/Noughtmare May 19 '22
How can
typbe more misleading thanexp?7
u/jonathancast May 19 '22
The standard abbreviation is ty (or τ), so typ might imply you're doing something weird
13
u/LordGothington May 19 '22
oOo.
τis a valid identifier in Haskell -- but I've never been able to convince myself that using Greek letters in code is a good idea.17
May 19 '22
[removed] — view removed comment
1
u/vinnceboi May 19 '22
But not for top-level identifiers, right? I’d hate to be constantly copy-pasting a single symbol…
6
u/bss03 May 19 '22
You could always use an input method or macro system so that when you type
\,t,a,uit turns into τ.9
u/vinnceboi May 19 '22
My poor coworkers
10
u/evincarofautumn May 19 '22
There’s only one way we’re getting out of the sixties, and if I make some enemies in the process, so be it
2
u/ObsessedJerk Jun 06 '22
One could also remap caps lock or another underused key into "mode switch" or "ISO Level 3 Shift" or whatever, and set up key maps so that when
tis pressed in combination with this modifier key it producesτ2
15
u/bss03 May 19 '22 edited May 19 '22
In addition to most of the other answers, I think I've seen tipe in the wild somewhere. (And that reminds me of using clazz in Java when I couldn't use class.)
3
u/Dasher38 May 19 '22
In python it's quite common to see typ and klass
2
u/bss03 May 19 '22
I thought there was a PEP that encouraged <keyword>_ for when the natural identifier conflicts with a language keyword.
But, either of those is also readable.
2
u/Dasher38 May 19 '22
I didn't know there was a pep about that but yes it's the convention. It can still be annoying and look somewhat ugly so alternative spellings are used sometimes. Class is also abbreviated in cls most of the time
2
u/bss03 May 19 '22 edited May 20 '22
PEP 8 in the same paragraph recommends both
clsandclass_when you are conflicted with theclasskeyword (and anti-recommends [decremends?]clss)2
1
u/ObsessedJerk Jun 06 '22
Ouch. This is such an eyezore. Is
tipetotipasdogeis todog? It's not likeclazzis better, anyway. I'd rather writeclasse, which is at least a real word.1
u/bss03 Jun 06 '22
Is tipe to tip as doge is to dog?
No. It's just typing to be a homophone of "type". At least in U.S. English there are many "-ipe" words that ryme with "type" like "ripe", "swipe", "gripe", so most people will naturally pronounce "tipe" the same as "type".
26
u/LordGothington May 19 '22
I would just use t, ty, typ, or type', but for a more rust-like experience, you could try r'type.
19
9
u/tikhonjelvis May 20 '22
I've been using the convention of adding a trailing underscore to reserved words in both Haskell, so I write type_ and module_. In Haskell I could use type' instead, but I usually read variable names like x' as "different version of x", so I wanted a different convention for working around reserved words. The fact that the same convention works in most languages is a bonus, and I've started doing the same thing in my Python code.
Having a consistent convention is nice. I still think of the variable name as "type" and I don't have to remember what I did to make it legal—it's always type_. Just one less thing to stress my working memory :).
7
5
2
u/qqwy May 20 '22
The convention to append a underscore is at least widely used for eDSLs which want to provide custom implementations for if, case, and similar keywords.
2
u/FuzzyPixelz May 20 '22
keyword’ I don’t see how that’s misleading. It the best thing since sliced bread.
2
u/Ninjanoel May 20 '22
type what? Product Type? Customer Type? just make the variable name more expressive.
2
3
u/Iceland_jack May 19 '22
tуpe
3
u/bss03 May 19 '22
??? I'm not sure what you are advocating here. Using Unicode characters that are confusingly similar to latin letters or invisible, using a language where
typeisn't a keyword, or something else?4
2
u/yairchu May 19 '22
Maybe he’s advocating a self-hosted language where types are the host language types too?
1
u/pbrisbin May 20 '22
Fun story, one of the core models at Freckle is Course. Teachers have Courses of Students only because calling them Classes was so painful.
1
u/ekd123 May 23 '22
This got me thinking how many reserved keywords can be made context-sensitive in Haskell. type is def one of them.
1
u/ObsessedJerk Jun 06 '22
Troubled by the same issue as an OCaml coder. typ feels incomplete and type_ is too long. (How about using a Cyrillic 'у' or 'p', as in tуpe and tyрe?) Anyways I settle on typ or ty.
P.S. I was making an interpreter for OCaml the other day and realized that it's possible to design the parser so that most reserved words (except those that may start an expression) can act as regular identifiers when put into parentheses. This is very similar to how the infix status of symbolic identifiers is cancelled by parentheses in OCaml and some other languages, e.g. (+) a b is equivalent to a+b.
0
u/RedToxiCore May 19 '22
type, kind, sort
2
u/migu3lt May 19 '22
kind is a type of types. Isn't it?
4
u/bss03 May 19 '22
And sort is the type of kinds.
Although that's not true in all systems, but type-kind-sort is common when talking about "pure type systems".
IIRC, GHC lets you opt into TypeInType, which flattens things down quite a bit, and I think GHC always acts like "KindInKind" so sorts are never relevant.
TypeInType and "KindInKind" are both inconsistent, though in ways that are extremely unlikely to affect "real" programs. Agda and most other systems that really care about being consistent mathematical assistants provide the infinite (generally cumulative) universes, with the "type" of "universe n" being "universe n+1".
3
u/MorrowM_ May 19 '22
TypeInTypeis not optional, it's just how the type system works since GHC 8.6. The extension just turns onPolyKinds,DataKinds, andKindSignaturesnow.
20
u/jberryman May 19 '22
pros just use
t