Add integer and strings to the language

This commit is contained in:
Danila Fedorin 2023-03-12 00:38:53 -08:00
parent fe7c01f6c2
commit ac37e82979
2 changed files with 9 additions and 0 deletions

View File

@ -26,6 +26,8 @@ instance toLatexExpr :: ToLatex k => ToLatex (Expr k) where
toLatex (Atom "snd" (t : Nil)) = "\\text{snd}\\ " <> toLatex t
toLatex (Atom s xs) = "\\text{" <> s <> "}(" <> intercalate ", " (toLatex <$> xs) <> ")"
toLatex (Var x) = toLatex x
toLatex (IntLit i) = show i
toLatex (StringLit s) = "\"" <> s <> "\""
instance toLatexRule :: ToLatex k => ToLatex (Rule k) where
toLatex (MkRule {head, tail}) = "\\cfrac{" <> intercalate "\\quad " (toLatex <$> tail) <> "}{" <> toLatex head <> "}"

View File

@ -10,6 +10,8 @@ import Data.List (List(..), (:))
data Expr v
= Var v
| IntLit Int
| StringLit String
| Atom String (List (Expr v))
derive instance Eq v => Eq (Expr v)
@ -28,10 +30,14 @@ instance UnificationVariable IntVar where
instance UnificationVariable k => Unifiable k Expr where
variable = Var
squash (Var f) = f
squash (IntLit i) = IntLit i
squash (StringLit s) = StringLit s
squash (Atom name args) = Atom name $ squash <$> args
alongside (Var k1) (Var k2) = Var (Merge k1 k2)
alongside (Var k) f = Var (Store k f)
alongside f (Var k) = Var (Store k f)
alongside (IntLit i1) (IntLit i2) | i1 == i2 = IntLit i1
alongside (StringLit s1) (StringLit s2) | s1 == s2 = StringLit s1
alongside (Atom n1 _) (Atom n2 _) | n1 /= n2 = Var Fail
alongside (Atom n1 args1) (Atom _ args2) = Atom n1 $ combine args1 args2
where
@ -39,3 +45,4 @@ instance UnificationVariable k => Unifiable k Expr where
combine (_:_) Nil = (Var Fail) : Nil
combine Nil (_:_) = (Var Fail) : Nil
combine (x:xs) (y:ys) = alongside x y : combine xs ys
alongside _ _ = Var Fail