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_quickPermutationOrder
  175 
  176     -- matrix only operators
  177     | L_flatten
  178     | L_concatenate
  179     | L_normIndices
  180 
  181     -- in the rule language
  182     -- | L_lambda
  183     -- | L_quantifier
  184     -- | L_representation
  185 
  186     -- arithmetic operators
  187 
  188     | L_Plus                --    +           -- sum, infix : (int,int) -> int
  189     | L_Minus               --    -           -- (subtraction, infix : (int,int) -> int) OR (unary minus : int -> int)
  190     | L_Times               --    *           -- multiplication, infix : (int,int) -> int
  191     | L_Div                 --    /           -- integer division, infix
  192     | L_Mod                 --    %           -- modulo, infix
  193     | L_Pow                 --    **          -- exponentiation, infix : (int,int) -> int
  194     | L_factorial
  195 
  196     -- equality
  197 
  198     | L_Eq                  --    =           -- equals, infix.
  199     | L_Neq                 --    !=          -- not-equals, infix
  200 
  201     -- comparison
  202 
  203     | L_Lt                  --    <           -- less-than, infix.
  204     | L_Leq                 --    <=          -- less-than-or-eq, infix.
  205     | L_Gt                  --    >           -- greater-than, infix.
  206     | L_Geq                 --    >=          -- greater-than-or-eq, infix.
  207 
  208     -- logical operators
  209 
  210     | L_And                 --    /\          -- logical-and, infix
  211     | L_Or                  --    \/          -- logical-or, infix.
  212     | L_Imply               --    ->          -- implication, infix
  213     | L_Iff                 --    <->         -- iff, infix.
  214     | L_Not                 --    !           -- negation, prefix
  215     | L_ExclamationMark     -- for poth L_Factorial and L_ExclamationMark
  216 
  217     -- the function arrow
  218 
  219     | L_LongArrow           --    -->         -- function domains and constants
  220 
  221     -- in rule language
  222 
  223     | L_Colon               --    :           -- has-domain, infix, (expr,domain) -> bool. also does pattern matching.
  224     | L_DoubleColon         --    ::          -- has-type, infix, (expr,type) -> bool. also does pattern matching.
  225     | L_At                  --    @           -- bubble operator.
  226 
  227     -- lex operators
  228 
  229     | L_LexGeq              --    >=lex
  230     | L_LexGt               --    >lex
  231     | L_LexLt               --    <=lex
  232     | L_LexLeq              --    <lex
  233 
  234     -- for "abs" and "card"
  235     | L_Bar                 --    |
  236 
  237     -- attaching a type to an expression
  238     | L_BackTick            --    `
  239 
  240     --Quantifiers
  241 
  242     | L_ForAll
  243     | L_Exists
  244     | L_Sum
  245     | L_Product
  246     | L_fXor
  247 
  248     | L_fAnd
  249     | L_fOr
  250 
  251 
  252     -- others
  253     | L_Dot
  254     | L_DoubleDot
  255     | L_Comma
  256     | L_SemiColon
  257 
  258     | L_OpenParen
  259     | L_CloseParen
  260     | L_OpenBracket
  261     | L_CloseBracket
  262     | L_OpenCurly
  263     | L_CloseCurly
  264 
  265     | L_Newline
  266     | L_Carriage
  267     | L_Space
  268     | L_Tab
  269 
  270     | L_SquigglyArrow
  271     | L_CaseSeparator
  272 
  273     | L_HasRepr
  274     | L_HasType
  275     | L_HasDomain
  276     | L_indices
  277 
  278     | L_DotLt
  279     | L_DotLeq
  280     | L_DotGt
  281     | L_DotGeq
  282 
  283     | L_TildeLt
  284     | L_TildeLeq
  285     | L_TildeGt
  286     | L_TildeGeq
  287 
  288     | L_LeftArrow
  289 
  290     | L_subsequence
  291     | L_substring
  292     | L_powerSet
  293 
  294     | L_pred
  295     | L_succ
  296 
  297     -- type functional
  298     | L_transform
  299 
  300     -- helper
  301     | L_Missing MissingStructuralElements
  302     | L_EOF
  303     | L_SpecialCase
  304 
  305     deriving (Eq, Ord, Show,Data,Generic) --Generic
  306 
  307 instance Hashable Lexeme
  308 
  309 data MissingStructuralElements = MissingExpression | MissingDomain | MissingUnknown
  310     deriving (Eq, Ord, Data,Generic) --Generic
  311 instance Show MissingStructuralElements where
  312     show MissingExpression = "Expression"
  313     show MissingDomain = "Domain"
  314     show MissingUnknown = "Unknown"
  315 
  316 instance Hashable MissingStructuralElements
  317 
  318 lexemes :: [(T.Text, Lexeme)]
  319 lexemes = sortBy (flip (comparing (T.length . fst))) $ map swap
  320     [ ( L_be         , "be"         )
  321     , ( L_from       , "from"       )
  322     , ( L_of         , "of"         )
  323     , ( L_domain     , "domain"     )
  324     , ( L_language   , "language"   )
  325     , ( L_dim        , "dim"        )
  326     , ( L_find       , "find"       )
  327     , ( L_given      , "given"      )
  328     , ( L_letting    , "letting"    )
  329     , ( L_where      , "where"      )
  330     , ( L_such       , "such"       )
  331     , ( L_that       , "that"       )
  332     , ( L_minimising , "minimising" )
  333     , ( L_maximising , "maximising" )
  334     , ( L_minimising , "minimizing" )
  335     , ( L_maximising , "maximizing" )
  336     , ( L_branching  , "branching"  )
  337     , ( L_on         , "on"         )
  338     , ( L_heuristic  , "heuristic"  )
  339 
  340     , ( L_bool, "bool" )
  341     , ( L_false, "false" )
  342     , ( L_true, "true" )
  343     , ( L_int, "int" )
  344     , ( L_new, "new" )
  345     , ( L_type, "type" )
  346     , ( L_enum, "enum" )
  347     , ( L_tuple, "tuple" )
  348     , ( L_record, "record" )
  349     , ( L_variant, "variant" )
  350     , ( L_active, "active" )
  351     , ( L_matrix, "matrix" )
  352     , ( L_indexed, "indexed" )
  353     , ( L_by, "by" )
  354     , ( L_set, "set" )
  355     , ( L_size, "size" )
  356     , ( L_minSize, "minSize" )
  357     , ( L_maxSize, "maxSize" )
  358     , ( L_mset, "mset" )
  359     , ( L_minOccur, "minOccur" )
  360     , ( L_maxOccur, "maxOccur" )
  361     , ( L_function, "function" )
  362     , ( L_total, "total" )
  363     , ( L_partial, "partial" )
  364     , ( L_injective, "injective" )
  365     , ( L_surjective, "surjective" )
  366     , ( L_bijective, "bijective" )
  367     , ( L_sequence, "sequence" )
  368     , ( L_permutation, "permutation" )
  369     , ( L_relation, "relation")
  370     , ( L_reflexive, "reflexive")
  371     , ( L_irreflexive, "irreflexive")
  372     , ( L_coreflexive, "coreflexive")
  373     , ( L_symmetric, "symmetric")
  374     , ( L_antiSymmetric, "antiSymmetric")
  375     , ( L_aSymmetric, "aSymmetric")
  376     , ( L_transitive, "transitive")
  377     , ( L_connex, "connex")
  378     , ( L_Euclidean, "Euclidean")
  379     , ( L_serial, "serial")
  380     , ( L_equivalence, "equivalence")
  381     , ( L_partialOrder, "partialOrder")
  382     , ( L_linearOrder , "linearOrder")
  383     , ( L_weakOrder , "weakOrder")
  384     , ( L_preOrder , "preOrder")
  385     , ( L_strictPartialOrder , "strictPartialOrder")
  386     , ( L_leftTotal , "leftTotal")
  387     , ( L_rightTotal , "rightTotal")
  388     , ( L_partition, "partition" )
  389     , ( L_regular, "regular" )
  390     , ( L_numMoved, "numMoved" )
  391     , ( L_minNumMoved, "minNumMoved" )
  392     , ( L_maxNumMoved, "maxNumMoved" )
  393     , ( L_partSize, "partSize" )
  394     , ( L_minPartSize, "minPartSize" )
  395     , ( L_maxPartSize, "maxPartSize" )
  396     , ( L_numParts, "numParts" )
  397     , ( L_minNumParts, "minNumParts" )
  398     , ( L_maxNumParts, "maxNumParts" )
  399     , ( L_union, "union" )
  400     , ( L_intersect, "intersect" )
  401     , ( L_subset, "subset" )
  402     , ( L_subsetEq, "subsetEq" )
  403     , ( L_supset, "supset" )
  404     , ( L_supsetEq, "supsetEq" )
  405     , ( L_in, "in" )
  406     , ( L_max, "max" )
  407     , ( L_min, "min" )
  408     , ( L_toSet, "toSet" )
  409     , ( L_toMSet, "toMSet" )
  410     , ( L_toRelation, "toRelation" )
  411     , ( L_defined, "defined" )
  412     , ( L_range, "range" )
  413     , ( L_restrict, "restrict" )
  414     , ( L_image, "image" )
  415     , ( L_imageSet, "imageSet" )
  416     , ( L_preImage, "preImage" )
  417     , ( L_inverse, "inverse" )
  418     , ( L_together, "together" )
  419     , ( L_apart, "apart" )
  420     , ( L_party, "party" )
  421     , ( L_permInverse, "permInverse" )
  422     , ( L_participants, "participants" )
  423     , ( L_parts, "parts" )
  424     , ( L_freq, "freq" )
  425     , ( L_hist, "hist" )
  426     , ( L_toInt, "toInt" )
  427     , ( L_makeTable, "makeTable" )
  428     , ( L_table, "table" )
  429 
  430     , ( L_compose, "compose" )
  431 
  432 
  433     , ( L_allDiff, "allDiff" )
  434     , ( L_alldifferent_except, "alldifferent_except" )
  435     , ( L_gcc, "gcc" )
  436     , ( L_elementId, "elementId" )
  437     , ( L_atleast, "atleast" )
  438     , ( L_atmost, "atmost" )
  439 
  440     , ( L_dontCare, "dontCare" )
  441     , ( L_catchUndef, "catchUndef" )
  442 
  443     , ( L_quickPermutationOrder, "quickPermutationOrder" )
  444 
  445     , ( L_flatten, "flatten" )
  446     , ( L_concatenate, "concatenate" )
  447     , ( L_normIndices, "normIndices" )
  448     -- , ( L_lambda, "lambda" )
  449     -- , ( L_quantifier, "quantifier" )
  450     -- , ( L_representation, "representation" )
  451 
  452     , ( L_ForAll            , "forAll"     )
  453     , ( L_Exists           , "exists"     )
  454     , ( L_Sum           , "sum"     )
  455     , ( L_Product           , "product"     )
  456     , ( L_Not           , "not"     )
  457     , ( L_fXor           , "xor"     )
  458     , ( L_fAnd           , "and"     )
  459     , ( L_fOr           , "or"     )
  460 
  461     , ( L_Plus            , "+"     )
  462     , ( L_Minus           , "-"     )
  463     , ( L_Times           , "*"     )
  464     , ( L_Div             , "/"     )
  465     , ( L_Mod             , "%"     )
  466     , ( L_Pow             , "**"    )
  467     , ( L_factorial       , "factorial" )
  468     , ( L_Eq              , "="     )
  469     , ( L_Neq             , "!="    )
  470     , ( L_Lt              , "<"     )
  471     , ( L_Leq             , "<="    )
  472     , ( L_Gt              , ">"     )
  473     , ( L_Geq             , ">="    )
  474     , ( L_And             , "/\\"   )
  475     , ( L_Or              , "\\/"   )
  476     , ( L_Imply           , "->"    )
  477     , ( L_Iff             , "<->"   )
  478     , ( L_ExclamationMark , "!"     )
  479     , ( L_LongArrow       , "-->"   )
  480     , ( L_Colon           , ":"     )
  481     , ( L_DoubleColon     , "::"    )
  482     , ( L_At              , "@"     )
  483     , ( L_LexGeq          , ">=lex" )
  484     , ( L_LexGt           , ">lex"  )
  485     , ( L_LexLeq          , "<=lex" )
  486     , ( L_LexLt           , "<lex"  )
  487     , ( L_Bar             , "|"     )
  488     , ( L_BackTick        , "`"     )
  489     , ( L_Dot             , "."     )
  490     , ( L_DoubleDot       , ".."    )
  491     , ( L_Comma           , ","     )
  492     , ( L_SemiColon       , ";"     )
  493     , ( L_OpenParen       , "("     )
  494     , ( L_CloseParen      , ")"     )
  495     , ( L_OpenBracket     , "["     )
  496     , ( L_CloseBracket    , "]"     )
  497     , ( L_OpenCurly       , "{"     )
  498     , ( L_CloseCurly      , "}"     )
  499 
  500     , ( L_Newline         , "\n"    )
  501     , ( L_Carriage        , "\r"    )
  502     , ( L_Space           , " "     )
  503     , ( L_Tab             , "\t"    )
  504 
  505     , ( L_SquigglyArrow   , "~~>"   )
  506     , ( L_CaseSeparator   , "***"   )
  507 
  508     , ( L_HasRepr         , "hasRepr"   )
  509     , ( L_HasType         , "hasType"   )
  510     , ( L_HasDomain       , "hasDomain" )
  511     , ( L_indices         , "indices"   )
  512 
  513     , ( L_DotLt           , ".<"    )
  514     , ( L_DotLeq          , ".<="   )
  515     , ( L_DotGt           , ".>"    )
  516     , ( L_DotGeq          , ".>="   )
  517 
  518     , ( L_TildeLt         , "~<"    )
  519     , ( L_TildeLeq        , "~<="   )
  520     , ( L_TildeGt         , "~>"    )
  521     , ( L_TildeGeq        , "~>="   )
  522 
  523     , ( L_LeftArrow       , "<-"   )
  524 
  525     , ( L_subsequence     , "subsequence"  )
  526     , ( L_substring       , "substring"    )
  527     , ( L_powerSet        , "powerSet"     )
  528 
  529     , ( L_pred, "pred" )
  530     , ( L_succ, "succ" )
  531 
  532 
  533     , ( L_transform, "transform")
  534 
  535     , ( L_SpecialCase, "?#")
  536     ]
  537 
  538 textToLexeme :: Text -> Maybe Lexeme
  539 textToLexeme t = M.lookup t mapTextToLexeme
  540 
  541 mapTextToLexeme :: M.HashMap T.Text Lexeme
  542 mapTextToLexeme = M.fromList lexemes
  543 
  544 mapLexemeToText :: M.HashMap Lexeme T.Text
  545 mapLexemeToText = M.fromList $ map swap lexemes
  546 
  547 lexemeFace :: Lexeme -> String
  548 lexemeFace L_Newline = "new line"
  549 lexemeFace L_Carriage = "\\r"
  550 lexemeFace L_Space   = "space character"
  551 lexemeFace L_Tab     = "tab character"
  552 lexemeFace (LIntLiteral i) = show i
  553 lexemeFace (LIdentifier i) = T.unpack i
  554 -- lexemeFace (LComment    i) = Pr.text (T.unpack i)
  555 lexemeFace l =
  556     case M.lookup l mapLexemeToText of
  557         Nothing ->  (show l)
  558         Just t  ->  (T.unpack t)
  559 
  560 lexemeFaceDoc :: Lexeme -> Doc
  561 lexemeFaceDoc = stringToDoc . lexemeFace
  562 
  563 lexemeText :: Lexeme -> T.Text
  564 lexemeText (LIdentifier t) =  t
  565 lexemeText l = fromMaybe (T.pack $ show l) (M.lookup l mapLexemeToText)
  566 
  567 --Categories
  568 functionAttributes :: [Lexeme]
  569 functionAttributes = [L_injective,L_size]
  570 
  571 
  572