How can I express the fairness of the range in Idris?

I am trying to simulate a simple survey form in Idris and am currently struggling with checking the user input, which comes as a string, wrt to the type of asked questions.

I currently have the following types:

data Question : Type where
  QCM : {numOptions : Nat}
      -> (question : String) 
      -> (qcmOptions : Vect numOptions String)
      -> (expected : Fin numOptions)
      -> Question

data Answer : (q : Question) -> Type where
   AnswerQCM : (option : Fin n) -> Answer (QCM {numOptions = n } q opts exp)

total 
isCorrectAnswer : (q : Question ) -> Answer q -> Bool
isCorrectAnswer (QCM {numOptions} question qcmOptions expected) (AnswerQCM option) = option == expected


data IsAnswer : (s : String) -> (q : Question) -> Type where
     ValidQCM : (option : Fin n) -> IsAnswer s (QCM {numOptions = n } q opts exp)

notInRange : (num : Fin n) -> { auto prf : GT numOptions n } 
           -> IsAnswer s (QCM {numOptions} q opts exp) -> Void
notInRange num x = ?notInRange_rhs

I don’t see how to write a function notInRange, which should be proof that some number may be an invalid answer to a multiple choice survey: this number should be in the correct range for the number of options proposed in the question.

In general, I want to write a function readAnswerthat will look like this:

readAnswer : (s : String) -> (q : Question) -> Dec (IsAnswer s q)
readAnswer s (QCM question qcmOptions expected) = ?readAnswer_rhs_1

, contra Dec, , , .

+2
1

, . . . Intead , , . , , , (, , , ), , .

isCorrectAnswer. Bool, Bool , Bool. , Refl True/False. Maybe Dec. Maybe, , , , Dec Maybe. Dec , , , .

, :

isCorrectAnswer : (q: Question) -> (a: Answer q) -> expected q = option a 

, - .

  • - , . Question Answer, , - , . , . .
  • , expected Question . , - , . expected . numOptions - Question. . Question .

2, :

record Question (numOptions : Nat) where
    constructor QCM
    question   : String
    qcmOptions : Vect numOptions String
    expected   : Fin  numOptions

record Answer (q : Question n) where
    constructor AnswerQCM
    option : Fin n

, :

data IsCorrectAnswer : (q : Question n) -> (a: Answer q) -> Type where
   IsCorrect : {q: Question n} 
            -> {a: Answer q} 
            -> expected q = option a 
            -> IsCorrectAnswer q a

:

isCorrectAnswer : (q : Question n) -> (a: Answer q) -> Dec (IsCorrectAnswer q a)
isCorrectAnswer (QCM  _ _ expected) (AnswerQCM option) =
    case decEq expected option of
        Yes prf   => Yes (IsCorrect prf)
        No contra => No (\(IsCorrect prf) => contra prf)

String Question, Answer Question. , qcmOptions, Fin index, Dec isCorrectAnswer. , . , . - , , .

s: String (option : Fin n) . , . IsAnswer , Elem Vect. isCorrectAnswer:

data IsAnswer : (s : String) -> (q : Question n) -> Type where
    ValidQCM : {q: Question n} -> Elem s (qcmOptions q) -> IsAnswer s q

readAnswer : (s : String) -> (q: Question n) -> Dec (IsAnswer s q)
readAnswer s (QCM _ options _) = case isElem s options of
    Yes prf   => Yes (ValidQCM prf)
    No contra => No (\(ValidQCM prf) => contra prf)

, isCorrectAnswer , , , - , . . .

P.S. , , , .

+4

All Articles