Giter Site home page Giter Site logo

ghc-typelits-symbols's Introduction

GHC Plugin to do some calculation on GHC's type-level symbols

What's this?

ghc-typelits-symbols augments haskell with GHC's type-level symbols with basic operations. Example:

{-# LANGUAGE GADTs, OverloadedLabels, TypeFamilies, TypeOperators #-}
{-# LANGUAGE DataKinds, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables #-}
{-# LANGUAGE ConstraintKinds #-}
{-# OPTIONS_GHC -fplugin GHC.TypeLits.Symbols.Solver #-}
{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}
module Main where
import GHC.TypeLits
import GHC.TypeLits.Symbols
import GHC.OverloadedLabels
import Data.Singletons.TypeLits
import Data.Singletons.Prelude.Maybe
import Data.Singletons.Prelude

type n < m = (n + 1) <= m

data Fin (n :: Nat) where
  Fin :: (KnownNat n, n < m) => SNat n -> Fin m

instance Show (Fin n) where
  showsPrec d (Fin sn) = showsPrec d $ natVal sn

instance (ReadInt (FromJust (StripPrefix "o" lab)) < n, KnownSymbol lab)
      => IsLabel lab (Fin n) where
  fromLabel pxy = reifyNat (read $ tail $ symbolVal' pxy) $ \(Proxy :: Proxy k) ->
    {- Some dirty but safe hacks -}

main :: IO ()
main = do
  print (#o1 :: Fin 4)
  -- print (#o5 :: Fin 2) -- Won't compile!

Functions

Basic supported operations:

  • ViewSymbol{.haskell}: type-level function to deconstruct the given symbol as below:
    1. ViewSymbol "" ~ 'SymNil
    2. ViewSymbol "abc" ~ 'SymCons "a" "bc"
  • Type-level appending operator (+++): "123" +++ "456" ~ "123456"

These operations can't be reduced by GHCi's :kind! command, but GHC simplifies operations on Symbols defined in terms of ViewSymbol and (+++) are reduced to simpler form as much as possible in the type-check phase.

This package currently provides the following aux functions defined in terms of aboves:

  • ReadInt
  • Head
  • Tail

ghc-typelits-symbols's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.