feat: simproc for turning "c" into String.singleton 'c' (#13054)
This PR adds the simproc String.reduceToSingleton`, which is disabled by default and turns `"c"` into `String.singleton 'c'`. Recall that the simproc `reduceSingleton`, which does the reverse, is part of the default `simp` set.
This commit is contained in:
parent
346c9cb16a
commit
e381960614
2 changed files with 15 additions and 0 deletions
|
|
@ -52,6 +52,12 @@ builtin_dsimproc [simp, seval] reduceSingleton (String.singleton _) := fun e =>
|
|||
let some c ← Char.fromExpr? e.appArg! | return .continue
|
||||
return .done <| toExpr (String.singleton c)
|
||||
|
||||
builtin_dsimproc_decl reduceToSingleton ((_ : String)) := fun e => do
|
||||
let some s ← fromExpr? e | return .continue
|
||||
let l := s.toList
|
||||
let [c] := l | return .continue
|
||||
return .done <| mkApp (mkConst ``String.singleton) (toExpr c)
|
||||
|
||||
@[inline] def reduceBinPred (declName : Name) (arity : Nat) (op : String → String → Bool) (e : Expr) : SimpM Step := do
|
||||
unless e.isAppOfArity declName arity do return .continue
|
||||
let some n ← fromExpr? e.appFn!.appArg! | return .continue
|
||||
|
|
|
|||
|
|
@ -55,5 +55,14 @@ example : String.singleton ' ' = " " := by simp
|
|||
example : String.singleton 'a' = "a" := by simp
|
||||
example : String.singleton '\n' = "\n" := by simp
|
||||
|
||||
-- String.reduceToSingleton (not in default simp set)
|
||||
example : "a" = String.singleton 'a' := by simp only [String.reduceToSingleton]
|
||||
example : " " = String.singleton ' ' := by simp only [String.reduceToSingleton]
|
||||
example : "\n" = String.singleton '\n' := by simp only [String.reduceToSingleton]
|
||||
-- Multi-character strings should not be reduced
|
||||
example : "ab" = "ab" := by simp only [String.reduceToSingleton]
|
||||
-- Empty strings should not be reduced
|
||||
example : "" = "" := by simp only [String.reduceToSingleton]
|
||||
|
||||
-- Combined: roundtrip toList/ofList
|
||||
example : String.ofList "hello".toList = "hello" := by simp
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue