haskell - Using a monad to implicitly check refinement type well-formedness -


while implementing refinement type system, need put in checks make sure types well-formed. example, type num[100,0] shouldn't happen, num[lb,ub] type of numbers larger lb , smaller ub. wrote:

-- formation rules  class refty t    tyok :: t -> bool  instance refty ty   tyok (numty (n1, n2)) = n1 <= n2         tyok (catty cs) = isset cs  {- data wellformed t = valid t                   | invalid  instance monad wellformed        (>>=) :: refty => wellformed -> (a -> wellformed b) -> wellformed b     valid t >>= f              | tyok t = f t             | otherwise = invalid     invalid >>= _ = invalid -} 

which got me known problem of "restricted monad". suggested answer have wellformed monad general restrict functions. go adding well-formed check everywhere. there better way around?

in case, don't think want monad, sugar accompanies do notation. example, have thought definition of applicative like? things messy fast when try cheat way through this.

instead, if want use do-notation, suggest use

{-# language rebindablesyntax #-} 

which allows redefine, amongst other thing, (>>=) , return used in desugaring do block. write like:

mybind :: refty t1 => wellformed t1 -> (t1 -> wellformed t2) -> wellformed t2 mybind invalid _ = invalid mybind (valid t) f | tyok t = f t                    | otherwise invalid   myreturn :: wellformed t myreturn t = valid t 

i'm not sure agree definitions, regardless able write like

do   ...   (>>=) = mybind         return = myreturn 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -