ou Comment j’ai appris à aimer les types
Arnaud Bailly - @dr_c0d3
2018-04-12
#beginners
elm-lang
List a
init : ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg ) view : Model -> H.Html Msg
type Msg = SubmitResponse (Maybe String) | UpdateResponse String | NoOp
data Nat = S Nat | Z
class (Eq (Answer q)) => Questionable q where question :: q -> Text expected :: q -> Answer q response :: q -> Maybe (Answer q) answered :: q -> Maybe (Answer q) -> q isCorrectAnswer :: q -> Bool isCorrectAnswer q = Just (expected q) == response q
type family Answer q = a | a -> q where Answer QCM = Int Answer Grade = Double Answer OpenQuestion = Text
data Question where Question :: (Questionable q) => q -> (Text -> Maybe (Answer q)) -> Question
data Command : Type -> Type where Prompt : Question -> Command Input AnswerQuestion : String -> Command Bool Back : Command () Quit : Command ()
runQuizz : Quizz n -> IO () runQuizz quizz@(MkQuizz answered current next) = do (input, quizz') <- runCommand quizz (Prompt current) case input of GoBack => ?hole_1 QuitGame => ?hole_2 (GiveAnswer x) => ?hole_3 Garbage => ?hole_4
Type
data Quizz : (numQuestions : Nat) -> Type where MkQuizz : (answered : Vect n Answered) -> (current : Question) -> (next : Vect m Question) -> Quizz (n + m)
data Answered : Type where MkAnswered : (question ** Answer question) -> Answered
Refl
plusOneCommutes : (n : Nat) -> (m : Nat) -> (n + S m = S n + m)
Questionnaire