never executed always true always false
    1 {-# LANGUAGE DeriveGeneric #-}
    2 {-# LANGUAGE DeriveDataTypeable #-}
    3 {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
    4 
    5 module Conjure.Language.Lexemes where
    6 
    7 import Conjure.Prelude
    8 import qualified Data.HashMap.Strict as M
    9 import qualified Data.Text as T
   10 
   11 
   12 
   13 data Lexeme
   14     = LIntLiteral Integer
   15     | LMissingIntLiteral   --helper for missing symbol
   16     | LIdentifier T.Text
   17     | LMissingIdentifier --helper for missing symbol
   18     | LMetaVar T.Text
   19     | LUnexpected T.Text
   20     | LMissingMetaVar --helper for missing symbol
   21     -- general
   22     | L_be
   23     | L_from
   24     | L_of
   25     | L_domain
   26 
   27     | L_language
   28     | L_dim
   29     | L_find
   30     | L_given
   31     | L_letting
   32     | L_where
   33     | L_such
   34     | L_that
   35     | L_minimising
   36     | L_maximising
   37     | L_branching
   38     | L_on
   39     | L_heuristic
   40 
   41     -- type: boolean
   42     | L_bool
   43     | L_false
   44     | L_true
   45 
   46     -- type: integer
   47     | L_int
   48 
   49     -- creating a new type
   50     | L_new
   51     | L_type
   52     | L_enum
   53 
   54     -- type tuple
   55     | L_tuple
   56 
   57     -- type record
   58     | L_record
   59 
   60     -- type variant
   61     | L_variant
   62     | L_active
   63 
   64     -- type: matrix
   65     | L_matrix
   66     | L_indexed
   67     | L_by
   68 
   69     -- type set
   70     | L_set
   71     | L_size
   72     | L_minSize
   73     | L_maxSize
   74 
   75     -- type: mset
   76     | L_mset
   77     | L_minOccur
   78     | L_maxOccur
   79 
   80     -- type: function
   81     | L_function
   82     | L_total
   83     | L_partial
   84     | L_injective
   85     | L_surjective
   86     | L_bijective
   87 
   88     -- type: sequence
   89     | L_sequence
   90 
   91     -- type: relation
   92     | L_relation
   93     | L_reflexive
   94     | L_irreflexive
   95     | L_coreflexive
   96     | L_symmetric
   97     | L_antiSymmetric
   98     | L_aSymmetric
   99     | L_transitive
  100     | L_connex
  101     | L_Euclidean
  102     | L_serial
  103     | L_equivalence
  104     | L_partialOrder
  105     | L_linearOrder
  106     | L_weakOrder
  107     | L_preOrder
  108     | L_strictPartialOrder
  109     | L_leftTotal
  110     | L_rightTotal
  111 
  112     -- type: partition
  113     | L_partition
  114     | L_regular
  115     | L_numMoved
  116     | L_minNumMoved
  117     | L_maxNumMoved
  118     | L_partSize
  119     | L_minPartSize
  120     | L_maxPartSize
  121     | L_numParts
  122     | L_minNumParts
  123     | L_maxNumParts
  124 
  125     -- type: permutation
  126     | L_permutation
  127     | L_compose
  128 
  129     -- operators, page 21 of the holy paper
  130     | L_union
  131     | L_intersect
  132     | L_subset
  133     | L_subsetEq
  134     | L_supset
  135     | L_supsetEq
  136     | L_in
  137     | L_max
  138     | L_min
  139     | L_toSet
  140     | L_toMSet
  141     | L_toRelation
  142     | L_defined
  143     | L_range
  144     | L_restrict
  145     | L_image
  146     | L_imageSet
  147     | L_preImage
  148     | L_inverse
  149     | L_together
  150     | L_apart
  151     | L_party
  152     | L_permInverse
  153     | L_participants
  154     | L_parts
  155     | L_freq
  156     | L_hist
  157 
  158     | L_toInt
  159     | L_makeTable
  160     | L_table
  161 
  162     -- global constraints
  163     | L_allDiff
  164     | L_alldifferent_except
  165     | L_gcc
  166     | L_elementId
  167     | L_atleast
  168     | L_atmost
  169 
  170     | L_dontCare
  171 
  172     | L_catchUndef
  173 
  174     | L_applySymmetriesEager
  175     | L_applySymmetriesDelayed
  176     | L_permutationOrderEager
  177     | L_permutationOrderDelayed
  178 
  179     -- matrix only operators
  180     | L_flatten
  181     | L_concatenate
  182     | L_normIndices
  183 
  184     -- in the rule language
  185     -- | L_lambda
  186     -- | L_quantifier
  187     -- | L_representation
  188 
  189     -- arithmetic operators
  190 
  191     | L_Plus                --    +           -- sum, infix : (int,int) -> int
  192     | L_Minus               --    -           -- (subtraction, infix : (int,int) -> int) OR (unary minus : int -> int)
  193     | L_Times               --    *           -- multiplication, infix : (int,int) -> int
  194     | L_Div                 --    /           -- integer division, infix
  195     | L_Mod                 --    %           -- modulo, infix
  196     | L_Pow                 --    **          -- exponentiation, infix : (int,int) -> int
  197     | L_factorial
  198 
  199     -- equality
  200 
  201     | L_Eq                  --    =           -- equals, infix.
  202     | L_Neq                 --    !=          -- not-equals, infix
  203 
  204     -- comparison
  205 
  206     | L_Lt                  --    <           -- less-than, infix.
  207     | L_Leq                 --    <=          -- less-than-or-eq, infix.
  208     | L_Gt                  --    >           -- greater-than, infix.
  209     | L_Geq                 --    >=          -- greater-than-or-eq, infix.
  210 
  211     -- logical operators
  212 
  213     | L_And                 --    /\          -- logical-and, infix
  214     | L_Or                  --    \/          -- logical-or, infix.
  215     | L_Imply               --    ->          -- implication, infix
  216     | L_Iff                 --    <->         -- iff, infix.
  217     | L_Not                 --    !           -- negation, prefix
  218     | L_ExclamationMark     -- for poth L_Factorial and L_ExclamationMark
  219 
  220     -- the function arrow
  221 
  222     | L_LongArrow           --    -->         -- function domains and constants
  223 
  224     -- in rule language
  225 
  226     | L_Colon               --    :           -- has-domain, infix, (expr,domain) -> bool. also does pattern matching.
  227     | L_DoubleColon         --    ::          -- has-type, infix, (expr,type) -> bool. also does pattern matching.
  228     | L_At                  --    @           -- bubble operator.
  229 
  230     -- lex operators
  231 
  232     | L_LexGeq              --    >=lex
  233     | L_LexGt               --    >lex
  234     | L_LexLt               --    <=lex
  235     | L_LexLeq              --    <lex
  236 
  237     -- for "abs" and "card"
  238     | L_Bar                 --    |
  239 
  240     -- attaching a type to an expression
  241     | L_BackTick            --    `
  242 
  243     --Quantifiers
  244 
  245     | L_ForAll
  246     | L_Exists
  247     | L_Sum
  248     | L_Product
  249     | L_fXor
  250 
  251     | L_fAnd
  252     | L_fOr
  253 
  254 
  255     -- others
  256     | L_Dot
  257     | L_DoubleDot
  258     | L_Comma
  259     | L_SemiColon
  260 
  261     | L_OpenParen
  262     | L_CloseParen
  263     | L_OpenBracket
  264     | L_CloseBracket
  265     | L_OpenCurly
  266     | L_CloseCurly
  267 
  268     | L_Newline
  269     | L_Carriage
  270     | L_Space
  271     | L_Tab
  272 
  273     | L_SquigglyArrow
  274     | L_CaseSeparator
  275 
  276     | L_HasRepr
  277     | L_HasType
  278     | L_HasDomain
  279     | L_indices
  280 
  281     | L_DotLt
  282     | L_DotLeq
  283     | L_DotGt
  284     | L_DotGeq
  285 
  286     | L_TildeLt
  287     | L_TildeLeq
  288     | L_TildeGt
  289     | L_TildeGeq
  290 
  291     | L_LeftArrow
  292 
  293     | L_subsequence
  294     | L_substring
  295     | L_powerSet
  296 
  297     | L_pred
  298     | L_succ
  299 
  300     -- type functional
  301     | L_transform
  302 
  303     -- helper
  304     | L_Missing MissingStructuralElements
  305     | L_EOF
  306     | L_SpecialCase
  307 
  308     deriving (Eq, Ord, Show,Data,Generic) --Generic
  309 
  310 instance Hashable Lexeme
  311 
  312 data MissingStructuralElements = MissingExpression | MissingDomain | MissingUnknown
  313     deriving (Eq, Ord, Data,Generic) --Generic
  314 instance Show MissingStructuralElements where
  315     show MissingExpression = "Expression"
  316     show MissingDomain = "Domain"
  317     show MissingUnknown = "Unknown"
  318 
  319 instance Hashable MissingStructuralElements
  320 
  321 lexemes :: [(T.Text, Lexeme)]
  322 lexemes = sortBy (flip (comparing (T.length . fst))) $ map swap
  323     [ ( L_be         , "be"         )
  324     , ( L_from       , "from"       )
  325     , ( L_of         , "of"         )
  326     , ( L_domain     , "domain"     )
  327     , ( L_language   , "language"   )
  328     , ( L_dim        , "dim"        )
  329     , ( L_find       , "find"       )
  330     , ( L_given      , "given"      )
  331     , ( L_letting    , "letting"    )
  332     , ( L_where      , "where"      )
  333     , ( L_such       , "such"       )
  334     , ( L_that       , "that"       )
  335     , ( L_minimising , "minimising" )
  336     , ( L_maximising , "maximising" )
  337     , ( L_minimising , "minimizing" )
  338     , ( L_maximising , "maximizing" )
  339     , ( L_branching  , "branching"  )
  340     , ( L_on         , "on"         )
  341     , ( L_heuristic  , "heuristic"  )
  342 
  343     , ( L_bool, "bool" )
  344     , ( L_false, "false" )
  345     , ( L_true, "true" )
  346     , ( L_int, "int" )
  347     , ( L_new, "new" )
  348     , ( L_type, "type" )
  349     , ( L_enum, "enum" )
  350     , ( L_tuple, "tuple" )
  351     , ( L_record, "record" )
  352     , ( L_variant, "variant" )
  353     , ( L_active, "active" )
  354     , ( L_matrix, "matrix" )
  355     , ( L_indexed, "indexed" )
  356     , ( L_by, "by" )
  357     , ( L_set, "set" )
  358     , ( L_size, "size" )
  359     , ( L_minSize, "minSize" )
  360     , ( L_maxSize, "maxSize" )
  361     , ( L_mset, "mset" )
  362     , ( L_minOccur, "minOccur" )
  363     , ( L_maxOccur, "maxOccur" )
  364     , ( L_function, "function" )
  365     , ( L_total, "total" )
  366     , ( L_partial, "partial" )
  367     , ( L_injective, "injective" )
  368     , ( L_surjective, "surjective" )
  369     , ( L_bijective, "bijective" )
  370     , ( L_sequence, "sequence" )
  371     , ( L_permutation, "permutation" )
  372     , ( L_relation, "relation")
  373     , ( L_reflexive, "reflexive")
  374     , ( L_irreflexive, "irreflexive")
  375     , ( L_coreflexive, "coreflexive")
  376     , ( L_symmetric, "symmetric")
  377     , ( L_antiSymmetric, "antiSymmetric")
  378     , ( L_aSymmetric, "aSymmetric")
  379     , ( L_transitive, "transitive")
  380     , ( L_connex, "connex")
  381     , ( L_Euclidean, "Euclidean")
  382     , ( L_serial, "serial")
  383     , ( L_equivalence, "equivalence")
  384     , ( L_partialOrder, "partialOrder")
  385     , ( L_linearOrder , "linearOrder")
  386     , ( L_weakOrder , "weakOrder")
  387     , ( L_preOrder , "preOrder")
  388     , ( L_strictPartialOrder , "strictPartialOrder")
  389     , ( L_leftTotal , "leftTotal")
  390     , ( L_rightTotal , "rightTotal")
  391     , ( L_partition, "partition" )
  392     , ( L_regular, "regular" )
  393     , ( L_numMoved, "numMoved" )
  394     , ( L_minNumMoved, "minNumMoved" )
  395     , ( L_maxNumMoved, "maxNumMoved" )
  396     , ( L_partSize, "partSize" )
  397     , ( L_minPartSize, "minPartSize" )
  398     , ( L_maxPartSize, "maxPartSize" )
  399     , ( L_numParts, "numParts" )
  400     , ( L_minNumParts, "minNumParts" )
  401     , ( L_maxNumParts, "maxNumParts" )
  402     , ( L_union, "union" )
  403     , ( L_intersect, "intersect" )
  404     , ( L_subset, "subset" )
  405     , ( L_subsetEq, "subsetEq" )
  406     , ( L_supset, "supset" )
  407     , ( L_supsetEq, "supsetEq" )
  408     , ( L_in, "in" )
  409     , ( L_max, "max" )
  410     , ( L_min, "min" )
  411     , ( L_toSet, "toSet" )
  412     , ( L_toMSet, "toMSet" )
  413     , ( L_toRelation, "toRelation" )
  414     , ( L_defined, "defined" )
  415     , ( L_range, "range" )
  416     , ( L_restrict, "restrict" )
  417     , ( L_image, "image" )
  418     , ( L_imageSet, "imageSet" )
  419     , ( L_preImage, "preImage" )
  420     , ( L_inverse, "inverse" )
  421     , ( L_together, "together" )
  422     , ( L_apart, "apart" )
  423     , ( L_party, "party" )
  424     , ( L_permInverse, "permInverse" )
  425     , ( L_participants, "participants" )
  426     , ( L_parts, "parts" )
  427     , ( L_freq, "freq" )
  428     , ( L_hist, "hist" )
  429     , ( L_toInt, "toInt" )
  430     , ( L_makeTable, "makeTable" )
  431     , ( L_table, "table" )
  432 
  433     , ( L_compose, "compose" )
  434 
  435 
  436     , ( L_allDiff, "allDiff" )
  437     , ( L_alldifferent_except, "alldifferent_except" )
  438     , ( L_gcc, "gcc" )
  439     , ( L_elementId, "elementId" )
  440     , ( L_atleast, "atleast" )
  441     , ( L_atmost, "atmost" )
  442 
  443     , ( L_dontCare, "dontCare" )
  444     , ( L_catchUndef, "catchUndef" )
  445 
  446     , ( L_applySymmetriesEager, "applySymmetriesEager" )
  447     , ( L_applySymmetriesDelayed, "applySymmetriesDelayed" )
  448     , ( L_permutationOrderEager, "permutationOrderEager" )
  449     , ( L_permutationOrderDelayed, "permutationOrderDelayed" )
  450 
  451     , ( L_flatten, "flatten" )
  452     , ( L_concatenate, "concatenate" )
  453     , ( L_normIndices, "normIndices" )
  454     -- , ( L_lambda, "lambda" )
  455     -- , ( L_quantifier, "quantifier" )
  456     -- , ( L_representation, "representation" )
  457 
  458     , ( L_ForAll            , "forAll"     )
  459     , ( L_Exists           , "exists"     )
  460     , ( L_Sum           , "sum"     )
  461     , ( L_Product           , "product"     )
  462     , ( L_Not           , "not"     )
  463     , ( L_fXor           , "xor"     )
  464     , ( L_fAnd           , "and"     )
  465     , ( L_fOr           , "or"     )
  466 
  467     , ( L_Plus            , "+"     )
  468     , ( L_Minus           , "-"     )
  469     , ( L_Times           , "*"     )
  470     , ( L_Div             , "/"     )
  471     , ( L_Mod             , "%"     )
  472     , ( L_Pow             , "**"    )
  473     , ( L_factorial       , "factorial" )
  474     , ( L_Eq              , "="     )
  475     , ( L_Neq             , "!="    )
  476     , ( L_Lt              , "<"     )
  477     , ( L_Leq             , "<="    )
  478     , ( L_Gt              , ">"     )
  479     , ( L_Geq             , ">="    )
  480     , ( L_And             , "/\\"   )
  481     , ( L_Or              , "\\/"   )
  482     , ( L_Imply           , "->"    )
  483     , ( L_Iff             , "<->"   )
  484     , ( L_ExclamationMark , "!"     )
  485     , ( L_LongArrow       , "-->"   )
  486     , ( L_Colon           , ":"     )
  487     , ( L_DoubleColon     , "::"    )
  488     , ( L_At              , "@"     )
  489     , ( L_LexGeq          , ">=lex" )
  490     , ( L_LexGt           , ">lex"  )
  491     , ( L_LexLeq          , "<=lex" )
  492     , ( L_LexLt           , "<lex"  )
  493     , ( L_Bar             , "|"     )
  494     , ( L_BackTick        , "`"     )
  495     , ( L_Dot             , "."     )
  496     , ( L_DoubleDot       , ".."    )
  497     , ( L_Comma           , ","     )
  498     , ( L_SemiColon       , ";"     )
  499     , ( L_OpenParen       , "("     )
  500     , ( L_CloseParen      , ")"     )
  501     , ( L_OpenBracket     , "["     )
  502     , ( L_CloseBracket    , "]"     )
  503     , ( L_OpenCurly       , "{"     )
  504     , ( L_CloseCurly      , "}"     )
  505 
  506     , ( L_Newline         , "\n"    )
  507     , ( L_Carriage        , "\r"    )
  508     , ( L_Space           , " "     )
  509     , ( L_Tab             , "\t"    )
  510 
  511     , ( L_SquigglyArrow   , "~~>"   )
  512     , ( L_CaseSeparator   , "***"   )
  513 
  514     , ( L_HasRepr         , "hasRepr"   )
  515     , ( L_HasType         , "hasType"   )
  516     , ( L_HasDomain       , "hasDomain" )
  517     , ( L_indices         , "indices"   )
  518 
  519     , ( L_DotLt           , ".<"    )
  520     , ( L_DotLeq          , ".<="   )
  521     , ( L_DotGt           , ".>"    )
  522     , ( L_DotGeq          , ".>="   )
  523 
  524     , ( L_TildeLt         , "~<"    )
  525     , ( L_TildeLeq        , "~<="   )
  526     , ( L_TildeGt         , "~>"    )
  527     , ( L_TildeGeq        , "~>="   )
  528 
  529     , ( L_LeftArrow       , "<-"   )
  530 
  531     , ( L_subsequence     , "subsequence"  )
  532     , ( L_substring       , "substring"    )
  533     , ( L_powerSet        , "powerSet"     )
  534 
  535     , ( L_pred, "pred" )
  536     , ( L_succ, "succ" )
  537 
  538 
  539     , ( L_transform, "transform")
  540 
  541     , ( L_SpecialCase, "?#")
  542     ]
  543 
  544 textToLexeme :: Text -> Maybe Lexeme
  545 textToLexeme t = M.lookup t mapTextToLexeme
  546 
  547 mapTextToLexeme :: M.HashMap T.Text Lexeme
  548 mapTextToLexeme = M.fromList lexemes
  549 
  550 mapLexemeToText :: M.HashMap Lexeme T.Text
  551 mapLexemeToText = M.fromList $ map swap lexemes
  552 
  553 lexemeFace :: Lexeme -> String
  554 lexemeFace L_Newline = "new line"
  555 lexemeFace L_Carriage = "\\r"
  556 lexemeFace L_Space   = "space character"
  557 lexemeFace L_Tab     = "tab character"
  558 lexemeFace (LIntLiteral i) = show i
  559 lexemeFace (LIdentifier i) = T.unpack i
  560 -- lexemeFace (LComment    i) = Pr.text (T.unpack i)
  561 lexemeFace l =
  562     case M.lookup l mapLexemeToText of
  563         Nothing ->  (show l)
  564         Just t  ->  (T.unpack t)
  565 
  566 lexemeFaceDoc :: Lexeme -> Doc
  567 lexemeFaceDoc = stringToDoc . lexemeFace
  568 
  569 lexemeText :: Lexeme -> T.Text
  570 lexemeText (LIdentifier t) =  t
  571 lexemeText l = fromMaybe (T.pack $ show l) (M.lookup l mapLexemeToText)
  572 
  573 --Categories
  574 functionAttributes :: [Lexeme]
  575 functionAttributes = [L_injective,L_size]
  576 
  577 
  578