-- |
-- Module      : Math.LaTeX.StringLiterals
-- Copyright   : (c) Justus Sagemüller 2017
-- License     : GPL v3
-- 
-- Maintainer  : (@) jsag $ hvl.no
-- Stability   : experimental
-- Portability : requires GHC>7 extensions
-- 
-- An orphan instance to the 'FromString' class, which allows maths expressions
-- to include literal strings if you enable @{-# LANGUAGE OverloadedStrings #-}@.
-- These will be rendered in roman font.

{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE UndecidableInstances #-}

module Math.LaTeX.StringLiterals where

import qualified Text.LaTeX as LaTeX
import Text.LaTeX.Base.Syntax (LaTeX)
import qualified Text.LaTeX.Packages.AMSMath as LaTeX

import CAS.Dumb
import CAS.Dumb.Tree
import CAS.Dumb.Symbols

import Data.String
import Data.Char (isAlpha)


instance (SymbolClass σ, SCConstraint σ LaTeX)
     => IsString (CAS' γ (Infix LaTeX) (Encapsulation LaTeX) (SymbolD σ LaTeX)) where
  fromString :: String
-> CAS' γ (Infix LaTeX) (Encapsulation LaTeX) (SymbolD σ LaTeX)
fromString String
s = forall γ s² s¹ s⁰. s⁰ -> CAS' γ s² s¹ s⁰
Symbol forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall σ c. c -> SymbolD σ c
StringSymbol forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (if forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Char -> Bool
isAlpha String
s then forall l. LaTeXC l => l -> l
LaTeX.mathrm
                      else forall a. a -> a
id) forall a b. (a -> b) -> a -> b
$ forall a. IsString a => String -> a
fromString String
s