Giter VIP home page Giter VIP logo

validated-literals's Introduction

validated-literals: Compile-time checking for partial smart-constructors

BSD3 Hackage hackage-ci Build Status

To disallow invalid input it is common to define (new)types with hidden data constructors. Forcing the user to go through a smart-constructor that enforces invariants and returns Maybe ResultType, preventing the construction of data with invalid values.

However, it is also common to want to include literal values of such types in source text. Things of textual literals for HTML, HTTP, etc. In such cases smart-constructors force us to handle potential conversion failures at runtime, or abusing functions like fromJust to break away all the safety smart-constructors provide. All this despite the fact that we can statically know at compile time that the conversion will always succeed or always fails.

This package provides a typeclasses for using TH to validate the correctness of provided literals at compile. This lets you define, e.g., newtype Even = Even Integer and write:

x :: Even
x = $$(valid 38)

This will check, at compile time, that the provided Integer is, in fact, even and unwrap it from Maybe, avoiding the runtime check.

validated-literals's People

Contributors

merijn avatar dminuoso avatar

Stargazers

Julian Ospald avatar  avatar olligobber avatar Vladimir Kalnitsky avatar Anatolii Prylutskyi avatar Jeff Carpenter avatar Hal Henke avatar Serhii Khoma avatar chessai avatar Evan Relf avatar Daniel Mendler avatar Josh Miller avatar Stephen Diehl avatar Veronika Romashkina avatar Dmitrii Kovanikov avatar  avatar Aleksei Vagarenko avatar  avatar Nikita Churaev avatar Ryan Scott avatar

Watchers

 avatar James Cloos avatar  avatar

validated-literals's Issues

Raise a CLC/GHC proposal

Since IsString is a broken interface, I suggest to raise a GHC and CLC proposal (since the class will need to be added to base).

I think the ByteString discussion is enough evidence that there is a problem.

The new OsString/OsPath interface in filepath is another evidence: it will never support IsString and OverloadedStrings.

So we need a new class and a new GHC extension.

I can't say how this will go on GHC steering committee side, but I can assure you my CLC vote will be +1 and I will vehemently promote it.

I don't have any knowledge about an actual implementation, but my guess is it would follow a similar pattern as OverloadedString and just inject class function at string literals?

One concern was that it will require TH and will make it hard to write trustworthy code. I believe @angerman had an idea of an IO-free TH. Maybe that will complement such an extension, but I think it could be proposed regardless.

Wdyt?

No error when invalid literal used in IO in repl

When an invalid literal is used to create an IO x in ghci, no error occurs.

Prelude> :set -XTemplateHaskell
Prelude> :m Functional.Iota.Free ValidLiterals 
Prelude Functional.Iota.Free ValidLiterals> ($$(valid '(') :: IFree)

<interactive>:3:5: error:
    • Invalid input used for type-safe validated literal!
An error occured during compile-time validation!

    • In the Template Haskell splice $$(valid '(')
      In the expression: ($$(valid '(') :: IFree)
      In an equation for ‘it’: it = ($$(valid '(') :: IFree)

<interactive>:3:5: error:
    • Exception when trying to run compile-time code:
        ValidationFailure "An error occured during compile-time validation!"
      Code: valid '('
    • In the Template Haskell splice $$(valid '(')
      In the expression: ($$(valid '(') :: IFree)
      In an equation for ‘it’: it = ($$(valid '(') :: IFree)
Prelude Functional.Iota.Free ValidLiterals> print ($$(valid '(') :: IFree)
Prelude Functional.Iota.Free ValidLiterals> print ($$(valid 'a') :: IFree)
IFree {fromIFree = 'a'}
Prelude Functional.Iota.Free ValidLiterals>

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.