0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

AOJ day 14: Implementing your own PureScript compiler Type Class

While the current set of compiler type classes are pretty nice, sometimes there's not a class for something I'd like to do. Other times, there's an operation I could perform manually with the existing classes, but the inference and performance of solving these constraints is terrible.

Whatever the reason, it would be nice to define some of my own compiler type classes.

"Implement your own compiler type class in PureScript"

In this post, I go over what all is involved in lining up the pieces in the PureScript compiler codebase to add your own compiler-solved type class.

Once we have all of the pieces in place from a few dozen lines of adding new definitions, documentation, and functional dependencies information, we can write the core logic that solves our instances. For example, checking if a Symbol pattern is contained within another Symbol and getting a Boolean-kinded result:

containsSymbol :: Type -> Type -> Type -> Maybe (Type, Type, Type)
containsSymbol patt@(TypeLevelString patt') sym@(TypeLevelString sym') _ = do
  flag <- T.isInfixOf <$> decodeString patt' <*> decodeString sym'
  pure (patt, sym, TypeConstructor $ if flag then C.booleanTrue else C.booleanFalse)
containsSymbol _ _ _ = Nothing

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?