bergamot-elm/src/Main.elm

87 lines
2.2 KiB
Elm
Raw Normal View History

2023-11-26 11:43:38 -08:00
module Main exposing (main)
import Browser
import Html exposing (Html)
2023-11-26 11:58:20 -08:00
import Html.Events exposing (onInput)
import Html.Attributes exposing (type_)
2023-11-26 11:43:38 -08:00
import Bergamot.Syntax exposing (..)
import Bergamot.Search exposing (..)
import Bergamot.Rules exposing (..)
import Bergamot.Parser exposing (..)
2023-11-26 14:34:52 -08:00
import Bergamot.Latex exposing (..)
2023-11-26 15:54:01 -08:00
import Json.Encode
2023-11-26 11:43:38 -08:00
import Maybe
import Tuple
import Debug
type alias Model =
{ program : String
, query : String
}
2023-11-26 11:43:38 -08:00
type alias Flags = ()
2023-11-26 11:58:20 -08:00
type Msg
= SetProgram String
| SetQuery String
2023-11-26 11:43:38 -08:00
init : Flags -> (Model, Cmd Msg)
init () = ({ program = "", query = "" }, Cmd.none)
2023-11-26 14:34:52 -08:00
printRules : String -> Maybe String
printRules progs =
case run program progs of
Just prog -> Just (String.join "\n\\quad" (List.map ruleToLatex prog.rules))
Nothing -> Nothing
proveQuery : String -> String -> Maybe ProofTree
proveQuery progs querys =
case (run program progs, run term querys) of
(Just prog, Just query) -> single prog (prove query |> Bergamot.Rules.andThen reifyProofTree)
_ -> Nothing
2023-11-26 11:43:38 -08:00
view : Model -> Html Msg
2023-11-26 11:58:20 -08:00
view m = Html.div []
[ Html.textarea [ onInput SetProgram ] []
, Html.br [] []
, Html.input [ type_ "text", onInput SetQuery ] []
2023-11-26 15:54:01 -08:00
, latex (Maybe.withDefault "" (printRules m.program))
, latex (
2023-11-26 14:34:52 -08:00
proveQuery m.program m.query
|> Maybe.map proofTreeToLatex
2023-11-26 15:54:01 -08:00
|> Maybe.withDefault "")
2023-11-26 11:58:20 -08:00
]
2023-11-26 11:43:38 -08:00
update : Msg -> Model -> (Model, Cmd Msg)
update msg m =
case msg of
2023-11-26 11:58:20 -08:00
SetProgram prog -> ({ m | program = prog }, Cmd.none)
SetQuery query -> ({ m | query = query }, Cmd.none)
2023-11-26 11:43:38 -08:00
subscriptions : Model -> Sub Msg
subscriptions _ = Sub.none
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
2023-11-26 15:54:01 -08:00
-- Latex support:
-- Based on https://stackoverflow.com/questions/75492820/embedding-mathematical-equations-in-an-elm-spa
latex : String -> Html Msg
latex expr =
Html.node "katex-expression"
[ Html.Attributes.attribute "expression" expr
, Html.Attributes.attribute "katex-options" (Json.Encode.encode 0 options)
]
[]
options : Json.Encode.Value
options =
Json.Encode.object
[ ( "displayMode", Json.Encode.bool True )
]