{-# LANGUAGE FlexibleContexts #-}

-----------------------------------------------------------------------------
-- Copyright 2019, Advise-Me project team. This file is distributed under 
-- the terms of the Apache License 2.0. For more information, see the files
-- "LICENSE.txt" and "NOTICE.txt", which are included in the distribution.
-----------------------------------------------------------------------------
-- |
-- Maintainer  :  bastiaan.heeren@ou.nl
-- Stability   :  provisional
-- Portability :  portable (depends on ghc)
--
-- Contains some default recognizer (diagnosis) functions.
-----------------------------------------------------------------------------

module Recognize.Recognizer where

import Recognize.Data.DiagnoseError
import Recognize.Expr.Functions
import Control.Monad.Identity
import Recognize.Data.Math
import Recognize.Parsing.Parser
import Recognize.Parsing.Interpretation
import Recognize.SubExpr.SEParser

-- The default recognizer works on @[Math]@ by using an instance of @Parse m s@
defaultRecognizer :: InterpretationParser a -> [Math] -> Either DiagnoseError a
defaultRecognizer p xs =
   case runIdentity (runParserT p initialState ys) of
      []          -> Left msg
      (a, _, _):_ -> Right a
 where
  (ys, _) = unchainAll xs
  msg | null xs     = Empty
      | otherwise   = Unknown

-- The default recognizer works on @[Math]@ by using an instance of @Parse m s@
seRecognizer :: SEParser a -> [Math] -> Either DiagnoseError a
seRecognizer p xs = maybe (Left msg) Right $ seParse p ys
 where
  (ys, _) = unchainAll xs
  msg | null xs     = Empty
      | otherwise   = Unknown