mirror of
https://github.com/chrisdone/duet.git
synced 2024-08-16 07:40:39 +03:00
Add Slice class
This commit is contained in:
parent
8c3eb90c3d
commit
dc886419fd
@ -1,3 +1,4 @@
|
||||
main = case "foo" of
|
||||
main =
|
||||
case "foo" of
|
||||
"bar" -> 0
|
||||
"foo" -> 1
|
||||
|
1
examples/string-substring.hs
Normal file
1
examples/string-substring.hs
Normal file
@ -0,0 +1 @@
|
||||
main = take 2 (drop 7 "Hello, World!")
|
@ -72,6 +72,8 @@ printPrimop =
|
||||
PrimopRationalPlus -> "plus"
|
||||
PrimopRationalDivide -> "divide"
|
||||
PrimopStringAppend -> "append"
|
||||
PrimopStringDrop -> "drop"
|
||||
PrimopStringTake -> "take"
|
||||
|
||||
instance Printable Identifier where
|
||||
printit _ =
|
||||
|
@ -61,6 +61,7 @@ setupEnv env typeMakers = do
|
||||
(negClass, subtract') <- makeNegClass function
|
||||
(fracClass, divide) <- makeFracClass function
|
||||
(monoidClass) <- makeMonoidClass function
|
||||
(sliceClass) <- makeSliceClass (specialTypesInteger specialTypes) function
|
||||
boolSigs <- dataTypeSignatures specialTypes boolDataType
|
||||
typesSigs <-
|
||||
fmap
|
||||
@ -70,7 +71,7 @@ setupEnv env typeMakers = do
|
||||
classSigs <-
|
||||
fmap
|
||||
concat
|
||||
(mapM classSignatures [numClass, negClass, fracClass, monoidClass])
|
||||
(mapM classSignatures [numClass, negClass, fracClass, monoidClass, sliceClass])
|
||||
primopSigs <- makePrimOps specialTypes
|
||||
let signatures = boolSigs <> classSigs <> primopSigs <> typesSigs
|
||||
specialSigs =
|
||||
@ -83,6 +84,25 @@ setupEnv env typeMakers = do
|
||||
, specialSigsDivide = divide
|
||||
}
|
||||
specials = Specials specialSigs specialTypes
|
||||
stringSlice <-
|
||||
makeInst
|
||||
specials
|
||||
(IsIn
|
||||
(className sliceClass)
|
||||
[ConstructorType (specialTypesString specialTypes)])
|
||||
[ ( "take"
|
||||
, ( ()
|
||||
, Alternative
|
||||
()
|
||||
[]
|
||||
(VariableExpression () (PrimopName PrimopStringTake))))
|
||||
, ( "drop"
|
||||
, ( ()
|
||||
, Alternative
|
||||
()
|
||||
[]
|
||||
(VariableExpression () (PrimopName PrimopStringDrop))))
|
||||
]
|
||||
stringMonoid <-
|
||||
makeInst
|
||||
specials
|
||||
@ -181,9 +201,11 @@ setupEnv env typeMakers = do
|
||||
addClass negClass >=>
|
||||
addClass fracClass >=>
|
||||
addClass monoidClass >=>
|
||||
addClass sliceClass >=>
|
||||
addInstance numInt >=>
|
||||
addInstance negInt >=>
|
||||
addInstance stringMonoid >=>
|
||||
addInstance stringSlice >=>
|
||||
addInstance fracRational >=>
|
||||
addInstance negRational >=> addInstance numRational
|
||||
in update env
|
||||
@ -236,7 +258,15 @@ makePrimOps SpecialTypes {..} = do
|
||||
PrimopStringAppend ->
|
||||
TypeSignature
|
||||
(PrimopName PrimopStringAppend)
|
||||
(toScheme (string --> string --> string))))
|
||||
(toScheme (string --> string --> string))
|
||||
PrimopStringTake ->
|
||||
TypeSignature
|
||||
(PrimopName PrimopStringTake)
|
||||
(toScheme (integer --> string --> string))
|
||||
PrimopStringDrop ->
|
||||
TypeSignature
|
||||
(PrimopName PrimopStringDrop)
|
||||
(toScheme (integer --> string --> string))))
|
||||
[minBound .. maxBound]
|
||||
pure sigs
|
||||
where
|
||||
@ -327,3 +357,22 @@ makeMonoidClass function = do
|
||||
infixr 1 -->
|
||||
(-->) :: Type Name -> Type Name -> Type Name
|
||||
a --> b = ApplicationType (ApplicationType (ConstructorType function) a) b
|
||||
|
||||
makeSliceClass :: MonadSupply Int m => TypeConstructor Name -> TypeConstructor Name -> m (Class Type Name l)
|
||||
makeSliceClass integer' function = do
|
||||
a <- fmap (\n -> TypeVariable n StarKind) (supplyTypeName "a")
|
||||
let a' = VariableType a
|
||||
drop' <- supplyMethodName "drop"
|
||||
take' <- supplyMethodName "take"
|
||||
cls <-
|
||||
makeClass
|
||||
"Slice"
|
||||
[a]
|
||||
[ (drop', Forall [a] (Qualified [] (ConstructorType integer' --> (a' --> a'))))
|
||||
, (take', Forall [a] (Qualified [] (ConstructorType integer' --> (a' --> a'))))
|
||||
]
|
||||
pure cls
|
||||
where
|
||||
infixr 1 -->
|
||||
(-->) :: Type Name -> Type Name -> Type Name
|
||||
a --> b = ApplicationType (ApplicationType (ConstructorType function) a) b
|
||||
|
@ -100,6 +100,19 @@ expandWhnf typeClassEnv specialSigs signatures e b = go e
|
||||
y' <- go y
|
||||
pure
|
||||
(ApplicationExpression l (ApplicationExpression l1 op x) y')
|
||||
LiteralExpression _ (IntegerLiteral n) ->
|
||||
case y of
|
||||
LiteralExpression _ (StringLiteral sy) ->
|
||||
case primop of
|
||||
PrimopStringTake ->
|
||||
pure (LiteralExpression l (StringLiteral (genericTake n sy)))
|
||||
PrimopStringDrop ->
|
||||
pure (LiteralExpression l (StringLiteral (genericDrop n sy)))
|
||||
_ -> error "Runtime type error that should not occurr"
|
||||
_ -> do
|
||||
y' <- go y
|
||||
pure
|
||||
(ApplicationExpression l (ApplicationExpression l1 op x) y')
|
||||
_ -> do
|
||||
x' <- go x
|
||||
pure (ApplicationExpression l (ApplicationExpression l1 op x') y)
|
||||
|
@ -167,6 +167,8 @@ data Primop
|
||||
| PrimopRationalSubtract
|
||||
| PrimopRationalTimes
|
||||
| PrimopStringAppend
|
||||
| PrimopStringDrop
|
||||
| PrimopStringTake
|
||||
deriving (Show, Generic, Data, Typeable, Eq, Ord, Enum, Bounded)
|
||||
|
||||
-- | State of inferring.
|
||||
|
10
test/Spec.hs
10
test/Spec.hs
@ -70,9 +70,9 @@ spec =
|
||||
(Alternative
|
||||
{ alternativeLabel = ()
|
||||
, alternativePatterns =
|
||||
[VariablePattern () (ValueName 42 "x")]
|
||||
[VariablePattern () (ValueName 49 "x")]
|
||||
, alternativeExpression =
|
||||
VariableExpression () (ValueName 42 "x")
|
||||
VariableExpression () (ValueName 49 "x")
|
||||
}))
|
||||
(LiteralExpression () (IntegerLiteral 1))
|
||||
, LiteralExpression () (IntegerLiteral 1)
|
||||
@ -106,13 +106,13 @@ spec =
|
||||
pure things)
|
||||
[1 ..])))))
|
||||
(Right
|
||||
((CaseExpression
|
||||
(CaseExpression
|
||||
()
|
||||
(VariableExpression () (ValueName 42 "loop"))
|
||||
(VariableExpression () (ValueName 49 "loop"))
|
||||
[ CaseAlt
|
||||
{ caseAltLabel = ()
|
||||
, caseAltPattern = BangPattern (WildcardPattern () "_")
|
||||
, caseAltExpression =
|
||||
LiteralExpression () (IntegerLiteral 1)
|
||||
}
|
||||
])))))
|
||||
]))))
|
||||
|
Loading…
Reference in New Issue
Block a user