From 4b9e9445182455c9c0a44785d73cefc34d374226 Mon Sep 17 00:00:00 2001 From: Lucas Kramer Date: Thu, 1 Aug 2024 11:54:32 -0700 Subject: [PATCH 1/5] Implement type-level string append function --- src/Libraries/Base1/Prelude.bs | 4 ++- src/comp/CType.hs | 6 +++- src/comp/ISyntax.hs | 5 ++- src/comp/PreIds.hs | 3 +- src/comp/PreStrings.hs | 1 + src/comp/Pred.hs | 4 ++- src/comp/{NumType.hs => TypeOps.hs} | 14 +++++++-- testsuite/bsc.typechecker/string/TApp.bs | 31 +++++++++++++++++++ testsuite/bsc.typechecker/string/string.exp | 1 + .../string/sysTApp.out.expected | 3 ++ 10 files changed, 64 insertions(+), 8 deletions(-) rename src/comp/{NumType.hs => TypeOps.hs} (70%) create mode 100644 testsuite/bsc.typechecker/string/TApp.bs create mode 100644 testsuite/bsc.typechecker/string/sysTApp.out.expected diff --git a/src/Libraries/Base1/Prelude.bs b/src/Libraries/Base1/Prelude.bs index 5c379d024..7bf80b829 100644 --- a/src/Libraries/Base1/Prelude.bs +++ b/src/Libraries/Base1/Prelude.bs @@ -7,7 +7,7 @@ package Prelude( PrimParam(..), PrimPort(..), Bit, Rules, Module, Integer, Real, String, Char, SizeOf, Id__, PrimAction, ActionValue, Action, ActionValue_, ActionWorld, AVStruct, - TAdd, TSub, TMul, TDiv, TLog, TExp, TMax, TMin, + TAdd, TSub, TMul, TDiv, TLog, TExp, TMax, TMin, TApp, Nat(..), IsModule(..), addModuleRules, addRules, @@ -2772,6 +2772,8 @@ primitive type TExp :: # -> # primitive type TMax :: # -> # -> # primitive type TMin :: # -> # -> # +primitive type TApp :: $ -> $ -> $ + ------------------ --- Bit operations diff --git a/src/comp/CType.hs b/src/comp/CType.hs index 21609e355..eed9551a4 100644 --- a/src/comp/CType.hs +++ b/src/comp/CType.hs @@ -71,7 +71,7 @@ import PreIds(idArrow, idPrimPair, idPrimUnit, idBit, idString, import Util(itos) import ErrorUtil import Pragma(IfcPragma) -import NumType +import TypeOps import PVPrint(PVPrint(..)) import FStringCompat @@ -507,6 +507,10 @@ normTAp (TCon (TyCon op _ _)) (TCon (TyNum x xpos)) | isJust (res) = cTNum (fromJust res) (getPosition op) where res = opNumT op [x] +normTAp (TAp (TCon (TyCon op _ _)) (TCon (TyStr x xpos))) (TCon (TyStr y ypos)) + | isJust (res) = cTStr (fromJust res) (getPosition op) + where res = opStrT op [x, y] + normTAp f a = TAp f a getTypeKind :: Type -> Maybe Kind diff --git a/src/comp/ISyntax.hs b/src/comp/ISyntax.hs index c8af1ea6b..756680dc9 100644 --- a/src/comp/ISyntax.hs +++ b/src/comp/ISyntax.hs @@ -96,7 +96,7 @@ import IdPrint import PreIds(idSizeOf, idId, idBind, idReturn, idPack, idUnpack, idMonad, idLiftModule, idBit, idFromInteger) import Backend import Prim(PrimOp(..)) -import NumType +import TypeOps import ConTagInfo import VModInfo(VModInfo, vArgs, vName, VName(..), {- VeriPortProp(..), -} VArgInfo(..), VFieldInfo(..), isParam, VWireInfo) @@ -425,6 +425,9 @@ normITAp (ITAp (ITCon op _ _) (ITNum x)) (ITNum y) | isJust (res) = normITAp (ITCon op _ _) (ITNum x) | isJust (res) = mkNumConT (fromJust res) where res = opNumT op [x] +normITAp (ITAp (ITCon op _ _) (ITStr x)) (ITStr y) | isJust (res) = + ITStr (fromJust res) + where res = opStrT op [x, y] normITAp f@(ITCon op _ _) a | op == idSizeOf && notVar a = -- trace ("normITAp: " ++ ppReadable (ITAp f a)) $ diff --git a/src/comp/PreIds.hs b/src/comp/PreIds.hs index 192621e27..500c59a66 100644 --- a/src/comp/PreIds.hs +++ b/src/comp/PreIds.hs @@ -81,7 +81,7 @@ idPrimSnd = prelude_id_no fsPrimSnd idPrimPair = prelude_id_no fsPrimPair idFalse = prelude_id_no fsFalse idTrue = prelude_id_no fsTrue -idSizeOf, idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin :: Id +idSizeOf, idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin, idTApp :: Id idSizeOf = prelude_id_no fsSizeOf idTAdd = prelude_id_no fsTAdd idTSub = prelude_id_no fsTSub @@ -91,6 +91,7 @@ idTLog = prelude_id_no fsTLog idTExp = prelude_id_no fsTExp idTMax = prelude_id_no fsTMax idTMin = prelude_id_no fsTMin +idTApp = prelude_id_no fsTApp idAction, idPrimAction, idToPrimAction, idFromPrimAction :: Id idAction = prelude_id_no fsAction idPrimAction = prelude_id_no fsPrimAction diff --git a/src/comp/PreStrings.hs b/src/comp/PreStrings.hs index cc06010b6..0d9623724 100644 --- a/src/comp/PreStrings.hs +++ b/src/comp/PreStrings.hs @@ -279,6 +279,7 @@ fsTLog = mkFString "TLog" fsTExp = mkFString "TExp" fsTMax = mkFString "TMax" fsTMin = mkFString "TMin" +fsTApp = mkFString "TApp" fsStaticAssert = mkFString "staticAssert" fsDynamicAssert = mkFString "dynamicAssert" fsContinuousAssert = mkFString "continuousAssert" diff --git a/src/comp/Pred.hs b/src/comp/Pred.hs index f91093565..1456592a4 100644 --- a/src/comp/Pred.hs +++ b/src/comp/Pred.hs @@ -20,7 +20,7 @@ import Position import Id import IdPrint import Type -import NumType +import TypeOps import PFPrint import CSyntax(CExpr) import CType @@ -267,6 +267,8 @@ apTFun :: Type -> Id -> [Type] -> Type apTFun _ i [TCon (TyNum x px), TCon (TyNum y py)] | Just n <- opNumT i [x, y] = TCon (TyNum n p') where p' = bestPosition px py apTFun _ i [TCon (TyNum x px)] | Just n <- opNumT i [x] = TCon (TyNum n px) +apTFun _ i [TCon (TyStr x px), TCon (TyStr y py)] | Just s <- opStrT i [x, y] = TCon (TyStr s p') + where p' = bestPosition px py apTFun t _ as = foldl TAp t as ----------------------------------------------------------------------------- diff --git a/src/comp/NumType.hs b/src/comp/TypeOps.hs similarity index 70% rename from src/comp/NumType.hs rename to src/comp/TypeOps.hs index 6446a55df..24b82b6ab 100644 --- a/src/comp/NumType.hs +++ b/src/comp/TypeOps.hs @@ -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, idTApp) 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 @@ -21,3 +22,10 @@ opNumT _ _ = Nothing numOpNames :: [Id] numOpNames = [idTAdd, idTSub, idTMul, idTDiv, idTExp, idTLog, idTMax, idTMin] + +opStrT :: Id -> [FString] -> Maybe FString +opStrT i xs | i == idTApp = Just $ concatFString xs +opStrT _ _ = Nothing + +strOpNames :: [Id] +strOpNames = [idTApp] diff --git a/testsuite/bsc.typechecker/string/TApp.bs b/testsuite/bsc.typechecker/string/TApp.bs new file mode 100644 index 000000000..aac199b89 --- /dev/null +++ b/testsuite/bsc.typechecker/string/TApp.bs @@ -0,0 +1,31 @@ +package TApp where + +data (WrapStr :: $ -> *) s = WrapStr + +printWrapStr :: WrapStr s -> Action +printWrapStr _ = $display (stringOf s) + +a :: WrapStr (TApp "aaa" "bbb") +a = WrapStr + +class FlatWrapStr a s | a -> s where {} + +instance (FlatWrapStr a s2) => FlatWrapStr (WrapStr s1, a) (TApp s1 (TApp "_" 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 + +sysTApp :: Module Empty +sysTApp = module + + rules + when True ==> do + printWrapStr a + printWrapStr b + printWrapStr c + $finish diff --git a/testsuite/bsc.typechecker/string/string.exp b/testsuite/bsc.typechecker/string/string.exp index b3b6581f1..7acab4bb4 100644 --- a/testsuite/bsc.typechecker/string/string.exp +++ b/testsuite/bsc.typechecker/string/string.exp @@ -15,3 +15,4 @@ test_c_veri StringOf test_c_veri_bsv StringOfBSV test_c_veri TypeClassString +test_c_veri TApp diff --git a/testsuite/bsc.typechecker/string/sysTApp.out.expected b/testsuite/bsc.typechecker/string/sysTApp.out.expected new file mode 100644 index 000000000..3246f2a70 --- /dev/null +++ b/testsuite/bsc.typechecker/string/sysTApp.out.expected @@ -0,0 +1,3 @@ +aaabbb +aaa_bbb_ccc + From b61fa899ea26f009eb655bfb4c917fe7aeb0ac6e Mon Sep 17 00:00:00 2001 From: Lucas Kramer Date: Fri, 10 Jan 2025 16:55:29 -0600 Subject: [PATCH 2/5] Rename TApp -> TStrCat, implement TNumToStr --- src/Libraries/Base1/Prelude.bs | 5 +-- src/comp/CType.hs | 6 +++- src/comp/ISyntax.hs | 5 ++- src/comp/PreIds.hs | 6 ++-- src/comp/PreStrings.hs | 3 +- src/comp/TypeOps.hs | 8 ++--- testsuite/bsc.typechecker/string/TNumToStr.bs | 32 +++++++++++++++++++ .../string/{TApp.bs => TStrCat.bs} | 10 +++--- testsuite/bsc.typechecker/string/string.exp | 3 +- .../string/sysTNumToStr.out.expected | 3 ++ ...p.out.expected => sysTStrCat.out.expected} | 0 11 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 testsuite/bsc.typechecker/string/TNumToStr.bs rename testsuite/bsc.typechecker/string/{TApp.bs => TStrCat.bs} (71%) create mode 100644 testsuite/bsc.typechecker/string/sysTNumToStr.out.expected rename testsuite/bsc.typechecker/string/{sysTApp.out.expected => sysTStrCat.out.expected} (100%) diff --git a/src/Libraries/Base1/Prelude.bs b/src/Libraries/Base1/Prelude.bs index 7bf80b829..850557a85 100644 --- a/src/Libraries/Base1/Prelude.bs +++ b/src/Libraries/Base1/Prelude.bs @@ -7,7 +7,7 @@ package Prelude( PrimParam(..), PrimPort(..), Bit, Rules, Module, Integer, Real, String, Char, SizeOf, Id__, PrimAction, ActionValue, Action, ActionValue_, ActionWorld, AVStruct, - TAdd, TSub, TMul, TDiv, TLog, TExp, TMax, TMin, TApp, + TAdd, TSub, TMul, TDiv, TLog, TExp, TMax, TMin, TStrCat, TNumToStr, Nat(..), IsModule(..), addModuleRules, addRules, @@ -2772,7 +2772,8 @@ primitive type TExp :: # -> # primitive type TMax :: # -> # -> # primitive type TMin :: # -> # -> # -primitive type TApp :: $ -> $ -> $ +primitive type TStrCat :: $ -> $ -> $ +primitive type TNumToStr :: # -> $ ------------------ diff --git a/src/comp/CType.hs b/src/comp/CType.hs index eed9551a4..06d968b44 100644 --- a/src/comp/CType.hs +++ b/src/comp/CType.hs @@ -66,7 +66,8 @@ import Position import Id import IdPrint import PreIds(idArrow, idPrimPair, idPrimUnit, idBit, idString, - idPrimAction, idAction, idActionValue_, idActionValue + idPrimAction, idAction, idActionValue_, idActionValue, + idTNumToStr {-, idSizeOf -}) import Util(itos) import ErrorUtil @@ -511,6 +512,9 @@ normTAp (TAp (TCon (TyCon op _ _)) (TCon (TyStr x xpos))) (TCon (TyStr y ypos)) | isJust (res) = cTStr (fromJust res) (getPosition op) where res = opStrT op [x, y] +normTAp (TCon (TyCon op _ _)) (TCon (TyNum x xpos)) + | op == idTNumToStr = cTStr (mkNumFString x) (getPosition op) + normTAp f a = TAp f a getTypeKind :: Type -> Maybe Kind diff --git a/src/comp/ISyntax.hs b/src/comp/ISyntax.hs index 756680dc9..858857d41 100644 --- a/src/comp/ISyntax.hs +++ b/src/comp/ISyntax.hs @@ -93,7 +93,7 @@ import Eval import Id import Wires(ResetId, ClockDomain, ClockId, noClockId, noResetId, noDefaultClockId, noDefaultResetId, WireProps) import IdPrint -import PreIds(idSizeOf, idId, idBind, idReturn, idPack, idUnpack, idMonad, idLiftModule, idBit, idFromInteger) +import PreIds(idSizeOf, idId, idBind, idReturn, idPack, idUnpack, idMonad, idLiftModule, idBit, idFromInteger, idTNumToStr) import Backend import Prim(PrimOp(..)) import TypeOps @@ -105,6 +105,7 @@ import Pragma(Pragma, PProp, RulePragma, ISchedulePragma, extractSchedPragmaIds, removeSchedPragmaIds, mapSPIds) import Position import Data.Maybe +import FStringCompat(mkNumFString) import qualified Data.Set as S import Flags @@ -428,6 +429,8 @@ normITAp (ITCon op _ _) (ITNum x) | isJust (res) = normITAp (ITAp (ITCon op _ _) (ITStr x)) (ITStr y) | isJust (res) = ITStr (fromJust res) where res = opStrT op [x, y] +normITAp (ITCon op _ _) (ITNum x) | op == idTNumToStr = + ITStr (mkNumFString x) normITAp f@(ITCon op _ _) a | op == idSizeOf && notVar a = -- trace ("normITAp: " ++ ppReadable (ITAp f a)) $ diff --git a/src/comp/PreIds.hs b/src/comp/PreIds.hs index 500c59a66..bcaf6515d 100644 --- a/src/comp/PreIds.hs +++ b/src/comp/PreIds.hs @@ -81,7 +81,7 @@ idPrimSnd = prelude_id_no fsPrimSnd idPrimPair = prelude_id_no fsPrimPair idFalse = prelude_id_no fsFalse idTrue = prelude_id_no fsTrue -idSizeOf, idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin, idTApp :: Id +idSizeOf, idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin :: Id idSizeOf = prelude_id_no fsSizeOf idTAdd = prelude_id_no fsTAdd idTSub = prelude_id_no fsTSub @@ -91,7 +91,9 @@ idTLog = prelude_id_no fsTLog idTExp = prelude_id_no fsTExp idTMax = prelude_id_no fsTMax idTMin = prelude_id_no fsTMin -idTApp = prelude_id_no fsTApp +idTStrCat, idTNumToStr :: Id +idTStrCat = prelude_id_no fsTStrCat +idTNumToStr = prelude_id_no fsTNumToStr idAction, idPrimAction, idToPrimAction, idFromPrimAction :: Id idAction = prelude_id_no fsAction idPrimAction = prelude_id_no fsPrimAction diff --git a/src/comp/PreStrings.hs b/src/comp/PreStrings.hs index 0d9623724..2184bc47c 100644 --- a/src/comp/PreStrings.hs +++ b/src/comp/PreStrings.hs @@ -279,7 +279,8 @@ fsTLog = mkFString "TLog" fsTExp = mkFString "TExp" fsTMax = mkFString "TMax" fsTMin = mkFString "TMin" -fsTApp = mkFString "TApp" +fsTStrCat = mkFString "TStrCat" +fsTNumToStr = mkFString "TNumToStr" fsStaticAssert = mkFString "staticAssert" fsDynamicAssert = mkFString "dynamicAssert" fsContinuousAssert = mkFString "continuousAssert" diff --git a/src/comp/TypeOps.hs b/src/comp/TypeOps.hs index 24b82b6ab..c4c1fe2d6 100644 --- a/src/comp/TypeOps.hs +++ b/src/comp/TypeOps.hs @@ -2,7 +2,7 @@ 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, idTApp) +import PreIds(idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin, idTStrCat, idTNumToStr) import Util(divC, log2) import FStringCompat(FString, concatFString) @@ -21,11 +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] opStrT :: Id -> [FString] -> Maybe FString -opStrT i xs | i == idTApp = Just $ concatFString xs +opStrT i xs | i == idTStrCat = Just $ concatFString xs opStrT _ _ = Nothing strOpNames :: [Id] -strOpNames = [idTApp] +strOpNames = [idTStrCat] diff --git a/testsuite/bsc.typechecker/string/TNumToStr.bs b/testsuite/bsc.typechecker/string/TNumToStr.bs new file mode 100644 index 000000000..c88abc54a --- /dev/null +++ b/testsuite/bsc.typechecker/string/TNumToStr.bs @@ -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 diff --git a/testsuite/bsc.typechecker/string/TApp.bs b/testsuite/bsc.typechecker/string/TStrCat.bs similarity index 71% rename from testsuite/bsc.typechecker/string/TApp.bs rename to testsuite/bsc.typechecker/string/TStrCat.bs index aac199b89..40912bee5 100644 --- a/testsuite/bsc.typechecker/string/TApp.bs +++ b/testsuite/bsc.typechecker/string/TStrCat.bs @@ -1,16 +1,16 @@ -package TApp where +package TStrCat where data (WrapStr :: $ -> *) s = WrapStr printWrapStr :: WrapStr s -> Action printWrapStr _ = $display (stringOf s) -a :: WrapStr (TApp "aaa" "bbb") +a :: WrapStr (TStrCat "aaa" "bbb") a = WrapStr class FlatWrapStr a s | a -> s where {} -instance (FlatWrapStr a s2) => FlatWrapStr (WrapStr s1, a) (TApp s1 (TApp "_" s2)) where {} +instance (FlatWrapStr a s2) => FlatWrapStr (WrapStr s1, a) (TStrCat s1 (TStrCat "_" s2)) where {} instance FlatWrapStr (WrapStr s) s where {} instance FlatWrapStr () "" where {} @@ -20,8 +20,8 @@ b = WrapStr c :: (FlatWrapStr () s) => WrapStr s c = WrapStr -sysTApp :: Module Empty -sysTApp = module +sysTStrCat :: Module Empty +sysTStrCat = module rules when True ==> do diff --git a/testsuite/bsc.typechecker/string/string.exp b/testsuite/bsc.typechecker/string/string.exp index 7acab4bb4..29f12329f 100644 --- a/testsuite/bsc.typechecker/string/string.exp +++ b/testsuite/bsc.typechecker/string/string.exp @@ -15,4 +15,5 @@ test_c_veri StringOf test_c_veri_bsv StringOfBSV test_c_veri TypeClassString -test_c_veri TApp +test_c_veri TStrCat +test_c_veri TNumToStr diff --git a/testsuite/bsc.typechecker/string/sysTNumToStr.out.expected b/testsuite/bsc.typechecker/string/sysTNumToStr.out.expected new file mode 100644 index 000000000..e9da7b195 --- /dev/null +++ b/testsuite/bsc.typechecker/string/sysTNumToStr.out.expected @@ -0,0 +1,3 @@ +42 +1_22_333 + diff --git a/testsuite/bsc.typechecker/string/sysTApp.out.expected b/testsuite/bsc.typechecker/string/sysTStrCat.out.expected similarity index 100% rename from testsuite/bsc.typechecker/string/sysTApp.out.expected rename to testsuite/bsc.typechecker/string/sysTStrCat.out.expected From 0c7cab2b160fda039effd3f370688a06df6cea46 Mon Sep 17 00:00:00 2001 From: Lucas Kramer Date: Fri, 10 Jan 2025 16:59:19 -0600 Subject: [PATCH 3/5] Add StrAlias typeclass --- src/Libraries/Base1/PreludeBSV.bsv | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Libraries/Base1/PreludeBSV.bsv b/src/Libraries/Base1/PreludeBSV.bsv index 22e12f124..f648d593a 100644 --- a/src/Libraries/Base1/PreludeBSV.bsv +++ b/src/Libraries/Base1/PreludeBSV.bsv @@ -1112,7 +1112,7 @@ endfunction: lcm // ========================= -// Alias and NumAlias +// Alias, NumAlias and StrAlias typeclass Alias#(type a, type b) dependencies (a determines b, @@ -1130,6 +1130,14 @@ endtypeclass instance NumAlias#(a,a); endinstance +typeclass StrAlias#(string type a, string type b) + dependencies (a determines b, + b determines a); +endtypeclass + +instance StrAlias#(a,a); +endinstance + // ========================= // Saturation Modes From a6a5a3eb3a8aa0cb10bf6792c18334fe7e0ae524 Mon Sep 17 00:00:00 2001 From: Lucas Kramer Date: Fri, 10 Jan 2025 17:48:36 -0600 Subject: [PATCH 4/5] Update docs --- doc/BSV_ref_guide/BSV_lang.tex | 7 +-- doc/libraries_ref_guide/LibDoc/Prelude.tex | 57 +++++++++++++++++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/doc/BSV_ref_guide/BSV_lang.tex b/doc/BSV_ref_guide/BSV_lang.tex index da82c3210..3a65f79da 100644 --- a/doc/BSV_ref_guide/BSV_lang.tex +++ b/doc/BSV_ref_guide/BSV_lang.tex @@ -3972,14 +3972,15 @@ \subsection{Type synonyms} \index{Alias} \index{NumAlias} +\index{StrAlias} The \te{typedef} statement must always be at the top level of a package, not within a module. To introduce a local name within a -module, use \te{Alias} or \te{NumAlias} (see \LibRefGuide). Since +module, use \te{Alias}, \te{NumAlias} or \te{StrAlias} (see \LibRefGuide). Since these introduce new names which are type variables as opposed to types, the new names must begin with lower case letters. -\te{NumAlias} is used to give new names to numeric types, while -\te{Alias} is used for types which can be the types of variables. +\te{Alias} is used for types which can be the types of variables, +while \te{NumAlias} and \te{StrAlias} are used to give new names to numeric and string types. Example: \begin{verbatim} diff --git a/doc/libraries_ref_guide/LibDoc/Prelude.tex b/doc/libraries_ref_guide/LibDoc/Prelude.tex index 3b6abd870..75c6dd58f 100644 --- a/doc/libraries_ref_guide/LibDoc/Prelude.tex +++ b/doc/libraries_ref_guide/LibDoc/Prelude.tex @@ -48,6 +48,8 @@ \subsection{Type classes} \hline \te{NumAlias} & Types which give a new name to a numeric type.\\ \hline +\te{StrAlias} & Types which give a new name to a string type.\\ +\hline \te{FShow} & Types which can convert a value to a \te{Fmt} representation for use with \te{\$display} system tasks.\\ \hline @@ -1457,8 +1459,10 @@ \subsubsection{Alias and NumAlias} \label{sec-alias} \index{Alias} \index{NumAlias} -\index[typeclass]{NumAlias} +\index{StrAlias} \index[typeclass]{Alias} +\index[typeclass]{NumAlias} +\index[typeclass]{StrAlias} \te{Alias} specifies that two types can be used interchangeably, providing a way to introduce local names for types within a module. @@ -1480,6 +1484,15 @@ \subsubsection{Alias and NumAlias} endtypeclass \end{verbatim} +\te{StrAlias} is used to give a new name to a string type. + +\begin{verbatim} +typeclass StrAlias#(string type a, string type b) + dependencies (a determines b, + b determines a); +endtypeclass +\end{verbatim} + {\bf Examples} \begin{verbatim} Alias#(fp, FixedPoint#(i,f)); @@ -4154,7 +4167,7 @@ \subsubsection{Rules} %================================================================ -\subsection{Operations on Numeric Types} +\subsection{Operations on Numeric and String Types} \subsubsection{Size Relationship/Provisos} @@ -4338,6 +4351,46 @@ \subsubsection{valueOf and SizeOf pseudo-functions} Bit#(SizeOf#(any_type)) = pack(structIn); \end{libverbatim} +% ================================================================ +\subsubsection{String type pseudo-functions} +\index{stringOf@\texttt{valueOf} (pseudo-function of types)} +\index{TStrCat@\texttt{TStrCat} (pseudo-function on types)} +\index{TNumToStr@\texttt{TNumToStr} (pseudo-function on types)} +\index[function]{Prelude!stringOf} +\index[function]{Prelude!TStrCat} +\index[function]{Prelude!TNumToStr} + +Prelude also provides similar pseudo-functions for string types. +The pseudo-function \te{stringOf} is used to convert a string type into a \te{String} value. +The type-level pseudo-function \te{TStrCat} is used to concatenate two string types, +and \te{TNumToStr} is used to convert a numeric type into a string type. + +\begin{center} +\begin{tabular}{|p{1 in}|p{4.6 in}|} +\hline +& \\ +\te{stringOf}&Converts a string type into its String value.\\ +\cline{2-2} +&\begin{libverbatim} +function String valueOf (t) ; +\end{libverbatim} +\\ +\hline +\end{tabular} +\end{center} + +\begin{center} +\begin{tabular}{|p {1 in}|p{1.5 in}| p{2.0 in}|} +\hline +Type Function& Type Relationship& Description\\ +\hline +\hline +\te{TStrCat}&\verb'TStrCat#(s1,s2)'&Concatenate $s1$ and $s2$\\ +\hline +\te{TNumToStr}&\verb'TNumToStr#(n1)'&Convert numeric type $n$ to a string type\\ +\hline +\end{tabular} +\end{center} % ================================================================ \subsection{Registers and Wires} From b5360c05b94f5ebc9d65440e0ce6c875f0ce2f49 Mon Sep 17 00:00:00 2001 From: Lucas Kramer Date: Tue, 21 Jan 2025 15:40:57 -0600 Subject: [PATCH 5/5] Fixes to address Julie's comments --- doc/libraries_ref_guide/LibDoc/Prelude.tex | 4 ++-- src/comp/Pred.hs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/libraries_ref_guide/LibDoc/Prelude.tex b/doc/libraries_ref_guide/LibDoc/Prelude.tex index e1b9b40cb..8e69ad802 100644 --- a/doc/libraries_ref_guide/LibDoc/Prelude.tex +++ b/doc/libraries_ref_guide/LibDoc/Prelude.tex @@ -4372,7 +4372,7 @@ \subsubsection{String type pseudo-functions} \te{stringOf}&Converts a string type into its String value.\\ \cline{2-2} &\begin{libverbatim} -function String valueOf (t) ; +function String stringOf (t) ; \end{libverbatim} \\ \hline @@ -4387,7 +4387,7 @@ \subsubsection{String type pseudo-functions} \hline \te{TStrCat}&\verb'TStrCat#(s1,s2)'&Concatenate $s1$ and $s2$\\ \hline -\te{TNumToStr}&\verb'TNumToStr#(n1)'&Convert numeric type $n$ to a string type\\ +\te{TNumToStr}&\verb'TNumToStr#(n)'&Convert numeric type $n$ to a string type\\ \hline \end{tabular} \end{center} diff --git a/src/comp/Pred.hs b/src/comp/Pred.hs index 1456592a4..affb80c48 100644 --- a/src/comp/Pred.hs +++ b/src/comp/Pred.hs @@ -261,7 +261,7 @@ expandSyn t0 = exp [] t0 [] truncType k n t = internalError ("expandSyn,truncType\n" ++ ppReadable (k, n, t0, t)) isTFun :: Id -> Bool -isTFun i = i `elem` numOpNames +isTFun i = i `elem` numOpNames ++ strOpNames apTFun :: Type -> Id -> [Type] -> Type apTFun _ i [TCon (TyNum x px), TCon (TyNum y py)] | Just n <- opNumT i [x, y] = TCon (TyNum n p')