Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

51 satır
1.5KB

  1. # Josua Kugler, Christian Merten
  2. library(tidyverse)
  3. library(rlang)
  4. variables <- LETTERS[1:7]
  5. names(variables) <- variables
  6. prop_ops <- exprs(`(`,`!`,`&`,`|`,`<=`,`>=`,`==`)
  7. prop_ops_str <- sapply(prop_ops, expr_text)
  8. ops_expr_str <- c("==", "<=", ">=", "&", "|", "!")
  9. ops_print_str <- c("\u2194","\u2190","\u2192","\u2227","\u2228","\u00AC")
  10. names(ops_print_str) <- ops_expr_str
  11. flip <- function(f) function(x,y) f(y,x)
  12. implies <- function(A, B) (!A) | B
  13. validate_Prop <- function(e) {
  14. stopifnot("class must be Prop" = "Prop" %in% class(e))
  15. stopifnot("only numbers 0 and 1 allowed" = !str_detect(expr_text(e), "[2-9]"))
  16. stopifnot("invalid operator or variable" = all(all.names(e) %in% c(variables, ops_expr_str, "(", ")")))
  17. e
  18. }
  19. Prop <- function(x) {
  20. e <- enexpr(x)
  21. if (typeof(e) == "symbol") e <- expr(( (!!e) ))
  22. validate_Prop(structure(e, class="Prop"))
  23. }
  24. tochar <- function(p) str_replace_all(expr_text(p), fixed(ops_print_str))
  25. print.Prop <- function(p) {
  26. cat(tochar(p), "\n")
  27. invisible(p)
  28. }
  29. interprete <- function(prop, vars, append=FALSE) {
  30. res <- as.logical(eval_tidy(prop, c(as.list(vars),
  31. `>=` = implies,
  32. `<=` = flip(implies))))
  33. var <- tochar(prop)
  34. if(append) add_column(vars, (!!sym(var)) := res)
  35. else res
  36. }
  37. combinations <- do.call(expand.grid, lapply(variables, function(v) c(0,1)))
  38. is_tautology <- function(prop) {
  39. all(interprete(prop, combinations))
  40. }