Extract common utility functions and convert symbols to strings

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-12-01 16:38:46 -08:00
parent 1e12dc8032
commit 012c1b0d0c
4 changed files with 44 additions and 36 deletions

View File

@ -2,29 +2,7 @@ module Bergamot.Latex exposing (..)
import Bergamot.Syntax exposing (..)
import Bergamot.Rules exposing (..)
encodeStr : String -> String
encodeStr s =
let
go l =
case l of
'\\' :: xs -> '\\' :: go xs
'"' :: xs -> '\\' :: '"' :: go xs
x :: xs -> x :: go xs
[] -> []
in
String.fromList (go (String.toList s))
encodeLatex : String -> String
encodeLatex s =
let
go l =
case l of
'\\' :: xs -> String.toList "\\textbackslash " ++ go xs
x :: xs -> x :: go xs
[] -> []
in
String.fromList (go (String.toList s))
import Bergamot.Utils exposing (..)
termToLatex : (a -> String) -> Term a -> String
termToLatex f t =

View File

@ -2,6 +2,7 @@ module Bergamot.Parser exposing (..)
import Bergamot.Syntax exposing (Term(..), Metavariable(..))
import Bergamot.Rules exposing (Rule, RuleEnv)
import Bergamot.Utils exposing (decodeStr)
import Parser exposing (Parser, Trailing(..), (|.), (|=))
import Set
@ -9,19 +10,6 @@ import Set
intLit : Parser Int
intLit = Parser.int
decodeStr : String -> String
decodeStr str =
let
go l =
case l of
'\\' :: '"' :: rest -> '"' :: go rest
'\\' :: c :: rest -> c :: go rest
c :: rest -> c :: go rest
[] -> []
noQuotes = String.dropLeft 1 <| String.dropRight 1 <| str
in
String.fromList (go (String.toList str))
strLit : Parser String
strLit =
let

View File

@ -2,6 +2,7 @@ module Bergamot.Rules exposing (..)
import Bergamot.Syntax exposing (Term(..), Metavariable, UnificationVar, unify, emptyUnificationState, instantiate, instantiateList, emptyInstantiationState, resetVars, InstantiationState, UnificationState, reify)
import Bergamot.Search as Search exposing (Search)
import Bergamot.Utils exposing (encodeStr)
import Debug
@ -137,6 +138,9 @@ builtinRules t =
Call "tostring" [IntLit i, Var output] ->
liftUnification unify (Var output) (StringLit (String.fromInt i))
|> map (\_ -> MkProofTree { name = "BuiltinToString", conclusion = t, premises = []})
Call "tostring" [Call s [], Var output] ->
liftUnification unify (Var output) (StringLit <| encodeStr s)
|> map (\_ -> MkProofTree { name = "BuiltinToString", conclusion = t, premises = []})
_ -> fail
provePremises : List (Term UnificationVar) -> Prover (List ProofTree)

38
src/Bergamot/Utils.elm Normal file
View File

@ -0,0 +1,38 @@
module Bergamot.Utils exposing (..)
decodeStr : String -> String
decodeStr str =
let
go l =
case l of
'\\' :: '"' :: rest -> '"' :: go rest
'\\' :: c :: rest -> c :: go rest
c :: rest -> c :: go rest
[] -> []
noQuotes = String.dropLeft 1 <| String.dropRight 1 <| str
in
String.fromList (go (String.toList str))
encodeStr : String -> String
encodeStr s =
let
go l =
case l of
'\\' :: xs -> '\\' :: go xs
'"' :: xs -> '\\' :: '"' :: go xs
x :: xs -> x :: go xs
[] -> []
in
String.fromList (go (String.toList s))
encodeLatex : String -> String
encodeLatex s =
let
go l =
case l of
'\\' :: xs -> String.toList "\\textbackslash " ++ go xs
x :: xs -> x :: go xs
[] -> []
in
String.fromList (go (String.toList s))