[LF] Kill expirmental optimized text primitives (#16759)

Those have never proved their usefullness.
This commit is contained in:
Remy 2023-04-25 17:42:03 +02:00 committed by GitHub
parent cfd814c9de
commit 47be2db143
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 2 additions and 357 deletions

View File

@ -347,16 +347,6 @@ data BuiltinExpr
-- TypeRep
| BETypeRepTyConName -- :: TypeRep -> Optional Text
-- Experimental Text Primitives
| BETextToUpper -- :: Text -> Text
| BETextToLower -- :: Text -> Text
| BETextSlice -- :: Int -> Int -> Text -> Text
| BETextSliceIndex -- :: Text -> Text -> Optional Int64
| BETextContainsOnly -- :: Text -> Text -> Bool
| BETextReplicate -- :: Int64 -> Text -> Text
| BETextSplitOn -- :: Text -> Text -> [Text]
| BETextIntercalate -- :: Text -> [Text] -> Text
deriving (Eq, Data, Generic, NFData, Ord, Show)

View File

@ -300,14 +300,6 @@ instance Pretty BuiltinExpr where
BECodePointsToText -> "CODE_POINTS_TO_TEXT"
BECoerceContractId -> "COERCE_CONTRACT_ID"
BETypeRepTyConName -> "TYPE_REP_TYCON_NAME"
BETextToUpper -> "TEXT_TO_UPPER"
BETextToLower -> "TEXT_TO_LOWER"
BETextSlice -> "TEXT_SLICE"
BETextSliceIndex -> "TEXT_SLICE_INDEX"
BETextContainsOnly -> "TEXT_CONTAINS_ONLY"
BETextReplicate -> "TEXT_REPLICATE"
BETextSplitOn -> "TEXT_SPLIT_ON"
BETextIntercalate -> "TEXT_INTERCALATE"
where
epochToText fmt secs =

View File

@ -521,15 +521,6 @@ decodeBuiltinFunction = \case
LF1.BuiltinFunctionTYPE_REP_TYCON_NAME -> pure BETypeRepTyConName
LF1.BuiltinFunctionTEXT_TO_UPPER -> pure BETextToUpper
LF1.BuiltinFunctionTEXT_TO_LOWER -> pure BETextToLower
LF1.BuiltinFunctionTEXT_SLICE -> pure BETextSlice
LF1.BuiltinFunctionTEXT_SLICE_INDEX -> pure BETextSliceIndex
LF1.BuiltinFunctionTEXT_CONTAINS_ONLY -> pure BETextContainsOnly
LF1.BuiltinFunctionTEXT_REPLICATE -> pure BETextReplicate
LF1.BuiltinFunctionTEXT_SPLIT_ON -> pure BETextSplitOn
LF1.BuiltinFunctionTEXT_INTERCALATE -> pure BETextIntercalate
LF1.BuiltinFunctionSCALE_BIGNUMERIC -> pure BEScaleBigNumeric
LF1.BuiltinFunctionPRECISION_BIGNUMERIC -> pure BEPrecisionBigNumeric
LF1.BuiltinFunctionADD_BIGNUMERIC -> pure BEAddBigNumeric

View File

@ -552,15 +552,6 @@ encodeBuiltinExpr = \case
BETypeRepTyConName -> builtin P.BuiltinFunctionTYPE_REP_TYCON_NAME
BETextToUpper -> builtin P.BuiltinFunctionTEXT_TO_UPPER
BETextToLower -> builtin P.BuiltinFunctionTEXT_TO_LOWER
BETextSlice -> builtin P.BuiltinFunctionTEXT_SLICE
BETextSliceIndex -> builtin P.BuiltinFunctionTEXT_SLICE_INDEX
BETextContainsOnly -> builtin P.BuiltinFunctionTEXT_CONTAINS_ONLY
BETextReplicate -> builtin P.BuiltinFunctionTEXT_REPLICATE
BETextSplitOn -> builtin P.BuiltinFunctionTEXT_SPLIT_ON
BETextIntercalate -> builtin P.BuiltinFunctionTEXT_INTERCALATE
where
builtin = pure . P.ExprSumBuiltin . P.Enumerated . Right
lit = P.ExprSumPrimLit . P.PrimLit . Just

View File

@ -167,14 +167,6 @@ safetyStep = \case
BETextToCodePoints -> Safe 1
BECoerceContractId -> Safe 1
BETypeRepTyConName -> Safe 1
BETextToUpper -> Safe 1
BETextToLower -> Safe 1
BETextSlice -> Safe 3
BETextSliceIndex -> Safe 2
BETextContainsOnly -> Safe 2
BETextReplicate -> Safe 2
BETextSplitOn -> Safe 2
BETextIntercalate -> Safe 2
ERecConF _ fs -> minimum (Safe 0 : map snd fs)
ERecProjF _ _ s -> s <> Safe 0

View File

@ -308,15 +308,6 @@ typeOfBuiltin = \case
BETypeRepTyConName -> pure (TTypeRep :-> TOptional TText)
BETextToUpper -> pure (TText :-> TText)
BETextToLower -> pure (TText :-> TText)
BETextSlice -> pure (TInt64 :-> TInt64 :-> TText :-> TText)
BETextSliceIndex -> pure (TText :-> TText :-> TOptional TInt64)
BETextContainsOnly -> pure (TText :-> TText :-> TBool)
BETextReplicate -> pure (TInt64 :-> TText :-> TText)
BETextSplitOn -> pure (TText :-> TText :-> TList TText)
BETextIntercalate -> pure (TText :-> TList TText :-> TText)
where
tComparison btype = TBuiltin btype :-> TBuiltin btype :-> TBool
tBinop typ = typ :-> typ :-> typ

View File

@ -234,16 +234,6 @@ convertPrim version "BEBigNumericToNumericLegacy" ty@(TBigNumeric :-> TNumeric n
whenRuntimeSupports version featureBigNumeric ty $
EBuiltin BEBigNumericToNumericLegacy `ETyApp` n
-- Experimental text primitives.
convertPrim _ "BETextToUpper" (TText :-> TText) = pure $ EBuiltin BETextToUpper
convertPrim _ "BETextToLower" (TText :-> TText) = pure $ EBuiltin BETextToLower
convertPrim _ "BETextSlice" (TInt64 :-> TInt64 :-> TText :-> TText) = pure $ EBuiltin BETextSlice
convertPrim _ "BETextSliceIndex" (TText :-> TText :-> TOptional TInt64) = pure $ EBuiltin BETextSliceIndex
convertPrim _ "BETextContainsOnly" (TText :-> TText :-> TBool) = pure $ EBuiltin BETextContainsOnly
convertPrim _ "BETextReplicate" (TInt64 :-> TText :-> TText) = pure $ EBuiltin BETextReplicate
convertPrim _ "BETextSplitOn" (TText :-> TText :-> TList TText) = pure $ EBuiltin BETextSplitOn
convertPrim _ "BETextIntercalate" (TText :-> TList TText :-> TText) = pure $ EBuiltin BETextIntercalate
-- Conversion from ContractId to Text
convertPrim _ "BEContractIdToText" (TContractId t :-> TOptional TText) =

View File

@ -52,25 +52,6 @@ module DA.Text
, DA.Text.fromCodePoints
, DA.Text.asciiToLower
, DA.Text.asciiToUpper
#ifdef DAML_UNSTABLE
-- unstable, experimental primitives
, unstableToUpper
, unstableToLower
, unstableSlice
, unstableSliceIndex
, unstableContainsOnly
, unstableReplicate
, unstableSplitOn
, unstableIntercalate
-- unstable, experimental derived functions
, unstableTake
, unstableDrop
, unstableBreakOn
, unstableSplitAt
, unstableStripInfix
#endif
) where
import Prelude
@ -359,51 +340,3 @@ asciiToUpper = fromCodePoints . fmap toUpperCP . toCodePoints
| cpa <= x && x <= cpz = x - (cpa - cpA)
| otherwise = x
[cpA, cpa, cpz] = toCodePoints "Aaz"
#ifdef DAML_UNSTABLE
unstableToUpper : Text -> Text
unstableToUpper = primitive @"BETextToUpper"
unstableToLower : Text -> Text
unstableToLower = primitive @"BETextToLower"
unstableSlice : Int -> Int -> Text -> Text
unstableSlice = primitive @"BETextSlice"
unstableTake : Int -> Text -> Text
unstableTake = unstableSlice 0
unstableDrop : Int -> Text -> Text
unstableDrop n x = unstableSlice n (DA.Text.length x) x
unstableSplitAt : Int -> Text -> (Text, Text)
unstableSplitAt n x = (unstableTake n x, unstableDrop n x)
unstableSliceIndex : Text -> Text -> Optional Int
unstableSliceIndex = primitive @"BETextSliceIndex"
unstableBreakOn : Text -> Text -> (Text, Text)
unstableBreakOn k x =
case unstableSliceIndex k x of
None -> (x, "")
Some i -> unstableSplitAt i x
unstableStripInfix : Text -> Text -> Optional (Text, Text)
unstableStripInfix k x = do
i <- unstableSliceIndex k x
Some (unstableTake i x, unstableDrop (i + DA.Text.length k) x)
unstableContainsOnly : Text -> Text -> Bool
unstableContainsOnly = primitive @"BETextContainsOnly"
unstableReplicate : Int -> Text -> Text
unstableReplicate = primitive @"BETextReplicate"
unstableSplitOn : Text -> Text -> [Text]
unstableSplitOn = primitive @"BETextSplitOn"
unstableIntercalate : Text -> [Text] -> Text
unstableIntercalate = primitive @"BETextIntercalate"
#endif

View File

@ -1,60 +0,0 @@
-- Copyright (c) 2020, Digital Asset (Switzerland) GmbH and/or its affiliates.
-- All rights reserved.
-- @SINCE-LF 1.dev
module UnstableText where
import Daml.Script
import DA.Text as T
import DA.Assert
testUnstableDrop : Script ()
testUnstableDrop = script do
T.unstableDrop 0 "abc" === "abc"
T.unstableDrop 1 "abc" === "bc"
T.unstableDrop 2 "abc" === "c"
T.unstableDrop 3 "abc" === ""
T.unstableDrop 4 "abc" === ""
testUnstableTake : Script ()
testUnstableTake = script do
T.unstableTake 0 "abc" === ""
T.unstableTake 1 "abc" === "a"
T.unstableTake 2 "abc" === "ab"
T.unstableTake 3 "abc" === "abc"
T.unstableTake 4 "abc" === "abc"
testBreakOn : Script ()
testBreakOn = script do
T.unstableBreakOn "|" "|" === ("", "|")
T.unstableBreakOn "|" "|b" === ("", "|b")
T.unstableBreakOn "|" "a|" === ("a", "|")
T.unstableBreakOn "|" "a|b" === ("a", "|b")
T.unstableBreakOn "|" "a|b|c" === ("a", "|b|c")
T.unstableBreakOn "|" "ab" === ("ab", "")
testUnstableStripInfix : Script ()
testUnstableStripInfix = script do
T.unstableStripInfix "|" "|" === Some ("", "")
T.unstableStripInfix "|" "|b" === Some ("", "b")
T.unstableStripInfix "|" "a|" === Some ("a", "")
T.unstableStripInfix "|" "a|b" === Some ("a", "b")
T.unstableStripInfix "|" "a|b|c" === Some ("a", "b|c")
T.unstableStripInfix "|" "ab" === None
testUnstableSplitOn = script do
T.unstableSplitOn "" "" === [ "" ]
T.unstableSplitOn "" " " === [ " " ]
T.unstableSplitOn " " "" === [ "" ]
T.unstableSplitOn " " " " === [ "", "" ]
T.unstableSplitOn " " "foo bar baz" === [ "foo", "bar", "baz" ]
T.unstableSplitOn " " " foo bar baz " === [ "", "", "foo", "bar", "baz", "", "" ]
T.unstableSplitOn " " "foo bar" === [ "foo", "", "bar" ]
T.unstableSplitOn "." "4" === [ "4" ]
T.unstableSplitOn "." "4." === [ "4", "" ]
T.unstableSplitOn "." "4.0" === [ "4", "0" ]
T.unstableSplitOn "." "4.3.2.1." === [ "4", "3", "2", "1", "" ]
T.unstableSplitOn "\\." "4" === [ "4" ]
T.unstableSplitOn "\\." "4." === [ "4." ]
T.unstableSplitOn "\\." "4.0" === [ "4.0" ]
T.unstableSplitOn "\\." "4.3.2.1." === [ "4.3.2.1." ]

View File

@ -579,16 +579,6 @@ enum BuiltinFunction {
TYPE_REP_TYCON_NAME = 148; // *Available in versions >= 1.dev*
// Next id is 156.
// EXPERIMENTAL TEXT PRIMITIVES -- these do not yet have stable numbers.
TEXT_TO_UPPER = 9901; // *Available in versions >= 1.dev*
TEXT_TO_LOWER = 9902; // *Available in versions >= 1.dev*
TEXT_SLICE = 9903; // *Available in versions >= 1.dev*
TEXT_SLICE_INDEX = 9904; // *Available in versions >= 1.dev*
TEXT_CONTAINS_ONLY = 9905; // *Available in versions >= 1.dev*
TEXT_REPLICATE = 9906; // *Available in versions >= 1.dev*
TEXT_SPLIT_ON = 9907; // *Available in versions >= 1.dev*
TEXT_INTERCALATE = 9908; // *Available in versions >= 1.dev*
}
// Builtin literals

View File

@ -2411,15 +2411,7 @@ private[archive] object DecodeV1 {
BuiltinFunctionInfo(NUMERIC_TO_BIGNUMERIC, BNumericToBigNumeric, minVersion = bigNumeric),
BuiltinFunctionInfo(BIGNUMERIC_TO_TEXT, BBigNumericToText, minVersion = bigNumeric),
BuiltinFunctionInfo(ANY_EXCEPTION_MESSAGE, BAnyExceptionMessage, minVersion = exceptions),
BuiltinFunctionInfo(TYPE_REP_TYCON_NAME, BTypeRepTyConName, minVersion = LV.v1_dev),
BuiltinFunctionInfo(TEXT_TO_UPPER, BTextToUpper, minVersion = unstable),
BuiltinFunctionInfo(TEXT_TO_LOWER, BTextToLower, minVersion = unstable),
BuiltinFunctionInfo(TEXT_SLICE, BTextSlice, minVersion = unstable),
BuiltinFunctionInfo(TEXT_SLICE_INDEX, BTextSliceIndex, minVersion = unstable),
BuiltinFunctionInfo(TEXT_CONTAINS_ONLY, BTextContainsOnly, minVersion = unstable),
BuiltinFunctionInfo(TEXT_REPLICATE, BTextReplicate, minVersion = unstable),
BuiltinFunctionInfo(TEXT_SPLIT_ON, BTextSplitOn, minVersion = unstable),
BuiltinFunctionInfo(TEXT_INTERCALATE, BTextIntercalate, minVersion = unstable),
BuiltinFunctionInfo(TYPE_REP_TYCON_NAME, BTypeRepTyConName, minVersion = unstable),
)
}

View File

@ -514,16 +514,6 @@ private[lf] final class PhaseOne(
// TypeRep
case BTypeRepTyConName => SBTypeRepTyConName
// Unstable Text Primitives
case BTextToUpper => SBTextToUpper
case BTextToLower => SBTextToLower
case BTextSlice => SBTextSlice
case BTextSliceIndex => SBTextSliceIndex
case BTextContainsOnly => SBTextContainsOnly
case BTextReplicate => SBTextReplicate
case BTextSplitOn => SBTextSplitOn
case BTextIntercalate => SBTextIntercalate
// Implemented using normal SExpr
case BCoerceContractId | BLessNumeric | BLessEqNumeric | BGreaterNumeric |

View File

@ -5,7 +5,6 @@ package com.daml.lf
package speedy
import java.util
import java.util.regex.Pattern
import com.daml.lf.data.Ref._
import com.daml.lf.data._
import com.daml.lf.data.Numeric.Scale
@ -17,8 +16,7 @@ import com.daml.lf.speedy.SExpr._
import com.daml.lf.speedy.Speedy._
import com.daml.lf.speedy.{SExpr0 => compileTime}
import com.daml.lf.speedy.{SExpr => runTime}
import com.daml.lf.speedy.SValue.{SValue => _, _}
import com.daml.lf.speedy.SValue.{SValue => SV}
import com.daml.lf.speedy.SValue.{SValue => SV, _}
import com.daml.lf.transaction.{
ContractStateMachine,
GlobalKey,
@ -1893,122 +1891,6 @@ private[lf] object SBuiltin {
}
}
// Unstable text primitives.
/** $text_to_upper :: Text -> Text */
case object SBTextToUpper extends SBuiltinPure(1) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SText = {
val t = getSText(args, 0)
// TODO [FM]: replace with ASCII-specific function, or not
SText(t.toUpperCase(util.Locale.ROOT))
}
}
/** $text_to_lower :: Text -> Text */
case object SBTextToLower extends SBuiltinPure(1) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SText = {
val t = getSText(args, 0)
// TODO [FM]: replace with ASCII-specific function, or not
SText(t.toLowerCase(util.Locale.ROOT))
}
}
/** $text_slice :: Int -> Int -> Text -> Text */
case object SBTextSlice extends SBuiltinPure(3) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SText = {
val from = getSInt64(args, 0)
val to = getSInt64(args, 1)
val t = getSText(args, 2)
val length = t.codePointCount(0, t.length).toLong
if (to <= 0 || from >= length || to <= from) {
SText("")
} else {
val rfrom = from.max(0).toInt
val rto = to.min(length).toInt
// NOTE [FM]: We use toInt only after ensuring the indices are
// between 0 and length, inclusive. Calling toInt prematurely
// would mean dropping the high order bits indiscriminitely,
// so for instance (0x100000000L).toInt == 0, resulting in an
// empty string below even though `to` was larger than length.
val ifrom = t.offsetByCodePoints(0, rfrom)
val ito = t.offsetByCodePoints(ifrom, rto - rfrom)
SText(t.slice(ifrom, ito))
}
}
}
/** $text_slice_index :: Text -> Text -> Optional Int */
case object SBTextSliceIndex extends SBuiltinPure(2) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SOptional = {
val slice = getSText(args, 0)
val t = getSText(args, 1)
val n = t.indexOfSlice(slice) // n is -1 if slice is not found.
if (n < 0) {
SOptional(None)
} else {
val rn = t.codePointCount(0, n).toLong // we want to return the number of codepoints!
SOptional(Some(SInt64(rn)))
}
}
}
/** $text_contains_only :: Text -> Text -> Bool */
case object SBTextContainsOnly extends SBuiltinPure(2) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SBool = {
val alphabet = getSText(args, 0)
val t = getSText(args, 1)
val alphabetSet = alphabet.codePoints().iterator().asScala.toSet
val result = t.codePoints().iterator().asScala.forall(alphabetSet.contains)
SBool(result)
}
}
/** $text_replicate :: Int -> Text -> Text */
case object SBTextReplicate extends SBuiltinPure(2) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SText = {
val n = getSInt64(args, 0)
val t = getSText(args, 1)
if (n < 0) {
SText("")
} else {
val rn = n.min(Int.MaxValue.toLong).toInt
SText(t * rn)
}
}
}
/** $text_split_on :: Text -> Text -> List Text */
case object SBTextSplitOn extends SBuiltinPure(2) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SList = {
val pattern = getSText(args, 0)
val t = getSText(args, 1)
val xs =
// Java will produce a two-element list for this with the second
// element being the empty string.
if (pattern.isEmpty) {
FrontStack(SText(t))
} else {
// We do not want to do a regex match so we use Pattern.quote
// and we want to keep empty strings, so we use -1 as the second argument.
t.split(Pattern.quote(pattern), -1).iterator.map(SText).to(FrontStack)
}
SList(xs)
}
}
/** $text_intercalate :: Text -> List Text -> Text */
final case object SBTextIntercalate extends SBuiltinPure(2) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SText = {
val sep = getSText(args, 0)
val vs = getSList(args, 1)
val xs = vs.map {
case SText(t) => t
case x => crash(s"type mismatch SBTextIntercalate, expected Text in list, got $x")
}
SText(xs.iterator.mkString(sep))
}
}
/** EQUAL_LIST :: (a -> a -> Bool) -> [a] -> [a] -> Bool */
final case object SBEqualList extends SBuiltin(3) {

View File

@ -546,16 +546,6 @@ object Ast {
// TypeRep
final case object BTypeRepTyConName extends BuiltinFunction // : TypeRep Optional Text
// Unstable Text Primitives
final case object BTextToUpper extends BuiltinFunction // Text Text
final case object BTextToLower extends BuiltinFunction // : Text Text
final case object BTextSlice extends BuiltinFunction // : Int64 Int64 Text Text
final case object BTextSliceIndex extends BuiltinFunction // : Text Text Optional Int64
final case object BTextContainsOnly extends BuiltinFunction // : Text Text Bool
final case object BTextReplicate extends BuiltinFunction // : Int64 Text Text
final case object BTextSplitOn extends BuiltinFunction // : Text Text List Text
final case object BTextIntercalate extends BuiltinFunction // : Text List Text Text
final case class EExperimental(name: String, typ: Type) extends Expr
//

View File

@ -278,15 +278,6 @@ private[validation] object Typing {
BAnyExceptionMessage -> (TAnyException ->: TText),
// TypeRep functions
BTypeRepTyConName -> (TTypeRep ->: TOptional(TText)),
// Unstable text functions
BTextToUpper -> (TText ->: TText),
BTextToLower -> (TText ->: TText),
BTextSlice -> (TInt64 ->: TInt64 ->: TText ->: TText),
BTextSliceIndex -> (TText ->: TText ->: TOptional(TInt64)),
BTextContainsOnly -> (TText ->: TText ->: TBool),
BTextReplicate -> (TInt64 ->: TText ->: TText),
BTextSplitOn -> (TText ->: TText ->: TList(TText)),
BTextIntercalate -> (TText ->: TList(TText) ->: TText),
)
}