oops, i already implemented an applicative
This commit is contained in:
commit
a577bddd7f
15 changed files with 321 additions and 0 deletions
6
src/Language/Java/Classfile.hs
Normal file
6
src/Language/Java/Classfile.hs
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Language.Java.Classfile () where
|
||||
import Language.Java.Classfile.Version (Version)
|
||||
|
||||
data Classfile = Classfile
|
||||
{ version :: Version
|
||||
}
|
39
src/Language/Java/Classfile/Extract.hs
Normal file
39
src/Language/Java/Classfile/Extract.hs
Normal file
|
@ -0,0 +1,39 @@
|
|||
{-# LANGUAGE DeriveFunctor #-}
|
||||
{-# LANGUAGE InstanceSigs #-}
|
||||
module Language.Java.Classfile.Extract (Extract()) where
|
||||
|
||||
import Data.ByteString.Lazy (ByteString)
|
||||
|
||||
data Extract a = Extract (Continuation a)
|
||||
|
||||
type Continuation a = ByteString -> Reply a
|
||||
|
||||
data Reply a
|
||||
= Done ByteString a -- rest, result
|
||||
| Fail
|
||||
deriving (Functor)
|
||||
|
||||
instance Functor Extract where
|
||||
fmap :: (a -> b) -> Extract a -> Extract b
|
||||
fmap f (Extract cont) = Extract $ \ input -> f <$> cont input
|
||||
|
||||
instance Applicative Extract where
|
||||
pure :: a -> Extract a
|
||||
pure x = Extract $ \ rest -> Done rest x
|
||||
(<*>) :: Extract (a -> b) -> Extract a -> Extract b
|
||||
(<*>) (Extract computeF) (Extract computeX) = Extract $ \ input -> case computeF input of
|
||||
Done rest f -> case computeX rest of
|
||||
Done rest' x -> Done rest' (f x)
|
||||
Fail -> Fail
|
||||
Fail -> Fail
|
||||
|
||||
|
||||
|
||||
{- It seems I cannot define a lawful monad instance
|
||||
instance Monad Extract where
|
||||
(>>=) :: Extract a -> (a -> Extract b) -> Extract b
|
||||
(>>=) (Extract computeA) f = Extract $ \ input -> case computeA input of
|
||||
Done rest a -> _
|
||||
Fail -> Fail
|
||||
-}
|
||||
|
9
src/Language/Java/Classfile/Extractable.hs
Normal file
9
src/Language/Java/Classfile/Extractable.hs
Normal file
|
@ -0,0 +1,9 @@
|
|||
module M () where
|
||||
|
||||
|
||||
class Extractable a where
|
||||
extract :: Extract a
|
||||
|
||||
instance Extractable Word8 where
|
||||
extract :: Extract Word8
|
||||
extract = _
|
8
src/Language/Java/Classfile/Version.hs
Normal file
8
src/Language/Java/Classfile/Version.hs
Normal file
|
@ -0,0 +1,8 @@
|
|||
module Language.Java.Classfile.Version (Version(..)) where
|
||||
|
||||
import Data.Word (Word16)
|
||||
|
||||
data Version = Version
|
||||
{ minor :: Word16
|
||||
, major :: Word16
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue