-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement type-level string append function #726
Changes from all commits
4b9e944
b61fa89
0c7cab2
a6a5a3e
1155d5f
b5360c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
module NumType(opNumT, numOpNames) where | ||
-- common routines for handling numeric types | ||
module TypeOps(opNumT, numOpNames, opStrT, strOpNames) where | ||
-- common routines for handling numeric and string types | ||
|
||
import Id | ||
import PreIds(idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin) | ||
import PreIds(idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin, idTStrCat, idTNumToStr) | ||
import Util(divC, log2) | ||
import FStringCompat(FString, concatFString) | ||
|
||
-- do a numeric type operation on a list of arguments | ||
-- note that we have to validate that the result is going to | ||
|
@@ -20,4 +21,11 @@ opNumT i [x, y] | i == idTMin = Just (min x y) | |
opNumT _ _ = Nothing | ||
|
||
numOpNames :: [Id] | ||
numOpNames = [idTAdd, idTSub, idTMul, idTDiv, idTExp, idTLog, idTMax, idTMin] | ||
numOpNames = [idTAdd, idTSub, idTMul, idTDiv, idTExp, idTLog, idTMax, idTMin, idTNumToStr] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice that this is used in Although, if we could avoid having lists that need to be kept in sync with functions, that'd be nice. For example, I wonder if the function can be changed to return a Maybe (using Nothing for unrecognized TFun IDs) and then you wouldn't need the separate list. But anyway, that's not required for your PR; it's sufficient to just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that the |
||
opStrT :: Id -> [FString] -> Maybe FString | ||
opStrT i xs | i == idTStrCat = Just $ concatFString xs | ||
opStrT _ _ = Nothing | ||
|
||
strOpNames :: [Id] | ||
strOpNames = [idTStrCat] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package TNumToStr where | ||
|
||
data (WrapStr :: $ -> *) s = WrapStr | ||
data (WrapNum :: # -> *) s = WrapNum | ||
|
||
printWrapStr :: WrapStr s -> Action | ||
printWrapStr _ = $display (stringOf s) | ||
|
||
a :: WrapStr (TNumToStr 42) | ||
a = WrapStr | ||
|
||
class FoldNumsStr a s | a -> s where {} | ||
|
||
instance (FoldNumsStr a s) => FoldNumsStr (WrapNum i, a) (TStrCat (TNumToStr i) (TStrCat "_" s)) where {} | ||
instance FoldNumsStr (WrapNum i) (TNumToStr i) where {} | ||
instance FoldNumsStr () "" where {} | ||
|
||
b :: (FoldNumsStr (WrapNum 1, WrapNum 22, WrapNum 333) s) => WrapStr s | ||
b = WrapStr | ||
|
||
c :: (FoldNumsStr () s) => WrapStr s | ||
c = WrapStr | ||
|
||
sysTNumToStr :: Module Empty | ||
sysTNumToStr = module | ||
|
||
rules | ||
when True ==> do | ||
printWrapStr a | ||
printWrapStr b | ||
printWrapStr c | ||
$finish |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package TStrCat where | ||
|
||
data (WrapStr :: $ -> *) s = WrapStr | ||
|
||
printWrapStr :: WrapStr s -> Action | ||
printWrapStr _ = $display (stringOf s) | ||
|
||
a :: WrapStr (TStrCat "aaa" "bbb") | ||
a = WrapStr | ||
|
||
class FlatWrapStr a s | a -> s where {} | ||
|
||
instance (FlatWrapStr a s2) => FlatWrapStr (WrapStr s1, a) (TStrCat s1 (TStrCat "_" s2)) where {} | ||
instance FlatWrapStr (WrapStr s) s where {} | ||
instance FlatWrapStr () "" where {} | ||
|
||
b :: (FlatWrapStr (WrapStr "aaa", WrapStr "bbb", WrapStr "ccc") s) => WrapStr s | ||
b = WrapStr | ||
|
||
c :: (FlatWrapStr () s) => WrapStr s | ||
c = WrapStr | ||
|
||
sysTStrCat :: Module Empty | ||
sysTStrCat = module | ||
|
||
rules | ||
when True ==> do | ||
printWrapStr a | ||
printWrapStr b | ||
printWrapStr c | ||
$finish |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
42 | ||
1_22_333 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
aaabbb | ||
aaa_bbb_ccc | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you have thoughts on whether this should be one subsection versus two separate subsections ("Operations on Numeric Types" and "Operations on String Types")?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no strong opinions here. I don't think there will be very many more string type operations added in the future, though.